Browse code

Container: Better serialization/reloading support

Andrea Luzzardi authored on 2013/01/23 04:13:22
Showing 1 changed files
... ...
@@ -16,12 +16,15 @@ import (
16 16
 type Container struct {
17 17
 	Id   string
18 18
 	Root string
19
+
20
+	Created time.Time
21
+
19 22
 	Path string
20 23
 	Args []string
21 24
 
22
-	*Config
23
-	*Filesystem
24
-	*State
25
+	Config     *Config
26
+	Filesystem *Filesystem
27
+	State      *State
25 28
 
26 29
 	lxcConfigPath string
27 30
 	cmd           *exec.Cmd
... ...
@@ -38,6 +41,7 @@ func createContainer(id string, root string, command string, args []string, laye
38 38
 	container := &Container{
39 39
 		Id:         id,
40 40
 		Root:       root,
41
+		Created:    time.Now(),
41 42
 		Path:       command,
42 43
 		Args:       args,
43 44
 		Config:     config,
... ...
@@ -52,7 +56,6 @@ func createContainer(id string, root string, command string, args []string, laye
52 52
 	if err := os.Mkdir(root, 0700); err != nil {
53 53
 		return nil, err
54 54
 	}
55
-
56 55
 	if err := container.save(); err != nil {
57 56
 		return nil, err
58 57
 	}
... ...
@@ -63,32 +66,23 @@ func createContainer(id string, root string, command string, args []string, laye
63 63
 }
64 64
 
65 65
 func loadContainer(containerPath string) (*Container, error) {
66
-	configPath := path.Join(containerPath, "config.json")
67
-	fi, err := os.Open(configPath)
66
+	data, err := ioutil.ReadFile(path.Join(containerPath, "config.json"))
68 67
 	if err != nil {
69 68
 		return nil, err
70 69
 	}
71
-	defer fi.Close()
72
-	enc := json.NewDecoder(fi)
73
-	container := &Container{}
74
-	if err := enc.Decode(container); err != nil {
70
+	var container *Container
71
+	if err := json.Unmarshal(data, container); err != nil {
75 72
 		return nil, err
76 73
 	}
77 74
 	return container, nil
78 75
 }
79 76
 
80
-func (container *Container) save() error {
81
-	configPath := path.Join(container.Root, "config.json")
82
-	fo, err := os.Create(configPath)
77
+func (container *Container) save() (err error) {
78
+	data, err := json.Marshal(container)
83 79
 	if err != nil {
84
-		return err
80
+		return
85 81
 	}
86
-	defer fo.Close()
87
-	enc := json.NewEncoder(fo)
88
-	if err := enc.Encode(container); err != nil {
89
-		return err
90
-	}
91
-	return nil
82
+	return ioutil.WriteFile(path.Join(container.Root, "config.json"), data, 0700)
92 83
 }
93 84
 
94 85
 func (container *Container) generateLXCConfig() error {
... ...
@@ -125,6 +119,7 @@ func (container *Container) Start() error {
125 125
 		return err
126 126
 	}
127 127
 	container.State.setRunning(container.cmd.Process.Pid)
128
+	container.save()
128 129
 	go container.monitor()
129 130
 
130 131
 	// Wait until we are out of the STARTING state before returning
... ...
@@ -189,6 +184,7 @@ func (container *Container) monitor() {
189 189
 
190 190
 	// Report status back
191 191
 	container.State.setStopped(exitCode)
192
+	container.save()
192 193
 }
193 194
 
194 195
 func (container *Container) kill() error {