|
@@ -16,12 +16,15 @@ import (
|
|
type Container struct {
|
|
type Container struct {
|
|
Id string
|
|
Id string
|
|
Root string
|
|
Root string
|
|
|
|
+
|
|
|
|
+ Created time.Time
|
|
|
|
+
|
|
Path string
|
|
Path string
|
|
Args []string
|
|
Args []string
|
|
|
|
|
|
- *Config
|
|
|
|
- *Filesystem
|
|
|
|
- *State
|
|
|
|
|
|
+ Config *Config
|
|
|
|
+ Filesystem *Filesystem
|
|
|
|
+ State *State
|
|
|
|
|
|
lxcConfigPath string
|
|
lxcConfigPath string
|
|
cmd *exec.Cmd
|
|
cmd *exec.Cmd
|
|
@@ -38,6 +41,7 @@ func createContainer(id string, root string, command string, args []string, laye
|
|
container := &Container{
|
|
container := &Container{
|
|
Id: id,
|
|
Id: id,
|
|
Root: root,
|
|
Root: root,
|
|
|
|
+ Created: time.Now(),
|
|
Path: command,
|
|
Path: command,
|
|
Args: args,
|
|
Args: args,
|
|
Config: config,
|
|
Config: config,
|
|
@@ -52,7 +56,6 @@ func createContainer(id string, root string, command string, args []string, laye
|
|
if err := os.Mkdir(root, 0700); err != nil {
|
|
if err := os.Mkdir(root, 0700); err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
-
|
|
|
|
if err := container.save(); err != nil {
|
|
if err := container.save(); err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
@@ -63,32 +66,23 @@ func createContainer(id string, root string, command string, args []string, laye
|
|
}
|
|
}
|
|
|
|
|
|
func loadContainer(containerPath string) (*Container, error) {
|
|
func loadContainer(containerPath string) (*Container, error) {
|
|
- configPath := path.Join(containerPath, "config.json")
|
|
|
|
- fi, err := os.Open(configPath)
|
|
|
|
|
|
+ data, err := ioutil.ReadFile(path.Join(containerPath, "config.json"))
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
- defer fi.Close()
|
|
|
|
- enc := json.NewDecoder(fi)
|
|
|
|
- container := &Container{}
|
|
|
|
- if err := enc.Decode(container); err != nil {
|
|
|
|
|
|
+ var container *Container
|
|
|
|
+ if err := json.Unmarshal(data, container); err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
return container, nil
|
|
return container, nil
|
|
}
|
|
}
|
|
|
|
|
|
-func (container *Container) save() error {
|
|
|
|
- configPath := path.Join(container.Root, "config.json")
|
|
|
|
- fo, err := os.Create(configPath)
|
|
|
|
|
|
+func (container *Container) save() (err error) {
|
|
|
|
+ data, err := json.Marshal(container)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return err
|
|
|
|
|
|
+ return
|
|
}
|
|
}
|
|
- defer fo.Close()
|
|
|
|
- enc := json.NewEncoder(fo)
|
|
|
|
- if err := enc.Encode(container); err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
- return nil
|
|
|
|
|
|
+ return ioutil.WriteFile(path.Join(container.Root, "config.json"), data, 0700)
|
|
}
|
|
}
|
|
|
|
|
|
func (container *Container) generateLXCConfig() error {
|
|
func (container *Container) generateLXCConfig() error {
|
|
@@ -125,6 +119,7 @@ func (container *Container) Start() error {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
container.State.setRunning(container.cmd.Process.Pid)
|
|
container.State.setRunning(container.cmd.Process.Pid)
|
|
|
|
+ container.save()
|
|
go container.monitor()
|
|
go container.monitor()
|
|
|
|
|
|
// Wait until we are out of the STARTING state before returning
|
|
// Wait until we are out of the STARTING state before returning
|
|
@@ -189,6 +184,7 @@ func (container *Container) monitor() {
|
|
|
|
|
|
// Report status back
|
|
// Report status back
|
|
container.State.setStopped(exitCode)
|
|
container.State.setStopped(exitCode)
|
|
|
|
+ container.save()
|
|
}
|
|
}
|
|
|
|
|
|
func (container *Container) kill() error {
|
|
func (container *Container) kill() error {
|