-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
57 lines (45 loc) · 1.09 KB
/
state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package runrunc
import (
"bytes"
"encoding/json"
"fmt"
"os/exec"
"code.cloudfoundry.org/lager"
)
type Status string
const CreatedStatus Status = "created"
const StoppedStatus Status = "stopped"
const RunningStatus Status = "running"
type State struct {
Pid int
Status Status
}
type Stater struct {
runner RuncCmdRunner
runc RuncBinary
}
func NewStater(runner RuncCmdRunner, runc RuncBinary) *Stater {
return &Stater{
runner, runc,
}
}
// State gets the state of the bundle
func (r *Stater) State(log lager.Logger, handle string) (state State, err error) {
log = log.Session("state", lager.Data{"handle": handle})
log.Debug("started")
defer log.Debug("finished")
buf := new(bytes.Buffer)
err = r.runner.RunAndLog(log, func(logFile string) *exec.Cmd {
cmd := r.runc.StateCommand(handle, logFile)
cmd.Stdout = buf
return cmd
})
if err != nil {
return State{}, fmt.Errorf("runc state: %s", err)
}
if err := json.NewDecoder(buf).Decode(&state); err != nil {
log.Error("decode-state-failed", err)
return State{}, fmt.Errorf("runc state: %s", err)
}
return state, nil
}