Skip to content

Commit

Permalink
Support of '--gateway=login@host' (Fix scaleway#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Aug 11, 2015
1 parent 776ddae commit a2628af
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address'

#### Features

* Support of `--gateway=login@host` ([#110](https://github.com/scaleway/scaleway-cli/issues/110))
* Upload local ssh key to scaleway account on `scw login` ([#100](https://github.com/scaleway/scaleway-cli/issues/100))
* Add a 'running indicator' for `scw run`, can be disabled with the new flag `--quiet`
* Support of `scw -V/--verbose` option ([#83](https://github.com/scaleway/scaleway-cli/issues/83))
Expand Down
4 changes: 2 additions & 2 deletions pkg/commands/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TarFromSource(ctx CommandContext, source string, gateway string) (*io.ReadC
}

// execCmd contains the ssh connection + the remoteCommand
execCmd := append(utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, false, nil, remoteCommand, gateway))
execCmd := append(utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, false, nil, remoteCommand, gateway, "root"))
logrus.Debugf("Executing: ssh %s", strings.Join(execCmd, " "))
spawnSrc := exec.Command("ssh", execCmd...)

Expand Down Expand Up @@ -188,7 +188,7 @@ func UntarToDest(ctx CommandContext, sourceStream *io.ReadCloser, destination st
}

// execCmd contains the ssh connection + the remoteCommand
execCmd := append(utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, false, nil, remoteCommand, gateway))
execCmd := append(utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, false, nil, remoteCommand, gateway, "root"))
logrus.Debugf("Executing: ssh %s", strings.Join(execCmd, " "))
spawnDst := exec.Command("ssh", execCmd...)

Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func RunKill(ctx CommandContext, args KillArgs) error {
}
}

execCmd := append(utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, true, nil, []string{command}, gateway))
execCmd := append(utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, true, nil, []string{command}, gateway, "root"))

logrus.Debugf("Executing: ssh %s", strings.Join(execCmd, " "))

Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func RunTop(ctx CommandContext, args TopArgs) error {
}
}

execCmd := utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, true, nil, []string{command}, gateway)
execCmd := utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, true, nil, []string{command}, gateway, "root")
logrus.Debugf("Executing: ssh %s", strings.Join(execCmd, " "))
out, err := exec.Command("ssh", execCmd...).CombinedOutput()
fmt.Printf("%s", out)
Expand Down
23 changes: 19 additions & 4 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ func quoteShellArgs(args []string) string {

// SSHExec executes a command over SSH and redirects file-descriptors
func SSHExec(publicIPAddress string, privateIPAddress string, command []string, checkConnection bool, gatewayIPAddress string) error {
gatewayUser := "root"
if strings.Contains(gatewayIPAddress, "@") {
parts := strings.Split(gatewayIPAddress, "@")
gatewayUser = parts[0]
gatewayIPAddress = parts[1]
}

if publicIPAddress == "" && gatewayIPAddress == "" {
return errors.New("server does not have public IP")
}
Expand All @@ -52,7 +59,7 @@ func SSHExec(publicIPAddress string, privateIPAddress string, command []string,
}
}

execCmd := append(NewSSHExecCmd(publicIPAddress, privateIPAddress, true, nil, command, gatewayIPAddress))
execCmd := append(NewSSHExecCmd(publicIPAddress, privateIPAddress, true, nil, command, gatewayUser+"@"+gatewayIPAddress, "root"))

log.Debugf("Executing: ssh %s", quoteShellArgs(execCmd))

Expand All @@ -64,8 +71,9 @@ func SSHExec(publicIPAddress string, privateIPAddress string, command []string,
}

// NewSSHExecCmd computes execve compatible arguments to run a command via ssh
func NewSSHExecCmd(publicIPAddress string, privateIPAddress string, allocateTTY bool, sshOptions []string, command []string, gatewayIPAddress string) []string {
func NewSSHExecCmd(publicIPAddress string, privateIPAddress string, allocateTTY bool, sshOptions []string, command []string, gatewayIPAddress string, sshUser string) []string {
useGateway := gatewayIPAddress != ""

execCmd := []string{}

if os.Getenv("DEBUG") != "1" {
Expand All @@ -80,9 +88,16 @@ func NewSSHExecCmd(publicIPAddress string, privateIPAddress string, allocateTTY
execCmd = append(execCmd, strings.Join(sshOptions, " "))
}

execCmd = append(execCmd, "-l", "root")
execCmd = append(execCmd, "-l", sshUser)
if useGateway {
proxyCommand := NewSSHExecCmd(gatewayIPAddress, "", allocateTTY, []string{"-W", "%h:%p"}, nil, "")
gatewayUser := "root"
if useGateway && strings.Contains(gatewayIPAddress, "@") {
parts := strings.Split(gatewayIPAddress, "@")
gatewayUser = parts[0]
gatewayIPAddress = parts[1]
}

proxyCommand := NewSSHExecCmd(gatewayIPAddress, "", allocateTTY, []string{"-W", "%h:%p"}, nil, "", gatewayUser)
execCmd = append(execCmd, privateIPAddress, "-o", "ProxyCommand=ssh "+strings.Join(proxyCommand, " "))
} else {
execCmd = append(execCmd, publicIPAddress)
Expand Down

0 comments on commit a2628af

Please sign in to comment.