/
server_ssh.go
199 lines (161 loc) · 3.8 KB
/
server_ssh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package infrastructure
import (
"bytes"
"encoding/json"
"fmt"
"net"
"time"
"github.com/yolo-sh/yolo/entities"
"golang.org/x/crypto/ssh"
)
const (
ServerSSHPort = 22
ServerRootUser = "root"
)
type RawInitServerScriptResults struct {
ExitCode string `json:"exit_code"`
SSHHostKeys string `json:"ssh_host_keys"`
CloudInitLogs string `json:"cloud_init_logs"`
}
type InitServerScriptResults struct {
ExitCode string `json:"exit_code"`
SSHHostKeys []entities.EnvSSHHostKey `json:"ssh_host_keys"`
}
func LookupServerInitScriptResults(
serverPublicIPAddress string,
serverSSHPort string,
serverLoginUser string,
sshPrivateKeyContent string,
) (returnedInitScriptResults *InitServerScriptResults, returnedError error) {
pollTimeoutChan := time.After(5 * time.Minute)
pollSleepDuration := time.Second * 5
for {
select {
case <-pollTimeoutChan:
return
default:
initScriptOutput, err := runCMDOnServerViaSSH(
serverPublicIPAddress,
serverSSHPort,
serverLoginUser,
sshPrivateKeyContent,
"cat /tmp/yolo-init-results",
)
// Make sure timeout returns last error
returnedError = err
if err != nil {
break // wait pollSleepDuration and retry until timeout
}
var initScriptResults *RawInitServerScriptResults
err = json.Unmarshal([]byte(initScriptOutput), &initScriptResults)
if err != nil {
returnedError = fmt.Errorf(
"server cloud init script exited with invalid JSON (\"%s\") (\"%+v\")",
initScriptOutput,
err,
)
return
}
if initScriptResults.ExitCode != "0" {
returnedError = fmt.Errorf(
"server cloud init script exited with code \"%s\".\n\n%s",
initScriptResults.ExitCode,
initScriptResults.CloudInitLogs,
)
return
}
parsedSSHHostKeys, err := entities.ParseSSHHostKeys(
initScriptResults.SSHHostKeys,
)
if err != nil {
returnedError = fmt.Errorf(
"server cloud init script exited with invalid SSH host keys (\"%s\") (\"%+v\")",
initScriptResults.SSHHostKeys,
err,
)
return
}
returnedInitScriptResults = &InitServerScriptResults{
ExitCode: initScriptResults.ExitCode,
SSHHostKeys: parsedSSHHostKeys,
}
return
} // <- end of select
time.Sleep(pollSleepDuration)
} // <- end of for
}
func WaitForSSHAvailableInServer(
serverPublicIPAddress string,
serverSSHPort string,
) (returnedError error) {
pollTimeoutChan := time.After(5 * time.Minute)
pollSleepDuration := time.Second * 5
SSHConnTimeout := time.Second * 8
for {
select {
case <-pollTimeoutChan:
return
default:
conn, err := net.DialTimeout(
"tcp",
net.JoinHostPort(
serverPublicIPAddress,
serverSSHPort,
),
SSHConnTimeout,
)
// Make sure timeout returns last error
returnedError = err
if err != nil {
break // wait pollSleepDuration and retry until timeout
}
conn.Close()
return
}
time.Sleep(pollSleepDuration)
}
}
func runCMDOnServerViaSSH(
serverPublicIPAddress string,
serverSSHPort string,
loginUser string,
privateKeyContent string,
cmd string,
) (string, error) {
signer, err := ssh.ParsePrivateKey([]byte(privateKeyContent))
if err != nil {
return "", err
}
SSHConnTimeout := time.Second * 8
config := &ssh.ClientConfig{
User: loginUser,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: SSHConnTimeout,
}
client, err := ssh.Dial(
"tcp",
net.JoinHostPort(
serverPublicIPAddress,
serverSSHPort,
),
config,
)
if err != nil {
return "", err
}
session, err := client.NewSession()
if err != nil {
return "", err
}
defer session.Close()
var output bytes.Buffer
session.Stdout = &output
err = session.Run(cmd)
if err != nil {
return "", err
}
return output.String(), nil
}