forked from nanovms/ops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ops.go
690 lines (587 loc) · 17.3 KB
/
ops.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"github.com/go-errors/errors"
api "github.com/nanovms/ops/lepton"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
func copy(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}
func panicOnError(err error) {
if err != nil {
fmt.Println(err.(*errors.Error).ErrorStack())
panic(err)
}
}
func unWarpConfig(file string) *api.Config {
var c api.Config
if file != "" {
data, err := ioutil.ReadFile(file)
if err != nil {
panic(err)
}
err = json.Unmarshal(data, &c)
if err != nil {
panic(err)
}
}
return &c
}
func runCommandHandler(cmd *cobra.Command, args []string) {
hypervisor := api.HypervisorInstance()
if hypervisor == nil {
panic(errors.New("No hypervisor found on $PATH"))
}
force, err := strconv.ParseBool(cmd.Flag("force").Value.String())
if err != nil {
panic(err)
}
nightly, err := strconv.ParseBool(cmd.Flag("nightly").Value.String())
if err != nil {
panic(err)
}
debugflags, err := strconv.ParseBool(cmd.Flag("debug").Value.String())
if err != nil {
panic(err)
}
verbose, err := strconv.ParseBool(cmd.Flag("verbose").Value.String())
if err != nil {
panic(err)
}
bridged, err := strconv.ParseBool(cmd.Flag("bridged").Value.String())
if err != nil {
panic(err)
}
skipbuild, err := strconv.ParseBool(cmd.Flag("skipbuild").Value.String())
if err != nil {
panic(err)
}
targetRoot, err := cmd.Flags().GetString("target-root")
if err != nil {
panic(err)
}
tapDeviceName, _ := cmd.Flags().GetString("tapname")
config, _ := cmd.Flags().GetString("config")
config = strings.TrimSpace(config)
cmdargs, _ := cmd.Flags().GetStringArray("args")
c := unWarpConfig(config)
c.Args = append(c.Args, cmdargs...)
c.Program = args[0]
if debugflags {
c.Debugflags = []string{"trace", "debugsyscalls", "futex_trace", "fault"}
}
c.TargetRoot = targetRoot
c.RunConfig.TapName = tapDeviceName
c.RunConfig.Verbose = verbose
c.RunConfig.Bridged = bridged
c.NightlyBuild = nightly
c.Force = force
setDefaultImageName(cmd, c)
if !skipbuild {
buildImages(c)
}
ports := []int{}
port, err := cmd.Flags().GetStringArray("port")
if err != nil {
panic(err)
}
for _, p := range port {
i, err := strconv.Atoi(p)
if err != nil {
panic(err)
}
ports = append(ports, i)
}
fmt.Printf("booting %s ...\n", c.RunConfig.Imagename)
InitDefaultRunConfigs(c, ports)
hypervisor.Start(&c.RunConfig)
}
func parseVersion(s string, width int) int64 {
strList := strings.Split(s, ".")
format := fmt.Sprintf("%%s%%0%ds", width)
v := ""
for _, value := range strList {
v = fmt.Sprintf(format, v, value)
}
var result int64
var err error
if result, err = strconv.ParseInt(v, 10, 64); err != nil {
panic(err)
}
return result
}
func downloadReleaseImages(c *api.Config) (string, error) {
var err error
// if it's first run or we have an update
local, remote := api.LocalReleaseVersion, api.LatestReleaseVersion
u := os.Getenv("NANOS_UPDATE")
if local == "0.0" || (u == "1" && parseVersion(local, 4) != parseVersion(remote, 4)) {
err = api.DownloadReleaseImages(remote)
if err != nil {
return "", err
}
}
return remote, nil
}
func downloadNightlyImages(c *api.Config) (string, error) {
var err error
err = api.DownloadNightlyImages(c)
return "nightly", err
}
func fixupConfigImages(c *api.Config, version string) {
if c.NightlyBuild {
version = "nightly"
}
if c.Boot == "" {
c.Boot = path.Join(api.GetOpsHome(), version, "boot.img")
}
if c.Kernel == "" {
c.Kernel = path.Join(api.GetOpsHome(), version, "stage3.img")
}
if c.Mkfs == "" {
c.Mkfs = path.Join(api.GetOpsHome(), version, "mkfs")
}
if c.NameServer == "" {
// google dns server
c.NameServer = "8.8.8.8"
}
}
func validateRequired(c *api.Config) {
if _, err := os.Stat(c.Kernel); os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
if _, err := os.Stat(c.Mkfs); os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
if _, err := os.Stat(c.Boot); os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
if _, err := os.Stat(c.Program); os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func buildImages(c *api.Config) {
var err error
var currversion string
if c.NightlyBuild {
currversion, err = downloadNightlyImages(c)
} else {
currversion, err = downloadReleaseImages(c)
}
panicOnError(err)
fixupConfigImages(c, currversion)
validateRequired(c)
err = api.BuildImage(*c)
panicOnError(err)
}
func setDefaultImageName(cmd *cobra.Command, c *api.Config) {
// if user have not supplied an imagename, use the default as program_image
// all images goes to $HOME/.ops/images
imageName, _ := cmd.Flags().GetString("imagename")
if imageName == "" {
imageName = api.GenerateImageName(c.Program)
} else {
images := path.Join(api.GetOpsHome(), "images")
imageName = path.Join(images, filepath.Base(imageName))
}
c.RunConfig.Imagename = imageName
}
func buildCommandHandler(cmd *cobra.Command, args []string) {
targetRoot, _ := cmd.Flags().GetString("target-root")
config, _ := cmd.Flags().GetString("config")
config = strings.TrimSpace(config)
c := unWarpConfig(config)
c.Program = args[0]
c.TargetRoot = targetRoot
setDefaultImageName(cmd, c)
buildImages(c)
fmt.Printf("Bootable image file:%s\n", c.RunConfig.Imagename)
}
func printManifestHandler(cmd *cobra.Command, args []string) {
targetRoot, _ := cmd.Flags().GetString("target-root")
config, _ := cmd.Flags().GetString("config")
config = strings.TrimSpace(config)
c := unWarpConfig(config)
c.Program = args[0]
c.TargetRoot = targetRoot
m, err := api.BuildManifest(c)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(m.String())
}
func printVersion(cmd *cobra.Command, args []string) {
fmt.Println(api.Version)
}
func updateCommandHandler(cmd *cobra.Command, args []string) {
fmt.Println("Checking for updates...")
err := api.DoUpdate(fmt.Sprintf(api.OpsReleaseUrl, runtime.GOOS))
if err != nil {
fmt.Println("Failed to update.", err)
} else {
fmt.Println("Successfully updated ops. Please restart.")
}
os.Exit(0)
}
func runningAsRoot() bool {
cmd := exec.Command("id", "-u")
output, _ := cmd.Output()
i, _ := strconv.Atoi(string(output[:len(output)-1]))
return i == 0
}
func netSetupCommandHandler(cmd *cobra.Command, args []string) {
if !runningAsRoot() {
fmt.Println("net command needs root permission")
return
}
if err := setupBridgeNetwork(); err != nil {
panic(err)
}
}
func netResetCommandHandler(cmd *cobra.Command, args []string) {
if !runningAsRoot() {
fmt.Println("net command needs root permission")
return
}
if err := resetBridgeNetwork(); err != nil {
panic(err)
}
}
// merge userconfig to package config, user config takes precedence
func mergeConfigs(pkgConfig *api.Config, usrConfig *api.Config) *api.Config {
pkgConfig.Args = append(pkgConfig.Args, usrConfig.Args...)
pkgConfig.Dirs = append(pkgConfig.Dirs, usrConfig.Dirs...)
pkgConfig.Files = append(pkgConfig.Files, usrConfig.Files...)
if pkgConfig.MapDirs == nil {
pkgConfig.MapDirs = make(map[string]string)
}
if pkgConfig.Env == nil {
pkgConfig.Env = make(map[string]string)
}
for k, v := range usrConfig.MapDirs {
pkgConfig.MapDirs[k] = v
}
for k, v := range usrConfig.Env {
pkgConfig.Env[k] = v
}
pkgConfig.RunConfig = usrConfig.RunConfig
pkgConfig.Kernel = usrConfig.Kernel
pkgConfig.Boot = usrConfig.Boot
pkgConfig.Mkfs = usrConfig.Mkfs
pkgConfig.TargetRoot = usrConfig.TargetRoot
pkgConfig.Force = usrConfig.Force
pkgConfig.NightlyBuild = usrConfig.NightlyBuild
pkgConfig.NameServer = usrConfig.NameServer
return pkgConfig
}
func buildFromPackage(packagepath string, c *api.Config) error {
var err error
var currversion string
if c.NightlyBuild {
currversion, err = downloadNightlyImages(c)
} else {
currversion, err = downloadReleaseImages(c)
}
panicOnError(err)
fixupConfigImages(c, currversion)
return api.BuildImageFromPackage(packagepath, *c)
}
func loadCommandHandler(cmd *cobra.Command, args []string) {
hypervisor := api.HypervisorInstance()
if hypervisor == nil {
panic(errors.New("No hypervisor found on $PATH"))
}
localstaging := path.Join(api.GetOpsHome(), ".staging")
expackage := path.Join(localstaging, args[0])
localpackage := api.DownloadPackage(args[0])
fmt.Printf("Extracting %s to %s\n", localpackage, expackage)
// Remove the folder first.
os.RemoveAll(expackage)
api.ExtractPackage(localpackage, localstaging)
// load the package manifest
manifest := path.Join(expackage, "package.manifest")
if _, err := os.Stat(manifest); err != nil {
panic(err)
}
pkgConfig := unWarpConfig(manifest)
debugflags, err := strconv.ParseBool(cmd.Flag("debug").Value.String())
if err != nil {
panic(err)
}
verbose, err := strconv.ParseBool(cmd.Flag("verbose").Value.String())
if err != nil {
panic(err)
}
bridged, err := strconv.ParseBool(cmd.Flag("bridged").Value.String())
if err != nil {
panic(err)
}
force, err := strconv.ParseBool(cmd.Flag("force").Value.String())
if err != nil {
panic(err)
}
nightly, err := strconv.ParseBool(cmd.Flag("nightly").Value.String())
if err != nil {
panic(err)
}
config, _ := cmd.Flags().GetString("config")
config = strings.TrimSpace(config)
cmdargs, _ := cmd.Flags().GetStringArray("args")
c := unWarpConfig(config)
c.Args = append(c.Args, cmdargs...)
if debugflags {
pkgConfig.Debugflags = []string{"trace", "debugsyscalls", "futex_trace", "fault"}
}
c = mergeConfigs(pkgConfig, c)
pkgConfig.RunConfig.Verbose = verbose
pkgConfig.RunConfig.Bridged = bridged
pkgConfig.NightlyBuild = nightly
pkgConfig.Force = force
setDefaultImageName(cmd, c)
if err = buildFromPackage(expackage, c); err != nil {
panic(err)
}
ports := []int{}
port, err := cmd.Flags().GetStringArray("port")
if err != nil {
panic(err)
}
for _, p := range port {
i, err := strconv.Atoi(p)
if err != nil {
panic(err)
}
ports = append(ports, i)
}
fmt.Printf("booting %s ...\n", c.RunConfig.Imagename)
InitDefaultRunConfigs(c, ports)
hypervisor.Start(&c.RunConfig)
}
func InitDefaultRunConfigs(c *api.Config, ports []int) {
if c.RunConfig.Memory == "" {
c.RunConfig.Memory = "2G"
}
c.RunConfig.Ports = append(c.RunConfig.Ports, ports...)
}
func packageManifestChanged(fino os.FileInfo, remoteUrl string) bool {
res, err := http.Head(remoteUrl)
if err != nil {
panic(err)
}
return fino.Size() != res.ContentLength
}
// PackageList contains a list of known packages.
type PackageList struct {
list map[string]Package
}
// Package is the definition of an OPS package.
type Package struct {
Runtime string `json:"runtime"`
Version string `json:"version"`
Language string `json:"language"`
Description string `json:"description,omitempty"`
MD5 string `json:"md5,omitempty"`
}
func cmdListPackages(cmd *cobra.Command, args []string) {
var err error
packageManifest := api.GetPackageManifestFile()
stat, err := os.Stat(packageManifest)
if os.IsNotExist(err) || packageManifestChanged(stat, api.PackageManifestURL) {
if err = api.DownloadFile(packageManifest, api.PackageManifestURL, 10); err != nil {
panic(err)
}
}
var packages PackageList
data, err := ioutil.ReadFile(packageManifest)
if err != nil {
panic(err)
}
err = json.Unmarshal(data, &packages.list)
if err != nil {
fmt.Println(err)
}
searchRegex, err := cmd.Flags().GetString("search")
if err != nil {
panic(err)
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"PackageName", "Version", "Language", "Runtime", "Description"})
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold, tablewriter.FgWhiteColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgWhiteColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgWhiteColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgWhiteColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgWhiteColor})
table.SetRowLine(true)
var r *regexp.Regexp
var filter bool
if len(searchRegex) > 0 {
filter = true
r, err = regexp.Compile(searchRegex)
if err != nil {
// If the regex cannot compile do not attempt to filter
filter = false
}
}
for key, val := range packages.list {
var row []string
// If we are told to filter and get no matches then filter out the
// current row. If we are not told to filter then just add the
// row.
if filter &&
!(r.MatchString(val.Language) ||
r.MatchString(val.Runtime) ||
r.MatchString(key)) {
continue
}
row = append(row, key)
row = append(row, val.Version)
row = append(row, val.Language)
row = append(row, val.Runtime)
row = append(row, val.Description)
table.Append(row)
}
table.Render()
}
func main() {
var cmdRun = &cobra.Command{
Use: "run [elf]",
Short: "Run ELF binary as unikernel",
Args: cobra.MinimumNArgs(1),
Run: runCommandHandler,
}
var ports []string
var force bool
var debugflags bool
var args []string
var config string
var verbose bool
var bridged bool
var nightly bool
var search string
var tap string
var targetRoot string
var skipbuild bool
var imageName string
cmdRun.PersistentFlags().StringArrayVarP(&ports, "port", "p", nil, "port to forward")
cmdRun.PersistentFlags().BoolVarP(&force, "force", "f", false, "update images")
cmdRun.PersistentFlags().BoolVarP(&nightly, "nightly", "n", false, "nightly build")
cmdRun.PersistentFlags().BoolVarP(&debugflags, "debug", "d", false, "enable all debug flags")
cmdRun.PersistentFlags().StringArrayVarP(&args, "args", "a", nil, "command line arguments")
cmdRun.PersistentFlags().StringVarP(&config, "config", "c", "", "ops config file")
cmdRun.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose")
cmdRun.PersistentFlags().BoolVarP(&bridged, "bridged", "b", false, "bridge networking")
cmdRun.PersistentFlags().StringVarP(&tap, "tapname", "t", "tap0", "tap device name")
cmdRun.PersistentFlags().StringVarP(&targetRoot, "target-root", "r", "", "target root")
cmdRun.PersistentFlags().BoolVarP(&skipbuild, "skipbuild", "s", false, "skip building image")
cmdRun.PersistentFlags().StringVarP(&imageName, "imagename", "i", "", "image name")
var cmdNetSetup = &cobra.Command{
Use: "setup",
Short: "Setup bridged network",
Run: netSetupCommandHandler,
}
var cmdNetReset = &cobra.Command{
Use: "reset",
Short: "Reset bridged network",
Run: netResetCommandHandler,
}
var cmdNet = &cobra.Command{
Use: "net",
Args: cobra.OnlyValidArgs,
ValidArgs: []string{"setup", "reset"},
Short: "Configure bridge network",
}
var cmdBuild = &cobra.Command{
Use: "build [ELF file]",
Short: "Build an image from ELF",
Args: cobra.MinimumNArgs(1),
Run: buildCommandHandler,
}
cmdBuild.PersistentFlags().StringVarP(&config, "config", "c", "", "ops config file")
cmdBuild.PersistentFlags().StringVarP(&targetRoot, "target-root", "r", "", "target root")
cmdBuild.PersistentFlags().StringVarP(&imageName, "imagename", "i", "", "image name")
var cmdPrintConfig = &cobra.Command{
Use: "manifest [ELF file]",
Short: "Print the manifest to console",
Args: cobra.MinimumNArgs(1),
Run: printManifestHandler,
}
cmdPrintConfig.PersistentFlags().StringVarP(&config, "config", "c", "", "ops config file")
cmdPrintConfig.PersistentFlags().StringVarP(&targetRoot, "target-root", "r", "", "target root")
var cmdVersion = &cobra.Command{
Use: "version",
Short: "Version",
Run: printVersion,
}
var cmdList = &cobra.Command{
Use: "list",
Short: "list packages",
Run: cmdListPackages,
}
cmdList.PersistentFlags().StringVarP(&search, "search", "s", "", "search package list")
var cmdLoadPackage = &cobra.Command{
Use: "load [packagename]",
Short: "load and run a package from ['ops list']",
Args: cobra.MinimumNArgs(1),
Run: loadCommandHandler,
}
cmdLoadPackage.PersistentFlags().StringArrayVarP(&ports, "port", "p", nil, "port to forward")
cmdLoadPackage.PersistentFlags().BoolVarP(&force, "force", "f", false, "update images")
cmdLoadPackage.PersistentFlags().BoolVarP(&nightly, "nightly", "n", false, "nightly build")
cmdLoadPackage.PersistentFlags().BoolVarP(&debugflags, "debug", "d", false, "enable all debug flags")
cmdLoadPackage.PersistentFlags().StringArrayVarP(&args, "args", "a", nil, "command line arguments")
cmdLoadPackage.PersistentFlags().StringVarP(&config, "config", "c", "", "ops config file")
cmdLoadPackage.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose")
cmdLoadPackage.PersistentFlags().BoolVarP(&bridged, "bridged", "b", false, "bridge networking")
cmdLoadPackage.PersistentFlags().StringVarP(&imageName, "imagename", "i", "", "image name")
var cmdUpdate = &cobra.Command{
Use: "update",
Short: "check for updates",
Run: updateCommandHandler,
}
var rootCmd = &cobra.Command{Use: "ops"}
rootCmd.AddCommand(cmdRun)
cmdNet.AddCommand(cmdNetReset)
cmdNet.AddCommand(cmdNetSetup)
rootCmd.AddCommand(cmdNet)
rootCmd.AddCommand(cmdBuild)
rootCmd.AddCommand(cmdPrintConfig)
rootCmd.AddCommand(cmdVersion)
rootCmd.AddCommand(cmdUpdate)
rootCmd.AddCommand(cmdList)
rootCmd.AddCommand(cmdLoadPackage)
rootCmd.Execute()
}