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

@ -2,7 +2,9 @@ package runtime
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
@ -308,6 +310,20 @@ func waitForStart(p *process, cmd *exec.Cmd) error {
return err
}
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
}
time.Sleep(100 * time.Millisecond)
@ -362,3 +378,29 @@ func (o *oom) Close() error {
}
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
}