forked from canonical/lxd
/
config.go
607 lines (536 loc) · 13.4 KB
/
config.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"github.com/gosexy/gettext"
"github.com/lxc/lxd"
"github.com/lxc/lxd/shared"
"gopkg.in/yaml.v2"
)
type configCmd struct {
httpAddr string
}
func (c *configCmd) showByDefault() bool {
return true
}
var profileEditHelp string = gettext.Gettext(
"### This is a yaml representation of the profile.\n" +
"### Any line starting with a '# will be ignored.\n" +
"###\n" +
"### A profile consists of a set of configuration items followed by a set of\n" +
"### devices.\n" +
"###\n" +
"### An example would look like:\n" +
"### name: onenic\n" +
"### config:\n" +
"### raw.lxc: lxc.aa_profile=unconfined\n" +
"### devices:\n" +
"### eth0:\n" +
"### nictype: bridged\n" +
"### parent: lxcbr0\n" +
"### type: nic\n" +
"###\n" +
"### Note that the name cannot be changed\n")
func (c *configCmd) usage() string {
return gettext.Gettext(
"Manage configuration.\n" +
"\n" +
"lxc config get <container> key Get configuration key\n" +
"lxc config device add <resource> <name> <type> [key=value]...\n" +
" Add a device to a resource\n" +
"lxc config device list <resource> List devices for resource\n" +
"lxc config device remove <resource> <name> Remove device from resource\n" +
"lxc config profile list [filters] List profiles\n" +
"lxc config profile create <profile> Create profile\n" +
"lxc config profile delete <profile> Delete profile\n" +
"lxc config profile device add <profile> <name> <type> [key=value]...\n" +
" Delete profile\n" +
"lxc config profile edit <profile> Edit profile in external editor\n" +
"lxc config profile device list <profile>\n" +
"lxc config profile device remove <profile> <name>\n" +
"lxc config profile set <profile> <key> <value> Set profile configuration\n" +
"lxc config profile apply <resource> <profile> Apply profile to container\n" +
"lxc config set [remote] password <newpwd> Set admin password\n" +
"lxc config set <container> key [value] Set container configuration key\n" +
"lxc config show <container> Show container configuration\n" +
"lxc config trust list [remote] List all trusted certs.\n" +
"lxc config trust add [remote] [certfile.crt] Add certfile.crt to trusted hosts.\n" +
"lxc config trust remove [remote] [hostname|fingerprint]\n" +
" Remove the cert from trusted hosts.\n")
}
func (c *configCmd) flags() {}
func doSet(config *lxd.Config, args []string) error {
// [[lxc config]] set dakara:c1 limits.memory 200000
remote, container := config.ParseRemoteAndContainer(args[1])
d, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
key := args[2]
var value string
if len(args) < 4 {
value = ""
} else {
value = args[3]
}
resp, err := d.SetContainerConfig(container, key, value)
if err != nil {
return err
}
return d.WaitForSuccess(resp.Operation)
}
func (c *configCmd) run(config *lxd.Config, args []string) error {
if len(args) < 1 {
return errArgs
}
switch args[0] {
case "unset":
if len(args) < 3 {
return errArgs
}
return doSet(config, args)
case "set":
if len(args) < 2 {
return errArgs
}
if args[1] == "password" {
if len(args) != 3 {
return errArgs
}
password := args[2]
c, err := lxd.NewClient(config, "")
if err != nil {
return err
}
_, err = c.SetRemotePwd(password)
return err
}
if len(args) < 3 {
return errArgs
}
return doSet(config, args)
case "trust":
if len(args) < 2 {
return errArgs
}
switch args[1] {
case "list":
var remote string
if len(args) == 3 {
remote = config.ParseRemote(args[2])
} else {
remote = config.DefaultRemote
}
d, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
trust, err := d.CertificateList()
if err != nil {
return err
}
for host, fingerprint := range trust {
fmt.Println(fmt.Sprintf("%s: %s", host, fingerprint))
}
return nil
case "add":
var remote string
if len(args) < 3 {
return fmt.Errorf(gettext.Gettext("No cert provided to add"))
} else if len(args) == 4 {
remote = config.ParseRemote(args[2])
} else {
remote = config.DefaultRemote
}
d, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
fname := args[len(args)-1]
cert, err := shared.ReadCert(fname)
if err != nil {
return err
}
name, _ := shared.SplitExt(fname)
return d.CertificateAdd(cert, name)
case "remove":
var remote string
if len(args) < 3 {
return fmt.Errorf(gettext.Gettext("No fingerprint specified."))
} else if len(args) == 4 {
remote = config.ParseRemote(args[2])
} else {
remote = config.DefaultRemote
}
d, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
toRemove := args[len(args)-1]
trust, err := d.CertificateList()
if err != nil {
return err
}
/* Try to remove by hostname first. */
for host, fingerprint := range trust {
if host == toRemove {
return d.CertificateRemove(fingerprint)
}
}
return d.CertificateRemove(args[len(args)-1])
default:
return fmt.Errorf(gettext.Gettext("Unkonwn config trust command %s"), args[1])
}
case "show":
if len(args) == 1 {
return fmt.Errorf(gettext.Gettext("Show for server is not yet supported\n"))
}
remote, container := config.ParseRemoteAndContainer(args[1])
if container == "" {
return fmt.Errorf(gettext.Gettext("Show for remotes is not yet supported\n"))
}
d, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
resp, err := d.GetContainerConfig(container)
if err != nil {
return err
}
fmt.Printf("%s\n", strings.Join(resp, "\n"))
return nil
case "get":
if len(args) != 3 {
return errArgs
}
remote, container := config.ParseRemoteAndContainer(args[1])
d, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
resp, err := d.ContainerStatus(container)
if err != nil {
return err
}
fmt.Printf("%s: %s\n", args[2], resp.Config[args[2]])
return nil
case "profile":
if len(args) < 2 {
return errArgs
}
if args[1] == "list" {
return doProfileList(config, args)
}
if len(args) < 3 {
return errArgs
}
if args[1] == "device" {
return doProfileDevice(config, args)
}
remote, profile := config.ParseRemoteAndContainer(args[2])
client, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
switch args[1] {
case "create":
return doProfileCreate(client, profile)
case "delete":
return doProfileDelete(client, profile)
case "edit":
return doProfileEdit(client, profile)
case "apply":
container := profile
switch len(args) {
case 3:
profile = ""
case 4:
profile = args[3]
default:
return errArgs
}
return doProfileApply(client, container, profile)
case "get":
return doProfileGet(client, profile, args[3:])
case "set":
return doProfileSet(client, profile, args[3:])
case "unset":
return doProfileSet(client, profile, args[3:])
case "copy":
return doProfileCopy(config, client, profile, args[3:])
case "show":
return doProfileShow(client, profile)
}
case "device":
if len(args) < 2 {
return errArgs
}
switch args[1] {
case "list":
return deviceList(config, "container", args)
case "add":
return deviceAdd(config, "container", args)
case "remove":
return deviceRm(config, "container", args)
default:
return errArgs
}
default:
return errArgs
}
return errArgs
}
func doProfileCreate(client *lxd.Client, p string) error {
err := client.ProfileCreate(p)
if err == nil {
fmt.Printf(gettext.Gettext("Profile %s created\n"), p)
}
return err
}
func doProfileEdit(client *lxd.Client, p string) error {
profile, err := client.ProfileConfig(p)
if err != nil {
return err
}
editor := os.Getenv("VISUAL")
if editor == "" {
editor = os.Getenv("EDITOR")
if editor == "" {
editor = "vi"
}
}
data, err := yaml.Marshal(&profile)
f, err := ioutil.TempFile("", "lxc_profile_")
if err != nil {
return err
}
fname := f.Name()
if err = f.Chmod(0700); err != nil {
f.Close()
os.Remove(fname)
return err
}
f.Write([]byte(profileEditHelp))
f.Write(data)
f.Close()
defer os.Remove(fname)
cmd := exec.Command(editor, fname)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return err
}
contents, err := ioutil.ReadFile(fname)
if err != nil {
return err
}
newdata := shared.ProfileConfig{}
err = yaml.Unmarshal(contents, &newdata)
if err != nil {
return err
}
err = client.PutProfile(p, newdata)
return err
}
func doProfileDelete(client *lxd.Client, p string) error {
err := client.ProfileDelete(p)
if err == nil {
fmt.Printf(gettext.Gettext("Profile %s deleted\n"), p)
}
return err
}
func doProfileApply(client *lxd.Client, c string, p string) error {
resp, err := client.ApplyProfile(c, p)
if err == nil {
if p == "" {
p = "(none)"
}
fmt.Printf(gettext.Gettext("Profile %s applied to %s\n"), p, c)
} else {
return err
}
return client.WaitForSuccess(resp.Operation)
}
func doProfileShow(client *lxd.Client, p string) error {
resp, err := client.GetProfileConfig(p)
if err != nil {
return err
}
for k, v := range resp {
fmt.Printf("%s = %s\n", k, v)
}
dresp, err := client.ProfileListDevices(p)
if err != nil {
return err
}
fmt.Printf("%s\n", strings.Join(dresp, "\n"))
return nil
}
func doProfileCopy(config *lxd.Config, client *lxd.Client, p string, args []string) error {
if len(args) != 1 {
return errArgs
}
remote, newname := config.ParseRemoteAndContainer(args[0])
if newname == "" {
newname = p
}
dest, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
return client.ProfileCopy(p, newname, dest)
}
func doProfileDevice(config *lxd.Config, args []string) error {
// profile device add b1 eth0 nic type=bridged
// profile device list b1
// profile device remove b1 eth0
if len(args) < 4 {
return errArgs
}
switch args[2] {
case "add":
return deviceAdd(config, "profile", args[1:])
case "remove":
return deviceRm(config, "profile", args[1:])
case "list":
return deviceList(config, "profile", args[1:])
default:
return errArgs
}
}
func doProfileGet(client *lxd.Client, p string, args []string) error {
// we shifted @args so so it should read "<key>"
if len(args) != 1 {
return errArgs
}
resp, err := client.GetProfileConfig(p)
if err != nil {
return err
}
for k, v := range resp {
if k == args[0] {
fmt.Printf("%s\n", v)
}
}
return nil
}
func doProfileSet(client *lxd.Client, p string, args []string) error {
// we shifted @args so so it should read "<key> [<value>]"
if len(args) < 1 {
return errArgs
}
key := args[0]
var value string
if len(args) < 2 {
value = ""
} else {
value = args[1]
}
err := client.SetProfileConfigItem(p, key, value)
return err
}
func doProfileList(config *lxd.Config, args []string) error {
var remote string
if len(args) > 2 {
var name string
remote, name = config.ParseRemoteAndContainer(args[2])
if name != "" {
return fmt.Errorf(gettext.Gettext("Cannot provide container name to list"))
}
} else {
remote = config.DefaultRemote
}
client, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
profiles, err := client.ListProfiles()
if err != nil {
return err
}
fmt.Printf("%s\n", strings.Join(profiles, "\n"))
return nil
}
func deviceAdd(config *lxd.Config, which string, args []string) error {
if len(args) < 5 {
return errArgs
}
remote, name := config.ParseRemoteAndContainer(args[2])
client, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
devname := args[3]
devtype := args[4]
var props []string
if len(args) > 5 {
props = args[5:]
} else {
props = []string{}
}
var resp *lxd.Response
if which == "profile" {
resp, err = client.ProfileDeviceAdd(name, devname, devtype, props)
} else {
resp, err = client.ContainerDeviceAdd(name, devname, devtype, props)
}
if err != nil {
return err
}
fmt.Printf(gettext.Gettext("Device %s added to %s\n"), devname, name)
if which == "profile" {
return nil
}
return client.WaitForSuccess(resp.Operation)
}
func deviceRm(config *lxd.Config, which string, args []string) error {
if len(args) < 4 {
return errArgs
}
remote, name := config.ParseRemoteAndContainer(args[2])
client, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
devname := args[3]
var resp *lxd.Response
if which == "profile" {
resp, err = client.ProfileDeviceDelete(name, devname)
} else {
resp, err = client.ContainerDeviceDelete(name, devname)
}
if err != nil {
return err
}
fmt.Printf(gettext.Gettext("Device %s removed from %s\n"), devname, name)
if which == "profile" {
return nil
}
return client.WaitForSuccess(resp.Operation)
}
func deviceList(config *lxd.Config, which string, args []string) error {
if len(args) < 3 {
return errArgs
}
remote, name := config.ParseRemoteAndContainer(args[2])
client, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
var resp []string
if which == "profile" {
resp, err = client.ProfileListDevices(name)
} else {
resp, err = client.ContainerListDevices(name)
}
if err != nil {
return err
}
fmt.Printf("%s\n", strings.Join(resp, "\n"))
return nil
}