/
run.go
348 lines (333 loc) · 9.27 KB
/
run.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
/*
* 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 cmd
import (
"fmt"
"github.com/cloudius-systems/capstan/core"
"github.com/cloudius-systems/capstan/hypervisor/gce"
"github.com/cloudius-systems/capstan/hypervisor/qemu"
"github.com/cloudius-systems/capstan/hypervisor/vbox"
"github.com/cloudius-systems/capstan/hypervisor/vmw"
"github.com/cloudius-systems/capstan/image"
"github.com/cloudius-systems/capstan/nat"
"github.com/cloudius-systems/capstan/util"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
)
type RunConfig struct {
InstanceName string
ImageName string
Hypervisor string
Verbose bool
Memory string
Cpus int
Networking string
Bridge string
NatRules []nat.Rule
GCEUploadDir string
MAC string
}
func Run(repo *util.Repo, config *RunConfig) error {
var path string
var cmd *exec.Cmd
// Start an existing instance
if config.ImageName == "" && config.InstanceName != "" {
instanceName, instancePlatform := util.SearchInstance(config.InstanceName)
if instanceName != "" {
defer fmt.Println("")
fmt.Printf("Created instance: %s\n", instanceName)
// Do not set RawTerm for gce
if instancePlatform != "gce" {
util.RawTerm()
defer util.ResetTerm()
}
var err error
switch instancePlatform {
case "qemu":
c, err := qemu.LoadConfig(instanceName)
if err != nil {
return err
}
cmd, err = qemu.LaunchVM(c)
case "vbox":
c, err := vbox.LoadConfig(instanceName)
if err != nil {
return err
}
cmd, err = vbox.LaunchVM(c)
case "vmw":
c, err := vmw.LoadConfig(instanceName)
if err != nil {
return err
}
cmd, err = vmw.LaunchVM(c)
case "gce":
c, err := gce.LoadConfig(instanceName)
if err != nil {
return err
}
cmd, err = gce.LaunchVM(c)
}
if err != nil {
return err
}
if cmd != nil {
return cmd.Wait()
}
return nil
} else {
// The InstanceName is actually a ImageName
// so, cmd like "capstan run cloudius/osv" will work
config.ImageName = config.InstanceName
config.InstanceName = strings.Replace(config.InstanceName, "/", "-", -1)
return Run(repo, config)
}
} else if config.ImageName != "" && config.InstanceName != "" {
// Both ImageName and InstanceName are specified
if f, err := os.Stat(config.ImageName); (f != nil && f.IsDir()) || os.IsNotExist(err) {
if repo.ImageExists(config.Hypervisor, config.ImageName) {
path = repo.ImagePath(config.Hypervisor, config.ImageName)
} else if image.IsCloudImage(config.ImageName) {
path = config.ImageName
} else {
remote, err := util.IsRemoteImage(repo.URL, config.ImageName)
if err != nil {
return err
}
if remote {
err := Pull(repo, config.Hypervisor, config.ImageName)
if err != nil {
return err
}
path = repo.ImagePath(config.Hypervisor, config.ImageName)
} else {
return fmt.Errorf("%s: no such image", config.ImageName)
}
}
if config.Hypervisor == "gce" && !image.IsCloudImage(config.ImageName) {
str, err := ioutil.ReadFile(path)
if err != nil {
return err
}
path = strings.Replace(string(str), "\n", "", -1)
}
} else {
if strings.HasSuffix(config.ImageName, ".jar") {
config, err = buildJarImage(repo, config)
if err != nil {
return err
}
path = repo.ImagePath(config.Hypervisor, config.ImageName)
} else {
path = config.ImageName
}
}
deleteInstance(config.InstanceName)
} else if config.ImageName == "" && config.InstanceName == "" {
// Valid only when Capstanfile is present
config.ImageName = repo.DefaultImage()
config.InstanceName = config.ImageName
if config.ImageName == "" {
return fmt.Errorf("No Capstanfile found, unable to run.")
}
if !repo.ImageExists(config.Hypervisor, config.ImageName) {
if !core.IsTemplateFile("Capstanfile") {
return fmt.Errorf("%s: no such image", config.ImageName)
}
image := &core.Image{
Name: config.ImageName,
Hypervisor: config.Hypervisor,
}
template, err := core.ReadTemplateFile("Capstanfile")
if err != nil {
return err
}
if err := Build(repo, image, template, config.Verbose, config.Memory); err != nil {
return err
}
}
path = repo.ImagePath(config.Hypervisor, config.ImageName)
deleteInstance(config.InstanceName)
} else {
// Cmdline option is not valid
usage()
return nil
}
format, err := image.Probe(path)
if err != nil {
return err
}
if format == image.Unknown {
return fmt.Errorf("%s: image format not recognized, unable to run it.", path)
}
size, err := util.ParseMemSize(config.Memory)
if err != nil {
return err
}
defer fmt.Println("")
id := config.InstanceName
fmt.Printf("Created instance: %s\n", id)
// Do not set RawTerm for gce
if config.Hypervisor != "gce" {
util.RawTerm()
defer util.ResetTerm()
}
switch config.Hypervisor {
case "qemu":
dir := filepath.Join(util.ConfigDir(), "instances/qemu", id)
bridge := config.Bridge
if bridge == "" {
bridge = "virbr0"
}
config := &qemu.VMConfig{
Name: id,
Image: path,
Verbose: true,
Memory: size,
Cpus: config.Cpus,
Networking: config.Networking,
Bridge: bridge,
NatRules: config.NatRules,
BackingFile: true,
InstanceDir: dir,
Monitor: filepath.Join(dir, "osv.monitor"),
ConfigFile: filepath.Join(dir, "osv.config"),
MAC: config.MAC,
}
cmd, err = qemu.LaunchVM(config)
case "vbox":
if format != image.VDI && format != image.VMDK {
return fmt.Errorf("%s: image format of %s is not supported, unable to run it.", config.Hypervisor, path)
}
dir := filepath.Join(util.ConfigDir(), "instances/vbox", id)
bridge := config.Bridge
if bridge == "" {
bridge = "vboxnet0"
}
config := &vbox.VMConfig{
Name: id,
Dir: filepath.Join(util.ConfigDir(), "instances/vbox"),
Image: path,
Memory: size,
Cpus: config.Cpus,
Networking: config.Networking,
Bridge: bridge,
NatRules: config.NatRules,
ConfigFile: filepath.Join(dir, "osv.config"),
MAC: config.MAC,
}
cmd, err = vbox.LaunchVM(config)
case "gce":
if format != image.GCE_TARBALL && format != image.GCE_GS {
return fmt.Errorf("%s: image format of %s is not supported, unable to run it.", config.Hypervisor, path)
}
dir := filepath.Join(util.ConfigDir(), "instances/gce", id)
c := &gce.VMConfig{
Name: id,
Image: id,
Network: "default",
MachineType: "n1-standard-1",
Zone: "us-central1-a",
ConfigFile: filepath.Join(dir, "osv.config"),
InstanceDir: dir,
}
if format == image.GCE_TARBALL {
c.CloudStoragePath = strings.TrimSuffix(config.GCEUploadDir, "/") + "/" + id + ".tar.gz"
c.Tarball = path
} else {
c.CloudStoragePath = path
c.Tarball = ""
}
cmd, err = gce.LaunchVM(c)
case "vmw":
if format != image.VMDK {
return fmt.Errorf("%s: image format of %s is not supported, unable to run it.", config.Hypervisor, path)
}
dir := filepath.Join(util.ConfigDir(), "instances/vmw", id)
config := &vmw.VMConfig{
Name: id,
Dir: dir,
Image: filepath.Join(dir, "osv.vmdk"),
Memory: size,
Cpus: config.Cpus,
NatRules: config.NatRules,
VMXFile: filepath.Join(dir, "osv.vmx"),
InstanceDir: dir,
OriginalVMDK: path,
ConfigFile: filepath.Join(dir, "osv.config"),
}
cmd, err = vmw.LaunchVM(config)
default:
err = fmt.Errorf("%s: is not a supported hypervisor", config.Hypervisor)
}
if err != nil {
return err
}
if cmd != nil {
return cmd.Wait()
} else {
return nil
}
}
func buildJarImage(repo *util.Repo, config *RunConfig) (*RunConfig, error) {
jarPath := config.ImageName
imageName, jarName := parseJarNames(jarPath)
image := &core.Image{
Name: imageName,
Hypervisor: config.Hypervisor,
}
targetJarPath := "/" + jarName
template := &core.Template{
Base: "cloudius/osv-openjdk",
Cmdline: fmt.Sprintf("/java.so -jar %s", targetJarPath),
Files: map[string]string{
targetJarPath: jarPath,
},
}
if err := Build(repo, image, template, config.Verbose, config.Memory); err != nil {
return nil, err
}
newConfig := *config
newConfig.ImageName = imageName
newConfig.InstanceName = imageName
return &newConfig, nil
}
func parseJarNames(filename string) (string, string) {
jarName := filepath.Base(filename)
imageName := strings.TrimSuffix(jarName, ".jar")
return imageName, jarName
}
func usage() {
fmt.Println("Please try one of the following:")
fmt.Println("1) capstan run")
fmt.Println(" run under a directory contains Capstanfile")
fmt.Println("2) capstan run $instance_name")
fmt.Println(" start an existing instance")
fmt.Println("3) capstan run -i $image_name $instance_name")
fmt.Println(" start an instance using $image_name")
}
func deleteInstance(name string) error {
instanceName, instancePlatform := util.SearchInstance(name)
if instanceName == "" {
return nil
}
var err error
switch instancePlatform {
case "qemu":
err = qemu.DeleteVM(name)
case "vbox":
err = vbox.DeleteVM(name)
case "vmw":
err = vmw.DeleteVM(name)
case "gce":
err = gce.DeleteVM(name)
}
return err
}