Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support bastion command #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/sup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ func parseArgs(conf *sup.Supfile) (*sup.Network, []*sup.Command, error) {
network.Env.Set(env[:i], env[i+1:])
}

bastion, err := network.ParseBastion()
if err != nil {
return nil, nil, err
}
network.Bastion = bastion

hosts, err := network.ParseInventory()
if err != nil {
return nil, nil, err
Expand Down
34 changes: 34 additions & 0 deletions supfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,37 @@ func (n Network) ParseInventory() ([]string, error) {
}
return hosts, nil
}

// ParseBastion returns the bastion if it is a valid connection string
// or runs the bastion command, if provided, and returns the first
// line of the command's output.
func (n Network) ParseBastion() (string, error) {
if n.Bastion == "" {
return "", nil
}

// check if its a connection string
testConn := &SSHClient{}
if err := testConn.Connect(n.Bastion); err == nil {
return n.Bastion, nil
}

cmd := exec.Command("/bin/sh", "-c", n.Bastion)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, n.Env.Slice()...)
cmd.Stderr = os.Stderr
output, err := cmd.Output()
if err != nil {
return "", err
}

buf := bytes.NewBuffer(output)

bastion, err := buf.ReadString('\n')
if err != nil {
return "", err
}

bastion = strings.TrimSpace(bastion)
return bastion, nil
}