forked from hyperhq/runv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.go
101 lines (92 loc) · 2.07 KB
/
spec.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"runtime"
"github.com/codegangsta/cli"
"github.com/opencontainers/runtime-spec/specs-go"
)
var specCommand = cli.Command{
Name: "spec",
Usage: "create a new specification file",
Flags: []cli.Flag{
cli.StringFlag{
Name: "bundle, b",
Usage: "path to the root of the bundle directory",
},
},
Action: func(context *cli.Context) {
spec := specs.Spec{
Version: specs.Version,
Platform: specs.Platform{
OS: runtime.GOOS,
Arch: runtime.GOARCH,
},
Root: specs.Root{
Path: "rootfs",
Readonly: true,
},
Process: specs.Process{
Terminal: true,
User: specs.User{},
Args: []string{
"sh",
},
Env: []string{
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"TERM=xterm",
},
},
Hostname: "shell",
Linux: specs.Linux{
Resources: &specs.Resources{},
},
}
checkNoFile := func(name string) error {
_, err := os.Stat(name)
if err == nil {
return fmt.Errorf("File %s exists. Remove it first", name)
}
if !os.IsNotExist(err) {
return err
}
return nil
}
bundle := context.String("bundle")
if bundle != "" {
if err := os.Chdir(bundle); err != nil {
fmt.Printf("Failed to chdir to bundle dir:%s\nerror:%v\n", bundle, err)
return
}
}
if err := checkNoFile(specConfig); err != nil {
fmt.Printf("%s\n", err.Error())
return
}
data, err := json.MarshalIndent(&spec, "", "\t")
if err != nil {
fmt.Printf("%s\n", err.Error())
return
}
if err := ioutil.WriteFile(specConfig, data, 0666); err != nil {
fmt.Printf("%s\n", err.Error())
return
}
},
}
func loadSpec(ocffile string) (*specs.Spec, error) {
var spec specs.Spec
if _, err := os.Stat(ocffile); os.IsNotExist(err) {
return nil, fmt.Errorf("Please make sure bundle directory contains config.json: %v\n", err.Error())
}
ocfData, err := ioutil.ReadFile(ocffile)
if err != nil {
return nil, err
}
if err := json.Unmarshal(ocfData, &spec); err != nil {
return nil, err
}
return &spec, nil
}