/
main.go
433 lines (378 loc) · 10.5 KB
/
main.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package main
import (
"context"
"fmt"
"os"
agent "github.com/kairos-io/kairos/internal/agent"
"github.com/kairos-io/kairos/internal/bus"
"github.com/kairos-io/kairos/internal/webui"
"github.com/kairos-io/kairos/internal/common"
"github.com/kairos-io/kairos/pkg/config"
machine "github.com/kairos-io/kairos/pkg/machine"
"github.com/kairos-io/kairos/pkg/utils"
bundles "github.com/kairos-io/kairos/sdk/bundles"
"github.com/kairos-io/kairos/sdk/state"
"github.com/urfave/cli"
)
var configScanDir = []string{"/oem", "/usr/local/cloud-config", "/run/initramfs/live"}
var cmds = []cli.Command{
{
Name: "upgrade",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "force",
Usage: "Force an upgrade",
},
&cli.BoolFlag{
Name: "debug",
Usage: "Show debug output",
},
&cli.StringFlag{
Name: "image",
Usage: "Specify an full image reference, e.g.: quay.io/some/image:tag",
},
},
Description: `
Manually upgrade a kairos node.
By default takes no arguments, defaulting to latest available release, to specify a version, pass it as argument:
$ kairos upgrade v1.20....
To retrieve all the available versions, use "kairos upgrade list-releases"
$ kairos upgrade list-releases
See https://kairos.io/docs/upgrade/manual/ for documentation.
`,
Subcommands: []cli.Command{
{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "output",
Usage: "Output format (json|yaml|terminal)",
},
},
Name: "list-releases",
Description: `List all available releases versions`,
Action: func(c *cli.Context) error {
releases := agent.ListReleases()
list := utils.ListOutput(releases, c.String("output"))
for _, i := range list {
fmt.Println(i)
}
return nil
},
},
},
Action: func(c *cli.Context) error {
args := c.Args()
var v string
if len(args) == 1 {
v = args[0]
}
return agent.Upgrade(v, c.String("image"), c.Bool("force"), c.Bool("debug"), configScanDir)
},
},
{
Name: "notify",
Usage: "notify <event> <config dir>...",
UsageText: "emits the given event with a generic event payload",
Description: `
Sends a generic event payload with the configuration found in the scanned directories.
`,
Aliases: []string{},
Flags: []cli.Flag{},
Action: func(c *cli.Context) error {
dirs := []string{"/oem", "/usr/local/cloud-config"}
args := c.Args()
if len(args) > 1 {
dirs = args[1:]
}
return agent.Notify(args[0], dirs)
},
},
{
Name: "start",
Usage: "Starts the kairos agent",
UsageText: "starts the agent",
Description: `
Starts the kairos agent which automatically bootstrap and advertize to the kairos network.
`,
Aliases: []string{"s"},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "restart",
},
&cli.BoolFlag{
Name: "force",
},
&cli.StringFlag{
Name: "api",
Value: "http://127.0.0.1:8080",
},
},
Action: func(c *cli.Context) error {
dirs := []string{"/oem", "/usr/local/cloud-config"}
args := c.Args()
if len(args) > 0 {
dirs = args
}
opts := []agent.Option{
agent.WithAPI(c.String("api")),
agent.WithDirectory(dirs...),
}
if c.Bool("force") {
opts = append(opts, agent.ForceAgent)
}
if c.Bool("restart") {
opts = append(opts, agent.RestartAgent)
}
return agent.Run(opts...)
},
},
{
Name: "install-bundle",
Usage: "Installs a kairos bundle",
Description: `
Manually installs a kairos bundle.
E.g. kairos-agent install-bundle container:quay.io/kairos/kairos...
`,
Aliases: []string{"i"},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "repository",
EnvVar: "REPOSITORY",
Value: "docker://quay.io/kairos/packages",
},
},
UsageText: "Install a bundle manually in the node",
Action: func(c *cli.Context) error {
args := c.Args()
if len(args) != 1 {
return fmt.Errorf("bundle name required")
}
return bundles.RunBundles([]bundles.BundleOption{bundles.WithRepository(c.String("repository")), bundles.WithTarget(args[0])})
},
},
{
Name: "uuid",
Usage: "Prints the local UUID",
Description: "Print node uuid",
Aliases: []string{"u"},
Action: func(c *cli.Context) error {
fmt.Print(machine.UUID())
return nil
},
},
{
Name: "webui",
Usage: "Starts the webui",
Description: "Starts the webui installer",
Aliases: []string{"w"},
Action: func(c *cli.Context) error {
return webui.Start(context.Background())
//return nil
},
},
{
Name: "config",
Usage: "get machine configuration",
Description: "Print machine state information, e.g. `state get uuid` returns the machine uuid",
Aliases: []string{"c"},
Action: func(c *cli.Context) error {
runtime, err := state.NewRuntime()
if err != nil {
return err
}
fmt.Print(runtime)
return err
},
Subcommands: []cli.Command{
{
Name: "show",
Usage: "Shows the machine configuration",
Description: "Show the runtime configuration of the machine. It will scan the machine for all the configuration and will return the config file processed and found.",
Aliases: []string{"s"},
Action: func(c *cli.Context) error {
config, err := config.Scan(config.Directories(configScanDir...), config.NoLogs)
if err != nil {
return err
}
fmt.Printf("%s", config.String())
return nil
},
},
{
Name: "get",
Usage: "Get specific data from the configuration",
UsageText: `
Use it to retrieve configuration programmatically from the CLI:
$ kairos-agent config get k3s.enabled
true
or
$ kairos-agent config get k3s
enabled: true`,
Description: "It allows to navigate the YAML config file by searching with 'yq' style keywords as `config get k3s` to retrieve the k3s config block",
Aliases: []string{"g"},
Action: func(c *cli.Context) error {
config, err := config.Scan(config.Directories(configScanDir...), config.NoLogs)
if err != nil {
return err
}
res, err := config.Query(c.Args().First())
if err != nil {
return err
}
fmt.Printf("%s", res)
return nil
},
},
},
},
{
Name: "state",
Usage: "get machine state",
Description: "Print machine state information, e.g. `state get uuid` returns the machine uuid",
Aliases: []string{"s"},
Action: func(c *cli.Context) error {
runtime, err := state.NewRuntime()
if err != nil {
return err
}
fmt.Print(runtime)
return err
},
Subcommands: []cli.Command{
{
Name: "apply",
Usage: "Applies a machine state",
Description: "Applies machine configuration in runtimes",
Aliases: []string{"a"},
Action: func(c *cli.Context) error {
// TODO
return nil
},
},
{
Name: "get",
Usage: "get specific ",
Description: "query state data",
Aliases: []string{"g"},
Action: func(c *cli.Context) error {
runtime, err := state.NewRuntime()
if err != nil {
return err
}
res, err := runtime.Query(c.Args().First())
fmt.Print(res)
return err
},
},
},
},
{
Name: "interactive-install",
Description: `
Starts kairos in interactive mode install.
It will ask prompt for several questions and perform an install depending on the providers available in the system.
See also https://kairos.io/installation/interactive_install/ for documentation.
This command is meant to be used from the boot GRUB menu, but can be also started manually`,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "shell",
},
},
Usage: "Starts interactive installation",
Action: func(c *cli.Context) error {
return agent.InteractiveInstall(c.Bool("shell"))
},
},
{
Name: "manual-install",
Usage: "Starts the manual installation",
Description: `
`,
Aliases: []string{"m"},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "device",
},
&cli.BoolFlag{
Name: "poweroff",
},
&cli.BoolFlag{
Name: "reboot",
},
},
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
return fmt.Errorf("expect one argument. the config file - if you don't have it, use the interactive-install")
}
config := c.Args().First()
options := map[string]string{"device": c.String("device")}
if c.Bool("poweroff") {
options["poweroff"] = "true"
}
if c.Bool("reboot") {
options["reboot"] = "true"
}
return agent.ManualInstall(config, options)
},
},
{
Name: "install",
Usage: "Starts the kairos pairing installation",
Description: `
Starts kairos in pairing mode.
It will print out a QR code which can be used with "kairos register" to send over a configuration and bootstraping a kairos node.
See also https://kairos.io/installation/device_pairing/ for documentation.
This command is meant to be used from the boot GRUB menu, but can be started manually`,
Aliases: []string{"i"},
Action: func(c *cli.Context) error {
return agent.Install(configScanDir...)
},
},
{
Name: "recovery",
Aliases: []string{"r"},
Action: func(c *cli.Context) error {
return agent.Recovery()
},
Usage: "Starts kairos recovery mode",
Description: `
Starts kairos recovery mode.
In recovery mode a QR code will be printed out on the screen which should be used in conjunction with "kairos bridge". Pass by the QR code as snapshot
to the bridge to connect over the machine which runs the "kairos recovery" command.
See also https://kairos.io/after_install/recovery_mode/ for documentation.
This command is meant to be used from the boot GRUB menu, but can likely be used standalone`,
},
{
Name: "reset",
Action: func(c *cli.Context) error {
return agent.Reset(configScanDir...)
},
Usage: "Starts kairos reset mode",
Description: `
Starts kairos reset mode, it will nuke completely the node data and restart fresh.
Attention ! this will delete any persistent data on the node. It is equivalent to re-init the node right after the installation.
In reset mode a the node will automatically reset
See also https://kairos.io/after_install/reset_mode/ for documentation.
This command is meant to be used from the boot GRUB menu, but can likely be used standalone`,
},
}
func main() {
bus.Manager.Initialize()
app := &cli.App{
Name: "kairos-agent",
Version: common.VERSION,
Author: "Ettore Di Giacinto",
Usage: "kairos agent start",
Description: `
The kairos agent is a component to abstract away node ops, providing a common feature-set across kairos variants.
`,
UsageText: ``,
Copyright: "kairos authors",
Commands: cmds,
}
err := app.Run(os.Args)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}