forked from hyperhq/runv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.go
111 lines (101 loc) · 3.04 KB
/
manage.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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"syscall"
"github.com/golang/glog"
"github.com/hyperhq/runv/driverloader"
"github.com/hyperhq/runv/hypervisor"
templatecore "github.com/hyperhq/runv/template"
"github.com/urfave/cli"
)
var manageSubCmds = []cli.Command{
createTemplateCommand,
removeTemplateCommand,
}
var manageCommand = cli.Command{
Name: "manage",
Usage: "manage VMs, network, defaults ....",
ArgsUsage: "COMMAND [arguments...]",
Subcommands: manageSubCmds,
Action: func(context *cli.Context) {
cli.ShowSubcommandHelp(context)
},
}
var createTemplateCommand = cli.Command{
Name: "create-template",
Flags: []cli.Flag{
cli.IntFlag{
Name: "cpu",
Value: 1,
Usage: "the initial number of CPUs of the template VM",
},
cli.IntFlag{
Name: "mem",
Value: 128,
Usage: "the initial size of memory of the template VM",
},
},
Usage: "create a template VM on the directory specified by the global option --template",
Action: func(context *cli.Context) error {
absOption := func(option string) (string, error) {
path := context.GlobalString(option)
if path == "" {
return "", fmt.Errorf("The global option --%s should be specified", option)
}
path, eabs := filepath.Abs(path)
if eabs != nil {
return "", fmt.Errorf("Failed to get the abs path of %s: %v", option, eabs)
}
return path, nil
}
kernel, err := absOption("kernel")
if err != nil {
return cli.NewExitError(err, -1)
}
initrd, err := absOption("initrd")
if err != nil {
return cli.NewExitError(err, -1)
}
template, err := absOption("template")
if err != nil {
return cli.NewExitError(err, -1)
}
if err := os.MkdirAll(template, 0700); err != nil {
return cli.NewExitError(fmt.Errorf("Failed to create the template directory: %v", err), -1)
}
if context.GlobalBool("debug") {
flag.CommandLine.Parse([]string{"-v", "3", "--log_dir", context.GlobalString("log_dir"), "--alsologtostderr"})
} else {
flag.CommandLine.Parse([]string{"-v", "1", "--log_dir", context.GlobalString("log_dir")})
}
if hypervisor.HDriver, err = driverloader.Probe(context.GlobalString("driver")); err != nil {
return cli.NewExitError(fmt.Errorf("Failed to setup the driver: %v", err), -1)
}
boot := hypervisor.BootConfig{
CPU: context.Int("cpu"),
Memory: context.Int("mem"),
Kernel: kernel,
Initrd: initrd,
EnableVsock: context.GlobalBool("vsock"),
}
if _, err := templatecore.CreateTemplateVM(template, "", boot); err != nil {
return cli.NewExitError(fmt.Errorf("Failed to create the template: %v", err), -1)
}
return nil
},
}
var removeTemplateCommand = cli.Command{
Name: "remove-template",
Usage: "remove the template VM on the directory specified by the global option --template",
Action: func(context *cli.Context) error {
if err := syscall.Unmount(context.GlobalString("template"), 0); err != nil {
err := fmt.Errorf("Failed to remove the template: %v", err)
glog.Error(err)
return cli.NewExitError(err.Error(), -1)
}
return nil
},
}