state.go 2.1 KB

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