forked from opencontainers/runc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
71 lines (66 loc) · 1.48 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// +build linux
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"text/tabwriter"
"time"
"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/opencontainers/runc/libcontainer"
)
var listCommand = cli.Command{
Name: "list",
Usage: "lists containers started by runc with the given root",
Action: func(context *cli.Context) {
factory, err := loadFactory(context)
if err != nil {
logrus.Fatal(err)
}
// 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)
if err != nil {
logrus.Fatal(err)
}
w := tabwriter.NewWriter(os.Stdout, 12, 1, 3, ' ', 0)
fmt.Fprint(w, "ID\tPID\tSTATUS\tCREATED\n")
// output containers
for _, item := range list {
if item.IsDir() {
if err := outputListInfo(item.Name(), factory, w); err != nil {
logrus.Fatal(err)
}
}
}
if err := w.Flush(); err != nil {
logrus.Fatal(err)
}
},
}
func outputListInfo(id string, factory libcontainer.Factory, w *tabwriter.Writer) error {
container, err := factory.Load(id)
if err != nil {
return err
}
containerStatus, err := container.Status()
if err != nil {
return err
}
state, err := container.State()
if err != nil {
return err
}
fmt.Fprintf(w, "%s\t%d\t%s\t%s\n",
container.ID(),
state.BaseState.InitProcessPid,
containerStatus.String(),
state.BaseState.Created.Format(time.RFC3339Nano))
return nil
}