-
Notifications
You must be signed in to change notification settings - Fork 64
/
pkg.go
222 lines (184 loc) · 6.78 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
// 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/spf13/cobra"
"kraftkit.sh/config"
"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/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"`
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"`
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"`
}
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{
cmdfactory.AnnotationHelpGroup: "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(
ctx,
app.WithProjectWorkdir(workdir),
app.WithProjectDefaultKraftfiles(),
)
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:
format := opts.Format
name := "Packaging " + targ.Name()
if opts.Format != "auto" {
format = opts.Format
} else if targ.Format() != "" {
format = targ.Format()
}
if format != "auto" {
name += " (" + format + ")"
}
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
}
}
single, err := project.WithTarget(targ)
if err != nil {
return err
}
if _, err := pm.Pack(ctx, single,
packmanager.PackOutput(opts.Output),
packmanager.PackInitrd(opts.Initrd),
); err != nil {
return err
}
return nil
},
))
default:
continue
}
}
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()
}