2016-12-02 23:37:16 +00:00
|
|
|
package execution
|
|
|
|
|
2016-12-05 23:38:32 +00:00
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
func NewContainer(stateRoot, id, bundle string) (*Container, error) {
|
|
|
|
stateDir, err := NewStateDir(stateRoot, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Container{
|
|
|
|
id: id,
|
|
|
|
bundle: bundle,
|
|
|
|
stateDir: stateDir,
|
|
|
|
processes: make(map[string]Process),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadContainer(dir StateDir, id, bundle string, initPid int) *Container {
|
|
|
|
return &Container{
|
|
|
|
id: id,
|
|
|
|
stateDir: dir,
|
|
|
|
bundle: bundle,
|
|
|
|
initPid: initPid,
|
|
|
|
processes: make(map[string]Process),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-02 23:37:16 +00:00
|
|
|
type Container struct {
|
2016-12-05 23:38:32 +00:00
|
|
|
id string
|
|
|
|
bundle string
|
|
|
|
stateDir StateDir
|
|
|
|
initPid int
|
2016-12-02 23:37:16 +00:00
|
|
|
|
2016-12-05 22:15:03 +00:00
|
|
|
processes map[string]Process
|
2016-12-02 23:37:16 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 23:38:32 +00:00
|
|
|
func (c *Container) ID() string {
|
|
|
|
return c.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) Bundle() string {
|
|
|
|
return c.bundle
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) StateDir() StateDir {
|
|
|
|
return c.stateDir
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) Wait() (uint32, error) {
|
|
|
|
for _, p := range c.processes {
|
|
|
|
if p.Pid() == c.initPid {
|
|
|
|
return p.Wait()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("no init process")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) AddProcess(p Process, isInit bool) {
|
|
|
|
if isInit {
|
|
|
|
c.initPid = p.Pid()
|
|
|
|
}
|
2016-12-05 22:15:03 +00:00
|
|
|
c.processes[p.ID()] = p
|
2016-12-02 23:37:16 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 22:15:03 +00:00
|
|
|
func (c *Container) GetProcess(id string) Process {
|
|
|
|
return c.processes[id]
|
2016-12-02 23:37:16 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 22:15:03 +00:00
|
|
|
func (c *Container) RemoveProcess(id string) {
|
|
|
|
delete(c.processes, id)
|
2016-12-02 23:37:16 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 22:15:03 +00:00
|
|
|
func (c *Container) Processes() []Process {
|
|
|
|
var out []Process
|
2016-12-02 23:37:16 +00:00
|
|
|
for _, p := range c.processes {
|
|
|
|
out = append(out, p)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|