-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
185 lines (167 loc) · 4.66 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
package main
import (
"fmt"
"io"
"os"
"github.com/opencontainers/runc/libcontainer/logs"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
// Globals to be populated at build time during Makefile processing.
var (
edition string // Sysbox Edition: CE or EE
version string // extracted from VERSION file
commitId string // latest sysbox-runc's git commit-id
builtAt string // build time
builtBy string // build owner
)
const (
specConfig = "config.json"
usage = `sysbox-runc
Nestybox's system container runtime.
Info: https://github.com/nestybox/sysbox
`
)
func main() {
app := cli.NewApp()
app.Name = "sysbox-runc"
app.Usage = usage
app.Version = version
// show-version specialization.
cli.VersionPrinter = func(c *cli.Context) {
fmt.Printf("sysbox-runc\n"+
"\tedition: \t%s\n"+
"\tversion: \t%s\n"+
"\tcommit: \t%s\n"+
"\tbuilt at: \t%s\n"+
"\tbuilt by: \t%s\n"+
"\toci-specs: \t%s\n",
edition, c.App.Version, commitId, builtAt, builtBy, specs.Version)
}
xdgRuntimeDir := ""
root := "/run/sysbox-runc"
if shouldHonorXDGRuntimeDir() {
if runtimeDir := os.Getenv("XDG_RUNTIME_DIR"); runtimeDir != "" {
root = runtimeDir + "/sysbox-runc"
xdgRuntimeDir = root
}
}
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug",
Usage: "enable debug output for logging",
},
cli.StringFlag{
Name: "log",
Value: "",
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: root,
Usage: "root directory for storage of container state (this should be located in tmpfs)",
},
cli.BoolFlag{
Name: "no-sysbox-fs",
Usage: "do not interact with sysbox-fs; meant for testing and debugging.",
},
cli.BoolFlag{
Name: "no-sysbox-mgr",
Usage: "do not interact with sysbox-mgr; meant for testing and debugging.",
},
cli.BoolFlag{
Name: "no-kernel-check",
Usage: "do not check kernel compatibility; meant for testing and debugging.",
},
cli.BoolFlag{
Name: "cpu-profiling",
Usage: "enable cpu-profiling data collection; profile data is stored in the cwd of the process invoking sysbox-runc. Ignore the 'cannot set cpu profile rate' message (it's expected).",
Hidden: true,
},
cli.BoolFlag{
Name: "memory-profiling",
Usage: "enable memory-profiling data collectionprofile data is stored in the cwd of the process invoking sysbox-runc.",
Hidden: true,
},
cli.BoolFlag{
Name: "systemd-cgroup",
Usage: "enable systemd cgroup support, expects cgroupsPath to be of form \"slice:prefix:name\" for e.g. \"system.slice:runc:434234\"",
},
}
app.Commands = []cli.Command{
createCommand,
deleteCommand,
eventsCommand,
execCommand,
initCommand,
killCommand,
listCommand,
pauseCommand,
psCommand,
resumeCommand,
runCommand,
specCommand,
startCommand,
stateCommand,
updateCommand,
}
app.Before = func(context *cli.Context) error {
if !context.IsSet("root") && xdgRuntimeDir != "" {
// According to the XDG specification, we need to set anything in
// XDG_RUNTIME_DIR to have a sticky bit if we don't want it to get
// auto-pruned.
if err := os.MkdirAll(root, 0700); err != nil {
fmt.Fprintln(os.Stderr, "the path in $XDG_RUNTIME_DIR must be writable by the user")
fatal(err)
}
if err := os.Chmod(root, 0700|os.ModeSticky); err != nil {
fmt.Fprintln(os.Stderr, "you should check permission of the path in $XDG_RUNTIME_DIR")
fatal(err)
}
}
if err := reviseRootDir(context); err != nil {
return err
}
return logs.ConfigureLogging(createLogConfig(context))
}
// 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)
}
}
type FatalWriter struct {
cliErrWriter io.Writer
}
func (f *FatalWriter) Write(p []byte) (n int, err error) {
logrus.Error(string(p))
if !logrusToStderr() {
return f.cliErrWriter.Write(p)
}
return len(p), nil
}
func createLogConfig(context *cli.Context) logs.Config {
logFilePath := context.GlobalString("log")
logPipeFd := ""
if logFilePath == "" {
logPipeFd = "2"
}
config := logs.Config{
LogPipeFd: logPipeFd,
LogLevel: logrus.InfoLevel,
LogFilePath: logFilePath,
LogFormat: context.GlobalString("log-format"),
}
if context.GlobalBool("debug") {
config.LogLevel = logrus.DebugLevel
}
return config
}