-
Notifications
You must be signed in to change notification settings - Fork 64
/
packager_cli_kernel.go
118 lines (98 loc) · 2.97 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
// 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
}
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)
}
if opts.Rootfs, err = utils.BuildRootfs(ctx, opts.Workdir, opts.Rootfs, targ); err != nil {
return nil, fmt.Errorf("could not build rootfs: %w", err)
}
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),
)
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
}