2014-06-25 07:54:50 +08:00
|
|
|
package libcontainer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-06-25 07:43:00 +08:00
|
|
|
|
|
|
|
"github.com/docker/libcontainer/network"
|
2014-06-25 07:54:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// State represents a running container's state
|
|
|
|
type State struct {
|
2014-06-26 02:42:08 +08:00
|
|
|
// InitPid is the init process id in the parent namespace
|
|
|
|
InitPid int `json:"init_pid,omitempty"`
|
|
|
|
// InitStartTime is the init process start time
|
|
|
|
InitStartTime string `json:"init_start_time,omitempty"`
|
2014-06-25 07:43:00 +08:00
|
|
|
// Network runtime state.
|
|
|
|
NetworkState network.NetworkState `json:"network_state,omitempty"`
|
2014-06-25 07:54:50 +08:00
|
|
|
}
|
|
|
|
|
2014-06-25 07:43:00 +08:00
|
|
|
// The name of the runtime state file
|
|
|
|
const stateFile = "state.json"
|
|
|
|
|
2014-06-25 08:17:41 +08:00
|
|
|
// SaveState writes the container's runtime state to a state.json file
|
2014-06-25 07:54:50 +08:00
|
|
|
// in the specified path
|
2014-06-25 08:17:41 +08:00
|
|
|
func SaveState(basePath string, state *State) error {
|
2014-06-25 07:43:00 +08:00
|
|
|
f, err := os.Create(filepath.Join(basePath, stateFile))
|
2014-06-25 07:54:50 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return json.NewEncoder(f).Encode(state)
|
|
|
|
}
|
|
|
|
|
2014-06-25 08:17:41 +08:00
|
|
|
// GetState reads the state.json file for a running container
|
|
|
|
func GetState(basePath string) (*State, error) {
|
2014-06-25 07:43:00 +08:00
|
|
|
f, err := os.Open(filepath.Join(basePath, stateFile))
|
2014-06-25 07:54:50 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
var state *State
|
|
|
|
if err := json.NewDecoder(f).Decode(&state); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return state, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteState deletes the state.json file
|
|
|
|
func DeleteState(basePath string) error {
|
2014-06-25 07:43:00 +08:00
|
|
|
return os.Remove(filepath.Join(basePath, stateFile))
|
2014-06-25 07:54:50 +08:00
|
|
|
}
|