forked from clearcontainers/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
160 lines (135 loc) · 3.94 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
// 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 ccLog = logrus.New()
func main() {
app := cli.NewApp()
app.Name = name
app.Usage = usage
v := make([]string, 0, 3)
if version != "" {
v = append(v, "runtime : "+version)
}
if commit != "" {
v = append(v, " commit : "+commit)
}
v = append(v, " OCI specs: "+specs.Version)
app.Version = strings.Join(v, "\n")
app.Flags = []cli.Flag{
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: "/run/clearcontainers",
Usage: "root directory for storage of container state (this should be located in tmpfs)",
},
}
app.Commands = []cli.Command{
createCommand,
deleteCommand,
execCommand,
killCommand,
startCommand,
stateCommand,
}
app.Before = func(context *cli.Context) error {
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("")
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)
}
}
// fatal prints the error's details exits the program.
func fatal(err error) {
ccLog.Error(err)
fmt.Fprintln(os.Stderr, err)
os.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)
}