Merge pull request #13 from mrunalp/finish_status

Report exit code and finish time on container status
This commit is contained in:
Antonio Murdaca 2016-09-17 09:31:08 +02:00 committed by GitHub
commit 66fdf3ae21
2 changed files with 34 additions and 3 deletions

View file

@ -4,9 +4,11 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"sync" "sync"
"syscall" "syscall"
@ -150,6 +152,27 @@ func (r *Runtime) UpdateStatus(c *Container) error {
if err := json.NewDecoder(stateReader).Decode(&c.state); err != nil { if err := json.NewDecoder(stateReader).Decode(&c.state); err != nil {
return fmt.Errorf("failed to decode container status for %s: %s", c.name, err) return fmt.Errorf("failed to decode container status for %s: %s", c.name, err)
} }
if c.state.Status == "stopped" {
exitFilePath := filepath.Join(c.bundlePath, "exit")
fi, err := os.Stat(exitFilePath)
if err != nil {
return fmt.Errorf("failed to find container exit file: %v", err)
}
st := fi.Sys().(*syscall.Stat_t)
c.state.Finished = time.Unix(int64(st.Ctim.Sec), int64(st.Ctim.Nsec))
statusCodeStr, err := ioutil.ReadFile(exitFilePath)
if err != nil {
return fmt.Errorf("failed to read exit file: %v", err)
}
statusCode, err := strconv.Atoi(string(statusCodeStr))
if err != nil {
return fmt.Errorf("status code conversion failed: %v", err)
}
c.state.ExitCode = int32(statusCode)
}
return nil return nil
} }
@ -177,6 +200,8 @@ type ContainerState struct {
specs.State specs.State
Created time.Time `json:"created"` Created time.Time `json:"created"`
Started time.Time `json:"started"` Started time.Time `json:"started"`
Finished time.Time `json:"finished"`
ExitCode int32 `json:"exitCode"`
} }
// NewContainer creates a container object. // NewContainer creates a container object.

View file

@ -248,6 +248,10 @@ func int64Ptr(i int64) *int64 {
return &i return &i
} }
func int32Ptr(i int32) *int32 {
return &i
}
func sPtr(s string) *string { func sPtr(s string) *string {
return &s return &s
} }
@ -668,12 +672,14 @@ func (s *Server) ContainerStatus(ctx context.Context, req *pb.ContainerStatusReq
started := cState.Started.Unix() started := cState.Started.Unix()
csr.Status.StartedAt = int64Ptr(started) csr.Status.StartedAt = int64Ptr(started)
case "stopped": case "stopped":
// TODO: Get the exit time
rStatus = pb.ContainerState_EXITED rStatus = pb.ContainerState_EXITED
created := cState.Created.Unix() created := cState.Created.Unix()
csr.Status.CreatedAt = int64Ptr(created) csr.Status.CreatedAt = int64Ptr(created)
started := cState.Started.Unix() started := cState.Started.Unix()
csr.Status.StartedAt = int64Ptr(started) csr.Status.StartedAt = int64Ptr(started)
finished := cState.Finished.Unix()
csr.Status.FinishedAt = int64Ptr(finished)
csr.Status.ExitCode = int32Ptr(cState.ExitCode)
} }
csr.Status.State = &rStatus csr.Status.State = &rStatus