This repository has been archived by the owner on Feb 8, 2021. It is now read-only.
/
spec.go
170 lines (155 loc) · 3.8 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
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/golang/glog"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/urfave/cli"
)
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",
},
},
Before: func(context *cli.Context) error {
return cmdPrepare(context, false, false)
},
Action: func(context *cli.Context) {
spec := specs.Spec{
Version: specs.Version,
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",
},
Cwd: "/",
},
Hostname: "shell",
Linux: &specs.Linux{
Resources: &specs.LinuxResources{},
},
}
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); err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("%q doesn't exists", ocffile)
}
return nil, fmt.Errorf("Stat %q error: %v", ocffile, err)
}
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
}
// loadProcessConfig loads the process configuration from the provided path.
func loadProcessConfig(path string) (*specs.Process, error) {
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("JSON configuration file for %s not found", path)
}
return nil, err
}
defer f.Close()
var s *specs.Process
if err := json.NewDecoder(f).Decode(&s); err != nil {
return nil, err
}
return s, nil
}
type State struct {
specs.State
ShimCreateTime uint64 `json:"shim_create_time"`
ContainerCreateTime int64 `json:"container_create_time"`
}
func saveStateFile(root, container string, state *State) error {
stateFile := filepath.Join(root, container, stateJSON)
stateData, err := json.MarshalIndent(state, "", "\t")
if err != nil {
glog.V(1).Infof("%s\n", err.Error())
return err
}
err = ioutil.WriteFile(stateFile, stateData, 0644)
if err != nil {
glog.V(1).Infof("%s\n", err.Error())
return err
}
return nil
}
func loadStateFile(root, container string) (*State, error) {
stateFile := filepath.Join(root, container, stateJSON)
file, err := os.Open(stateFile)
if err != nil {
return nil, err
}
defer file.Close()
var state State
if err = json.NewDecoder(file).Decode(&state); err != nil {
return nil, fmt.Errorf("Decode state file %s error: %s", stateFile, err.Error())
}
return &state, nil
}
func loadStateAndSpec(root, container string) (*State, *specs.Spec, error) {
state, err := loadStateFile(root, container)
if err != nil {
return nil, nil, err
}
spec, err := loadSpec(filepath.Join(state.Bundle, specConfig))
if err != nil {
return nil, nil, err
}
return state, spec, nil
}