-
Notifications
You must be signed in to change notification settings - Fork 63
/
packager_cli_kernel.go
139 lines (115 loc) · 3.5 KB
/
packager_cli_kernel.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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2023, 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"
"strings"
"github.com/mattn/go-shellwords"
"kraftkit.sh/config"
"kraftkit.sh/internal/cli/kraft/utils"
"kraftkit.sh/log"
"kraftkit.sh/pack"
"kraftkit.sh/packmanager"
"kraftkit.sh/tui/processtree"
"kraftkit.sh/unikraft/arch"
"kraftkit.sh/unikraft/plat"
"kraftkit.sh/unikraft/target"
)
type packagerCliKernel struct{}
// String implements fmt.Stringer.
func (p *packagerCliKernel) String() string {
return "cli-kernel"
}
// Packagable implements packager.
func (p *packagerCliKernel) Packagable(ctx context.Context, opts *PkgOptions, args ...string) (bool, error) {
if len(opts.Kernel) > 0 && len(opts.Architecture) > 0 && len(opts.Platform) > 0 {
return true, nil
}
if len(opts.Kernel) > 0 {
log.G(ctx).Warn("--kernel flag set but must be used in conjunction with -m|--arch and -p|--plat")
}
return false, fmt.Errorf("cannot package without path to -k|-kernel, -m|--arch and -p|--plat")
}
// Pack implements packager.
func (p *packagerCliKernel) Pack(ctx context.Context, opts *PkgOptions, args ...string) ([]pack.Package, error) {
ac, err := arch.NewArchitectureFromOptions(
arch.WithName(opts.Architecture),
)
if err != nil {
return nil, fmt.Errorf("could not prepare architecture: %w", err)
}
pc, err := plat.NewPlatformFromOptions(
plat.WithName(opts.Platform),
)
if err != nil {
return nil, fmt.Errorf("could not prepare architecture: %w", err)
}
targ, err := target.NewTargetFromOptions(
target.WithArchitecture(ac),
target.WithPlatform(pc),
target.WithKernel(opts.Kernel),
target.WithCommand(opts.Args),
)
if err != nil {
return nil, fmt.Errorf("could not prepare phony target: %w", err)
}
var cmds []string
var envs []string
if opts.Rootfs, cmds, envs, err = utils.BuildRootfs(ctx, opts.Workdir, opts.Rootfs, opts.Compress, targ); err != nil {
return nil, fmt.Errorf("could not build rootfs: %w", err)
}
if len(opts.Args) == 0 && cmds != nil {
opts.Args = cmds
}
if envs != nil {
opts.Env = append(opts.Env, envs...)
}
cmdShellArgs, err := shellwords.Parse(strings.Join(opts.Args, " "))
if err != nil {
return nil, err
}
var result []pack.Package
norender := log.LoggerTypeFromString(config.G[config.KraftKit](ctx).Log.Type) != log.FANCY
model, err := processtree.NewProcessTree(
ctx,
[]processtree.ProcessTreeOption{
processtree.IsParallel(false),
processtree.WithRenderer(norender),
},
processtree.NewProcessTreeItem(
"packaging "+opts.Name+" ("+opts.Format+")",
opts.Platform+"/"+opts.Architecture,
func(ctx context.Context) error {
popts := append(opts.packopts,
packmanager.PackArgs(cmdShellArgs...),
packmanager.PackInitrd(opts.Rootfs),
packmanager.PackKConfig(!opts.NoKConfig),
packmanager.PackName(opts.Name),
packmanager.PackOutput(opts.Output),
)
envs := opts.aggregateEnvs()
if len(envs) > 0 {
popts = append(popts, packmanager.PackWithEnvs(envs))
} else if len(opts.Env) > 0 {
popts = append(popts, packmanager.PackWithEnvs(opts.Env))
}
more, err := opts.pm.Pack(ctx, targ, popts...)
if err != nil {
return err
}
result = append(result, more...)
return nil
},
),
)
if err != nil {
return nil, err
}
if err := model.Start(); err != nil {
return nil, err
}
return result, nil
}