Handle start errors sync with runc

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2016-03-11 10:11:42 -08:00
parent 22dac6af92
commit b68bc651a8
3 changed files with 47 additions and 5 deletions

View file

@ -12,15 +12,12 @@ import (
"github.com/docker/docker/pkg/term" "github.com/docker/docker/pkg/term"
) )
var runtimeLog string
func setupLogger() { func setupLogger() {
f, err := os.OpenFile("/tmp/shim.log", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0755) f, err := os.OpenFile("/tmp/shim.log", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0755)
if err != nil { if err != nil {
panic(err) panic(err)
} }
logrus.SetOutput(f) logrus.SetOutput(f)
runtimeLog = "/tmp/runtime.log"
} }
// containerd-shim is a small shim that sits in front of a runtime implementation // containerd-shim is a small shim that sits in front of a runtime implementation

View file

@ -84,7 +84,11 @@ func (p *process) start() error {
if err != nil { if err != nil {
return err return err
} }
args := []string{"--log", runtimeLog} logPath := filepath.Join(cwd, "log.json")
args := []string{
"--log", logPath,
"--log-format", "json",
}
if p.state.Exec { if p.state.Exec {
args = append(args, "exec", args = append(args, "exec",
"--process", filepath.Join(cwd, "process.json"), "--process", filepath.Join(cwd, "process.json"),
@ -140,7 +144,6 @@ func (p *process) start() error {
} }
p.containerPid = pid p.containerPid = pid
return nil return nil
} }
func (p *process) pid() int { func (p *process) pid() int {

View file

@ -2,7 +2,9 @@ package runtime
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
@ -308,6 +310,20 @@ func waitForStart(p *process, cmd *exec.Cmd) error {
return err return err
} }
if !alive { if !alive {
// runc could have failed to run the container so lets get the error
// out of the logs
messages, err := readLogMessages(filepath.Join(p.root, "log.json"))
if err != nil {
if os.IsNotExist(err) {
return ErrContainerNotStarted
}
return err
}
for _, m := range messages {
if m.Level == "error" {
return errors.New(m.Msg)
}
}
return ErrContainerNotStarted return ErrContainerNotStarted
} }
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
@ -362,3 +378,29 @@ func (o *oom) Close() error {
} }
return err return err
} }
type message struct {
Level string `json:"level"`
Msg string `json:"msg"`
}
func readLogMessages(path string) ([]message, error) {
var out []message
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
dec := json.NewDecoder(f)
for {
var m message
if err := dec.Decode(&m); err != nil {
if err == io.EOF {
return out, nil
}
return nil, err
}
out = append(out, m)
}
return out, nil
}