forked from cloudius-systems/capstan
-
Notifications
You must be signed in to change notification settings - Fork 7
/
capstan.go
827 lines (755 loc) · 27.6 KB
/
capstan.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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
/*
* Copyright (C) 2014 Cloudius Systems, Ltd.
* Modifications copyright (C) 2015 XLAB, Ltd.
*
* This work is open source software, licensed under the terms of the
* BSD license as described in the LICENSE file in the top-level directory.
*/
package main
import (
"fmt"
"os"
"github.com/mikelangelo-project/capstan/cmd"
"github.com/mikelangelo-project/capstan/core"
"github.com/mikelangelo-project/capstan/hypervisor"
"github.com/mikelangelo-project/capstan/nat"
"github.com/mikelangelo-project/capstan/provider/openstack"
"github.com/mikelangelo-project/capstan/runtime"
"github.com/mikelangelo-project/capstan/util"
"github.com/urfave/cli"
)
var (
VERSION string
)
const (
// These exit codes were taken from BSD:
// https://www.freebsd.org/cgi/man.cgi?query=sysexits&apropos=0&sektion=0&manpath=FreeBSD+4.3-RELEASE&format=html
// The command was used incorrectly, e.g., with the wrong number of arguments,
// a bad flag, a bad syntax in a parameter, or whatever.
EX_USAGE = 64
// The input data was incorrect in some way. This should only be used for
// user's data & not system files.
EX_DATAERR = 65
)
func main() {
app := cli.NewApp()
app.Name = "capstan"
app.Version = VERSION
app.Usage = "pack, ship, and run applications in light-weight VMs"
app.Flags = []cli.Flag{
cli.StringFlag{Name: "u", Usage: fmt.Sprintf("remote repository URL (default: \"%s\")", util.DefaultRepositoryUrl)},
}
app.Commands = []cli.Command{
{
Name: "config",
Usage: "Capstan configuration",
Subcommands: []cli.Command{
{
Name: "print",
Usage: "print current capstan configuration",
Action: func(c *cli.Context) error {
cmd.ConfigPrint(c)
return nil
},
},
},
},
{
Name: "info",
Usage: "show disk image information",
Action: func(c *cli.Context) error {
if len(c.Args()) != 1 {
return cli.NewExitError("usage: capstan info [image-file]", EX_USAGE)
}
image := c.Args()[0]
if err := cmd.Info(image); err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
{
Name: "import",
Usage: "import an image to the local repository",
Flags: []cli.Flag{
cli.StringFlag{Name: "v", Value: "", Usage: "image version"},
cli.StringFlag{Name: "c", Value: "", Usage: "image creation date"},
cli.StringFlag{Name: "d", Value: "", Usage: "image description"},
cli.StringFlag{Name: "b", Value: "", Usage: "image build command"},
},
Action: func(c *cli.Context) error {
if len(c.Args()) != 2 {
return cli.NewExitError("usage: capstan import [image-name] [image-file]", EX_USAGE)
}
repo := util.NewRepo(c.GlobalString("u"))
err := repo.ImportImage(c.Args()[0], c.Args()[1], c.String("v"), c.String("c"), c.String("d"), c.String("b"))
if err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
{
Name: "pull",
Usage: "pull an image from a repository",
Flags: []cli.Flag{
cli.StringFlag{Name: "p", Value: hypervisor.Default(), Usage: "hypervisor: qemu|vbox|vmw|gce"},
},
Action: func(c *cli.Context) error {
if len(c.Args()) != 1 {
return cli.NewExitError("usage: capstan pull [image-name]", EX_USAGE)
}
hypervisor := c.String("p")
if !isValidHypervisor(hypervisor) {
return cli.NewExitError(fmt.Sprintf("error: '%s' is not a supported hypervisor\n", c.String("p")), EX_DATAERR)
}
repo := util.NewRepo(c.GlobalString("u"))
err := cmd.Pull(repo, hypervisor, c.Args().First())
if err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
{
Name: "rmi",
Usage: "delete an image from a repository",
Action: func(c *cli.Context) error {
if len(c.Args()) != 1 {
return cli.NewExitError("usage: capstan rmi [image-name]", EX_USAGE)
}
repo := util.NewRepo(c.GlobalString("u"))
err := repo.RemoveImage(c.Args().First())
if err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
{
Name: "run",
Usage: "launch a VM. You may pass the image name as the first argument.",
ArgsUsage: "instance-name",
Flags: []cli.Flag{
cli.StringFlag{Name: "i", Value: "", Usage: "image_name"},
cli.StringFlag{Name: "p", Value: hypervisor.Default(), Usage: "hypervisor: qemu|vbox|vmw|gce"},
cli.StringFlag{Name: "m", Value: "1G", Usage: "memory size"},
cli.IntFlag{Name: "c", Value: 2, Usage: "number of CPUs"},
cli.StringFlag{Name: "n", Value: "nat", Usage: "networking: nat|bridge|tap|vhost"},
cli.BoolFlag{Name: "v", Usage: "verbose mode"},
cli.StringFlag{Name: "b", Value: "", Usage: "networking device (bridge or tap): e.g., virbr0, vboxnet0, tap0"},
cli.StringSliceFlag{Name: "f", Value: new(cli.StringSlice), Usage: "port forwarding rules"},
cli.StringFlag{Name: "gce-upload-dir", Value: "", Usage: "Directory to upload local image to: e.g., gs://osvimg"},
cli.StringFlag{Name: "mac", Value: "", Usage: "MAC address. If not specified, the MAC address will be generated automatically."},
cli.StringFlag{Name: "execute,e", Usage: "set the command line to execute"},
cli.StringSliceFlag{Name: "boot", Usage: "specify config_set name to boot unikernel with (repeatable, will be run left to right)"},
cli.BoolFlag{Name: "persist", Usage: "persist instance parameters (only relevant for qemu instances)"},
cli.StringSliceFlag{Name: "env", Value: new(cli.StringSlice), Usage: "specify value of environment variable e.g. PORT=8000 (repeatable)"},
cli.StringSliceFlag{Name: "volume", Value: new(cli.StringSlice), Usage: `{path}[:{key=val}], e.g. ./volume.img:format=raw (repeatable)
Default options are :format=raw:aio=native:cache=none`},
},
Action: func(c *cli.Context) error {
// Check for orphaned instances (those with osv.monitor and disk.qcow2, but
// without osv.config) and remove them.
if err := util.RemoveOrphanedInstances(c.Bool("v")); err != nil {
return cli.NewExitError(err, EX_DATAERR)
}
bootOpts := cmd.BootOptions{
Cmd: c.String("execute"),
Boot: c.StringSlice("boot"),
EnvList: c.StringSlice("env"),
}
bootCmd, err := bootOpts.GetCmd()
if err != nil {
return cli.NewExitError(err, EX_DATAERR)
}
config := &runtime.RunConfig{
InstanceName: c.Args().First(),
ImageName: c.String("i"),
Hypervisor: c.String("p"),
Verbose: c.Bool("v"),
Memory: c.String("m"),
Cpus: c.Int("c"),
Networking: c.String("n"),
Bridge: c.String("b"),
NatRules: nat.Parse(c.StringSlice("f")),
GCEUploadDir: c.String("gce-upload-dir"),
MAC: c.String("mac"),
Cmd: bootCmd,
Persist: c.Bool("persist"),
Volumes: c.StringSlice("volume"),
}
if !isValidHypervisor(config.Hypervisor) {
return cli.NewExitError(fmt.Sprintf("error: '%s' is not a supported hypervisor\n", config.Hypervisor), EX_DATAERR)
}
repo := util.NewRepo(c.GlobalString("u"))
if err := cmd.RunInstance(repo, config); err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
{
Name: "build",
Usage: "build an image",
Flags: []cli.Flag{
cli.StringFlag{Name: "p", Value: hypervisor.Default(), Usage: "hypervisor: qemu|vbox|vmw|gce"},
cli.StringFlag{Name: "m", Value: "512M", Usage: "memory size"},
cli.BoolFlag{Name: "v", Usage: "verbose mode"},
},
Action: func(c *cli.Context) error {
imageName := c.Args().First()
repo := util.NewRepo(c.GlobalString("u"))
if len(c.Args()) != 1 {
imageName = repo.DefaultImage()
}
if imageName == "" {
return cli.NewExitError("usage: capstan build [image-name]", EX_USAGE)
}
hypervisor := c.String("p")
if !isValidHypervisor(hypervisor) {
return cli.NewExitError(fmt.Sprintf("error: '%s' is not a supported hypervisor\n", c.String("p")), EX_DATAERR)
}
image := &core.Image{
Name: imageName,
Hypervisor: hypervisor,
}
template, err := core.ReadTemplateFile("Capstanfile")
if err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
if err := cmd.Build(repo, image, template, c.Bool("v"), c.String("m")); err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
{
Name: "compose",
Usage: "compose the image from a folder or a file",
Flags: []cli.Flag{
cli.StringFlag{Name: "loader_image, l", Value: "mike/osv-loader", Usage: "the base loader image"},
cli.StringFlag{Name: "size, s", Value: "10G", Usage: "size of the target user partition (use M or G suffix)"},
},
Action: func(c *cli.Context) error {
if len(c.Args()) != 2 {
return cli.NewExitError("Usage: capstan compose [image-name] [path-to-upload]", EX_USAGE)
}
// Name of the application (or image) that will be used in the internal repository.
appName := c.Args()[0]
// File or directory path that needs to be uploaded
uploadPath := c.Args()[1]
repo := util.NewRepo(c.GlobalString("u"))
loaderImage := c.String("l")
imageSize, err := util.ParseMemSize(c.String("size"))
if err != nil {
return cli.NewExitError(fmt.Sprintf("Incorrect image size format: %s\n", err), EX_DATAERR)
}
if err := cmd.Compose(repo, loaderImage, imageSize, uploadPath, appName); err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
{
Name: "images",
ShortName: "i",
Usage: "list images",
Action: func(c *cli.Context) error {
repo := util.NewRepo(c.GlobalString("u"))
fmt.Print(repo.ListImages())
return nil
},
},
{
Name: "search",
Usage: "search a remote images",
Action: func(c *cli.Context) error {
image := ""
if len(c.Args()) > 0 {
image = c.Args()[0]
}
repo := util.NewRepo(c.GlobalString("u"))
err := util.ListImagesRemote(repo.URL, image)
if err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
{
Name: "instances",
ShortName: "I",
Usage: "list instances",
Action: func(c *cli.Context) error {
cmd.Instances()
return nil
},
},
{
Name: "stop",
Usage: "stop an instance",
Action: func(c *cli.Context) error {
if len(c.Args()) != 1 {
return cli.NewExitError("usage: capstan stop [instance_name]", EX_USAGE)
}
instance := c.Args()[0]
if err := cmd.Stop(instance); err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
{
Name: "delete",
Usage: "delete an instance",
Action: func(c *cli.Context) error {
if len(c.Args()) != 1 {
return cli.NewExitError("usage: capstan delete [instance_name]", EX_USAGE)
}
instance := c.Args()[0]
if err := cmd.Delete(instance); err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
{
Name: "package",
Usage: "package manipulation tools",
Subcommands: []cli.Command{
{
Name: "init",
Usage: "initialise package structure",
ArgsUsage: "[path]",
Flags: []cli.Flag{
cli.StringFlag{Name: "name,n", Usage: "package name"},
cli.StringFlag{Name: "title,t", Usage: "package title"},
cli.StringFlag{Name: "author,a", Usage: "package author"},
cli.StringFlag{Name: "version,v", Usage: "package version"},
cli.StringSliceFlag{Name: "require", Usage: "specify package dependency"},
cli.StringFlag{Name: "runtime", Usage: "runtime to stub package for. Use 'capstan runtime list' to list all"},
cli.StringFlag{Name: "p, platform", Usage: "platform where package was built on"},
},
Action: func(c *cli.Context) error {
if len(c.Args()) > 1 {
return cli.NewExitError("usage: capstan package init [path]", EX_USAGE)
}
// The package path is the current working dir...
packagePath, _ := os.Getwd()
// ... unless the user has provided the exact location.
if len(c.Args()) == 1 {
packagePath = c.Args()[0]
}
// Author is a mandatory field.
if c.String("name") == "" {
return cli.NewExitError("You must provide the name of the package (--name or -n)", EX_USAGE)
}
// Author is a mandatory field.
if c.String("title") == "" {
return cli.NewExitError("You must provide the title of the package (--title or -t)", EX_USAGE)
}
// Author is a mandatory field.
if c.String("author") == "" {
return cli.NewExitError("You must provide the author of the package (--author or -a)", EX_USAGE)
}
// Initialise the package structure. The version may be empty as it is not
// mandatory field.
p := &core.Package{
Name: c.String("name"),
Title: c.String("title"),
Author: c.String("author"),
Version: c.String("version"),
Require: c.StringSlice("require"),
Platform: c.String("platform"),
}
// Init package
if err := cmd.InitPackage(packagePath, p); err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
// Scaffold runtime if runtime name is provided
if c.String("runtime") != "" {
if err := cmd.RuntimeInit(c.String("runtime"), false, true); err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
}
return nil
},
},
{
Name: "build",
Usage: "builds the package into a compressed file",
Action: func(c *cli.Context) error {
packageDir, _ := os.Getwd()
_, err := cmd.BuildPackage(packageDir)
if err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
{
Name: "compose",
Usage: "composes the package and all its dependencies into OSv image",
ArgsUsage: "image-name",
Flags: []cli.Flag{
cli.StringFlag{Name: "size, s", Value: "10G", Usage: "total size of the target image (use M or G suffix)"},
cli.BoolFlag{Name: "update", Usage: "updates the existing target VM by uploading only modified files"},
cli.BoolFlag{Name: "verbose, v", Usage: "verbose mode"},
cli.StringFlag{Name: "run", Usage: "the command line to be executed in the VM"},
cli.BoolFlag{Name: "pull-missing, p", Usage: "attempt to pull packages missing from a local repository"},
cli.StringSliceFlag{Name: "boot", Usage: "specify default config_set name to boot unikernel with (repeatable, will be run left to right)"},
cli.StringSliceFlag{Name: "env", Value: new(cli.StringSlice), Usage: "specify value of environment variable e.g. PORT=8000 (repeatable)"},
cli.StringFlag{Name: "fs", Usage: "specify type of filesystem: zfs or rofs"},
},
Action: func(c *cli.Context) error {
if len(c.Args()) != 1 {
return cli.NewExitError("Usage: capstan package compose [image-name]", EX_USAGE)
}
// Use the provided repository.
repo := util.NewRepo(c.GlobalString("u"))
// Get the name of the application to be imported into Capstan's repository.
appName := c.Args().First()
// Parse image size descriptor.
imageSize, err := util.ParseMemSize(c.String("size"))
if err != nil {
return cli.NewExitError(fmt.Sprintf("Incorrect image size format: %s\n", err), EX_USAGE)
}
updatePackage := c.Bool("update")
verbose := c.Bool("verbose")
pullMissing := c.Bool("pull-missing")
// Always use the current directory for the package to compose.
packageDir, _ := os.Getwd()
bootOpts := cmd.BootOptions{
Cmd: c.String("run"),
Boot: c.StringSlice("boot"),
EnvList: c.StringSlice("env"),
PackageDir: packageDir,
}
filesystem := c.String("fs")
if filesystem == "" || (filesystem != "zfs" && filesystem != "rofs") {
filesystem = "zfs"
}
if err := cmd.ComposePackage(repo, imageSize, updatePackage, verbose, pullMissing,
packageDir, appName, &bootOpts, filesystem); err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
{
Name: "compose-remote",
Usage: "composes the package and all its dependencies and uploads resulting files into remote OSv instance",
ArgsUsage: "remote-instance",
Flags: []cli.Flag{
cli.BoolFlag{Name: "verbose, v", Usage: "verbose mode"},
cli.BoolFlag{Name: "pull-missing, p", Usage: "attempt to pull packages missing from a local repository"},
},
Action: func(c *cli.Context) error {
if len(c.Args()) != 1 {
return cli.NewExitError("Usage: capstan package compose-remote [remote-instance]", EX_USAGE)
}
// Use the provided repository.
repo := util.NewRepo(c.GlobalString("u"))
// Get the remote host instance or IP address where files of the composed package will be uploaded to
remoteHostInstance := c.Args().First()
verbose := c.Bool("verbose")
pullMissing := c.Bool("pull-missing")
// Always use the current directory for the package to compose.
packageDir, _ := os.Getwd()
if err := cmd.ComposePackageAndUploadToRemoteInstance(repo, verbose, pullMissing, packageDir, remoteHostInstance); err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
{
Name: "collect",
Usage: "collects contents of this package and all required packages",
Flags: []cli.Flag{
cli.BoolFlag{Name: "pull-missing, p", Usage: "attempt to pull packages missing from a local repository"},
cli.BoolFlag{Name: "verbose, v", Usage: "verbose mode"},
cli.BoolFlag{Name: "remote", Usage: "set when previewing the compose-remote"},
},
Action: func(c *cli.Context) error {
repo := util.NewRepo(c.GlobalString("u"))
packageDir, _ := os.Getwd()
pullMissing := c.Bool("pull-missing")
if err := cmd.CollectPackage(repo, packageDir, pullMissing, c.Bool("remote"), c.Bool("verbose")); err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
{
Name: "list",
Usage: "lists the available packages",
Action: func(c *cli.Context) error {
repo := util.NewRepo(c.GlobalString("u"))
fmt.Print(repo.ListPackages())
return nil
},
},
{
Name: "import",
Usage: "builds the package at the given path and imports it into a chosen repository",
Action: func(c *cli.Context) error {
// Use the provided repository.
repo := util.NewRepo(c.GlobalString("u"))
packageDir, err := os.Getwd()
if err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
if err = cmd.ImportPackage(repo, packageDir); err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
{
Name: "search",
Usage: "searches for packages in the remote repository (partial name matches are also supported)",
ArgsUsage: "[package-name]",
Action: func(c *cli.Context) error {
packageName := c.Args().First()
repo := util.NewRepo(c.GlobalString("u"))
if err := util.ListPackagesRemote(repo.URL, packageName); err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
{
Name: "pull",
Usage: "pulls the package from remote repository and imports it into local package storage",
ArgsUsage: "[package-name]",
Action: func(c *cli.Context) error {
// Name of the package is required argument.
if len(c.Args()) != 1 {
return cli.NewExitError("usage: capstan package pull [package-name]", EX_USAGE)
}
// Initialise the repository
repo := util.NewRepo(c.GlobalString("u"))
if err := cmd.PullPackage(repo, c.Args().First()); err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
{
Name: "describe",
Usage: "describes the package from local repository",
ArgsUsage: "[package-name]",
Action: func(c *cli.Context) error {
// Name of the package is required argument.
if len(c.Args()) != 1 {
return cli.NewExitError("usage: capstan package describe [package-name]", EX_USAGE)
}
// Initialise the repository
repo := util.NewRepo(c.GlobalString("u"))
packageName := c.Args()[0]
// Describe the package
if s, err := cmd.DescribePackage(repo, packageName); err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
} else {
fmt.Println(s)
}
return nil
},
},
{
Name: "update",
Usage: "updates local packages from remote if remote version is newer",
ArgsUsage: "[package-name]",
Flags: []cli.Flag{
cli.BoolFlag{Name: "created", Usage: "update package also if created date is newer (regardless version)"},
cli.BoolFlag{Name: "verbose, v", Usage: "verbose mode"},
},
Action: func(c *cli.Context) error {
// Initialise the repository
repo := util.NewRepo(c.GlobalString("u"))
search := ""
if len(c.Args()) > 0 {
search = c.Args().First()
}
// Update the package
if err := cmd.UpdatePackages(repo, search, c.Bool("created"), c.Bool("verbose")); err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
},
},
{
Name: "stack",
Aliases: []string{"openstack"},
Usage: "OpenStack manipulation tools",
Subcommands: []cli.Command{
{
Name: "push",
Usage: "composes OSv image and pushes it to OpenStack",
Flags: append(
[]cli.Flag{
cli.StringFlag{Name: "size, s", Value: "10G", Usage: "minimal size of the target user partition (use M or G suffix).\n" +
"\tNOTE: will be enlarged to match flavor size."},
cli.StringFlag{Name: "flavor, f", Usage: "OpenStack flavor name that created OSv image should fit to"},
cli.StringFlag{Name: "run", Usage: "the command line to be executed in the VM"},
cli.BoolFlag{Name: "keep-image", Usage: "don't delete local composed image in .capstan/repository/stack"},
cli.BoolFlag{Name: "verbose, v", Usage: "verbose mode"},
cli.BoolFlag{Name: "pull-missing, p", Usage: "attempt to pull packages missing from a local repository"},
cli.StringSliceFlag{Name: "boot", Usage: "specify config_set name to boot unikernel with (repeatable, will be run left to right)"},
cli.StringSliceFlag{Name: "env", Value: new(cli.StringSlice), Usage: "specify value of environment variable e.g. PORT=8000 (repeatable)"},
}, openstack.OPENSTACK_CREDENTIALS_FLAGS...),
ArgsUsage: "image-name",
Description: "Compose package, build .qcow2 image and upload it to OpenStack under nickname <image-name>.",
Action: func(c *cli.Context) error {
err := cmd.OpenStackPush(c)
if err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
{
Name: "run",
Usage: "runs image that was previously pushed to OpenStack",
Flags: append(
[]cli.Flag{
cli.StringFlag{Name: "flavor, f", Usage: "OpenStack flavor to be run with"},
cli.StringFlag{Name: "mem, m", Usage: "MB of memory (RAM) to be run with"},
cli.StringFlag{Name: "name, n", Usage: "instance name"},
cli.IntFlag{Name: "count, c", Value: 1, Usage: "number of instances to run"},
cli.BoolFlag{Name: "verbose, v", Usage: "verbose mode"},
}, openstack.OPENSTACK_CREDENTIALS_FLAGS...),
ArgsUsage: "image-name",
Description: "Run image that you've previously uploaded with 'capstan stack push'.\n " +
"Please note that image size CANNOT be changed at this point (wont' boot on\n " +
"too small flavor, wont use extra space on too big flavor), but feel free\n " +
"to adjust amount of memory (RAM).",
Action: func(c *cli.Context) error {
err := cmd.OpenStackRun(c)
if err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
},
},
{
Name: "runtime",
Usage: "package runtime manipulation tools (meta/run.yaml)",
Subcommands: []cli.Command{
{
Name: "preview",
Usage: "prints runtime yaml template to the console",
Flags: []cli.Flag{
cli.StringFlag{Name: "runtime, r", Usage: "Runtime name. Use 'capstan runtime list' to see available names."},
cli.BoolFlag{Name: "plain", Usage: "Remove comments"},
},
Action: func(c *cli.Context) error {
if c.String("runtime") == "" {
return cli.NewExitError("usage: capstan runtime preview -r [runtime-name]", EX_USAGE)
}
if err := cmd.RuntimePreview(c.String("runtime"), c.Bool("plain")); err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
{
Name: "init",
Usage: "prepares meta/run.yaml stub for selected runtime",
Flags: []cli.Flag{
cli.StringFlag{Name: "runtime, r", Usage: "Runtime name. Use 'capstan runtime list' to see available names."},
cli.BoolFlag{Name: "plain", Usage: "Remove comments"},
cli.BoolFlag{Name: "force, f", Usage: "Override existing meta/run.yaml"},
},
Action: func(c *cli.Context) error {
if c.String("runtime") == "" {
return cli.NewExitError("usage: capstan runtime preview -r [runtime-name]", EX_USAGE)
}
if err := cmd.RuntimeInit(c.String("runtime"), c.Bool("plain"), c.Bool("force")); err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
{
Name: "list",
Usage: "list available runtimes",
Flags: []cli.Flag{},
Action: func(c *cli.Context) error {
fmt.Print(cmd.RuntimeList())
return nil
},
},
},
},
{
Name: "volume",
Usage: "volume manipulation tools",
Subcommands: []cli.Command{
{
Name: "create",
Usage: "create empty volume and store it into ./volumes directory",
ArgsUsage: "[volume-name]",
Flags: []cli.Flag{
cli.StringFlag{Name: "size, s", Value: "1G", Usage: "total size of the target image (use M or G suffix)"},
cli.StringFlag{Name: "format, f", Usage: "volume format, e.g. [raw|qcow2|vdi|vmdk|...]"},
},
Action: func(c *cli.Context) error {
if len(c.Args()) != 1 {
return cli.NewExitError("usage: capstan volume create [volume-name]", EX_USAGE)
}
size, err := util.ParseMemSize(c.String("size"))
if err != nil {
return cli.NewExitError(fmt.Sprintf("Incorrect image size format: %s", err), EX_DATAERR)
}
volume := cmd.Volume{
Volume: hypervisor.Volume{
Format: c.String("format"),
},
SizeMB: size,
Name: c.Args().First(),
}
packagePath, _ := os.Getwd()
if err := cmd.CreateVolume(packagePath, volume); err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
{
Name: "delete",
Usage: "delete volume by name",
ArgsUsage: "[volume-name]",
Flags: []cli.Flag{
cli.BoolFlag{Name: "verbose, v", Usage: "verbose mode"},
},
Action: func(c *cli.Context) error {
if len(c.Args()) != 1 {
return cli.NewExitError("usage: capstan volume delete [volume-name]", EX_USAGE)
}
name := c.Args().First()
packagePath, _ := os.Getwd()
if err := cmd.DeleteVolume(packagePath, name, c.Bool("verbose")); err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
}
return nil
},
},
},
},
}
app.Run(os.Args)
}
func isValidHypervisor(hypervisor string) bool {
switch hypervisor {
case "qemu", "vbox", "vmw", "gce":
return true
default:
return false
}
}