forked from cloudius-systems/capstan
/
capstan.go
175 lines (171 loc) · 3.89 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
/*
* 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/nat"
"github.com/cloudius-systems/capstan/hypervisor"
"github.com/cloudius-systems/capstan/util"
"github.com/codegangsta/cli"
"os"
)
var (
VERSION string
)
func main() {
repo := util.NewRepo()
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]
cmd.Info(image)
},
},
{
Name: "push",
Usage: "push an image to a repository",
Action: func(c *cli.Context) {
if len(c.Args()) != 2 {
fmt.Println("usage: capstan push [image-name] [image-file]")
return
}
err := repo.PushImage(c.Args()[0], c.Args()[1])
if err != nil {
fmt.Println(err.Error())
}
},
},
{
Name: "pull",
Usage: "pull an image from a repository",
Flags: []cli.Flag{
cli.StringFlag{"p", hypervisor.Default(), "hypervisor"},
},
Action: func(c *cli.Context) {
if len(c.Args()) != 1 {
fmt.Println("usage: capstan pull [image-name]")
return
}
err := cmd.Pull(repo, c.String("p"), c.Args().First())
if err != nil {
fmt.Println(err.Error())
}
},
},
{
Name: "rmi",
Usage: "delete an image from a repository",
Action: func(c *cli.Context) {
if len(c.Args()) != 1 {
fmt.Println("usage: capstan rmi [image-name]")
return
}
err := repo.RemoveImage(c.Args().First())
if err != nil {
fmt.Println(err.Error())
}
},
},
{
Name: "run",
Usage: "launch a VM",
Flags: []cli.Flag{
cli.StringFlag{"p", hypervisor.Default(), "hypervisor"},
cli.StringFlag{"m", "1G", "memory size"},
cli.IntFlag{"c", 2, "number of CPUs"},
cli.BoolFlag{"v", "verbose mode"},
cli.StringSliceFlag{"f", new(cli.StringSlice), "port forwarding rules"},
},
Action: func(c *cli.Context) {
config := &cmd.RunConfig{
ImageName: c.Args().First(),
Hypervisor: c.String("p"),
Verbose: c.Bool("v"),
Memory: c.String("m"),
Cpus: c.Int("c"),
NatRules: nat.Parse(c.StringSlice("f")),
}
err := cmd.Run(repo, config)
if err != nil {
fmt.Println(err.Error())
}
},
},
{
Name: "build",
Usage: "build an image",
Flags: []cli.Flag{
cli.StringFlag{"p", hypervisor.Default(), "hypervisor"},
cli.BoolFlag{"v", "verbose mode"},
},
Action: func(c *cli.Context) {
image := c.Args().First()
if len(c.Args()) != 1 {
image = repo.DefaultImage()
}
if image == "" {
fmt.Println("usage: capstan build [image-name]")
return
}
hypervisor := c.String("p")
err := cmd.Build(repo, hypervisor, image, c.Bool("v"))
if err != nil {
fmt.Println(err.Error())
}
},
},
{
Name: "images",
Usage: "list images",
Action: func(c *cli.Context) {
repo.ListImages()
},
},
{
Name: "search",
Usage: "search a remote images",
Action: func(c *cli.Context) {
image := ""
if len(c.Args()) > 0 {
image = c.Args()[0]
}
util.ListImagesRemote(image)
},
},
{
Name: "instances",
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 info [instance_name]")
return
}
instance := c.Args()[0]
cmd.Stop(instance)
},
},
}
app.Run(os.Args)
}