1// +build linux 2 3package main 4 5import ( 6 "encoding/json" 7 "os" 8 "time" 9 10 "github.com/codegangsta/cli" 11 "github.com/opencontainers/runc/libcontainer/utils" 12) 13 14// cState represents the platform agnostic pieces relating to a running 15// container's status and state. Note: The fields in this structure adhere to 16// the opencontainers/runtime-spec/specs-go requirement for json fields that must be returned 17// in a state command. 18type cState struct { 19 // Version is the OCI version for the container 20 Version string `json:"ociVersion"` 21 // ID is the container ID 22 ID string `json:"id"` 23 // InitProcessPid is the init process id in the parent namespace 24 InitProcessPid int `json:"pid"` 25 // Bundle is the path on the filesystem to the bundle 26 Bundle string `json:"bundlePath"` 27 // Rootfs is a path to a directory containing the container's root filesystem. 28 Rootfs string `json:"rootfsPath"` 29 // Status is the current status of the container, running, paused, ... 30 Status string `json:"status"` 31 // Created is the unix timestamp for the creation time of the container in UTC 32 Created time.Time `json:"created"` 33} 34 35var stateCommand = cli.Command{ 36 Name: "state", 37 Usage: "output the state of a container", 38 ArgsUsage: `<container-id> 39 40Where "<container-id>" is your name for the instance of the container.`, 41 Description: `The state command outputs current state information for the 42instance of a container.`, 43 Action: func(context *cli.Context) { 44 container, err := getContainer(context) 45 if err != nil { 46 fatal(err) 47 } 48 containerStatus, err := container.Status() 49 if err != nil { 50 fatal(err) 51 } 52 state, err := container.State() 53 if err != nil { 54 fatal(err) 55 } 56 cs := cState{ 57 Version: state.BaseState.Config.Version, 58 ID: state.BaseState.ID, 59 InitProcessPid: state.BaseState.InitProcessPid, 60 Status: containerStatus.String(), 61 Bundle: utils.SearchLabels(state.Config.Labels, "bundle"), 62 Rootfs: state.BaseState.Config.Rootfs, 63 Created: state.BaseState.Created} 64 data, err := json.MarshalIndent(cs, "", " ") 65 if err != nil { 66 fatal(err) 67 } 68 os.Stdout.Write(data) 69 }, 70} 71