-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh.go
166 lines (135 loc) · 4.17 KB
/
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
package main
import (
"bytes"
"context"
"crypto/rand"
"crypto/rsa"
"encoding/base64"
"fmt"
"log/slog"
"math"
"net"
"time"
"golang.org/x/crypto/ssh"
)
type sshKey struct {
pubKeyString string
key *rsa.PrivateKey
}
func newSSHKey() (sshKey, error) {
key, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return sshKey{}, fmt.Errorf("generate key: %w", err)
}
sshPubKey, err := ssh.NewPublicKey(&key.PublicKey)
if err != nil {
return sshKey{}, fmt.Errorf("ssh key: %w", err)
}
pubKeyString := fmt.Sprintf("ssh-rsa %s", base64.StdEncoding.EncodeToString(sshPubKey.Marshal()))
return sshKey{
pubKeyString: pubKeyString,
key: key,
}, nil
}
func connectAndRunWorker(runner runner, state *serverState, hetzner *hetzner, githubClient *githubClient) {
maxAttempts := 10
go func() {
// if the server is brand new, wait a little bit for the ssh server to be ready
if time.Since(runner.createdAt) < 11*time.Second {
time.Sleep(11 * time.Second)
}
attempts := 0
for {
// break out after max attempts
if attempts > maxAttempts {
slog.Error("could not start up worker, giving up")
break
}
// try to ssh into server
err := attemptConnectAndRunWorker(runner, hetzner.key, githubClient)
if err != nil {
attempts += 1
slog.Error("failed to start worker", "error", err, "attempts", attempts)
time.Sleep((175 * time.Millisecond) * time.Duration(math.Pow(2, float64(attempts))))
continue
}
break
}
// release runner
slog.Info("rebuilding server", "id", runner.name)
err := hetzner.reimageServer(context.Background(), runner.server)
if err != nil {
slog.Error("could not rebuild server", "error", err)
releaseRunner(runner.name, state, serverStatusDead)
} else {
slog.Info("server rebuilt", "id", runner.name)
releaseRunner(runner.name, state, serverStatusIdle)
}
}()
}
func releaseRunner(id string, state *serverState, status string) {
state.mutex.Lock()
defer state.mutex.Unlock()
if runner, ok := state.runners[id]; ok {
runner.status = status
state.runners[id] = runner
}
}
const runWorkerBash = `mkdir actions-runner && cd actions-runner && \
curl -o actions-runner-linux-%s-2.320.0.tar.gz -L https://github.com/actions/runner/releases/download/v2.320.0/actions-runner-linux-%s-2.320.0.tar.gz && \
tar xzf ./actions-runner-linux-%s-2.320.0.tar.gz && \
./config.sh --unattended --url https://github.com/%s --token %s --name %s --no-default-labels --labels self-hosted,%s --ephemeral --disableupdate && \
./run.sh; sudo poweroff
`
func attemptConnectAndRunWorker(runner runner, key sshKey, githubClient *githubClient) error {
ip := runner.server.PublicNet.IPv4.IP
id := runner.name
arch := runner.arch
repository := githubClient.owner + "/" + githubClient.repo
token, err := githubClient.runnerToken()
if err != nil {
return fmt.Errorf("get runner token from github: %w", err)
}
signer, err := ssh.NewSignerFromKey(key.key)
if err != nil {
return fmt.Errorf("make signer: %w", err)
}
config := &ssh.ClientConfig{
User: "gh",
Timeout: time.Minute * 30,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
}
client, err := ssh.Dial("tcp", net.JoinHostPort(ip.String(), "22"), config)
if err != nil {
return fmt.Errorf("dial: %w", err)
}
session, err := client.NewSession()
if err != nil {
return fmt.Errorf("open session: %w", err)
}
defer func() { _ = session.Close() }()
var out bytes.Buffer
var stdErr bytes.Buffer
session.Stdout = &out
session.Stderr = &stdErr
slog.Info("opened session", "id", id)
githubArch := "unknown-github-arch"
if arch == archARM64 {
githubArch = "arm64"
} else if arch == archAMD64 {
githubArch = "x86"
}
cmd := fmt.Sprintf(runWorkerBash, githubArch, githubArch, githubArch, repository, token, id, arch)
err = session.Start(cmd)
if err != nil {
return fmt.Errorf("run: %w; out = %s, err = %s", err, out.String(), stdErr.String())
}
if err := session.Wait(); err != nil {
return fmt.Errorf("waiting: %w; out = %s, err = %s", err, out.String(), stdErr.String())
}
slog.Info("session complete", "out", out.String(), "err", stdErr.String())
return nil
}