mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Container: Better serialization/reloading support
This commit is contained in:
parent
78c02daf47
commit
8e9bb02ce5
1 changed files with 16 additions and 20 deletions
36
container.go
36
container.go
|
@ -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
|
Config *Config
|
||||||
*Filesystem
|
Filesystem *Filesystem
|
||||||
*State
|
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")
|
data, err := ioutil.ReadFile(path.Join(containerPath, "config.json"))
|
||||||
fi, err := os.Open(configPath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer fi.Close()
|
var container *Container
|
||||||
enc := json.NewDecoder(fi)
|
if err := json.Unmarshal(data, container); err != nil {
|
||||||
container := &Container{}
|
|
||||||
if err := enc.Decode(container); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return container, nil
|
return container, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (container *Container) save() error {
|
func (container *Container) save() (err error) {
|
||||||
configPath := path.Join(container.Root, "config.json")
|
data, err := json.Marshal(container)
|
||||||
fo, err := os.Create(configPath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
defer fo.Close()
|
return ioutil.WriteFile(path.Join(container.Root, "config.json"), data, 0700)
|
||||||
enc := json.NewEncoder(fo)
|
|
||||||
if err := enc.Encode(container); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
|
|
Loading…
Reference in a new issue