Skip to content

Commit c3b5eb7

Browse files
committed
Added -g flag to commands cp, exec, kill, logs, port, run, top. Fixes #70
1 parent 6552c27 commit c3b5eb7

File tree

8 files changed

+55
-23
lines changed

8 files changed

+55
-23
lines changed

commands/cp.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ var cmdCp = &types.Command{
4343

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

4849
// Flags
49-
var cpHelp bool // -h, --help flag
50+
var cpHelp bool // -h, --help flag
51+
var cpGateway string // -g, --gateway flag
5052

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

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

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

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

commands/exec.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ func init() {
3737
cmdExec.Flag.BoolVar(&execHelp, []string{"h", "-help"}, false, "Print usage")
3838
cmdExec.Flag.Float64Var(&execTimeout, []string{"T", "-timeout"}, 0, "Set timeout values to seconds")
3939
cmdExec.Flag.BoolVar(&execW, []string{"w", "-wait"}, false, "Wait for SSH to be ready")
40+
cmdExec.Flag.StringVar(&execGateway, []string{"g", "-gateway"}, "", "Use a SSH gateway")
4041
}
4142

4243
// Flags
4344
var execW bool // -w, --wait flag
4445
var execTimeout float64 // -T flag
4546
var execHelp bool // -h, --help flag
47+
var execGateway string // -g, --gateway flag
4648

4749
func runExec(cmd *types.Command, args []string) {
4850
if execHelp {
@@ -77,7 +79,7 @@ func runExec(cmd *types.Command, args []string) {
7779
}()
7880
}
7981

80-
err = utils.SSHExec(server.PublicAddress.IP, args[1:], !execW)
82+
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, args[1:], !execW, execGateway)
8183
if err != nil {
8284
log.Fatalf("%v", err)
8385
os.Exit(1)

commands/kill.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ var cmdKill = &types.Command{
2424

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

3031
// Flags
31-
var killHelp bool // -h, --help flag
32+
var killHelp bool // -h, --help flag
33+
var killGateway string // -g, --gateway flag
3234

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

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

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

commands/logs.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ var cmdLogs = &types.Command{
2020

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

2526
// FLags
26-
var logsHelp bool // -h, --help flag
27+
var logsHelp bool // -h, --help flag
28+
var logsGateway string // -g, --gateway flag
2729

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

4446
command := []string{"dmesg"}
45-
err = utils.SSHExec(server.PublicAddress.IP, command, true)
47+
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, command, true, logsGateway)
4648
if err != nil {
4749
log.Fatalf("Command execution failed: %v", err)
4850
}

commands/port.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ var cmdPort = &types.Command{
2020

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

2526
// FLags
26-
var portHelp bool // -h, --help flag
27+
var portHelp bool // -h, --help flag
28+
var portGateway string // -g, --gateway flag
2729

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

4244
command := []string{"netstat -lutn 2>/dev/null | grep LISTEN"}
43-
err = utils.SSHExec(server.PublicAddress.IP, command, true)
45+
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, command, true, portGateway)
4446
if err != nil {
4547
log.Fatalf("Command execution failed: %v", err)
4648
}

commands/run.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func init() {
3939
cmdRun.Flag.BoolVar(&runHelpFlag, []string{"h", "-help"}, false, "Print usage")
4040
cmdRun.Flag.BoolVar(&runAttachFlag, []string{"a", "-attach"}, false, "Attach to serial console")
4141
cmdRun.Flag.BoolVar(&runDetachFlag, []string{"d", "-detach"}, false, "Run server in background and print server ID")
42+
cmdRun.Flag.StringVar(&runGateway, []string{"g", "-gateway"}, "", "Use a SSH gateway")
4243
// FIXME: handle start --timeout
4344
}
4445

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

5456
func runRun(cmd *types.Command, args []string) {
5557
if runHelpFlag {
@@ -110,9 +112,9 @@ func runRun(cmd *types.Command, args []string) {
110112
// exec -w SERVER COMMAND ARGS...
111113
log.Debugf("Executing command")
112114
if len(args) < 2 {
113-
err = utils.SSHExec(server.PublicAddress.IP, []string{}, false)
115+
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, []string{}, false, runGateway)
114116
} else {
115-
err = utils.SSHExec(server.PublicAddress.IP, args[1:], false)
117+
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, args[1:], false, runGateway)
116118
}
117119
if err != nil {
118120
log.Debugf("Command execution failed: %v", err)

commands/top.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@ var cmdTop = &types.Command{
2424

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

2930
// Flags
30-
var topHelp bool // -h, --help flag
31+
var topHelp bool // -h, --help flag
32+
var topGateway string // -g, --gateway flag
3133

3234
func runTop(cmd *types.Command, args []string) {
3335
if topHelp {
3436
cmd.PrintUsage()
3537
}
36-
if len(args) != 2 {
38+
if len(args) != 1 {
3739
cmd.PrintShortUsage()
3840
}
3941

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

47-
execCmd := append(utils.NewSSHExecCmd(server.PublicAddress.IP, true, []string{command}))
48-
49+
execCmd := utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, true, []string{command}, topGateway)
4950
log.Debugf("Executing: ssh %s", strings.Join(execCmd, " "))
5051
out, err := exec.Command("ssh", execCmd...).CombinedOutput()
5152
fmt.Printf("%s", out)

utils/utils.go

+26-7
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,28 @@ import (
2121
)
2222

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

2932
if checkConnection {
30-
if !IsTCPPortOpen(fmt.Sprintf("%s:22", ipAddress)) {
33+
useGateway := gatewayIpAddress != ""
34+
if useGateway && !IsTCPPortOpen(fmt.Sprintf("%s:22", gatewayIpAddress)) {
35+
return errors.New("gateway is not available, try again later")
36+
}
37+
if !useGateway && !IsTCPPortOpen(fmt.Sprintf("%s:22", publicIpAddress)) {
3138
return errors.New("server is not ready, try again later")
3239
}
3340
}
3441

35-
execCmd := append(NewSSHExecCmd(ipAddress, true, command))
42+
execCmd := append(NewSSHExecCmd(publicIpAddress, privateIpAddress, true, command, gatewayIpAddress))
3643

3744
log.Debugf("Executing: ssh %s", strings.Join(execCmd, " "))
45+
3846
spawn := exec.Command("ssh", execCmd...)
3947
spawn.Stdout = os.Stdout
4048
spawn.Stdin = os.Stdin
@@ -43,7 +51,8 @@ func SSHExec(ipAddress string, command []string, checkConnection bool) error {
4351
}
4452

4553
// NewSSHExecCmd computes execve compatible arguments to run a command via ssh
46-
func NewSSHExecCmd(ipAddress string, allocateTTY bool, command []string) []string {
54+
func NewSSHExecCmd(publicIpAddress string, privateIpAddress string, allocateTTY bool, command []string, gatewayIpAddress string) []string {
55+
useGateway := len(gatewayIpAddress) != 0
4756
execCmd := []string{}
4857

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

57-
execCmd = append(execCmd, "-l", "root", ipAddress)
66+
execCmd = append(execCmd, "-l", "root")
67+
if useGateway {
68+
execCmd = append(execCmd, privateIpAddress, "-o")
69+
if allocateTTY {
70+
execCmd = append(execCmd, "ProxyCommand=ssh -q -t -t -l root -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -W %h:%p "+gatewayIpAddress)
71+
} else {
72+
execCmd = append(execCmd, "ProxyCommand=ssh -q -l root -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -W %h:%p "+gatewayIpAddress)
73+
}
74+
} else {
75+
execCmd = append(execCmd, publicIpAddress)
76+
}
5877

5978
if allocateTTY {
60-
execCmd = append(execCmd, "-t")
79+
execCmd = append(execCmd, "-t", "-t")
6180
}
6281

6382
if len(command) > 0 {

0 commit comments

Comments
 (0)