adds list command, and a timestamp in the container state
Signed-off-by: Mike Brown <brownwm@us.ibm.com>
This commit is contained in:
parent
3268a1ea00
commit
4c871267db
|
@ -6,6 +6,7 @@ package libcontainer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/opencontainers/runc/libcontainer/configs"
|
"github.com/opencontainers/runc/libcontainer/configs"
|
||||||
)
|
)
|
||||||
|
@ -56,9 +57,12 @@ type BaseState struct {
|
||||||
// InitProcessPid is the init process id in the parent namespace.
|
// InitProcessPid is the init process id in the parent namespace.
|
||||||
InitProcessPid int `json:"init_process_pid"`
|
InitProcessPid int `json:"init_process_pid"`
|
||||||
|
|
||||||
// InitProcessStartTime is the init process start 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
|
||||||
|
CreatedTime time.Time `json:"created_time"`
|
||||||
|
|
||||||
// Config is the container's configuration.
|
// Config is the container's configuration.
|
||||||
Config configs.Config `json:"config"`
|
Config configs.Config `json:"config"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
|
@ -38,6 +39,7 @@ type linuxContainer struct {
|
||||||
m sync.Mutex
|
m sync.Mutex
|
||||||
criuVersion int
|
criuVersion int
|
||||||
state containerState
|
state containerState
|
||||||
|
createdTime time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
// State represents a running container's state
|
// State represents a running container's state
|
||||||
|
@ -117,6 +119,11 @@ 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
|
||||||
|
@ -189,6 +196,9 @@ func (c *linuxContainer) Start(process *Process) error {
|
||||||
}
|
}
|
||||||
return newSystemError(err)
|
return newSystemError(err)
|
||||||
}
|
}
|
||||||
|
// generate a timestamp indicating when the container was started
|
||||||
|
c.createdTime = time.Now().UTC()
|
||||||
|
|
||||||
c.state = &runningState{
|
c.state = &runningState{
|
||||||
c: c,
|
c: c,
|
||||||
}
|
}
|
||||||
|
@ -1042,6 +1052,7 @@ func (c *linuxContainer) currentState() (*State, error) {
|
||||||
Config: *c.config,
|
Config: *c.config,
|
||||||
InitProcessPid: pid,
|
InitProcessPid: pid,
|
||||||
InitProcessStartTime: startTime,
|
InitProcessStartTime: startTime,
|
||||||
|
CreatedTime: c.createdTime,
|
||||||
},
|
},
|
||||||
CgroupPaths: c.cgroupManager.GetPaths(),
|
CgroupPaths: c.cgroupManager.GetPaths(),
|
||||||
NamespacePaths: make(map[configs.NamespaceType]string),
|
NamespacePaths: make(map[configs.NamespaceType]string),
|
||||||
|
|
|
@ -201,6 +201,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,
|
||||||
}
|
}
|
||||||
c.state = &nullState{c: c}
|
c.state = &nullState{c: c}
|
||||||
if err := c.refreshState(); err != nil {
|
if err := c.refreshState(); err != nil {
|
||||||
|
|
|
@ -0,0 +1,83 @@
|
||||||
|
// +build linux
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"text/tabwriter"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/Sirupsen/logrus"
|
||||||
|
"github.com/codegangsta/cli"
|
||||||
|
)
|
||||||
|
|
||||||
|
var listCommand = cli.Command{
|
||||||
|
Name: "list",
|
||||||
|
Usage: "lists containers started by runc with the given root",
|
||||||
|
Action: func(context *cli.Context) {
|
||||||
|
|
||||||
|
// preload the container factory
|
||||||
|
if factory == nil {
|
||||||
|
err := factoryPreload(context)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the list of containers
|
||||||
|
root := context.GlobalString("root")
|
||||||
|
absRoot, err := filepath.Abs(root)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
list, err := ioutil.ReadDir(absRoot)
|
||||||
|
|
||||||
|
w := tabwriter.NewWriter(os.Stdout, 12, 1, 3, ' ', 0)
|
||||||
|
fmt.Fprint(w, "ID\tPID\tSTATUS\tCREATED\n")
|
||||||
|
|
||||||
|
// output containers
|
||||||
|
for _, item := range list {
|
||||||
|
switch {
|
||||||
|
case !item.IsDir():
|
||||||
|
// do nothing with misc files in the containers directory
|
||||||
|
case item.IsDir():
|
||||||
|
outputListInfo(item.Name(), w)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := w.Flush(); err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func outputListInfo(id string, w *tabwriter.Writer) {
|
||||||
|
container, err := factory.Load(id)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
containerStatus, err := container.Status()
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
state, err := container.State()
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(w, "%s\t%d\t%s\t%s\n", container.ID(),
|
||||||
|
state.BaseState.InitProcessPid,
|
||||||
|
containerStatus.String(),
|
||||||
|
state.BaseState.CreatedTime.Format(time.RFC3339Nano))
|
||||||
|
|
||||||
|
}
|
1
main.go
1
main.go
|
@ -79,6 +79,7 @@ func main() {
|
||||||
pauseCommand,
|
pauseCommand,
|
||||||
resumeCommand,
|
resumeCommand,
|
||||||
execCommand,
|
execCommand,
|
||||||
|
listCommand,
|
||||||
}
|
}
|
||||||
app.Before = func(context *cli.Context) error {
|
app.Before = func(context *cli.Context) error {
|
||||||
if context.GlobalBool("debug") {
|
if context.GlobalBool("debug") {
|
||||||
|
|
Loading…
Reference in New Issue