2016-03-18 21:50:19 +03:00
|
|
|
package libcontainerd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-06-02 21:10:55 +03:00
|
|
|
"io"
|
2016-03-18 21:50:19 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
2016-04-18 23:10:15 +03:00
|
|
|
"time"
|
2016-03-18 21:50:19 +03:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
containerd "github.com/docker/containerd/api/grpc/types"
|
2016-09-20 18:10:04 +03:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2016-04-18 21:48:28 +03:00
|
|
|
"github.com/docker/docker/restartmanager"
|
2016-08-17 19:38:34 +03:00
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
2016-03-18 21:50:19 +03:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
type container struct {
|
|
|
|
containerCommon
|
|
|
|
|
|
|
|
// Platform specific fields are below here.
|
|
|
|
pauseMonitor
|
2016-05-24 00:49:50 +03:00
|
|
|
oom bool
|
|
|
|
runtime string
|
|
|
|
runtimeArgs []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type runtime struct {
|
|
|
|
path string
|
|
|
|
args []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithRuntime sets the runtime to be used for the created container
|
|
|
|
func WithRuntime(path string, args []string) CreateOption {
|
|
|
|
return runtime{path, args}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt runtime) Apply(p interface{}) error {
|
|
|
|
if pr, ok := p.(*container); ok {
|
|
|
|
pr.runtime = rt.path
|
|
|
|
pr.runtimeArgs = rt.args
|
|
|
|
}
|
|
|
|
return nil
|
2016-03-18 21:50:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctr *container) clean() error {
|
2016-04-21 23:58:21 +03:00
|
|
|
if os.Getenv("LIBCONTAINERD_NOCLEAN") == "1" {
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-18 21:50:19 +03:00
|
|
|
if _, err := os.Lstat(ctr.dir); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.RemoveAll(ctr.dir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-18 21:01:02 +03:00
|
|
|
// cleanProcess removes the fifos used by an additional process.
|
|
|
|
// Caller needs to lock container ID before calling this method.
|
|
|
|
func (ctr *container) cleanProcess(id string) {
|
|
|
|
if p, ok := ctr.processes[id]; ok {
|
|
|
|
for _, i := range []int{syscall.Stdin, syscall.Stdout, syscall.Stderr} {
|
|
|
|
if err := os.Remove(p.fifo(i)); err != nil {
|
2016-07-23 01:20:14 +03:00
|
|
|
logrus.Warnf("libcontainerd: failed to remove %v for process %v: %v", p.fifo(i), id, err)
|
2016-04-18 21:01:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete(ctr.processes, id)
|
|
|
|
}
|
|
|
|
|
2016-03-18 21:50:19 +03:00
|
|
|
func (ctr *container) spec() (*specs.Spec, error) {
|
|
|
|
var spec specs.Spec
|
|
|
|
dt, err := ioutil.ReadFile(filepath.Join(ctr.dir, configFilename))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(dt, &spec); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &spec, nil
|
|
|
|
}
|
|
|
|
|
2016-05-12 17:52:00 +03:00
|
|
|
func (ctr *container) start(checkpoint string, checkpointDir string) error {
|
2016-03-18 21:50:19 +03:00
|
|
|
spec, err := ctr.spec()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-20 18:10:04 +03:00
|
|
|
createChan := make(chan struct{})
|
2016-03-18 21:50:19 +03:00
|
|
|
iopipe, err := ctr.openFifos(spec.Process.Terminal)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-20 18:10:04 +03:00
|
|
|
// we need to delay stdin closure after container start or else "stdin close"
|
|
|
|
// event will be rejected by containerd.
|
|
|
|
// stdin closure happens in AttachStreams
|
|
|
|
stdin := iopipe.Stdin
|
|
|
|
iopipe.Stdin = ioutils.NewWriteCloserWrapper(stdin, func() error {
|
|
|
|
go func() {
|
|
|
|
<-createChan
|
|
|
|
stdin.Close()
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2016-03-18 21:50:19 +03:00
|
|
|
r := &containerd.CreateContainerRequest{
|
2016-05-12 17:52:00 +03:00
|
|
|
Id: ctr.containerID,
|
|
|
|
BundlePath: ctr.dir,
|
|
|
|
Stdin: ctr.fifo(syscall.Stdin),
|
|
|
|
Stdout: ctr.fifo(syscall.Stdout),
|
|
|
|
Stderr: ctr.fifo(syscall.Stderr),
|
|
|
|
Checkpoint: checkpoint,
|
|
|
|
CheckpointDir: checkpointDir,
|
2016-03-31 08:03:10 +03:00
|
|
|
// check to see if we are running in ramdisk to disable pivot root
|
|
|
|
NoPivotRoot: os.Getenv("DOCKER_RAMDISK") != "",
|
2016-05-24 00:49:50 +03:00
|
|
|
Runtime: ctr.runtime,
|
|
|
|
RuntimeArgs: ctr.runtimeArgs,
|
2016-03-18 21:50:19 +03:00
|
|
|
}
|
|
|
|
ctr.client.appendContainer(ctr)
|
|
|
|
|
2016-09-20 18:10:04 +03:00
|
|
|
if err := ctr.client.backend.AttachStreams(ctr.containerID, *iopipe); err != nil {
|
|
|
|
close(createChan)
|
2016-03-18 21:50:19 +03:00
|
|
|
ctr.closeFifos(iopipe)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-20 18:10:04 +03:00
|
|
|
resp, err := ctr.client.remote.apiClient.CreateContainer(context.Background(), r)
|
|
|
|
if err != nil {
|
|
|
|
close(createChan)
|
|
|
|
ctr.closeFifos(iopipe)
|
2016-03-18 21:50:19 +03:00
|
|
|
return err
|
|
|
|
}
|
2016-09-20 18:10:04 +03:00
|
|
|
ctr.startedAt = time.Now()
|
2016-03-18 21:50:19 +03:00
|
|
|
ctr.systemPid = systemPid(resp.Container)
|
2016-09-20 18:10:04 +03:00
|
|
|
close(createChan)
|
2016-03-18 21:50:19 +03:00
|
|
|
|
|
|
|
return ctr.client.backend.StateChanged(ctr.containerID, StateInfo{
|
2016-04-02 03:02:38 +03:00
|
|
|
CommonStateInfo: CommonStateInfo{
|
|
|
|
State: StateStart,
|
|
|
|
Pid: ctr.systemPid,
|
|
|
|
}})
|
2016-03-18 21:50:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctr *container) newProcess(friendlyName string) *process {
|
|
|
|
return &process{
|
|
|
|
dir: ctr.dir,
|
|
|
|
processCommon: processCommon{
|
|
|
|
containerID: ctr.containerID,
|
|
|
|
friendlyName: friendlyName,
|
|
|
|
client: ctr.client,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctr *container) handleEvent(e *containerd.Event) error {
|
|
|
|
ctr.client.lock(ctr.containerID)
|
|
|
|
defer ctr.client.unlock(ctr.containerID)
|
|
|
|
switch e.Type {
|
|
|
|
case StateExit, StatePause, StateResume, StateOOM:
|
2016-08-03 23:45:46 +03:00
|
|
|
var waitRestart chan error
|
2016-03-18 21:50:19 +03:00
|
|
|
st := StateInfo{
|
2016-04-02 03:02:38 +03:00
|
|
|
CommonStateInfo: CommonStateInfo{
|
|
|
|
State: e.Type,
|
|
|
|
ExitCode: e.Status,
|
|
|
|
},
|
2016-03-18 21:50:19 +03:00
|
|
|
OOMKilled: e.Type == StateExit && ctr.oom,
|
|
|
|
}
|
|
|
|
if e.Type == StateOOM {
|
|
|
|
ctr.oom = true
|
|
|
|
}
|
|
|
|
if e.Type == StateExit && e.Pid != InitFriendlyName {
|
|
|
|
st.ProcessID = e.Pid
|
|
|
|
st.State = StateExitProcess
|
|
|
|
}
|
|
|
|
if st.State == StateExit && ctr.restartManager != nil {
|
2016-04-18 23:10:15 +03:00
|
|
|
restart, wait, err := ctr.restartManager.ShouldRestart(e.Status, false, time.Since(ctr.startedAt))
|
2016-03-18 21:50:19 +03:00
|
|
|
if err != nil {
|
2016-07-23 01:20:14 +03:00
|
|
|
logrus.Warnf("libcontainerd: container %s %v", ctr.containerID, err)
|
2016-03-18 21:50:19 +03:00
|
|
|
} else if restart {
|
|
|
|
st.State = StateRestart
|
|
|
|
ctr.restarting = true
|
2016-04-07 17:05:29 +03:00
|
|
|
ctr.client.deleteContainer(e.Id)
|
2016-08-03 23:45:46 +03:00
|
|
|
waitRestart = wait
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove process from list if we have exited
|
|
|
|
// We need to do so here in case the Message Handler decides to restart it.
|
|
|
|
switch st.State {
|
|
|
|
case StateExit:
|
|
|
|
ctr.clean()
|
|
|
|
ctr.client.deleteContainer(e.Id)
|
|
|
|
case StateExitProcess:
|
|
|
|
ctr.cleanProcess(st.ProcessID)
|
|
|
|
}
|
|
|
|
ctr.client.q.append(e.Id, func() {
|
|
|
|
if err := ctr.client.backend.StateChanged(e.Id, st); err != nil {
|
|
|
|
logrus.Errorf("libcontainerd: backend.StateChanged(): %v", err)
|
|
|
|
}
|
|
|
|
if st.State == StateRestart {
|
2016-03-18 21:50:19 +03:00
|
|
|
go func() {
|
2016-08-03 23:45:46 +03:00
|
|
|
err := <-waitRestart
|
2016-04-21 23:58:21 +03:00
|
|
|
ctr.client.lock(ctr.containerID)
|
|
|
|
defer ctr.client.unlock(ctr.containerID)
|
2016-03-18 21:50:19 +03:00
|
|
|
ctr.restarting = false
|
2016-08-04 01:28:52 +03:00
|
|
|
if err == nil {
|
2016-05-12 17:52:00 +03:00
|
|
|
if err = ctr.start("", ""); err != nil {
|
2016-08-04 01:28:52 +03:00
|
|
|
logrus.Errorf("libcontainerd: error restarting %v", err)
|
|
|
|
}
|
|
|
|
}
|
2016-03-18 21:50:19 +03:00
|
|
|
if err != nil {
|
|
|
|
st.State = StateExit
|
2016-04-21 23:58:21 +03:00
|
|
|
ctr.clean()
|
2016-03-18 21:50:19 +03:00
|
|
|
ctr.client.q.append(e.Id, func() {
|
|
|
|
if err := ctr.client.backend.StateChanged(e.Id, st); err != nil {
|
2016-07-23 01:20:14 +03:00
|
|
|
logrus.Errorf("libcontainerd: %v", err)
|
2016-03-18 21:50:19 +03:00
|
|
|
}
|
|
|
|
})
|
2016-04-18 21:48:28 +03:00
|
|
|
if err != restartmanager.ErrRestartCanceled {
|
2016-07-23 01:20:14 +03:00
|
|
|
logrus.Errorf("libcontainerd: %v", err)
|
2016-04-18 21:48:28 +03:00
|
|
|
}
|
2016-03-18 21:50:19 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Type == StatePause || e.Type == StateResume {
|
|
|
|
ctr.pauseMonitor.handle(e.Type)
|
|
|
|
}
|
|
|
|
if e.Type == StateExit {
|
|
|
|
if en := ctr.client.getExitNotifier(e.Id); en != nil {
|
|
|
|
en.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
default:
|
2016-07-23 01:20:14 +03:00
|
|
|
logrus.Debugf("libcontainerd: event unhandled: %+v", e)
|
2016-03-18 21:50:19 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-06-02 21:10:55 +03:00
|
|
|
|
|
|
|
// discardFifos attempts to fully read the container fifos to unblock processes
|
|
|
|
// that may be blocked on the writer side.
|
|
|
|
func (ctr *container) discardFifos() {
|
|
|
|
for _, i := range []int{syscall.Stdout, syscall.Stderr} {
|
|
|
|
f := ctr.fifo(i)
|
|
|
|
c := make(chan struct{})
|
|
|
|
go func() {
|
2016-07-14 20:13:54 +03:00
|
|
|
r := openReaderFromFifo(f)
|
2016-06-02 21:10:55 +03:00
|
|
|
close(c) // this channel is used to not close the writer too early, before readonly open has been called.
|
2016-07-14 20:13:54 +03:00
|
|
|
io.Copy(ioutil.Discard, r)
|
2016-06-02 21:10:55 +03:00
|
|
|
}()
|
|
|
|
<-c
|
|
|
|
closeReaderFifo(f) // avoid blocking permanently on open if there is no writer side
|
|
|
|
}
|
|
|
|
}
|