Add container start and supervisor
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
e620833c9e
commit
21a53c1d70
11 changed files with 380 additions and 139 deletions
|
@ -29,21 +29,19 @@ type OCIRuntime struct {
|
|||
runc *runc.Runc
|
||||
}
|
||||
|
||||
func (r *OCIRuntime) Create(id string, o execution.CreateOpts) (*execution.Container, error) {
|
||||
var err error
|
||||
|
||||
stateDir, err := NewStateDir(r.root, id)
|
||||
if err != nil {
|
||||
func (r *OCIRuntime) Create(id string, o execution.CreateOpts) (container *execution.Container, err error) {
|
||||
if container, err = execution.NewContainer(r.root, id, o.Bundle); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
initStateDir, err := stateDir.NewProcess()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// /run/runc/redis/1/pid
|
||||
pidFile := filepath.Join(initStateDir, "pid")
|
||||
defer func() {
|
||||
if err != nil {
|
||||
container.StateDir().Delete()
|
||||
}
|
||||
}()
|
||||
var (
|
||||
initDir = container.StateDir().NewProcess()
|
||||
pidFile = filepath.Join(initDir, "pid")
|
||||
)
|
||||
err = r.runc.Create(id, o.Bundle, &runc.CreateOpts{
|
||||
Pidfile: pidfile,
|
||||
Stdin: o.Stdin,
|
||||
|
@ -55,40 +53,40 @@ func (r *OCIRuntime) Create(id string, o execution.CreateOpts) (*execution.Conta
|
|||
}
|
||||
pid, err := runc.ReadPifFile(pidfile)
|
||||
if err != nil {
|
||||
// TODO: kill the container if we are going to return
|
||||
return nil, err
|
||||
}
|
||||
process, err := newProcess(pid)
|
||||
process, err := newProcess(filepath.Base(initDir), pid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
container := &execution.Container{
|
||||
ID: id,
|
||||
Bundle: o.Bundle,
|
||||
StateDir: stateDir,
|
||||
}
|
||||
container.AddProcess(process)
|
||||
container.AddProcess(process, true)
|
||||
|
||||
return container, nil
|
||||
}
|
||||
|
||||
func (r *OCIRuntime) load(runcC *runc.Container) (*execution.Container, error) {
|
||||
container := &execution.Container{
|
||||
ID: runcC.ID,
|
||||
Bundle: runcC.Bundle,
|
||||
StateDir: StateDir(filepath.Join(r.root, runcC.ID)),
|
||||
}
|
||||
container := execution.LoadContainer(
|
||||
execution.StateDir(filepath.Join(r.root, runcC.ID)),
|
||||
runcC.ID,
|
||||
runcC.Bundle,
|
||||
)
|
||||
|
||||
process, err := newProcess(runcC.Pid)
|
||||
dirs, err := ioutil.ReadDir(filepath.Join(container.StateDir().Processes()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
container.AddProcess(process)
|
||||
|
||||
// /run/containerd/container-id/processess/process-id
|
||||
dirs, err := ioutil.ReadDir(filepath.Join(container.Root))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
for _, d := range dirs {
|
||||
pid, err := runc.ReadPidFile(filepath.Join(d, "pid"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
process, err := newProcess(filepath.Base(d), pid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
container.AddProcess(process, pid == runcC.Pid)
|
||||
}
|
||||
|
||||
return container, nil
|
||||
|
@ -171,7 +169,7 @@ func (r *OCIRuntime) StartProcess(c *execution.Container, o CreateProcessOpts) (
|
|||
return nil, err
|
||||
}
|
||||
|
||||
container.AddProcess(process)
|
||||
container.AddProcess(process, false)
|
||||
|
||||
return process, nil
|
||||
}
|
||||
|
|
|
@ -7,20 +7,26 @@ import (
|
|||
"github.com/docker/containerd/execution"
|
||||
)
|
||||
|
||||
func newProcess(pid int) (execution.Process, error) {
|
||||
func newProcess(id string, pid int) (execution.Process, error) {
|
||||
proc, err := os.FindProcess(pid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &process{
|
||||
id: id,
|
||||
proc: proc,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type process struct {
|
||||
id string
|
||||
proc *os.Process
|
||||
}
|
||||
|
||||
func (p *process) ID() string {
|
||||
return p.id
|
||||
}
|
||||
|
||||
func (p *process) Pid() int {
|
||||
return p.proc.Pid
|
||||
}
|
||||
|
@ -30,6 +36,7 @@ func (p *process) Wait() (uint32, error) {
|
|||
if err != nil {
|
||||
return 0, nil
|
||||
}
|
||||
// TODO: implement kill-all if we are the init pid
|
||||
return uint32(state.Sys().(syscall.WaitStatus).ExitStatus()), nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue