-
Notifications
You must be signed in to change notification settings - Fork 63
/
pkg.go
245 lines (204 loc) · 7.47 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
// 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 pkg
import (
"context"
"fmt"
"os"
"github.com/MakeNowJust/heredoc"
"github.com/mattn/go-shellwords"
"github.com/spf13/cobra"
"kraftkit.sh/config"
"kraftkit.sh/machine/platform"
"kraftkit.sh/pack"
"kraftkit.sh/unikraft"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/log"
"kraftkit.sh/packmanager"
"kraftkit.sh/tui/processtree"
"kraftkit.sh/unikraft/app"
"kraftkit.sh/cmd/kraft/pkg/list"
"kraftkit.sh/cmd/kraft/pkg/pull"
"kraftkit.sh/cmd/kraft/pkg/push"
"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"`
Args string `local:"true" long:"args" short:"a" usage:"Pass arguments that will be part of the running kernel's command line"`
Dbg bool `local:"true" long:"dbg" usage:"Package the debuggable (symbolic) kernel image instead of the stripped image"`
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"`
Kraftfile string `long:"kraftfile" usage:"Set an alternative path of the Kraftfile"`
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"`
WithKConfig bool `local:"true" long:"with-kconfig" usage:"Include the target .config"`
}
func New() *cobra.Command {
cmd, err := 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.
`, "`"),
Example: heredoc.Doc(`
# Package a project as an OCI archive and embed the target's KConfig.
$ kraft pkg --as oci --name unikraft.org/nginx:latest --with-kconfig`),
Annotations: map[string]string{
cmdfactory.AnnotationHelpGroup: "pkg",
},
})
if err != nil {
panic(err)
}
cmd.AddCommand(list.New())
cmd.AddCommand(pull.New())
cmd.AddCommand(push.New())
cmd.AddCommand(source.New())
cmd.AddCommand(unsource.New())
cmd.AddCommand(update.New())
return cmd
}
func (opts *Pkg) Pre(cmd *cobra.Command, _ []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`")
}
ctx := cmd.Context()
pm, err := packmanager.NewUmbrellaManager(ctx)
if err != nil {
return err
}
cmd.SetContext(packmanager.WithPackageManager(ctx, pm))
opts.Platform = platform.PlatformByName(opts.Platform).String()
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()
popts := []app.ProjectOption{
app.WithProjectWorkdir(workdir),
}
if len(opts.Kraftfile) > 0 {
popts = append(popts, app.WithProjectKraftfile(opts.Kraftfile))
} else {
popts = append(popts, app.WithProjectDefaultKraftfiles())
}
// Interpret the project directory
project, err := app.NewProjectFromOptions(ctx, popts...)
if err != nil {
return err
}
var tree []*processtree.ProcessTreeItem
parallel := !config.G[config.KraftKit](ctx).NoParallel
norender := log.LoggerTypeFromString(config.G[config.KraftKit](ctx).Log.Type) != log.FANCY
// Generate a package for every matching requested target
for _, targ := range project.Targets() {
// See: https://github.com/golang/go/wiki/CommonMistakes#using-reference-to-loop-iterator-variable
targ := targ
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:
var format pack.PackageFormat
name := "packaging " + targ.Name()
if opts.Format != "auto" {
format = pack.PackageFormat(opts.Format)
} else if targ.Format().String() != "" {
format = targ.Format()
}
if format.String() != "auto" {
name += " (" + format.String() + ")"
}
cmdShellArgs, err := shellwords.Parse(opts.Args)
if err != nil {
return err
}
tree = append(tree, processtree.NewProcessTreeItem(
name,
targ.Architecture().Name()+"/"+targ.Platform().Name(),
func(ctx context.Context) error {
var err error
pm := packmanager.G(ctx)
// Switch the package manager the desired format for this target
if format != "auto" {
pm, err = pm.From(format)
if err != nil {
return err
}
}
popts := []packmanager.PackOption{
packmanager.PackArgs(cmdShellArgs...),
packmanager.PackInitrd(opts.Initrd),
packmanager.PackKConfig(opts.WithKConfig),
packmanager.PackName(opts.Name),
packmanager.PackOutput(opts.Output),
}
if ukversion, ok := targ.KConfig().Get(unikraft.UK_FULLVERSION); ok {
popts = append(popts,
packmanager.PackWithKernelVersion(ukversion.Value),
)
}
if _, err := pm.Pack(ctx, targ, popts...); err != nil {
return err
}
return nil
},
))
default:
continue
}
}
model, err := processtree.NewProcessTree(
ctx,
[]processtree.ProcessTreeOption{
processtree.IsParallel(parallel),
processtree.WithRenderer(norender),
},
tree...,
)
if err != nil {
return err
}
return model.Start()
}