2017-02-01 00:45:59 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
|
2017-02-06 20:16:36 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
2017-02-01 00:45:59 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/utils"
|
2017-02-06 20:16:36 +00:00
|
|
|
"github.com/urfave/cli"
|
2017-02-01 00:45:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var stateCommand = cli.Command{
|
|
|
|
Name: "state",
|
|
|
|
Usage: "output the state of a container",
|
|
|
|
ArgsUsage: `<container-id>
|
|
|
|
|
|
|
|
Where "<container-id>" is your name for the instance of the container.`,
|
|
|
|
Description: `The state command outputs current state information for the
|
|
|
|
instance of a container.`,
|
2017-02-06 20:16:36 +00:00
|
|
|
Action: func(context *cli.Context) error {
|
|
|
|
if err := checkArgs(context, 1, exactArgs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-01 00:45:59 +00:00
|
|
|
container, err := getContainer(context)
|
|
|
|
if err != nil {
|
2017-02-06 20:16:36 +00:00
|
|
|
return err
|
2017-02-01 00:45:59 +00:00
|
|
|
}
|
|
|
|
containerStatus, err := container.Status()
|
|
|
|
if err != nil {
|
2017-02-06 20:16:36 +00:00
|
|
|
return err
|
2017-02-01 00:45:59 +00:00
|
|
|
}
|
|
|
|
state, err := container.State()
|
|
|
|
if err != nil {
|
2017-02-06 20:16:36 +00:00
|
|
|
return err
|
2017-02-01 00:45:59 +00:00
|
|
|
}
|
2017-02-06 20:16:36 +00:00
|
|
|
pid := state.BaseState.InitProcessPid
|
|
|
|
if containerStatus == libcontainer.Stopped {
|
|
|
|
pid = 0
|
|
|
|
}
|
|
|
|
bundle, annotations := utils.Annotations(state.Config.Labels)
|
|
|
|
cs := containerState{
|
2017-02-01 00:45:59 +00:00
|
|
|
Version: state.BaseState.Config.Version,
|
|
|
|
ID: state.BaseState.ID,
|
2017-02-06 20:16:36 +00:00
|
|
|
InitProcessPid: pid,
|
2017-02-01 00:45:59 +00:00
|
|
|
Status: containerStatus.String(),
|
2017-02-06 20:16:36 +00:00
|
|
|
Bundle: bundle,
|
2017-02-01 00:45:59 +00:00
|
|
|
Rootfs: state.BaseState.Config.Rootfs,
|
2017-02-06 20:16:36 +00:00
|
|
|
Created: state.BaseState.Created,
|
|
|
|
Annotations: annotations,
|
|
|
|
}
|
2017-02-01 00:45:59 +00:00
|
|
|
data, err := json.MarshalIndent(cs, "", " ")
|
|
|
|
if err != nil {
|
2017-02-06 20:16:36 +00:00
|
|
|
return err
|
2017-02-01 00:45:59 +00:00
|
|
|
}
|
|
|
|
os.Stdout.Write(data)
|
2017-02-06 20:16:36 +00:00
|
|
|
return nil
|
2017-02-01 00:45:59 +00:00
|
|
|
},
|
|
|
|
}
|