2016-03-02 00:59:34 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
|
2016-08-26 00:42:21 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
2016-04-06 23:57:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/utils"
|
2016-06-07 02:45:46 +08:00
|
|
|
"github.com/urfave/cli"
|
2016-03-02 00:59:34 +08: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.`,
|
2016-05-10 13:58:09 +08:00
|
|
|
Action: func(context *cli.Context) error {
|
2016-03-02 00:59:34 +08:00
|
|
|
container, err := getContainer(context)
|
|
|
|
if err != nil {
|
2016-05-10 13:58:09 +08:00
|
|
|
return err
|
2016-03-02 00:59:34 +08:00
|
|
|
}
|
|
|
|
containerStatus, err := container.Status()
|
|
|
|
if err != nil {
|
2016-05-10 13:58:09 +08:00
|
|
|
return err
|
2016-03-02 00:59:34 +08:00
|
|
|
}
|
|
|
|
state, err := container.State()
|
|
|
|
if err != nil {
|
2016-05-10 13:58:09 +08:00
|
|
|
return err
|
2016-03-02 00:59:34 +08:00
|
|
|
}
|
2016-08-26 00:42:21 +08:00
|
|
|
pid := state.BaseState.InitProcessPid
|
|
|
|
if containerStatus == libcontainer.Stopped {
|
|
|
|
pid = 0
|
|
|
|
}
|
2016-06-03 03:44:43 +08:00
|
|
|
bundle, annotations := utils.Annotations(state.Config.Labels)
|
2016-09-16 01:53:14 +08:00
|
|
|
cs := containerState{
|
2016-03-02 00:59:34 +08:00
|
|
|
Version: state.BaseState.Config.Version,
|
|
|
|
ID: state.BaseState.ID,
|
2016-08-26 00:42:21 +08:00
|
|
|
InitProcessPid: pid,
|
2016-03-02 00:59:34 +08:00
|
|
|
Status: containerStatus.String(),
|
2016-06-03 03:44:43 +08:00
|
|
|
Bundle: bundle,
|
2016-03-02 00:59:34 +08:00
|
|
|
Rootfs: state.BaseState.Config.Rootfs,
|
2016-06-03 03:44:43 +08:00
|
|
|
Created: state.BaseState.Created,
|
|
|
|
Annotations: annotations,
|
|
|
|
}
|
2016-03-02 00:59:34 +08:00
|
|
|
data, err := json.MarshalIndent(cs, "", " ")
|
|
|
|
if err != nil {
|
2016-05-10 13:58:09 +08:00
|
|
|
return err
|
2016-03-02 00:59:34 +08:00
|
|
|
}
|
|
|
|
os.Stdout.Write(data)
|
2016-05-10 13:58:09 +08:00
|
|
|
return nil
|
2016-03-02 00:59:34 +08:00
|
|
|
},
|
|
|
|
}
|