@@ -21,20 +21,28 @@ import (
21
21
)
22
22
23
23
// 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 == "" {
26
26
return errors .New ("server does not have public IP" )
27
27
}
28
+ if privateIpAddress == "" && gatewayIpAddress != "" {
29
+ return errors .New ("server does not have private IP" )
30
+ }
28
31
29
32
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 )) {
31
38
return errors .New ("server is not ready, try again later" )
32
39
}
33
40
}
34
41
35
- execCmd := append (NewSSHExecCmd (ipAddress , true , command ))
42
+ execCmd := append (NewSSHExecCmd (publicIpAddress , privateIpAddress , true , command , gatewayIpAddress ))
36
43
37
44
log .Debugf ("Executing: ssh %s" , strings .Join (execCmd , " " ))
45
+
38
46
spawn := exec .Command ("ssh" , execCmd ... )
39
47
spawn .Stdout = os .Stdout
40
48
spawn .Stdin = os .Stdin
@@ -43,7 +51,8 @@ func SSHExec(ipAddress string, command []string, checkConnection bool) error {
43
51
}
44
52
45
53
// 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
47
56
execCmd := []string {}
48
57
49
58
if os .Getenv ("DEBUG" ) != "1" {
@@ -54,10 +63,20 @@ func NewSSHExecCmd(ipAddress string, allocateTTY bool, command []string) []strin
54
63
execCmd = append (execCmd , "-o" , "UserKnownHostsFile=/dev/null" , "-o" , "StrictHostKeyChecking=no" )
55
64
}
56
65
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
+ }
58
77
59
78
if allocateTTY {
60
- execCmd = append (execCmd , "-t" )
79
+ execCmd = append (execCmd , "-t" , "-t" )
61
80
}
62
81
63
82
if len (command ) > 0 {
0 commit comments