forked from hyperhq/runv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
179 lines (167 loc) · 4.5 KB
/
main.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"fmt"
"io/ioutil"
"log"
"net"
"os"
"time"
"github.com/codegangsta/cli"
"github.com/docker/containerd/api/grpc/types"
"github.com/docker/docker/pkg/reexec"
"github.com/golang/protobuf/ptypes"
"github.com/hyperhq/runv/containerd"
_ "github.com/hyperhq/runv/supervisor/proxy" // for proxy.init()
netcontext "golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
)
const (
version = "0.6.2"
specConfig = "config.json"
stateJson = "state.json"
usage = `Open Container Initiative hypervisor-based runtime
runv is a command line client for running applications packaged according to
the Open Container Format (OCF) and is a compliant implementation of the
Open Container Initiative specification. However, due to the difference
between hypervisors and containers, the following sections of OCF don't
apply to runV:
Namespace
Capability
Device
"linux" and "mount" fields in OCI specs are ignored
The current release of "runV" supports the following hypervisors:
KVM (QEMU 2.0 or later)
Xen (4.5 or later)
VirtualBox (Mac OS X)
After creating a spec for your root filesystem, you can execute a container
in your shell by running:
# cd /mycontainer
# runv start start [ -b bundle ] <container-id>
If not specified, the default value for the 'bundle' is the current directory.
'Bundle' is the directory where '` + specConfig + `' must be located.`
)
func main() {
if reexec.Init() {
return
}
app := cli.NewApp()
app.Name = "runv"
app.Usage = usage
app.Version = version
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug",
Usage: "enable debug output for logging, saved on the dir specified by log_dir via glog style",
},
cli.StringFlag{
Name: "log_dir",
Value: "/var/log/hyper",
Usage: "the directory for the logging (glog style)",
},
cli.StringFlag{
Name: "log",
Usage: "[ignored on runv] set the log file path where internal debug information is written",
},
cli.StringFlag{
Name: "log-format",
Usage: "[ignored on runv] set the format used by logs ('text' (default), or 'json')",
},
cli.StringFlag{
Name: "root",
Value: "/run/runv",
Usage: "root directory for storage of container state (this should be located in tmpfs)",
},
cli.StringFlag{
Name: "driver",
Usage: "hypervisor driver (supports: kvm xen vbox)",
},
cli.IntFlag{
Name: "default_cpus",
Usage: "default number of vcpus to assign pod",
Value: 1,
},
cli.IntFlag{
Name: "default_memory",
Usage: "default memory to assign pod (mb)",
Value: 128,
},
cli.StringFlag{
Name: "kernel",
Usage: "kernel for the container",
},
cli.StringFlag{
Name: "initrd",
Usage: "runv-compatible initrd for the container",
},
cli.StringFlag{
Name: "template",
Usage: "path to the template vm state directory",
},
cli.StringFlag{
Name: "vbox",
Usage: "runv-compatible boot ISO for the container for vbox driver",
},
}
app.Commands = []cli.Command{
startCommand,
specCommand,
execCommand,
killCommand,
listCommand,
stateCommand,
manageCommand,
containerd.ContainerdCommand,
}
if err := app.Run(os.Args); err != nil {
fmt.Printf("%s\n", err.Error())
}
}
func getClient(address string) types.APIClient {
// reset the logger for grpc to log to dev/null so that it does not mess with our stdio
grpclog.SetLogger(log.New(ioutil.Discard, "", log.LstdFlags))
dialOpts := []grpc.DialOption{grpc.WithInsecure(), grpc.WithTimeout(5 * time.Second)}
dialOpts = append(dialOpts,
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("unix", addr, timeout)
},
))
conn, err := grpc.Dial(address, dialOpts...)
if err != nil {
fmt.Printf("grpc.Dial error: %v", err)
os.Exit(-1)
}
return types.NewAPIClient(conn)
}
func containerEvents(c types.APIClient, container string) <-chan *types.Event {
evChan := make(chan *types.Event)
ts := time.Now()
go func() {
for {
tsp, err := ptypes.TimestampProto(ts)
if err != nil {
close(evChan)
return
}
events, err := c.Events(netcontext.Background(), &types.EventsRequest{Timestamp: tsp})
if err != nil {
fmt.Printf("c.Events error: %v", err)
// TODO try to find a way to kill the process ?
close(evChan)
return
}
for {
e, err := events.Recv()
if err != nil {
time.Sleep(1 * time.Second)
break
}
ts, err = ptypes.Timestamp(e.Timestamp)
if e.Id == container {
evChan <- e
}
}
}
}()
return evChan
}