2016-01-23 09:29:36 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-02-25 03:11:10 +08:00
|
|
|
"strings"
|
2016-01-23 09:29:36 +08:00
|
|
|
"text/tabwriter"
|
|
|
|
"time"
|
|
|
|
|
2016-02-18 16:08:38 +08:00
|
|
|
"encoding/json"
|
|
|
|
|
2016-01-23 09:29:36 +08:00
|
|
|
"github.com/codegangsta/cli"
|
|
|
|
)
|
|
|
|
|
2016-02-18 16:08:38 +08:00
|
|
|
const formatOptions = `table or json`
|
|
|
|
|
|
|
|
// containerState represents the platform agnostic pieces relating to a
|
|
|
|
// running container's status and state
|
|
|
|
type containerState struct {
|
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-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-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-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-18 16:08:38 +08:00
|
|
|
s, err := getContainers(context)
|
2016-01-29 05:32:24 +08:00
|
|
|
if err != nil {
|
2016-03-12 11:16:30 +08:00
|
|
|
fatal(err)
|
2016-01-23 09:29:36 +08:00
|
|
|
}
|
2016-02-18 16:08:38 +08:00
|
|
|
|
|
|
|
switch context.String("format") {
|
|
|
|
case "", "table":
|
|
|
|
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-03-12 11:16:30 +08:00
|
|
|
fatal(err)
|
2016-02-18 16:08:38 +08:00
|
|
|
}
|
|
|
|
case "json":
|
|
|
|
data, err := json.Marshal(s)
|
|
|
|
if err != nil {
|
2016-03-12 11:16:30 +08:00
|
|
|
fatal(err)
|
2016-02-18 16:08:38 +08:00
|
|
|
}
|
|
|
|
os.Stdout.Write(data)
|
|
|
|
|
|
|
|
default:
|
2016-03-12 11:16:30 +08:00
|
|
|
fatalf("invalid format option")
|
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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
containerStatus, err := container.Status()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
state, err := container.State()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s = append(s, containerState{
|
|
|
|
ID: state.BaseState.ID,
|
|
|
|
InitProcessPid: state.BaseState.InitProcessPid,
|
|
|
|
Status: containerStatus.String(),
|
2016-02-25 03:19:30 +08:00
|
|
|
Bundle: searchLabels(state.Config.Labels, "bundle"),
|
2016-02-18 16:08:38 +08:00
|
|
|
Created: state.BaseState.Created})
|
|
|
|
}
|
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
|
|
|
}
|
2016-02-25 03:11:10 +08:00
|
|
|
|
2016-02-25 03:19:30 +08:00
|
|
|
func searchLabels(labels []string, query string) string {
|
2016-02-25 03:11:10 +08:00
|
|
|
for _, l := range labels {
|
|
|
|
parts := strings.SplitN(l, "=", 2)
|
2016-02-25 03:19:30 +08:00
|
|
|
if len(parts) < 2 {
|
2016-02-25 03:11:10 +08:00
|
|
|
continue
|
|
|
|
}
|
2016-02-25 03:19:30 +08:00
|
|
|
if parts[0] == query {
|
2016-02-25 03:11:10 +08:00
|
|
|
return parts[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|