-
Notifications
You must be signed in to change notification settings - Fork 635
/
Copy pathstart.go
292 lines (257 loc) · 7.92 KB
/
start.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package start
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"text/template"
"time"
"github.com/lima-vm/lima/pkg/driver"
"github.com/lima-vm/lima/pkg/driverutil"
"github.com/lima-vm/lima/pkg/downloader"
hostagentevents "github.com/lima-vm/lima/pkg/hostagent/events"
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/store"
"github.com/lima-vm/lima/pkg/store/filenames"
"github.com/sirupsen/logrus"
)
// DefaultWatchHostAgentEventsTimeout is the duration to wait for the instance
// to be running before timing out.
const DefaultWatchHostAgentEventsTimeout = 10 * time.Minute
// ensureNerdctlArchiveCache prefetches the nerdctl-full-VERSION-linux-GOARCH.tar.gz archive
// into the cache before launching the hostagent process, so that we can show the progress in tty.
// https://github.com/lima-vm/lima/issues/326
func ensureNerdctlArchiveCache(y *limayaml.LimaYAML) (string, error) {
if !*y.Containerd.System && !*y.Containerd.User {
// nerdctl archive is not needed
return "", nil
}
errs := make([]error, len(y.Containerd.Archives))
for i := range y.Containerd.Archives {
f := &y.Containerd.Archives[i]
if f.Arch != *y.Arch {
errs[i] = fmt.Errorf("unsupported arch: %q", f.Arch)
continue
}
logrus.WithField("digest", f.Digest).Infof("Attempting to download the nerdctl archive from %q", f.Location)
res, err := downloader.Download("", f.Location, downloader.WithCache(), downloader.WithExpectedDigest(f.Digest))
if err != nil {
errs[i] = fmt.Errorf("failed to download %q: %w", f.Location, err)
continue
}
switch res.Status {
case downloader.StatusDownloaded:
logrus.Infof("Downloaded the nerdctl archive from %q", f.Location)
case downloader.StatusUsedCache:
logrus.Infof("Using cache %q", res.CachePath)
default:
logrus.Warnf("Unexpected result from downloader.Download(): %+v", res)
}
if res.CachePath == "" {
if downloader.IsLocal(f.Location) {
return f.Location, nil
}
return "", fmt.Errorf("cache did not contain %q", f.Location)
}
return res.CachePath, nil
}
return "", fmt.Errorf("failed to download the nerdctl archive, attempted %d candidates, errors=%v",
len(y.Containerd.Archives), errs)
}
func Start(ctx context.Context, inst *store.Instance) error {
haPIDPath := filepath.Join(inst.Dir, filenames.HostAgentPID)
if _, err := os.Stat(haPIDPath); !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("instance %q seems running (hint: remove %q if the instance is not actually running)", inst.Name, haPIDPath)
}
haSockPath := filepath.Join(inst.Dir, filenames.HostAgentSock)
y, err := inst.LoadYAML()
if err != nil {
return err
}
limaDriver := driverutil.CreateTargetDriverInstance(&driver.BaseDriver{
Instance: inst,
Yaml: y,
})
if err := limaDriver.Validate(); err != nil {
return err
}
if err := limaDriver.CreateDisk(); err != nil {
return err
}
nerdctlArchiveCache, err := ensureNerdctlArchiveCache(y)
if err != nil {
return err
}
self, err := os.Executable()
if err != nil {
return err
}
haStdoutPath := filepath.Join(inst.Dir, filenames.HostAgentStdoutLog)
haStderrPath := filepath.Join(inst.Dir, filenames.HostAgentStderrLog)
if err := os.RemoveAll(haStdoutPath); err != nil {
return err
}
if err := os.RemoveAll(haStderrPath); err != nil {
return err
}
haStdoutW, err := os.Create(haStdoutPath)
if err != nil {
return err
}
// no defer haStdoutW.Close()
haStderrW, err := os.Create(haStderrPath)
if err != nil {
return err
}
// no defer haStderrW.Close()
var args []string
if logrus.GetLevel() >= logrus.DebugLevel {
args = append(args, "--debug")
}
args = append(args,
"hostagent",
"--pidfile", haPIDPath,
"--socket", haSockPath)
if nerdctlArchiveCache != "" {
args = append(args, "--nerdctl-archive", nerdctlArchiveCache)
}
args = append(args, inst.Name)
haCmd := exec.CommandContext(ctx, self, args...)
haCmd.Stdout = haStdoutW
haCmd.Stderr = haStderrW
begin := time.Now() // used for logrus propagation
if err := haCmd.Start(); err != nil {
return err
}
if err := waitHostAgentStart(ctx, haPIDPath, haStderrPath); err != nil {
return err
}
watchErrCh := make(chan error)
go func() {
watchErrCh <- watchHostAgentEvents(ctx, inst, haStdoutPath, haStderrPath, begin)
close(watchErrCh)
}()
waitErrCh := make(chan error)
go func() {
waitErrCh <- haCmd.Wait()
close(waitErrCh)
}()
select {
case watchErr := <-watchErrCh:
// watchErr can be nil
return watchErr
// leave the hostagent process running
case waitErr := <-waitErrCh:
// waitErr should not be nil
return fmt.Errorf("host agent process has exited: %w", waitErr)
}
}
func waitHostAgentStart(_ context.Context, haPIDPath, haStderrPath string) error {
begin := time.Now()
deadlineDuration := 5 * time.Second
deadline := begin.Add(deadlineDuration)
for {
if _, err := os.Stat(haPIDPath); !errors.Is(err, os.ErrNotExist) {
return nil
}
if time.Now().After(deadline) {
return fmt.Errorf("hostagent (%q) did not start up in %v (hint: see %q)", haPIDPath, deadlineDuration, haStderrPath)
}
}
}
func watchHostAgentEvents(ctx context.Context, inst *store.Instance, haStdoutPath, haStderrPath string, begin time.Time) error {
ctx, cancel := context.WithTimeout(ctx, watchHostAgentTimeout(ctx))
defer cancel()
var (
printedSSHLocalPort bool
receivedRunningEvent bool
err error
)
onEvent := func(ev hostagentevents.Event) bool {
if !printedSSHLocalPort && ev.Status.SSHLocalPort != 0 {
logrus.Infof("SSH Local Port: %d", ev.Status.SSHLocalPort)
printedSSHLocalPort = true
}
if len(ev.Status.Errors) > 0 {
logrus.Errorf("%+v", ev.Status.Errors)
}
if ev.Status.Exiting {
err = fmt.Errorf("exiting, status=%+v (hint: see %q)", ev.Status, haStderrPath)
return true
} else if ev.Status.Running {
receivedRunningEvent = true
if ev.Status.Degraded {
logrus.Warnf("DEGRADED. The VM seems running, but file sharing and port forwarding may not work. (hint: see %q)", haStderrPath)
err = fmt.Errorf("degraded, status=%+v", ev.Status)
return true
}
logrus.Infof("READY. Run `%s` to open the shell.", LimactlShellCmd(inst.Name))
ShowMessage(inst)
err = nil
return true
}
return false
}
if xerr := hostagentevents.Watch(ctx, haStdoutPath, haStderrPath, begin, onEvent); xerr != nil {
return xerr
}
if err != nil {
return err
}
if !receivedRunningEvent {
return errors.New("did not receive an event with the \"running\" status")
}
return nil
}
type watchHostAgentEventsTimeoutKey = struct{}
// WithWatchHostAgentEventsTimeout sets the value of the timeout to use for
// watchHostAgentEvents in the given Context.
func WithWatchHostAgentTimeout(ctx context.Context, timeout time.Duration) context.Context {
return context.WithValue(ctx, watchHostAgentEventsTimeoutKey{}, timeout)
}
// watchHostAgentEventsTimeout returns the value of the timeout to use for
// watchHostAgentEvents contained in the given Context, or its default value.
func watchHostAgentTimeout(ctx context.Context) time.Duration {
if timeout, ok := ctx.Value(watchHostAgentEventsTimeoutKey{}).(time.Duration); ok {
return timeout
}
return DefaultWatchHostAgentEventsTimeout
}
func LimactlShellCmd(instName string) string {
shellCmd := fmt.Sprintf("limactl shell %s", instName)
if instName == "default" {
shellCmd = "lima"
}
return shellCmd
}
func ShowMessage(inst *store.Instance) error {
if inst.Message == "" {
return nil
}
t, err := template.New("message").Parse(inst.Message)
if err != nil {
return err
}
data, err := store.AddGlobalFields(inst)
if err != nil {
return err
}
var b bytes.Buffer
if err := t.Execute(&b, data); err != nil {
return err
}
scanner := bufio.NewScanner(&b)
logrus.Infof("Message from the instance %q:", inst.Name)
for scanner.Scan() {
// Avoid prepending logrus "INFO" header, for ease of copypasting
fmt.Println(scanner.Text())
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}