forked from cloudius-systems/capstan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapstan.go
266 lines (260 loc) · 7.56 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
/*
* Copyright (C) 2014 Cloudius Systems, 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"
"github.com/cloudius-systems/capstan/cmd"
"github.com/cloudius-systems/capstan/core"
"github.com/cloudius-systems/capstan/hypervisor"
"github.com/cloudius-systems/capstan/nat"
"github.com/cloudius-systems/capstan/util"
"github.com/codegangsta/cli"
"os"
)
var (
VERSION string
)
const (
DEFAULT_REPO_URL = "https://s3.amazonaws.com/osv.capstan/"
)
func main() {
app := cli.NewApp()
app.Name = "capstan"
app.Version = VERSION
app.Usage = "pack, ship, and run applications in light-weight VMs"
app.Commands = []cli.Command{
{
Name: "info",
Usage: "show disk image information",
Action: func(c *cli.Context) {
if len(c.Args()) != 1 {
fmt.Println("usage: capstan info [image-file]")
return
}
image := c.Args()[0]
err := cmd.Info(image)
if err != nil {
fmt.Println(err.Error())
}
},
},
{
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"},
cli.StringFlag{Name: "u", Value: DEFAULT_REPO_URL, Usage: "remote repository URL"},
},
Action: func(c *cli.Context) {
if len(c.Args()) != 2 {
fmt.Println("usage: capstan import [image-name] [image-file]")
return
}
repo := util.NewRepo(c.String("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 {
fmt.Println(err.Error())
}
},
},
{
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"},
cli.StringFlag{Name: "u", Value: DEFAULT_REPO_URL, Usage: "remote repository URL"},
},
Action: func(c *cli.Context) {
if len(c.Args()) != 1 {
fmt.Println("usage: capstan pull [image-name]")
return
}
hypervisor := c.String("p")
if !isValidHypervisor(hypervisor) {
fmt.Printf("error: '%s' is not a supported hypervisor\n", c.String("p"))
return
}
repo := util.NewRepo(c.String("u"))
err := cmd.Pull(repo, hypervisor, c.Args().First())
if err != nil {
fmt.Println(err.Error())
}
},
},
{
Name: "rmi",
Usage: "delete an image from a repository",
Flags: []cli.Flag{
cli.StringFlag{Name: "u", Value: DEFAULT_REPO_URL, Usage: "remote repository URL"},
},
Action: func(c *cli.Context) {
if len(c.Args()) != 1 {
fmt.Println("usage: capstan rmi [image-name]")
return
}
repo := util.NewRepo(c.String("u"))
err := repo.RemoveImage(c.Args().First())
if err != nil {
fmt.Println(err.Error())
}
},
},
{
Name: "run",
Usage: "launch a VM. You may pass the image name as the first argument.",
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"},
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: "u", Value: DEFAULT_REPO_URL, Usage: "remote repository URL"},
},
Action: func(c *cli.Context) {
config := &cmd.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"),
}
if !isValidHypervisor(config.Hypervisor) {
fmt.Printf("error: '%s' is not a supported hypervisor\n", config.Hypervisor)
return
}
repo := util.NewRepo(c.String("u"))
err := cmd.Run(repo, config)
if err != nil {
fmt.Println(err.Error())
}
},
},
{
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"},
cli.StringFlag{Name: "u", Value: DEFAULT_REPO_URL, Usage: "remote repository URL"},
},
Action: func(c *cli.Context) {
imageName := c.Args().First()
repo := util.NewRepo(c.String("u"))
if len(c.Args()) != 1 {
imageName = repo.DefaultImage()
}
if imageName == "" {
fmt.Println("usage: capstan build [image-name]")
return
}
hypervisor := c.String("p")
if !isValidHypervisor(hypervisor) {
fmt.Printf("error: '%s' is not a supported hypervisor\n", c.String("p"))
return
}
image := &core.Image{
Name: imageName,
Hypervisor: hypervisor,
}
template, err := core.ReadTemplateFile("Capstanfile")
if err != nil {
fmt.Println(err.Error())
return
}
if err := cmd.Build(repo, image, template, c.Bool("v"), c.String("m")); err != nil {
fmt.Println(err.Error())
return
}
},
},
{
Name: "images",
ShortName: "i",
Usage: "list images",
Flags: []cli.Flag{
cli.StringFlag{Name: "u", Value: DEFAULT_REPO_URL, Usage: "remote repository URL"},
},
Action: func(c *cli.Context) {
repo := util.NewRepo(c.String("u"))
repo.ListImages()
},
},
{
Name: "search",
Usage: "search a remote images",
Flags: []cli.Flag{
cli.StringFlag{Name: "u", Value: DEFAULT_REPO_URL, Usage: "remote repository URL"},
},
Action: func(c *cli.Context) {
image := ""
if len(c.Args()) > 0 {
image = c.Args()[0]
}
err := util.ListImagesRemote(c.String("u"), image)
if err != nil {
fmt.Println(err.Error())
}
},
},
{
Name: "instances",
ShortName: "I",
Usage: "list instances",
Action: func(c *cli.Context) {
cmd.Instances()
},
},
{
Name: "stop",
Usage: "stop an instance",
Action: func(c *cli.Context) {
if len(c.Args()) != 1 {
fmt.Println("usage: capstan stop [instance_name]")
return
}
instance := c.Args()[0]
cmd.Stop(instance)
},
},
{
Name: "delete",
Usage: "delete an instance",
Action: func(c *cli.Context) {
if len(c.Args()) != 1 {
fmt.Println("usage: capstan delete [instance_name]")
return
}
instance := c.Args()[0]
cmd.Delete(instance)
},
},
}
app.Run(os.Args)
}
func isValidHypervisor(hypervisor string) bool {
switch hypervisor {
case "qemu", "vbox", "vmw", "gce":
return true
default:
return false
}
}