forked from hyperhq/runv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
139 lines (123 loc) · 3.34 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"text/tabwriter"
"time"
"github.com/codegangsta/cli"
"github.com/opencontainers/runtime-spec/specs-go"
)
const formatOptions = `table or json`
// containerState represents the platform agnostic pieces relating to a
// running container's status and state
type containerState struct {
// ID is the container ID
ID string `json:"id"`
// InitProcessPid is the init process id in the parent namespace
InitProcessPid int `json:"pid"`
// Status is the current status of the container, running, paused, ...
Status string `json:"status"`
// Bundle is the path on the filesystem to the bundle
Bundle string `json:"bundle"`
// Created is the unix timestamp for the creation time of the container in UTC
Created time.Time `json:"created"`
}
var listCommand = cli.Command{
Name: "list",
Usage: "lists containers started by runv with the given root",
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:
# runv list -f json`,
},
},
Action: func(context *cli.Context) {
s, err := getContainers(context)
if err != nil {
fatal(err)
}
switch context.String("format") {
case "", "table":
w := tabwriter.NewWriter(os.Stdout, 12, 1, 3, ' ', 0)
fmt.Fprint(w, "ID\tPID\tSTATUS\tBUNDLE\tCREATED\n")
for _, item := range s {
fmt.Fprintf(w, "%s\t%d\t%s\t%s\t%s\n",
item.ID,
item.InitProcessPid,
item.Status,
item.Bundle,
item.Created.Format(time.RFC3339Nano))
}
if err := w.Flush(); err != nil {
fatal(err)
}
case "json":
data, err := json.Marshal(s)
if err != nil {
fatal(err)
}
os.Stdout.Write(data)
default:
fatal(fmt.Errorf("invalid format option"))
}
},
}
func getContainers(context *cli.Context) ([]containerState, error) {
root := context.GlobalString("root")
absRoot, err := filepath.Abs(root)
if err != nil {
return nil, err
}
list, err := ioutil.ReadDir(absRoot)
if err != nil {
return nil, err
}
var s []containerState
for _, item := range list {
if item.IsDir() {
stateFile := filepath.Join(absRoot, item.Name(), stateJson)
fi, err := os.Stat(stateFile)
if err != nil && !os.IsNotExist(err) {
return nil, fmt.Errorf("Stat file %s error: %s", stateFile, err.Error())
}
state, err := loadStateFile(stateFile)
if err != nil {
return nil, fmt.Errorf("Load state file %s failed: %s", stateFile, err.Error())
}
s = append(s, containerState{
ID: state.ID,
InitProcessPid: state.Pid,
Status: "running",
Bundle: state.BundlePath,
Created: fi.ModTime(),
})
}
}
return s, nil
}
func loadStateFile(stateFile string) (*specs.State, error) {
file, err := os.Open(stateFile)
if err != nil {
return nil, err
}
defer file.Close()
var state specs.State
if err = json.NewDecoder(file).Decode(&state); err != nil {
return nil, fmt.Errorf("Decode state file %s error: %s", stateFile, err.Error())
}
return &state, nil
}
// fatal prints the error's details if it is a runv specific error type
// then exits the program with an exit status of 1.
func fatal(err error) {
// make sure the error is written to the logger
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}