Merge pull request #522 from crosbymichael/created

Update list command and created methods
This commit is contained in:
Mrunal Patel 2016-02-04 09:47:10 +05:30
commit 11a238b891
5 changed files with 25 additions and 53 deletions

View File

@ -65,8 +65,8 @@ type BaseState struct {
// InitProcessStartTime is the init process start time in clock cycles since boot time. // InitProcessStartTime is the init process start time in clock cycles since boot time.
InitProcessStartTime string `json:"init_process_start"` InitProcessStartTime string `json:"init_process_start"`
// CreatedTime is the unix timestamp for the creation time of the container in UTC // Created is the unix timestamp for the creation time of the container in UTC
CreatedTime time.Time `json:"created_time"` Created time.Time `json:"created"`
// Config is the container's configuration. // Config is the container's configuration.
Config configs.Config `json:"config"` Config configs.Config `json:"config"`

View File

@ -40,7 +40,7 @@ type linuxContainer struct {
m sync.Mutex m sync.Mutex
criuVersion int criuVersion int
state containerState state containerState
createdTime time.Time created time.Time
} }
// State represents a running container's state // State represents a running container's state
@ -120,11 +120,6 @@ func (c *linuxContainer) ID() string {
return c.id return c.id
} }
// CreatedTime returns the timestamp from when the container was CREATED
func (c *linuxContainer) CreatedTime() time.Time {
return c.createdTime
}
// Config returns the container's configuration // Config returns the container's configuration
func (c *linuxContainer) Config() configs.Config { func (c *linuxContainer) Config() configs.Config {
return *c.config return *c.config
@ -198,7 +193,7 @@ func (c *linuxContainer) Start(process *Process) error {
return newSystemError(err) return newSystemError(err)
} }
// generate a timestamp indicating when the container was started // generate a timestamp indicating when the container was started
c.createdTime = time.Now().UTC() c.created = time.Now().UTC()
c.state = &runningState{ c.state = &runningState{
c: c, c: c,
@ -1053,7 +1048,7 @@ func (c *linuxContainer) currentState() (*State, error) {
Config: *c.config, Config: *c.config,
InitProcessPid: pid, InitProcessPid: pid,
InitProcessStartTime: startTime, InitProcessStartTime: startTime,
CreatedTime: c.createdTime, Created: c.created,
}, },
CgroupPaths: c.cgroupManager.GetPaths(), CgroupPaths: c.cgroupManager.GetPaths(),
NamespacePaths: make(map[configs.NamespaceType]string), NamespacePaths: make(map[configs.NamespaceType]string),

View File

@ -202,7 +202,7 @@ func (l *LinuxFactory) Load(id string) (Container, error) {
criuPath: l.CriuPath, criuPath: l.CriuPath,
cgroupManager: l.NewCgroupsManager(state.Config.Cgroups, state.CgroupPaths), cgroupManager: l.NewCgroupsManager(state.Config.Cgroups, state.CgroupPaths),
root: containerRoot, root: containerRoot,
createdTime: state.CreatedTime, created: state.Created,
} }
c.state = &createdState{c: c, s: Created} c.state = &createdState{c: c, s: Created}
if err := c.refreshState(); err != nil { if err := c.refreshState(); err != nil {

46
list.go
View File

@ -12,72 +12,60 @@ import (
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"github.com/opencontainers/runc/libcontainer"
) )
var listCommand = cli.Command{ var listCommand = cli.Command{
Name: "list", Name: "list",
Usage: "lists containers started by runc with the given root", Usage: "lists containers started by runc with the given root",
Action: func(context *cli.Context) { Action: func(context *cli.Context) {
factory, err := loadFactory(context)
// preload the container factory
if factory == nil {
err := factoryPreload(context)
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
return
} }
}
// get the list of containers // get the list of containers
root := context.GlobalString("root") root := context.GlobalString("root")
absRoot, err := filepath.Abs(root) absRoot, err := filepath.Abs(root)
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
return
} }
list, err := ioutil.ReadDir(absRoot) list, err := ioutil.ReadDir(absRoot)
if err != nil {
logrus.Fatal(err)
}
w := tabwriter.NewWriter(os.Stdout, 12, 1, 3, ' ', 0) w := tabwriter.NewWriter(os.Stdout, 12, 1, 3, ' ', 0)
fmt.Fprint(w, "ID\tPID\tSTATUS\tCREATED\n") fmt.Fprint(w, "ID\tPID\tSTATUS\tCREATED\n")
// output containers // output containers
for _, item := range list { for _, item := range list {
switch { if item.IsDir() {
case !item.IsDir(): if err := outputListInfo(item.Name(), factory, w); err != nil {
// do nothing with misc files in the containers directory logrus.Fatal(err)
case item.IsDir(): }
outputListInfo(item.Name(), w)
} }
} }
if err := w.Flush(); err != nil { if err := w.Flush(); err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
}, },
} }
func outputListInfo(id string, w *tabwriter.Writer) { func outputListInfo(id string, factory libcontainer.Factory, w *tabwriter.Writer) error {
container, err := factory.Load(id) container, err := factory.Load(id)
if err != nil { if err != nil {
logrus.Fatal(err) return err
return
} }
containerStatus, err := container.Status() containerStatus, err := container.Status()
if err != nil { if err != nil {
logrus.Fatal(err) return err
return
} }
state, err := container.State() state, err := container.State()
if err != nil { if err != nil {
logrus.Fatal(err) return err
return
} }
fmt.Fprintf(w, "%s\t%d\t%s\t%s\n",
fmt.Fprintf(w, "%s\t%d\t%s\t%s\n", container.ID(), container.ID(),
state.BaseState.InitProcessPid, state.BaseState.InitProcessPid,
containerStatus.String(), containerStatus.String(),
state.BaseState.CreatedTime.Format(time.RFC3339Nano)) state.BaseState.Created.Format(time.RFC3339Nano))
return nil
} }

View File

@ -86,17 +86,6 @@ func containerPreload(context *cli.Context) error {
return nil return nil
} }
var factory libcontainer.Factory
func factoryPreload(context *cli.Context) error {
f, err := loadFactory(context)
if err != nil {
return err
}
factory = f
return nil
}
// loadFactory returns the configured factory instance for execing containers. // loadFactory returns the configured factory instance for execing containers.
func loadFactory(context *cli.Context) (libcontainer.Factory, error) { func loadFactory(context *cli.Context) (libcontainer.Factory, error) {
root := context.GlobalString("root") root := context.GlobalString("root")