From ac43d4a0abc5bee146bb37c24ccf33f19b88f655 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Wed, 24 Feb 2016 11:11:10 -0800 Subject: [PATCH] Save bundle path in labels This saves and returns the bundle path for the container in the container's config and state. It also returns the information via runc list. Signed-off-by: Michael Crosby --- list.go | 29 ++++++++++++++++++++++------- spec.go | 10 +++++++++- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/list.go b/list.go index eba1e2af8..643e8c1b7 100644 --- a/list.go +++ b/list.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "os" "path/filepath" + "strings" "text/tabwriter" "time" @@ -21,15 +22,14 @@ const formatOptions = `table or json` // containerState represents the platform agnostic pieces relating to a // running container's status and state type containerState struct { - // ID is the container ID. + // ID is the container ID ID string `json:"id"` - - // InitProcessPid is the init process id in the parent namespace. + // InitProcessPid is the init process id in the parent namespace InitProcessPid int `json:"pid"` - // Status is the current status of the container, running, paused, ... Status string `json:"status"` - + // Bundle is the path on the filesystem to the bundle + Bundle string `json:"bundle"` // Created is the unix timestamp for the creation time of the container in UTC Created time.Time `json:"created"` } @@ -58,12 +58,13 @@ in json format: switch context.String("format") { case "", "table": w := tabwriter.NewWriter(os.Stdout, 12, 1, 3, ' ', 0) - fmt.Fprint(w, "ID\tPID\tSTATUS\tCREATED\n") + fmt.Fprint(w, "ID\tPID\tSTATUS\tBUNDLE\tCREATED\n") for _, item := range s { - fmt.Fprintf(w, "%s\t%d\t%s\t%s\n", + fmt.Fprintf(w, "%s\t%d\t%s\t%s\t%s\n", item.ID, item.InitProcessPid, item.Status, + item.Bundle, item.Created.Format(time.RFC3339Nano)) } if err := w.Flush(); err != nil { @@ -116,8 +117,22 @@ func getContainers(context *cli.Context) ([]containerState, error) { ID: state.BaseState.ID, InitProcessPid: state.BaseState.InitProcessPid, Status: containerStatus.String(), + Bundle: getBundlePath(state.Config.Labels), Created: state.BaseState.Created}) } } return s, nil } + +func getBundlePath(labels []string) string { + for _, l := range labels { + parts := strings.SplitN(l, "=", 2) + if len(parts) < 1 { + continue + } + if parts[0] == "bundle" { + return parts[1] + } + } + return "" +} diff --git a/spec.go b/spec.go index 70ccdd81c..d7fef6fca 100644 --- a/spec.go +++ b/spec.go @@ -231,7 +231,12 @@ func loadSpec(cPath string) (spec *specs.LinuxSpec, err error) { } func createLibcontainerConfig(cgroupName string, spec *specs.LinuxSpec) (*configs.Config, error) { - cwd, err := os.Getwd() + // runc's cwd will always be the bundle path + rcwd, err := os.Getwd() + if err != nil { + return nil, err + } + cwd, err := filepath.Abs(rcwd) if err != nil { return nil, err } @@ -244,6 +249,9 @@ func createLibcontainerConfig(cgroupName string, spec *specs.LinuxSpec) (*config Capabilities: spec.Linux.Capabilities, Readonlyfs: spec.Root.Readonly, Hostname: spec.Hostname, + Labels: []string{ + "bundle=" + cwd, + }, } exists := false