2016-01-23 09:29:36 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"text/tabwriter"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/codegangsta/cli"
|
2016-01-29 05:32:24 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
2016-01-23 09:29:36 +08:00
|
|
|
)
|
|
|
|
|
2016-02-17 22:46:06 +08:00
|
|
|
const formatOptions = `table, json, yaml, xml, or "go=<go-template text>"`
|
|
|
|
|
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-02-17 22:46:06 +08:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "format, f",
|
|
|
|
Value: "",
|
|
|
|
Usage: `select one of: ` + formatOptions + `.
|
|
|
|
|
|
|
|
The default format is table. The following will output the list of containers
|
|
|
|
in json format:
|
|
|
|
|
|
|
|
# runc list -f json`,
|
|
|
|
},
|
|
|
|
},
|
2016-01-23 09:29:36 +08:00
|
|
|
Action: func(context *cli.Context) {
|
2016-02-17 22:46:06 +08:00
|
|
|
format := context.String("format")
|
|
|
|
if format != "" {
|
|
|
|
switch format {
|
|
|
|
default:
|
|
|
|
logrus.Fatal("invalid format, valid formats are: " + formatOptions)
|
|
|
|
case "":
|
|
|
|
format = "table"
|
|
|
|
case "json", "yaml", "xml":
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//quiet := context.Bool("quiet")
|
|
|
|
//noTrunc := context.Bool("no-trunc")
|
|
|
|
|
2016-01-29 05:32:24 +08:00
|
|
|
factory, err := loadFactory(context)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
2016-01-23 09:29:36 +08:00
|
|
|
}
|
|
|
|
// get the list of containers
|
|
|
|
root := context.GlobalString("root")
|
|
|
|
absRoot, err := filepath.Abs(root)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
list, err := ioutil.ReadDir(absRoot)
|
2016-01-29 05:32:24 +08:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2016-01-23 09:29:36 +08:00
|
|
|
w := tabwriter.NewWriter(os.Stdout, 12, 1, 3, ' ', 0)
|
|
|
|
fmt.Fprint(w, "ID\tPID\tSTATUS\tCREATED\n")
|
|
|
|
// output containers
|
|
|
|
for _, item := range list {
|
2016-01-29 05:32:24 +08:00
|
|
|
if item.IsDir() {
|
|
|
|
if err := outputListInfo(item.Name(), factory, w); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2016-01-23 09:29:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := w.Flush(); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-01-29 05:32:24 +08:00
|
|
|
func outputListInfo(id string, factory libcontainer.Factory, w *tabwriter.Writer) error {
|
2016-01-23 09:29:36 +08:00
|
|
|
container, err := factory.Load(id)
|
|
|
|
if err != nil {
|
2016-01-29 05:32:24 +08:00
|
|
|
return err
|
2016-01-23 09:29:36 +08:00
|
|
|
}
|
|
|
|
containerStatus, err := container.Status()
|
|
|
|
if err != nil {
|
2016-01-29 05:32:24 +08:00
|
|
|
return err
|
2016-01-23 09:29:36 +08:00
|
|
|
}
|
|
|
|
state, err := container.State()
|
|
|
|
if err != nil {
|
2016-01-29 05:32:24 +08:00
|
|
|
return err
|
2016-01-23 09:29:36 +08:00
|
|
|
}
|
2016-01-29 05:32:24 +08:00
|
|
|
fmt.Fprintf(w, "%s\t%d\t%s\t%s\n",
|
|
|
|
container.ID(),
|
2016-01-23 09:29:36 +08:00
|
|
|
state.BaseState.InitProcessPid,
|
|
|
|
containerStatus.String(),
|
2016-01-29 05:32:24 +08:00
|
|
|
state.BaseState.Created.Format(time.RFC3339Nano))
|
|
|
|
return nil
|
2016-01-23 09:29:36 +08:00
|
|
|
}
|