2016-12-02 23:37:16 +00:00
|
|
|
package execution
|
|
|
|
|
2016-12-05 23:38:32 +00:00
|
|
|
import "fmt"
|
|
|
|
|
2016-12-11 19:07:32 +00:00
|
|
|
func NewContainer(stateRoot, id, bundle string) (*Container, error) {
|
2016-12-05 23:38:32 +00:00
|
|
|
stateDir, err := NewStateDir(stateRoot, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Container{
|
|
|
|
id: id,
|
|
|
|
bundle: bundle,
|
|
|
|
stateDir: stateDir,
|
2016-12-11 19:07:32 +00:00
|
|
|
status: "created",
|
2016-12-05 23:38:32 +00:00
|
|
|
processes: make(map[string]Process),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-12-07 18:44:05 +00:00
|
|
|
func LoadContainer(dir StateDir, id, bundle, status string, initPid int64) *Container {
|
2016-12-05 23:38:32 +00:00
|
|
|
return &Container{
|
|
|
|
id: id,
|
|
|
|
stateDir: dir,
|
|
|
|
bundle: bundle,
|
|
|
|
initPid: initPid,
|
2016-12-07 18:44:05 +00:00
|
|
|
status: status,
|
2016-12-05 23:38:32 +00:00
|
|
|
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
|
2016-12-06 00:17:46 +00:00
|
|
|
initPid int64
|
2016-12-07 18:44:05 +00:00
|
|
|
status string
|
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
|
|
|
|
}
|
|
|
|
|
2016-12-07 18:44:05 +00:00
|
|
|
func (c *Container) Status() string {
|
|
|
|
return c.status
|
|
|
|
}
|
|
|
|
|
2016-12-05 23:38:32 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
2016-12-06 00:17:46 +00:00
|
|
|
return 0, fmt.Errorf("no init process")
|
2016-12-05 23:38:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|