-
Notifications
You must be signed in to change notification settings - Fork 64
/
run.go
475 lines (409 loc) · 13.7 KB
/
run.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package run
import (
"context"
"fmt"
"os"
"path/filepath"
"sort"
"github.com/MakeNowJust/heredoc"
"github.com/erikgeiser/promptkit/selection"
"github.com/moby/moby/pkg/namesgenerator"
"github.com/rancher/wrangler/pkg/signals"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
"kraftkit.sh/exec"
"kraftkit.sh/iostreams"
"kraftkit.sh/log"
"kraftkit.sh/machine"
machinedriver "kraftkit.sh/machine/driver"
machinedriveropts "kraftkit.sh/machine/driveropts"
"kraftkit.sh/pack"
"kraftkit.sh/packmanager"
"kraftkit.sh/tui/paraprogress"
"kraftkit.sh/unikraft"
"kraftkit.sh/unikraft/app"
"kraftkit.sh/unikraft/target"
"kraftkit.sh/utils"
)
type Run struct {
Architecture string `long:"arch" short:"m" usage:"Set the architecture"`
Detach bool `long:"detach" short:"d" usage:"Run unikernel in background"`
DisableAccel bool `long:"disable-acceleration" short:"W" usage:"Disable acceleration of CPU (usually enables TCG)"`
Hypervisor string
InitRd string `long:"initrd" short:"i" usage:"Use the specified initrd"`
Memory int `long:"memory" short:"M" usage:"Assign MB memory to the unikernel"`
Name string `long:"name" short:"n" usage:"Name of the instance"`
Platform string `long:"plat" short:"p" usage:"Set the platform"`
Remove bool `long:"rm" usage:"Automatically remove the unikernel when it shutsdown"`
Target string `long:"target" short:"t" usage:"Explicitly use the defined project target"`
WithKernelDbg bool `long:"symbolic" usage:"Use the debuggable (symbolic) unikernel"`
}
func New() *cobra.Command {
cmd := cmdfactory.New(&Run{}, cobra.Command{
Short: "Run a unikernel",
Use: "run [FLAGS] PROJECT|KERNEL -- [UNIKRAFT ARGS] -- [APP ARGS]",
Args: cobra.MaximumNArgs(1),
Aliases: []string{"r"},
Long: heredoc.Doc(`
Launch a unikernel`),
Example: heredoc.Doc(`
# Run a unikernel kernel image
kraft run path/to/kernel-x86_64-kvm
# Run a project which only has one target
kraft run path/to/project`),
Annotations: map[string]string{
cmdfactory.AnnotationHelpGroup: "run",
},
})
cmd.Flags().VarP(
cmdfactory.NewEnumFlag(machinedriver.DriverNames(), "auto"),
"hypervisor",
"H",
"Set the hypervisor machine driver.",
)
return cmd
}
func (opts *Run) Pre(cmd *cobra.Command, args []string) error {
opts.Hypervisor = cmd.Flag("hypervisor").Value.String()
return nil
}
func (opts *Run) Run(cmd *cobra.Command, args []string) error {
var err error
ctx := cmd.Context()
driverType := machinedriver.UnknownDriver
if opts.Hypervisor == "auto" {
driverType, err = machinedriver.DetectHostHypervisor()
if err != nil {
return err
}
} else if opts.Hypervisor == "config" {
opts.Hypervisor = config.G[config.KraftKit](ctx).DefaultPlat
} else {
driverType = machinedriver.DriverTypeFromName(opts.Hypervisor)
}
if driverType == machinedriver.UnknownDriver {
return fmt.Errorf("unknown hypervisor driver: %s", opts.Hypervisor)
}
debug := log.Levels()[config.G[config.KraftKit](ctx).Log.Level] >= logrus.DebugLevel
store, err := machine.NewMachineStoreFromPath(config.G[config.KraftKit](ctx).RuntimeDir)
if err != nil {
return fmt.Errorf("could not access machine store: %v", err)
}
driver, err := machinedriver.New(driverType,
machinedriveropts.WithBackground(opts.Detach),
machinedriveropts.WithRuntimeDir(config.G[config.KraftKit](ctx).RuntimeDir),
machinedriveropts.WithMachineStore(store),
machinedriveropts.WithDebug(debug),
machinedriveropts.WithExecOptions(
exec.WithStdout(os.Stdout),
exec.WithStderr(os.Stderr),
),
)
if err != nil {
return err
}
mopts := []machine.MachineOption{
machine.WithDriverName(driverType.String()),
machine.WithDestroyOnExit(opts.Remove),
}
// The following sequence checks the position argument of `kraft run ENTITY`
// where ENTITY can either be:
// a). path to a project which either uses the only specified target or one
// specified via the -t flag, e.g.:
//
// $ kraft run path/to/project # with 1 default target
// # or for multiple targets
// $ kraft run -t target-name path/to/project
//
// b). path to a kernel, e.g.:
//
// $ kraft run path/to/kernel
//
// c). A package manager image reference containing a unikernel;
//
// d). a target defined within the context of `workdir` (which is either set
// via -w or is the current working directory), e.g.:
//
// $ cd path/to/project
// $ kraft run target-name
// # or
// $ kraft run -w path/to/project target-name
//
var workdir string
var entity string
var kernelArgs []string
// Determine if more than one positional arguments have been provided. If
// this is the case, everything after the first position argument are kernel
// parameters which should be passed appropriately.
if len(args) > 1 {
entity = args[0]
kernelArgs = args[1:]
} else if len(args) == 1 {
entity = args[0]
}
// a). path to a project
if len(entity) > 0 && app.IsWorkdirInitialized(entity) {
workdir = entity
// Otherwise use the current working directory
} else {
cwd, err := os.Getwd()
if err != nil {
return err
}
if app.IsWorkdirInitialized(cwd) {
workdir = cwd
kernelArgs = args
}
}
// b). Is the provided first position argument a binary image?
if f, err := os.Stat(entity); err == nil && !f.IsDir() {
if len(opts.Architecture) == 0 || len(opts.Platform) == 0 {
return fmt.Errorf("cannot use `kraft run KERNEL` without specifying --arch and --plat")
}
if opts.Name == "" {
opts.Name = namesgenerator.GetRandomName(0)
}
mopts = append(mopts,
machine.WithArchitecture(opts.Architecture),
machine.WithPlatform(opts.Platform),
machine.WithName(machine.MachineName(opts.Name)),
machine.WithKernel(entity),
machine.WithSource("kernel://"+filepath.Base(entity)),
machine.WithInitRd(opts.InitRd),
)
// c). use the provided package manager
} else if pm, compatible, err := packmanager.G(ctx).IsCompatible(ctx, entity); err == nil && compatible {
packs, err := pm.Catalog(ctx, packmanager.CatalogQuery{
Types: []unikraft.ComponentType{unikraft.ComponentTypeApp},
Name: entity,
NoCache: true,
})
if err != nil {
return err
}
if len(packs) > 1 {
return fmt.Errorf("could not determine what to run, too many options")
} else if len(packs) == 0 {
return fmt.Errorf("not found: %s", entity)
}
// Create a temporary directory where the image can be stored
dir, err := os.MkdirTemp("", "")
if err != nil {
return err
}
// TODO(nderjung): Somehow this needs to be garbage collected if in
// detach-mode.
if !opts.Detach {
defer os.RemoveAll(dir)
}
paramodel, err := paraprogress.NewParaProgress(
ctx,
[]*paraprogress.Process{paraprogress.NewProcess(
fmt.Sprintf("pulling %s", entity),
func(ctx context.Context, w func(progress float64)) error {
return packs[0].Pull(
ctx,
pack.WithPullProgressFunc(w),
pack.WithPullWorkdir(dir),
)
},
)},
paraprogress.IsParallel(false),
paraprogress.WithRenderer(
log.LoggerTypeFromString(config.G[config.KraftKit](ctx).Log.Type) != log.FANCY,
),
paraprogress.WithFailFast(true),
)
if err != nil {
return err
}
if err := paramodel.Start(); err != nil {
return err
}
// Crucially, the catalog should return an interface that also implements
// target.Target. This demonstrates that the implementing package can
// resolve application kernels.
targ, ok := packs[0].(target.Target)
if !ok {
return fmt.Errorf("package does not convert to target")
}
if opts.Name == "" {
opts.Name = namesgenerator.GetRandomName(0)
}
mopts = append(mopts,
machine.WithArchitecture(targ.Architecture().Name()),
machine.WithPlatform(targ.Platform().Name()),
machine.WithName(machine.MachineName(opts.Name)),
machine.WithKernel(targ.Kernel()),
machine.WithSource(fmt.Sprintf("%s://%s", pm.Format(), entity)),
)
// d). use a defined working directory as a Unikraft project
} else if len(workdir) > 0 {
targetName := opts.Target
project, err := app.NewProjectFromOptions(
ctx,
app.WithProjectWorkdir(workdir),
app.WithProjectDefaultKraftfiles(),
)
if err != nil {
return err
}
if len(project.TargetNames()) == 1 {
targetName = project.TargetNames()[0]
if len(opts.Target) > 0 && opts.Target != targetName {
return fmt.Errorf("unknown target: %s", opts.Target)
}
} else if targetName == "" && len(project.Targets()) > 1 {
if config.G[config.KraftKit](ctx).NoPrompt {
return fmt.Errorf("with 'no prompt' enabled please select a target")
}
sp := selection.New("select target:", selectionTargets(project.Targets()))
sp.Filter = nil
selectedTarget, err := sp.RunPrompt()
if err != nil {
return err
}
targetName = selectedTarget
} else if targetName != "" && utils.Contains(project.TargetNames(), targetName) {
return fmt.Errorf("unknown target: %s", targetName)
}
t, err := project.TargetByName(targetName)
if err != nil {
return err
}
// Validate selection of target
if len(opts.Architecture) > 0 && (opts.Architecture != t.Architecture().Name()) {
return fmt.Errorf("selected target (%s) does not match specified architecture (%s)", target.TargetPlatArchName(t), opts.Architecture)
}
if len(opts.Platform) > 0 && (opts.Platform != t.Platform().Name()) {
return fmt.Errorf("selected target (%s) does not match specified platform (%s)", target.TargetPlatArchName(t), opts.Platform)
}
name := t.Name()
if opts.Name != "" {
name = opts.Name
}
mopts = append(mopts,
machine.WithArchitecture(t.Architecture().Name()),
machine.WithPlatform(t.Platform().Name()),
machine.WithName(machine.MachineName(name)),
machine.WithAcceleration(!opts.DisableAccel),
machine.WithSource("project://"+project.Name()+":"+t.Name()),
machine.WithInitRd(opts.InitRd),
)
// Use the symbolic debuggable kernel image?
if opts.WithKernelDbg {
mopts = append(mopts, machine.WithKernel(t.KernelDbg()))
} else {
mopts = append(mopts, machine.WithKernel(t.Kernel()))
}
// If no entity was set earlier and we're not within the context of a working
// directory, then we're unsure what to run
} else if len(entity) == 0 {
return fmt.Errorf("cannot run without providing a working directory (and target), kernel binary or package")
// c). Is the provided first position argument a binary image?
} else if f, err := os.Stat(entity); err == nil && !f.IsDir() {
if len(opts.Architecture) == 0 || len(opts.Platform) == 0 {
return fmt.Errorf("cannot use `kraft run KERNEL` without specifying --arch and --plat")
}
if opts.Name == "" {
opts.Name = namesgenerator.GetRandomName(0)
}
mopts = append(mopts,
machine.WithArchitecture(opts.Architecture),
machine.WithPlatform(opts.Platform),
machine.WithName(machine.MachineName(opts.Name)),
machine.WithKernel(entity),
machine.WithSource("kernel://"+filepath.Base(entity)),
machine.WithInitRd(opts.InitRd),
)
} else {
return fmt.Errorf("could not determine what to run: %s", entity)
}
mopts = append(mopts,
machine.WithMemorySize(uint64(opts.Memory)),
machine.WithArguments(kernelArgs),
)
// Create the machine
mid, err := driver.Create(ctx, mopts...)
if err != nil {
return err
}
log.G(ctx).Debugf("created %s instance %s", driverType.String(), mid.ShortString())
// Tail the logs if -d|--detach is not provided
if !opts.Detach {
go func() {
events, errs, err := driver.ListenStatusUpdate(ctx, mid)
if err != nil {
log.G(ctx).Errorf("could not listen for machine updates: %v", err)
signals.RequestShutdown()
return
}
log.G(ctx).Debug("waiting for machine events")
loop:
for {
// Wait on either channel
select {
case status := <-events:
store.SaveMachineState(mid, status)
switch status {
case machine.MachineStateExited, machine.MachineStateDead:
signals.RequestShutdown()
break loop
}
case err := <-errs:
log.G(ctx).Errorf("received event error: %v", err)
break loop
case <-ctx.Done():
break loop
}
}
}()
}
// Start the machine
if err := driver.Start(ctx, mid); err != nil {
signals.RequestShutdown()
return err
}
if !opts.Detach {
driver.TailWriter(ctx, mid, iostreams.G(ctx).Out)
// Wait for the context to be cancelled, which can occur if a fatal error
// occurs or the user has requested a SIGINT (Ctrl+C).
<-ctx.Done()
// Reading the state to determine whether to invoke a stop request but this
// also simultaneously updates the state if it has exited.
state, stateErr := driver.State(ctx, mid)
// Remove the instance on Ctrl+C if the --rm flag is passed
if opts.Remove {
log.G(ctx).Debugf("removing %s", mid.ShortString())
if err := driver.Destroy(ctx, mid); stateErr == nil && err != nil {
log.G(ctx).
WithField("mid", mid).
Errorf("could not remove: %v", err)
}
} else if state == machine.MachineStateRunning {
log.G(ctx).Debugf("stopping %s", mid.ShortString())
if err := driver.Stop(ctx, mid); stateErr == nil && err != nil {
log.G(ctx).
WithField("mid", mid).
Errorf("could not stop: %v", err)
}
}
}
return nil
}
// selectionTargets returns the given target.Targets in a format suitable for
// interactive prompts.
func selectionTargets(tgts target.Targets) []string {
tgtStrings := make([]string, 0, len(tgts))
for _, tgt := range tgts {
tgtStrings = append(tgtStrings, fmt.Sprintf("%s (%s)", tgt.Name(), target.TargetPlatArchName(tgt)))
}
sort.Strings(tgtStrings)
return tgtStrings
}