-
Notifications
You must be signed in to change notification settings - Fork 64
/
runner_kraftfile_runtime.go
242 lines (205 loc) · 6.32 KB
/
runner_kraftfile_runtime.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
// 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"
machineapi "kraftkit.sh/api/machine/v1alpha1"
"kraftkit.sh/config"
"kraftkit.sh/log"
"kraftkit.sh/machine/platform"
"kraftkit.sh/pack"
"kraftkit.sh/packmanager"
"kraftkit.sh/tui/paraprogress"
"kraftkit.sh/unikraft/app"
ukarch "kraftkit.sh/unikraft/arch"
"kraftkit.sh/unikraft/target"
)
type runnerKraftfileRuntime struct {
args []string
project app.Application
}
// String implements Runner.
func (runner *runnerKraftfileRuntime) String() string {
return "kraftfile-runtime"
}
// Runnable implements Runner.
func (runner *runnerKraftfileRuntime) Runnable(ctx context.Context, opts *Run, args ...string) (bool, error) {
var err error
cwd, err := os.Getwd()
if err != nil {
return false, fmt.Errorf("getting current working directory: %w", err)
}
if len(args) == 0 {
opts.workdir = cwd
} else {
opts.workdir = cwd
runner.args = args
if f, err := os.Stat(args[0]); err == nil && f.IsDir() {
opts.workdir = args[0]
runner.args = args[1:]
}
}
if !app.IsWorkdirInitialized(opts.workdir) {
return false, fmt.Errorf("path is not project: %s", opts.workdir)
}
popts := []app.ProjectOption{
app.WithProjectWorkdir(opts.workdir),
}
if len(opts.Kraftfile) > 0 {
popts = append(popts, app.WithProjectKraftfile(opts.Kraftfile))
} else {
popts = append(popts, app.WithProjectDefaultKraftfiles())
}
runner.project, err = app.NewProjectFromOptions(ctx, popts...)
if err != nil {
return false, fmt.Errorf("could not instantiate project directory %s: %v", opts.workdir, err)
}
if runner.project.Runtime() == nil {
return false, fmt.Errorf("cannot run project without runtime directive")
}
return true, nil
}
// Prepare implements Runner.
func (runner *runnerKraftfileRuntime) Prepare(ctx context.Context, opts *Run, machine *machineapi.Machine, args ...string) error {
var err error
var targ target.Target
targets := runner.project.Targets()
qopts := []packmanager.QueryOption{
packmanager.WithName(runner.project.Runtime().Name()),
packmanager.WithVersion(runner.project.Runtime().Version()),
packmanager.WithUpdate(true),
}
if len(targets) == 1 {
targ = targets[0]
} else if len(targets) > 1 {
// Filter project targets by any provided CLI options
targets = target.Filter(
targets,
opts.Architecture,
opts.platform.String(),
opts.Target,
)
switch {
case len(targets) == 0:
return fmt.Errorf("could not detect any project targets based on plat=\"%s\" arch=\"%s\"", opts.platform.String(), opts.Architecture)
case len(targets) == 1:
targ = targets[0]
case config.G[config.KraftKit](ctx).NoPrompt && len(targets) > 1:
return fmt.Errorf("could not determine what to run based on provided CLI arguments")
default:
targ, err = target.Select(targets)
if err != nil {
return fmt.Errorf("could not select target: %v", err)
}
}
}
if targ != nil {
var kconfigs []string
for _, kc := range targ.KConfig() {
kconfigs = append(kconfigs, kc.String())
}
qopts = append(qopts,
packmanager.WithPlatform(targ.Platform().Name()),
packmanager.WithArchitecture(targ.Architecture().Name()),
packmanager.WithKConfig(kconfigs),
)
} else {
arch, err := ukarch.HostArchitecture()
if err != nil {
return fmt.Errorf("could not get host architecture: %w", err)
}
plat, _, err := platform.Detect(ctx)
if err != nil {
return fmt.Errorf("could not get host platform: %w", err)
}
// Use host information
qopts = append(qopts,
packmanager.WithPlatform(plat.String()),
packmanager.WithArchitecture(arch),
)
}
packs, err := packmanager.G(ctx).Catalog(ctx, qopts...)
if err != nil {
return fmt.Errorf("could not query catalog: %w", err)
} else if len(packs) == 0 {
return fmt.Errorf("coud not find runtime '%s'", runner.project.Runtime().Name())
} else if len(packs) > 1 {
return fmt.Errorf("could not find runtime: too many options")
}
if runner.project.Rootfs() != "" && opts.Rootfs == "" {
opts.Rootfs = runner.project.Rootfs()
}
// Create a temporary directory where the image can be stored
tempDir, err := os.MkdirTemp("", "kraft-run-")
if err != nil {
return err
}
paramodel, err := paraprogress.NewParaProgress(
ctx,
[]*paraprogress.Process{paraprogress.NewProcess(
fmt.Sprintf("pulling %s", packs[0].String()),
func(ctx context.Context, w func(progress float64)) error {
popts := []pack.PullOption{
pack.WithPullWorkdir(tempDir),
}
if log.LoggerTypeFromString(config.G[config.KraftKit](ctx).Log.Type) == log.FANCY {
popts = append(popts, pack.WithPullProgressFunc(w))
}
return packs[0].Pull(
ctx,
popts...,
)
},
)},
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.
runtime, ok := packs[0].(target.Target)
if !ok {
return fmt.Errorf("package does not convert to target")
}
machine.Spec.Architecture = runtime.Architecture().Name()
machine.Spec.Platform = runtime.Platform().Name()
machine.Spec.Kernel = fmt.Sprintf("%s://%s:%s", packs[0].Format(), runtime.Name(), runtime.Version())
if len(runner.project.Command()) > 0 {
machine.Spec.ApplicationArgs = runner.project.Command()
} else if len(runtime.Command()) > 0 {
machine.Spec.ApplicationArgs = runtime.Command()
}
// Use the symbolic debuggable kernel image?
if opts.WithKernelDbg {
machine.Status.KernelPath = runtime.KernelDbg()
} else {
machine.Status.KernelPath = runtime.Kernel()
}
if opts.Rootfs == "" {
if runner.project.Rootfs() != "" {
opts.Rootfs = runner.project.Rootfs()
} else if runtime.Initrd() != nil {
machine.Status.InitrdPath, err = runtime.Initrd().Build(ctx)
if err != nil {
return err
}
}
}
if err := opts.parseKraftfileVolumes(ctx, runner.project, machine); err != nil {
return err
}
return nil
}