forked from clearcontainers/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
208 lines (173 loc) · 5.01 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright (c) 2014,2015,2016 Docker, Inc.
// Copyright (c) 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"io"
"os"
"strings"
"github.com/Sirupsen/logrus"
vc "github.com/containers/virtcontainers"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/urfave/cli"
)
// name holds the name of this program
const name = "cc-runtime"
// version is the runtime version. It is be specified at compilation time (see
// Makefile).
var version = ""
// commit is the git commit the runtime is compiled from. It is specified at
// compilation time (see Makefile)
var commit = ""
// specConfig is the name of the file holding the containers configuration
const specConfig = "config.json"
const usage = `Clear Containers runtime
cc-runtime is a command line program for running applications packaged
according to the Open Container Initiative (OCI).`
var defaultRootDirectory = "/run/clear-containers"
var ccLog = logrus.New()
func main() {
app := cli.NewApp()
app.Name = name
app.Usage = usage
v := make([]string, 0, 3)
if version != "" {
v = append(v, name+" : "+version)
}
if commit != "" {
v = append(v, " commit : "+commit)
}
v = append(v, " OCI specs: "+specs.Version)
app.Version = strings.Join(v, "\n")
// Override the default function to display version details to
// ensure the "--version" option and "version" command are identical.
cli.VersionPrinter = func(c *cli.Context) {
fmt.Println(c.App.Version)
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "cc-config",
Usage: "clear containers config file path",
},
cli.BoolFlag{
Name: "debug",
Usage: "enable debug output for logging",
},
cli.StringFlag{
Name: "log",
Value: "/dev/null",
Usage: "set the log file path where internal debug information is written",
},
cli.StringFlag{
Name: "log-format",
Value: "text",
Usage: "set the format used by logs ('text' (default), or 'json')",
},
cli.StringFlag{
Name: "root",
Value: defaultRootDirectory,
Usage: "root directory for storage of container state (this should be located in tmpfs)",
},
}
app.Commands = []cli.Command{
createCommand,
deleteCommand,
execCommand,
killCommand,
listCommand,
runCommand,
startCommand,
stateCommand,
versionCommand,
}
app.Before = func(context *cli.Context) error {
if userWantsUsage(context) {
// No setup required if the user just
// wants to see the usage statement.
return nil
}
if context.GlobalBool("debug") {
ccLog.Level = logrus.DebugLevel
}
if path := context.GlobalString("log"); path != "" {
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND|os.O_SYNC, 0640)
if err != nil {
return err
}
ccLog.Out = f
}
switch context.GlobalString("log-format") {
case "text":
// retain logrus's default.
case "json":
ccLog.Formatter = new(logrus.JSONFormatter)
default:
return fmt.Errorf("unknown log-format %q", context.GlobalString("log-format"))
}
// Set virtcontainers logger.
vc.SetLog(ccLog)
runtimeConfig, err := loadConfiguration(context.GlobalString("cc-config"))
if err != nil {
return err
}
ccLog.Infof("%v (version %v, commit %v) called as: %v", name, version, commit, context.Args())
// make the data accessible to the sub-commands.
context.App.Metadata = map[string]interface{}{
"runtimeConfig": runtimeConfig,
}
return nil
}
// If the command returns an error, cli takes upon itself to print
// the error on cli.ErrWriter and exit.
// Use our own writer here to ensure the log gets sent to the right
// location.
cli.ErrWriter = &fatalWriter{cli.ErrWriter}
if err := app.Run(os.Args); err != nil {
fatal(err)
}
}
// userWantsUsage determines if the user only wishes to see the usage
// statement.
func userWantsUsage(context *cli.Context) bool {
if context.NArg() == 0 {
return true
}
if context.NArg() == 1 && (context.Args()[0] == "help" || context.Args()[0] == "version") {
return true
}
if context.NArg() >= 2 && (context.Args()[1] == "-h" || context.Args()[1] == "--help") {
return true
}
return false
}
// fatal prints the error's details exits the program.
func fatal(err error) {
ccLog.Error(err)
fmt.Fprintln(os.Stderr, err)
exit(1)
}
type fatalWriter struct {
cliErrWriter io.Writer
}
func (f *fatalWriter) Write(p []byte) (n int, err error) {
ccLog.Error(string(p))
return f.cliErrWriter.Write(p)
}
func fileExists(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}