2016-01-23 09:29:36 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"text/tabwriter"
|
|
|
|
"time"
|
|
|
|
|
2016-02-18 16:08:38 +08:00
|
|
|
"encoding/json"
|
|
|
|
|
2016-08-26 00:42:21 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
2016-04-06 23:57:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/utils"
|
2016-06-07 02:45:46 +08:00
|
|
|
"github.com/urfave/cli"
|
2016-01-23 09:29:36 +08:00
|
|
|
)
|
|
|
|
|
2016-06-04 16:57:13 +08:00
|
|
|
const formatOptions = `table or json`
|
2016-02-18 16:08:38 +08:00
|
|
|
|
|
|
|
// containerState represents the platform agnostic pieces relating to a
|
|
|
|
// running container's status and state
|
|
|
|
type containerState struct {
|
2016-09-16 01:53:14 +08:00
|
|
|
// Version is the OCI version for the container
|
|
|
|
Version string `json:"ociVersion"`
|
2016-02-25 03:11:10 +08:00
|
|
|
// ID is the container ID
|
2016-02-18 16:08:38 +08:00
|
|
|
ID string `json:"id"`
|
2016-02-25 03:11:10 +08:00
|
|
|
// InitProcessPid is the init process id in the parent namespace
|
2016-02-18 16:08:38 +08:00
|
|
|
InitProcessPid int `json:"pid"`
|
|
|
|
// Status is the current status of the container, running, paused, ...
|
|
|
|
Status string `json:"status"`
|
2016-02-25 03:11:10 +08:00
|
|
|
// Bundle is the path on the filesystem to the bundle
|
|
|
|
Bundle string `json:"bundle"`
|
2016-09-16 01:53:14 +08:00
|
|
|
// Rootfs is a path to a directory containing the container's root filesystem.
|
|
|
|
Rootfs string `json:"rootfs"`
|
2016-02-18 16:08:38 +08:00
|
|
|
// Created is the unix timestamp for the creation time of the container in UTC
|
|
|
|
Created time.Time `json:"created"`
|
2016-06-03 03:44:43 +08:00
|
|
|
// Annotations is the user defined annotations added to the config.
|
|
|
|
Annotations map[string]string `json:"annotations,omitempty"`
|
2016-02-18 16:08:38 +08:00
|
|
|
}
|
2016-02-17 22:46:06 +08:00
|
|
|
|
2016-01-23 09:29:36 +08:00
|
|
|
var listCommand = cli.Command{
|
|
|
|
Name: "list",
|
|
|
|
Usage: "lists containers started by runc with the given root",
|
2016-07-11 22:31:07 +08:00
|
|
|
ArgsUsage: `
|
|
|
|
|
|
|
|
Where the given root is specified via the global option "--root"
|
|
|
|
(default: "/run/runc").
|
|
|
|
|
|
|
|
EXAMPLE 1:
|
|
|
|
To list containers created via the default "--root":
|
2016-07-19 08:56:25 +08:00
|
|
|
# runc list
|
2016-07-11 22:31:07 +08:00
|
|
|
|
|
|
|
EXAMPLE 2:
|
|
|
|
To list containers created using a non-default value for "--root":
|
2016-07-19 08:56:25 +08:00
|
|
|
# runc --root value list`,
|
2016-02-17 22:46:06 +08:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "format, f",
|
2016-06-04 16:57:13 +08:00
|
|
|
Value: "table",
|
2016-05-24 17:30:43 +08:00
|
|
|
Usage: `select one of: ` + formatOptions,
|
2016-02-17 22:46:06 +08:00
|
|
|
},
|
2016-04-14 07:28:14 +08:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "quiet, q",
|
|
|
|
Usage: "display only container IDs",
|
|
|
|
},
|
2016-02-17 22:46:06 +08:00
|
|
|
},
|
2016-05-10 13:58:09 +08:00
|
|
|
Action: func(context *cli.Context) error {
|
2016-10-28 23:43:10 +08:00
|
|
|
if err := checkArgs(context, 0, exactArgs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-18 16:08:38 +08:00
|
|
|
s, err := getContainers(context)
|
2016-01-29 05:32:24 +08:00
|
|
|
if err != nil {
|
2016-05-10 13:58:09 +08:00
|
|
|
return err
|
2016-01-23 09:29:36 +08:00
|
|
|
}
|
2016-02-18 16:08:38 +08:00
|
|
|
|
2016-04-14 07:28:14 +08:00
|
|
|
if context.Bool("quiet") {
|
|
|
|
for _, item := range s {
|
|
|
|
fmt.Println(item.ID)
|
|
|
|
}
|
2016-05-10 13:58:09 +08:00
|
|
|
return nil
|
2016-04-14 07:28:14 +08:00
|
|
|
}
|
|
|
|
|
2016-02-18 16:08:38 +08:00
|
|
|
switch context.String("format") {
|
2016-06-04 16:57:13 +08:00
|
|
|
case "table":
|
2016-02-18 16:08:38 +08:00
|
|
|
w := tabwriter.NewWriter(os.Stdout, 12, 1, 3, ' ', 0)
|
2016-02-25 03:11:10 +08:00
|
|
|
fmt.Fprint(w, "ID\tPID\tSTATUS\tBUNDLE\tCREATED\n")
|
2016-02-18 16:08:38 +08:00
|
|
|
for _, item := range s {
|
2016-02-25 03:11:10 +08:00
|
|
|
fmt.Fprintf(w, "%s\t%d\t%s\t%s\t%s\n",
|
2016-02-18 16:08:38 +08:00
|
|
|
item.ID,
|
|
|
|
item.InitProcessPid,
|
|
|
|
item.Status,
|
2016-02-25 03:11:10 +08:00
|
|
|
item.Bundle,
|
2016-02-18 16:08:38 +08:00
|
|
|
item.Created.Format(time.RFC3339Nano))
|
2016-01-23 09:29:36 +08:00
|
|
|
}
|
2016-02-18 16:08:38 +08:00
|
|
|
if err := w.Flush(); err != nil {
|
2016-05-10 13:58:09 +08:00
|
|
|
return err
|
2016-02-18 16:08:38 +08:00
|
|
|
}
|
|
|
|
case "json":
|
2016-04-26 06:21:07 +08:00
|
|
|
if err := json.NewEncoder(os.Stdout).Encode(s); err != nil {
|
2016-05-10 13:58:09 +08:00
|
|
|
return err
|
2016-02-18 16:08:38 +08:00
|
|
|
}
|
|
|
|
default:
|
2016-05-10 13:58:09 +08:00
|
|
|
return fmt.Errorf("invalid format option")
|
2016-01-23 09:29:36 +08:00
|
|
|
}
|
2016-05-10 13:58:09 +08:00
|
|
|
return nil
|
2016-01-23 09:29:36 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-02-18 16:08:38 +08:00
|
|
|
func getContainers(context *cli.Context) ([]containerState, error) {
|
|
|
|
factory, err := loadFactory(context)
|
2016-01-23 09:29:36 +08:00
|
|
|
if err != nil {
|
2016-02-18 16:08:38 +08:00
|
|
|
return nil, err
|
2016-01-23 09:29:36 +08:00
|
|
|
}
|
2016-02-18 16:08:38 +08:00
|
|
|
root := context.GlobalString("root")
|
|
|
|
absRoot, err := filepath.Abs(root)
|
2016-01-23 09:29:36 +08:00
|
|
|
if err != nil {
|
2016-02-18 16:08:38 +08:00
|
|
|
return nil, err
|
2016-01-23 09:29:36 +08:00
|
|
|
}
|
2016-02-18 16:08:38 +08:00
|
|
|
list, err := ioutil.ReadDir(absRoot)
|
2016-01-23 09:29:36 +08:00
|
|
|
if err != nil {
|
2016-03-12 11:16:30 +08:00
|
|
|
fatal(err)
|
2016-02-18 16:08:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var s []containerState
|
|
|
|
for _, item := range list {
|
|
|
|
if item.IsDir() {
|
|
|
|
container, err := factory.Load(item.Name())
|
|
|
|
if err != nil {
|
2016-09-13 01:32:47 +08:00
|
|
|
fmt.Fprintf(os.Stderr, "load container %s: %v\n", item.Name(), err)
|
|
|
|
continue
|
2016-02-18 16:08:38 +08:00
|
|
|
}
|
|
|
|
containerStatus, err := container.Status()
|
|
|
|
if err != nil {
|
2016-09-13 01:32:47 +08:00
|
|
|
fmt.Fprintf(os.Stderr, "status for %s: %v\n", item.Name(), err)
|
|
|
|
continue
|
2016-02-18 16:08:38 +08:00
|
|
|
}
|
|
|
|
state, err := container.State()
|
|
|
|
if err != nil {
|
2016-09-13 01:32:47 +08:00
|
|
|
fmt.Fprintf(os.Stderr, "state for %s: %v\n", item.Name(), err)
|
|
|
|
continue
|
2016-02-18 16:08:38 +08:00
|
|
|
}
|
2016-08-26 00:42:21 +08:00
|
|
|
pid := state.BaseState.InitProcessPid
|
|
|
|
if containerStatus == libcontainer.Stopped {
|
|
|
|
pid = 0
|
|
|
|
}
|
2016-06-03 03:44:43 +08:00
|
|
|
bundle, annotations := utils.Annotations(state.Config.Labels)
|
2016-02-18 16:08:38 +08:00
|
|
|
s = append(s, containerState{
|
2016-09-16 01:53:14 +08:00
|
|
|
Version: state.BaseState.Config.Version,
|
2016-02-18 16:08:38 +08:00
|
|
|
ID: state.BaseState.ID,
|
2016-08-26 00:42:21 +08:00
|
|
|
InitProcessPid: pid,
|
2016-02-18 16:08:38 +08:00
|
|
|
Status: containerStatus.String(),
|
2016-06-03 03:44:43 +08:00
|
|
|
Bundle: bundle,
|
2016-09-16 01:53:14 +08:00
|
|
|
Rootfs: state.BaseState.Config.Rootfs,
|
2016-06-03 03:44:43 +08:00
|
|
|
Created: state.BaseState.Created,
|
|
|
|
Annotations: annotations,
|
|
|
|
})
|
2016-02-18 16:08:38 +08:00
|
|
|
}
|
2016-01-23 09:29:36 +08:00
|
|
|
}
|
2016-02-18 16:08:38 +08:00
|
|
|
return s, nil
|
2016-01-23 09:29:36 +08:00
|
|
|
}
|