forked from hyperhq/runv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage.go
99 lines (89 loc) · 2.66 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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"syscall"
"github.com/codegangsta/cli"
"github.com/golang/glog"
"github.com/hyperhq/runv/driverloader"
"github.com/hyperhq/runv/hypervisor"
templatecore "github.com/hyperhq/runv/template"
)
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) {
absOption := func(option string) string {
path := context.GlobalString(option)
if path == "" {
fmt.Printf("The global option --%s should be specified\n", option)
os.Exit(-1)
}
path, eabs := filepath.Abs(path)
if eabs != nil {
fmt.Printf("Failed to get the abs path of %s: %v\n", option, eabs)
os.Exit(-1)
}
return path
}
kernel := absOption("kernel")
initrd := absOption("initrd")
template := absOption("template")
if err := os.MkdirAll(template, 0700); err != nil {
fmt.Printf("Failed to create the template directory: %v\n", err)
os.Exit(-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")})
}
var err error
if hypervisor.HDriver, err = driverloader.Probe(context.GlobalString("driver")); err != nil {
glog.V(1).Infof("%v\n", err)
fmt.Printf("Failed to setup the driver: %v\n", err)
os.Exit(-1)
}
if _, err := templatecore.CreateTemplateVM(template, "", context.Int("cpu"), context.Int("mem"), kernel, initrd, context.GlobalBool("vsock")); err != nil {
fmt.Printf("Failed to create the template: %v\n", err)
os.Exit(-1)
}
},
}
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) {
if err := syscall.Unmount(context.GlobalString("template"), 0); err != nil {
fmt.Printf("Failed to remove the template: %v\n", err)
os.Exit(-1)
}
},
}