-
Notifications
You must be signed in to change notification settings - Fork 64
/
kraft.go
205 lines (174 loc) · 6.43 KB
/
kraft.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
// 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 kraft
import (
"context"
"fmt"
"os"
"github.com/MakeNowJust/heredoc"
"github.com/rancher/wrangler/pkg/signals"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
"kraftkit.sh/internal/bootstrap"
"kraftkit.sh/internal/cli"
"kraftkit.sh/internal/cli/kraft/lib"
"kraftkit.sh/internal/cli/kraft/pause"
kitupdate "kraftkit.sh/internal/update"
kitversion "kraftkit.sh/internal/version"
"kraftkit.sh/iostreams"
"kraftkit.sh/log"
"kraftkit.sh/internal/cli/kraft/build"
"kraftkit.sh/internal/cli/kraft/clean"
"kraftkit.sh/internal/cli/kraft/cloud"
"kraftkit.sh/internal/cli/kraft/compose"
"kraftkit.sh/internal/cli/kraft/events"
"kraftkit.sh/internal/cli/kraft/fetch"
"kraftkit.sh/internal/cli/kraft/login"
"kraftkit.sh/internal/cli/kraft/logs"
"kraftkit.sh/internal/cli/kraft/menu"
"kraftkit.sh/internal/cli/kraft/net"
"kraftkit.sh/internal/cli/kraft/pkg"
"kraftkit.sh/internal/cli/kraft/ps"
"kraftkit.sh/internal/cli/kraft/remove"
"kraftkit.sh/internal/cli/kraft/run"
"kraftkit.sh/internal/cli/kraft/set"
"kraftkit.sh/internal/cli/kraft/start"
"kraftkit.sh/internal/cli/kraft/stop"
"kraftkit.sh/internal/cli/kraft/unset"
"kraftkit.sh/internal/cli/kraft/version"
"kraftkit.sh/internal/cli/kraft/volume"
"kraftkit.sh/internal/cli/kraft/x"
// Additional initializers
_ "kraftkit.sh/manifest"
_ "kraftkit.sh/oci"
)
type KraftOptions struct{}
func NewCmd() *cobra.Command {
cmd, err := cmdfactory.New(&KraftOptions{}, cobra.Command{
Short: "Build and use highly customized and ultra-lightweight unikernels",
Use: "kraft [FLAGS] SUBCOMMAND",
Long: heredoc.Docf(`
.
/^\ Build and use highly customized and ultra-lightweight unikernels.
:[ ]:
| = | Version: %s
/|/=\|\ Documentation: https://kraftkit.sh/
(_:| |:_) Issues & support: https://github.com/unikraft/kraftkit/issues
v v Platform: https://kraft.cloud/ (Join the beta!)
' '`, kitversion.Version()),
CompletionOptions: cobra.CompletionOptions{
HiddenDefaultCmd: true,
},
})
if err != nil {
panic(err)
}
cmd.AddGroup(&cobra.Group{ID: "build", Title: "BUILD COMMANDS"})
cmd.AddCommand(build.NewCmd())
cmd.AddCommand(clean.NewCmd())
cmd.AddCommand(fetch.NewCmd())
cmd.AddCommand(menu.NewCmd())
cmd.AddCommand(set.NewCmd())
cmd.AddCommand(unset.NewCmd())
cmd.AddGroup(&cobra.Group{ID: "lib", Title: "PROJECT LIBRARY COMMANDS"})
cmd.AddCommand(lib.NewCmd())
cmd.AddGroup(&cobra.Group{ID: "pkg", Title: "PACKAGING COMMANDS"})
cmd.AddCommand(pkg.NewCmd())
cmd.AddGroup(&cobra.Group{ID: "run", Title: "LOCAL RUNTIME COMMANDS"})
cmd.AddCommand(events.NewCmd())
cmd.AddCommand(logs.NewCmd())
cmd.AddCommand(ps.NewCmd())
cmd.AddCommand(remove.NewCmd())
cmd.AddCommand(run.NewCmd())
cmd.AddCommand(start.NewCmd())
cmd.AddCommand(stop.NewCmd())
cmd.AddCommand(pause.NewCmd())
cmd.AddGroup(&cobra.Group{ID: "net", Title: "LOCAL NETWORKING COMMANDS"})
cmd.AddCommand(net.NewCmd())
cmd.AddGroup(&cobra.Group{ID: "kraftcloud", Title: "KRAFT CLOUD COMMANDS"})
cmd.AddCommand(cloud.NewCmd())
cmd.AddGroup(&cobra.Group{ID: "kraftcloud-img", Title: "KRAFT CLOUD IMAGE COMMANDS"})
cmd.AddGroup(&cobra.Group{ID: "kraftcloud-instance", Title: "KRAFT CLOUD INSTANCE COMMANDS"})
cmd.AddGroup(&cobra.Group{ID: "kraftcloud-vol", Title: "KRAFT CLOUD VOLUME COMMANDS"})
cmd.AddGroup(&cobra.Group{ID: "kraftcloud-svc", Title: "KRAFT CLOUD SERVICE GROUP COMMANDS"})
cmd.AddGroup(&cobra.Group{ID: "kraftcloud-scale", Title: "KRAFT CLOUD AUTOSCALE COMMANDS"})
cmd.AddGroup(&cobra.Group{ID: "compose", Title: "COMPOSE COMMANDS"})
cmd.AddCommand(compose.NewCmd())
cmd.AddGroup(&cobra.Group{ID: "vol", Title: "LOCAL VOLUME COMMANDS"})
cmd.AddCommand(volume.NewCmd())
cmd.AddCommand(login.NewCmd())
cmd.AddCommand(version.NewCmd())
cmd.AddCommand(x.NewCmd())
return cmd
}
func (k *KraftOptions) Run(_ context.Context, args []string) error {
return pflag.ErrHelp
}
func Main(args []string) int {
cmd := NewCmd()
ctx := signals.SetupSignalContext()
copts := &cli.CliOptions{}
for _, o := range []cli.CliOption{
cli.WithDefaultConfigManager(cmd),
cli.WithDefaultIOStreams(),
cli.WithDefaultPluginManager(),
cli.WithDefaultLogger(),
cli.WithDefaultHTTPClient(),
} {
if err := o(copts); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// Set up the config manager in the context if it is available
if copts.ConfigManager != nil {
ctx = config.WithConfigManager(ctx, copts.ConfigManager)
}
// Hydrate KraftCloud configuration
if newCtx, err := config.HydrateKraftCloudAuthInContext(ctx); err == nil {
ctx = newCtx
}
// Set up the logger in the context if it is available
if copts.Logger != nil {
ctx = log.WithLogger(ctx, copts.Logger)
}
// Set up the iostreams in the context if it is available
if copts.IOStreams != nil {
ctx = iostreams.WithIOStreams(ctx, copts.IOStreams)
}
if (os.Getenv("SUDO_UID") != "" || os.Getenv("SUDO_GID") != "" || os.Getenv("SUDO_USER") != "") && !config.G[config.KraftKit](ctx).NoWarnSudo {
log.G(ctx).Warn("detected invocation via sudo!")
log.G(ctx).Warn("")
log.G(ctx).Warn("mixing invocations of kraft with sudo can lead to unexpected behavior")
log.G(ctx).Warn("read more on how to use kraft without sudo at:")
log.G(ctx).Warn("")
log.G(ctx).Warn("\thttps://unikraft.org/sudoless")
log.G(ctx).Warn("")
log.G(ctx).Warn("to hide and ignore this warning message, set the environmental variable:")
log.G(ctx).Warn("")
log.G(ctx).Warn("\texport KRAFTKIT_NO_WARN_SUDO=1")
log.G(ctx).Warn("")
}
// Add the kraftkit version to the debug logs
log.G(ctx).Debugf("kraftkit %s", kitversion.Version())
if !config.G[config.KraftKit](ctx).NoCheckUpdates {
if err := kitupdate.Check(ctx); err != nil {
log.G(ctx).Debugf("could not check for updates: %v", err)
log.G(ctx).Debug("")
log.G(ctx).Debug("to turn off this check, set:")
log.G(ctx).Debug("")
log.G(ctx).Debug("\texport KRAFTKIT_NO_CHECK_UPDATES=true")
log.G(ctx).Debug("")
log.G(ctx).Debug("or use the globally accessible flag '--no-check-updates'")
}
}
if err := bootstrap.InitKraftkit(ctx); err != nil {
log.G(ctx).Errorf("could not init kraftkit: %v", err)
os.Exit(1)
}
return cmdfactory.Main(ctx, cmd)
}