2017-01-24 23:55:32 +00:00
|
|
|
package shim
|
2017-01-24 00:18:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
|
2017-01-26 19:29:19 +00:00
|
|
|
"github.com/crosbymichael/console"
|
2017-01-24 00:18:10 +00:00
|
|
|
runc "github.com/crosbymichael/go-runc"
|
2017-02-13 18:23:28 +00:00
|
|
|
"github.com/docker/containerd"
|
2017-02-09 07:31:26 +00:00
|
|
|
shimapi "github.com/docker/containerd/api/services/shim"
|
2017-01-24 00:18:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type initProcess struct {
|
|
|
|
sync.WaitGroup
|
|
|
|
|
|
|
|
id string
|
|
|
|
bundle string
|
2017-01-26 19:29:19 +00:00
|
|
|
console console.Console
|
2017-01-24 00:18:10 +00:00
|
|
|
io runc.IO
|
|
|
|
runc *runc.Runc
|
|
|
|
status int
|
|
|
|
pid int
|
|
|
|
}
|
|
|
|
|
2017-02-09 07:31:26 +00:00
|
|
|
func newInitProcess(context context.Context, r *shimapi.CreateRequest) (*initProcess, error) {
|
2017-01-24 00:18:10 +00:00
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-13 18:23:28 +00:00
|
|
|
for _, rm := range r.Rootfs {
|
|
|
|
m := &containerd.Mount{
|
|
|
|
Type: rm.Type,
|
|
|
|
Source: rm.Source,
|
|
|
|
Options: rm.Options,
|
|
|
|
}
|
|
|
|
if err := m.Mount(filepath.Join(cwd, "rootfs")); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 00:18:10 +00:00
|
|
|
runtime := &runc.Runc{
|
|
|
|
Command: r.Runtime,
|
|
|
|
Log: filepath.Join(cwd, "log.json"),
|
|
|
|
LogFormat: runc.JSON,
|
|
|
|
PdeathSignal: syscall.SIGKILL,
|
|
|
|
}
|
|
|
|
p := &initProcess{
|
|
|
|
id: r.ID,
|
|
|
|
bundle: r.Bundle,
|
|
|
|
runc: runtime,
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
socket *runc.ConsoleSocket
|
|
|
|
io runc.IO
|
|
|
|
)
|
|
|
|
if r.Terminal {
|
|
|
|
if socket, err = runc.NewConsoleSocket(filepath.Join(cwd, "pty.sock")); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-26 19:29:19 +00:00
|
|
|
defer os.Remove(socket.Path())
|
2017-01-24 00:18:10 +00:00
|
|
|
} else {
|
|
|
|
// TODO: get uid/gid
|
|
|
|
if io, err = runc.NewPipeIO(0, 0); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-24 22:45:18 +00:00
|
|
|
p.io = io
|
2017-01-24 00:18:10 +00:00
|
|
|
}
|
|
|
|
opts := &runc.CreateOpts{
|
2017-01-24 22:45:18 +00:00
|
|
|
PidFile: filepath.Join(cwd, "init.pid"),
|
2017-01-24 00:18:10 +00:00
|
|
|
ConsoleSocket: socket,
|
|
|
|
IO: io,
|
|
|
|
NoPivot: r.NoPivot,
|
|
|
|
}
|
|
|
|
if err := p.runc.Create(context, r.ID, r.Bundle, opts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if socket != nil {
|
|
|
|
console, err := socket.ReceiveMaster()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.console = console
|
|
|
|
if err := copyConsole(context, console, r.Stdin, r.Stdout, r.Stderr, &p.WaitGroup); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := copyPipes(context, io, r.Stdin, r.Stdout, r.Stderr, &p.WaitGroup); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pid, err := runc.ReadPidFile(opts.PidFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.pid = pid
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *initProcess) Pid() int {
|
|
|
|
return p.pid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *initProcess) Status() int {
|
|
|
|
return p.status
|
|
|
|
}
|
|
|
|
|
2017-03-02 09:42:55 +00:00
|
|
|
// ContainerStatus return the state of the container (created, running, paused, stopped)
|
|
|
|
func (p *initProcess) ContainerStatus(ctx context.Context) (string, error) {
|
|
|
|
c, err := p.runc.State(ctx, p.id)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return c.Status, nil
|
|
|
|
}
|
|
|
|
|
2017-01-24 00:18:10 +00:00
|
|
|
func (p *initProcess) Start(context context.Context) error {
|
|
|
|
return p.runc.Start(context, p.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *initProcess) Exited(status int) {
|
|
|
|
p.status = status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *initProcess) Delete(context context.Context) error {
|
|
|
|
p.killAll(context)
|
|
|
|
p.Wait()
|
|
|
|
err := p.runc.Delete(context, p.id)
|
2017-01-26 19:29:19 +00:00
|
|
|
if p.io != nil {
|
|
|
|
p.io.Close()
|
|
|
|
}
|
2017-01-24 00:18:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:29:19 +00:00
|
|
|
func (p *initProcess) Resize(ws console.WinSize) error {
|
2017-01-24 00:18:10 +00:00
|
|
|
if p.console == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return p.console.Resize(ws)
|
|
|
|
}
|
|
|
|
|
2017-02-01 21:25:28 +00:00
|
|
|
func (p *initProcess) Pause(context context.Context) error {
|
|
|
|
return p.runc.Pause(context, p.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *initProcess) Resume(context context.Context) error {
|
|
|
|
return p.runc.Resume(context, p.id)
|
|
|
|
}
|
|
|
|
|
2017-01-24 00:18:10 +00:00
|
|
|
func (p *initProcess) killAll(context context.Context) error {
|
|
|
|
return p.runc.Kill(context, p.id, int(syscall.SIGKILL), &runc.KillOpts{
|
|
|
|
All: true,
|
|
|
|
})
|
|
|
|
}
|