-
Notifications
You must be signed in to change notification settings - Fork 5
/
state.go
79 lines (67 loc) · 1.83 KB
/
state.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
package main
import (
"encoding/json"
"fmt"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"io/ioutil"
"os"
"path/filepath"
)
var stateCommand = cli.Command{
Name: "state",
ArgsUsage: `<container-id>`,
Action: func(context *cli.Context) error {
args := context.Args()
if args.Present() == false {
return fmt.Errorf("Missing container ID")
}
root := context.GlobalString("root")
name := context.Args().First()
stateFile := filepath.Join(root, name, stateJSON)
stateData, _ := ioutil.ReadFile(stateFile)
os.Stdout.Write(stateData)
logrus.Debug(string(stateData))
return nil
},
}
func saveState(status specs.ContainerState, container string, context *cli.Context) error {
root := context.GlobalString("root")
absRoot, _ := filepath.Abs(root)
spec, err := setupSpec(context)
if err != nil {
return fmt.Errorf("setupSepc err: %v", err)
}
rootfs, _ := filepath.Abs(spec.Root.Path)
stateFile := filepath.Join(absRoot, container, stateJSON)
cs := &specs.State{
Version: spec.Version,
ID: context.Args().Get(0),
Status: status,
Bundle: rootfs,
}
stateData, _ := json.MarshalIndent(cs, "", "\t")
if err := ioutil.WriteFile(stateFile, stateData, 0666); err != nil {
panic(err)
}
return nil
}
func createContainer(container, bundle, stateRoot string, spec *specs.Spec) error {
// Prepare container state directory
stateDir := filepath.Join(stateRoot, container)
_, err := os.Stat(stateDir)
if err == nil {
logrus.Errorf("Container %s exists", container)
return fmt.Errorf("Container %s exists", container)
}
err = os.MkdirAll(stateDir, 0755)
if err != nil {
fmt.Printf("%s\n", err.Error())
return err
}
return nil
}
func deleteContainer(root, container string) error {
return os.RemoveAll(filepath.Join(root, container))
}