-
Notifications
You must be signed in to change notification settings - Fork 64
/
pkg.go
309 lines (257 loc) · 9.27 KB
/
pkg.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
// 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 expect in compliance with the License.
package pkg
import (
"context"
"fmt"
"os"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"kraftkit.sh/config"
"kraftkit.sh/unikraft"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/initrd"
"kraftkit.sh/log"
"kraftkit.sh/pack"
"kraftkit.sh/packmanager"
"kraftkit.sh/tui/processtree"
"kraftkit.sh/unikraft/app"
"kraftkit.sh/unikraft/target"
"kraftkit.sh/cmd/kraft/pkg/list"
"kraftkit.sh/cmd/kraft/pkg/pull"
"kraftkit.sh/cmd/kraft/pkg/source"
"kraftkit.sh/cmd/kraft/pkg/unsource"
"kraftkit.sh/cmd/kraft/pkg/update"
)
type Pkg struct {
Architecture string `local:"true" long:"arch" short:"m" usage:"Filter the creation of the package by architecture of known targets"`
DotConfig string `local:"true" long:"config" short:"c" usage:"Override the path to the KConfig .config file"`
Force bool `local:"true" long:"force-format" usage:"Force the use of a packaging handler format"`
Format string `local:"true" long:"as" short:"M" usage:"Force the packaging despite possible conflicts" default:"auto"`
Initrd string `local:"true" long:"initrd" short:"i" usage:"Path to init ramdisk to bundle within the package (passing a path will automatically generate a CPIO image)"`
Kernel string `local:"true" long:"kernel" short:"k" usage:"Override the path to the unikernel image"`
KernelDbg bool `local:"true" long:"dbg" usage:"Package the debuggable (symbolic) kernel image instead of the stripped image"`
Name string `local:"true" long:"name" short:"n" usage:"Specify the name of the package"`
Output string `local:"true" long:"output" short:"o" usage:"Save the package at the following output"`
Platform string `local:"true" long:"plat" short:"p" usage:"Filter the creation of the package by platform of known targets"`
Target string `local:"true" long:"target" short:"t" usage:"Package a particular known target"`
Volumes []string `local:"true" long:"volume" short:"v" usage:"Additional volumes to bundle within the package"`
WithDbg bool `local:"true" long:"with-dbg" usage:"In addition to the stripped kernel, include the debug image"`
}
func New() *cobra.Command {
cmd := cmdfactory.New(&Pkg{}, cobra.Command{
Short: "Package and distribute Unikraft unikernels and their dependencies",
Use: "pkg [FLAGS] [SUBCOMMAND|DIR]",
Args: cmdfactory.MaxDirArgs(1),
Long: heredoc.Docf(`
Package and distribute Unikraft unikernels and their dependencies.
With %[1]skraft pkg%[1]s you are able to turn output artifacts from %[1]skraft build%[1]s
into a distributable archive ready for deployment. At the same time,
%[1]skraft pkg%[1]s allows you to manage these archives: pulling, pushing, or
adding them to a project.
The default behaviour of %[1]skraft pkg%[1]s is to package a project. Given no
arguments, you will be guided through interactive mode.
For initram and disk images, passing in a directory as the argument will
result automatically packaging that directory into the requested format.
Separating the input with a %[1]s:%[1]s delimiter allows you to set the
output that of the artifact.
`, "`"),
Example: heredoc.Doc(`
# Package the current Unikraft project (cwd)
$ kraft pkg
# Package path to a Unikraft project
$ kraft pkg path/to/application
# Package with an additional initramfs
$ kraft pkg --initrd ./root-fs .
# Same as above but also save the resulting CPIO artifact locally
$ kraft pkg --initrd ./root-fs:./root-fs.cpio .`),
Annotations: map[string]string{
"help:group": "pkg",
},
})
cmd.AddCommand(list.New())
cmd.AddCommand(pull.New())
cmd.AddCommand(source.New())
cmd.AddCommand(unsource.New())
cmd.AddCommand(update.New())
return cmd
}
func (opts *Pkg) Pre(cmd *cobra.Command, args []string) error {
if (len(opts.Architecture) > 0 || len(opts.Platform) > 0) && len(opts.Target) > 0 {
return fmt.Errorf("the `--arch` and `--plat` options are not supported in addition to `--target`")
}
return nil
}
func (opts *Pkg) Run(cmd *cobra.Command, args []string) error {
var err error
var workdir string
if len(args) == 0 {
workdir, err = os.Getwd()
if err != nil {
return err
}
} else {
workdir = args[0]
}
ctx := cmd.Context()
// Interpret the project directory
project, err := app.NewProjectFromOptions(
app.WithProjectWorkdir(workdir),
app.WithProjectDefaultKraftfiles(),
)
if err != nil {
return err
}
var packages []pack.Package
// Generate a package for every matching requested target
targets, err := project.Targets()
if err != nil {
return err
}
for _, targ := range targets {
switch true {
case
// If no arguments are supplied
len(opts.Target) == 0 &&
len(opts.Architecture) == 0 &&
len(opts.Platform) == 0,
// If the --target flag is supplied and the target name match
len(opts.Target) > 0 &&
targ.Name() == opts.Target,
// If only the --arch flag is supplied and the target's arch matches
len(opts.Architecture) > 0 &&
len(opts.Platform) == 0 &&
targ.Architecture.Name() == opts.Architecture,
// If only the --plat flag is supplied and the target's platform matches
len(opts.Platform) > 0 &&
len(opts.Architecture) == 0 &&
targ.Platform.Name() == opts.Platform,
// If both the --arch and --plat flag are supplied and match the target
len(opts.Platform) > 0 &&
len(opts.Architecture) > 0 &&
targ.Architecture.Name() == opts.Architecture &&
targ.Platform.Name() == opts.Platform:
packs, err := initAppPackage(ctx, project, targ, opts)
if err != nil {
return fmt.Errorf("could not create package: %s", err)
}
packages = append(packages, packs...)
default:
continue
}
}
if len(packages) == 0 {
log.G(ctx).Info("nothing to package")
return nil
}
parallel := !config.G(ctx).NoParallel
norender := log.LoggerTypeFromString(config.G(ctx).Log.Type) != log.FANCY
var tree []*processtree.ProcessTreeItem
for _, p := range packages {
// See: https://github.com/golang/go/wiki/CommonMistakes#using-reference-to-loop-iterator-variable
p := p
tree = append(tree, processtree.NewProcessTreeItem(
"Packaging "+p.CanonicalName(),
p.Options().ArchPlatString(),
func(ctx context.Context) error {
return p.Pack(ctx)
},
))
}
model, err := processtree.NewProcessTree(
ctx,
[]processtree.ProcessTreeOption{
processtree.WithVerb("Packaging..."),
processtree.IsParallel(parallel),
processtree.WithRenderer(norender),
},
tree...,
)
if err != nil {
return err
}
return model.Start()
}
func initAppPackage(ctx context.Context,
project *app.ApplicationConfig,
targ target.TargetConfig,
opts *Pkg,
) ([]pack.Package, error) {
var err error
log.G(ctx).Tracef("initializing package")
// Path to the kernel image
kernel := opts.Kernel
if len(kernel) == 0 {
kernel = targ.Kernel
}
// Prefer the debuggable (symbolic) kernel as the main kernel
if opts.KernelDbg && !opts.WithDbg {
kernel = targ.KernelDbg
}
name := opts.Name
targets, err := project.Targets()
if err != nil {
return nil, err
}
// This is a built in naming convention format, which for now allows us to
// differentiate between different targets. This should be further discussed
// the community if this is the best approach. This can ultimately be
// overwritten using the --tag flag.
if len(name) == 0 && len(targets) == 1 {
name = project.Name()
} else if len(name) == 0 {
name = project.Name() + "-" + targ.Name()
}
version := project.Version()
if len(version) == 0 {
version = "latest"
}
extraPackOpts := []pack.PackageOption{
pack.WithName(name),
pack.WithVersion(version),
pack.WithType(unikraft.ComponentTypeApp),
pack.WithArchitecture(targ.Architecture.Name()),
pack.WithPlatform(targ.Platform.Name()),
pack.WithKernel(kernel),
pack.WithWorkdir(project.WorkingDir()),
pack.WithLocalLocation(opts.Output, opts.Force),
}
// Options for the initramfs if set
initrdConfig := targ.Initrd
if len(opts.Initrd) > 0 {
initrdConfig, err = initrd.ParseInitrdConfig(project.WorkingDir(), opts.Initrd)
if err != nil {
return nil, fmt.Errorf("could not parse --initrd flag with value %s: %s", opts.Initrd, err)
}
}
// Warn if potentially missing configuration options
// if initrdConfig != nil && unikraft.EnabledInitramfs()
extraPackOpts = append(extraPackOpts,
pack.WithInitrdConfig(initrdConfig),
)
packOpts, err := pack.NewPackageOptions(extraPackOpts...)
if err != nil {
return nil, fmt.Errorf("could not prepare package for target: %s: %v", targ.Name(), err)
}
pm := packmanager.G(ctx)
// Switch the package manager the desired format for this target
if len(targ.Format) > 0 && targ.Format != "auto" {
if pm.Format() == "umbrella" {
pm, err = pm.From(targ.Format)
if err != nil {
return nil, err
}
// Skip this target as we cannot package it
} else if pm.Format() != targ.Format && !opts.Force {
log.G(ctx).Warnf("skipping %s target %s", targ.Format, targ.Name())
return nil, nil
}
}
pack, err := pm.NewPackageFromOptions(ctx, packOpts)
if err != nil {
return nil, fmt.Errorf("could not initialize package: %s", err)
}
return pack, nil
}