Skip to content

Commit

Permalink
Added -g flag to commands cp, exec, kill, logs, port, run, top. Fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
aimxhaisse committed Jul 13, 2015
1 parent 6552c27 commit c3b5eb7
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 23 deletions.
8 changes: 5 additions & 3 deletions commands/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ var cmdCp = &types.Command{

func init() {
cmdCp.Flag.BoolVar(&cpHelp, []string{"h", "-help"}, false, "Print usage")
cmdCp.Flag.StringVar(&cpGateway, []string{"g", "-gateway"}, "", "Use a SSH gateway")
}

// Flags
var cpHelp bool // -h, --help flag
var cpHelp bool // -h, --help flag
var cpGateway string // -g, --gateway flag

// TarFromSource creates a stream buffer with the tarballed content of the user source
func TarFromSource(api *api.ScalewayAPI, source string) (*io.ReadCloser, error) {
Expand Down Expand Up @@ -81,7 +83,7 @@ func TarFromSource(api *api.ScalewayAPI, source string) (*io.ReadCloser, error)
remoteCommand = append(remoteCommand, base)

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

Expand Down Expand Up @@ -164,7 +166,7 @@ func UntarToDest(api *api.ScalewayAPI, sourceStream *io.ReadCloser, destination
remoteCommand = append(remoteCommand, "-xf", "-")

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

Expand Down
4 changes: 3 additions & 1 deletion commands/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ func init() {
cmdExec.Flag.BoolVar(&execHelp, []string{"h", "-help"}, false, "Print usage")
cmdExec.Flag.Float64Var(&execTimeout, []string{"T", "-timeout"}, 0, "Set timeout values to seconds")
cmdExec.Flag.BoolVar(&execW, []string{"w", "-wait"}, false, "Wait for SSH to be ready")

This comment has been minimized.

Copy link
@moul

moul Jul 13, 2015

Contributor

--wait won't work with gateway

cmdExec.Flag.StringVar(&execGateway, []string{"g", "-gateway"}, "", "Use a SSH gateway")
}

// Flags
var execW bool // -w, --wait flag
var execTimeout float64 // -T flag
var execHelp bool // -h, --help flag
var execGateway string // -g, --gateway flag

func runExec(cmd *types.Command, args []string) {
if execHelp {
Expand Down Expand Up @@ -77,7 +79,7 @@ func runExec(cmd *types.Command, args []string) {
}()
}

err = utils.SSHExec(server.PublicAddress.IP, args[1:], !execW)
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, args[1:], !execW, execGateway)
if err != nil {
log.Fatalf("%v", err)
os.Exit(1)
Expand Down
6 changes: 4 additions & 2 deletions commands/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ var cmdKill = &types.Command{

func init() {
cmdKill.Flag.BoolVar(&killHelp, []string{"h", "-help"}, false, "Print usage")
cmdKill.Flag.StringVar(&killGateway, []string{"g", "-gateway"}, "", "Use a SSH gateway")
// FIXME: add --signal option
}

// Flags
var killHelp bool // -h, --help flag
var killHelp bool // -h, --help flag
var killGateway string // -g, --gateway flag

func runKill(cmd *types.Command, args []string) {
if killHelp {
Expand All @@ -45,7 +47,7 @@ func runKill(cmd *types.Command, args []string) {
log.Fatalf("Failed to get server information for %s: %v", serverID, err)
}

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

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

Expand Down
6 changes: 4 additions & 2 deletions commands/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ var cmdLogs = &types.Command{

func init() {
cmdLogs.Flag.BoolVar(&logsHelp, []string{"h", "-help"}, false, "Print usage")
cmdLogs.Flag.StringVar(&logsGateway, []string{"g", "-gateway"}, "", "Use a SSH gateway")
}

// FLags
var logsHelp bool // -h, --help flag
var logsHelp bool // -h, --help flag
var logsGateway string // -g, --gateway flag

func runLogs(cmd *types.Command, args []string) {
if logsHelp {
Expand All @@ -42,7 +44,7 @@ func runLogs(cmd *types.Command, args []string) {
// FIXME: switch to serial history when API is ready

command := []string{"dmesg"}
err = utils.SSHExec(server.PublicAddress.IP, command, true)
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, command, true, logsGateway)
if err != nil {
log.Fatalf("Command execution failed: %v", err)
}
Expand Down
6 changes: 4 additions & 2 deletions commands/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ var cmdPort = &types.Command{

func init() {
cmdPort.Flag.BoolVar(&portHelp, []string{"h", "-help"}, false, "Print usage")
cmdPort.Flag.StringVar(&portGateway, []string{"g", "-gateway"}, "", "Use a SSH gateway")
}

// FLags
var portHelp bool // -h, --help flag
var portHelp bool // -h, --help flag
var portGateway string // -g, --gateway flag

func runPort(cmd *types.Command, args []string) {
if portHelp {
Expand All @@ -40,7 +42,7 @@ func runPort(cmd *types.Command, args []string) {
}

command := []string{"netstat -lutn 2>/dev/null | grep LISTEN"}
err = utils.SSHExec(server.PublicAddress.IP, command, true)
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, command, true, portGateway)
if err != nil {
log.Fatalf("Command execution failed: %v", err)
}
Expand Down
6 changes: 4 additions & 2 deletions commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func init() {
cmdRun.Flag.BoolVar(&runHelpFlag, []string{"h", "-help"}, false, "Print usage")
cmdRun.Flag.BoolVar(&runAttachFlag, []string{"a", "-attach"}, false, "Attach to serial console")
cmdRun.Flag.BoolVar(&runDetachFlag, []string{"d", "-detach"}, false, "Run server in background and print server ID")
cmdRun.Flag.StringVar(&runGateway, []string{"g", "-gateway"}, "", "Use a SSH gateway")
// FIXME: handle start --timeout
}

Expand All @@ -50,6 +51,7 @@ var runCreateVolume string // -v, --volume flag
var runHelpFlag bool // -h, --help flag
var runAttachFlag bool // -a, --attach flag
var runDetachFlag bool // -d, --detach flag
var runGateway string // -g, --gateway flag

func runRun(cmd *types.Command, args []string) {
if runHelpFlag {
Expand Down Expand Up @@ -110,9 +112,9 @@ func runRun(cmd *types.Command, args []string) {
// exec -w SERVER COMMAND ARGS...
log.Debugf("Executing command")
if len(args) < 2 {
err = utils.SSHExec(server.PublicAddress.IP, []string{}, false)
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, []string{}, false, runGateway)
} else {
err = utils.SSHExec(server.PublicAddress.IP, args[1:], false)
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, args[1:], false, runGateway)
}
if err != nil {
log.Debugf("Command execution failed: %v", err)
Expand Down
9 changes: 5 additions & 4 deletions commands/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ var cmdTop = &types.Command{

func init() {
cmdTop.Flag.BoolVar(&topHelp, []string{"h", "-help"}, false, "Print usage")
cmdTop.Flag.StringVar(&topGateway, []string{"g", "-gateway"}, "", "Use a SSH gateway")
}

// Flags
var topHelp bool // -h, --help flag
var topHelp bool // -h, --help flag
var topGateway string // -g, --gateway flag

func runTop(cmd *types.Command, args []string) {
if topHelp {
cmd.PrintUsage()
}
if len(args) != 2 {
if len(args) != 1 {

This comment has been minimized.

Copy link
@moul

moul Jul 13, 2015

Contributor

👌

cmd.PrintShortUsage()
}

Expand All @@ -44,8 +46,7 @@ func runTop(cmd *types.Command, args []string) {
log.Fatalf("Failed to get server information for %s: %v", serverID, err)
}

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

execCmd := utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, true, []string{command}, topGateway)
log.Debugf("Executing: ssh %s", strings.Join(execCmd, " "))
out, err := exec.Command("ssh", execCmd...).CombinedOutput()
fmt.Printf("%s", out)
Expand Down
33 changes: 26 additions & 7 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,28 @@ import (
)

// SSHExec executes a command over SSH and redirects file-descriptors
func SSHExec(ipAddress string, command []string, checkConnection bool) error {
if ipAddress == "" {
func SSHExec(publicIpAddress string, privateIpAddress string, command []string, checkConnection bool, gatewayIpAddress string) error {
if publicIpAddress == "" && gatewayIpAddress == "" {
return errors.New("server does not have public IP")
}
if privateIpAddress == "" && gatewayIpAddress != "" {
return errors.New("server does not have private IP")
}

if checkConnection {
if !IsTCPPortOpen(fmt.Sprintf("%s:22", ipAddress)) {
useGateway := gatewayIpAddress != ""
if useGateway && !IsTCPPortOpen(fmt.Sprintf("%s:22", gatewayIpAddress)) {
return errors.New("gateway is not available, try again later")
}
if !useGateway && !IsTCPPortOpen(fmt.Sprintf("%s:22", publicIpAddress)) {
return errors.New("server is not ready, try again later")
}
}

execCmd := append(NewSSHExecCmd(ipAddress, true, command))
execCmd := append(NewSSHExecCmd(publicIpAddress, privateIpAddress, true, command, gatewayIpAddress))

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

spawn := exec.Command("ssh", execCmd...)
spawn.Stdout = os.Stdout
spawn.Stdin = os.Stdin
Expand All @@ -43,7 +51,8 @@ func SSHExec(ipAddress string, command []string, checkConnection bool) error {
}

// NewSSHExecCmd computes execve compatible arguments to run a command via ssh
func NewSSHExecCmd(ipAddress string, allocateTTY bool, command []string) []string {
func NewSSHExecCmd(publicIpAddress string, privateIpAddress string, allocateTTY bool, command []string, gatewayIpAddress string) []string {
useGateway := len(gatewayIpAddress) != 0

This comment has been minimized.

Copy link
@moul

moul Jul 13, 2015

Contributor

gatewayIpAddress != "" ?

execCmd := []string{}

if os.Getenv("DEBUG") != "1" {
Expand All @@ -54,10 +63,20 @@ func NewSSHExecCmd(ipAddress string, allocateTTY bool, command []string) []strin
execCmd = append(execCmd, "-o", "UserKnownHostsFile=/dev/null", "-o", "StrictHostKeyChecking=no")
}

execCmd = append(execCmd, "-l", "root", ipAddress)
execCmd = append(execCmd, "-l", "root")
if useGateway {
execCmd = append(execCmd, privateIpAddress, "-o")
if allocateTTY {
execCmd = append(execCmd, "ProxyCommand=ssh -q -t -t -l root -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -W %h:%p "+gatewayIpAddress)
} else {
execCmd = append(execCmd, "ProxyCommand=ssh -q -l root -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -W %h:%p "+gatewayIpAddress)
}
} else {
execCmd = append(execCmd, publicIpAddress)
}

if allocateTTY {
execCmd = append(execCmd, "-t")
execCmd = append(execCmd, "-t", "-t")
}

if len(command) > 0 {
Expand Down

0 comments on commit c3b5eb7

Please sign in to comment.