2017-02-01 00:45:59 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-02-06 20:16:36 +00:00
|
|
|
"io"
|
2017-02-01 00:45:59 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
2017-02-06 20:16:36 +00:00
|
|
|
"github.com/urfave/cli"
|
2017-02-01 00:45:59 +00:00
|
|
|
)
|
|
|
|
|
2017-02-06 20:16:36 +00:00
|
|
|
// version will be populated by the Makefile, read from
|
|
|
|
// VERSION file of the source code.
|
|
|
|
var version = ""
|
|
|
|
|
2017-02-01 00:45:59 +00:00
|
|
|
// gitCommit will be the hash that the binary was built from
|
|
|
|
// and will be populated by the Makefile
|
|
|
|
var gitCommit = ""
|
|
|
|
|
|
|
|
const (
|
|
|
|
specConfig = "config.json"
|
|
|
|
usage = `Open Container Initiative runtime
|
2017-02-06 20:16:36 +00:00
|
|
|
|
2017-02-01 00:45:59 +00:00
|
|
|
runc is a command line client for running applications packaged according to
|
2017-02-06 20:16:36 +00:00
|
|
|
the Open Container Initiative (OCI) format and is a compliant implementation of the
|
2017-02-01 00:45:59 +00:00
|
|
|
Open Container Initiative specification.
|
|
|
|
|
|
|
|
runc integrates well with existing process supervisors to provide a production
|
|
|
|
container runtime environment for applications. It can be used with your
|
|
|
|
existing process monitoring tools and the container will be spawned as a
|
|
|
|
direct child of the process supervisor.
|
|
|
|
|
|
|
|
Containers are configured using bundles. A bundle for a container is a directory
|
|
|
|
that includes a specification file named "` + specConfig + `" and a root filesystem.
|
2017-02-06 20:16:36 +00:00
|
|
|
The root filesystem contains the contents of the container.
|
2017-02-01 00:45:59 +00:00
|
|
|
|
|
|
|
To start a new instance of a container:
|
|
|
|
|
2017-02-06 20:16:36 +00:00
|
|
|
# runc run [ -b bundle ] <container-id>
|
2017-02-01 00:45:59 +00:00
|
|
|
|
|
|
|
Where "<container-id>" is your name for the instance of the container that you
|
|
|
|
are starting. The name you provide for the container instance must be unique on
|
|
|
|
your host. Providing the bundle directory using "-b" is optional. The default
|
|
|
|
value for "bundle" is the current directory.`
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := cli.NewApp()
|
|
|
|
app.Name = "runc"
|
|
|
|
app.Usage = usage
|
2017-02-06 20:16:36 +00:00
|
|
|
|
|
|
|
var v []string
|
|
|
|
if version != "" {
|
|
|
|
v = append(v, version)
|
2017-02-01 00:45:59 +00:00
|
|
|
}
|
|
|
|
if gitCommit != "" {
|
|
|
|
v = append(v, fmt.Sprintf("commit: %s", gitCommit))
|
|
|
|
}
|
|
|
|
v = append(v, fmt.Sprintf("spec: %s", 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/runc",
|
|
|
|
Usage: "root directory for storage of container state (this should be located in tmpfs)",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "criu",
|
|
|
|
Value: "criu",
|
|
|
|
Usage: "path to the criu binary used for checkpoint and restore",
|
|
|
|
},
|
|
|
|
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{
|
|
|
|
checkpointCommand,
|
2017-02-06 20:16:36 +00:00
|
|
|
createCommand,
|
2017-02-01 00:45:59 +00:00
|
|
|
deleteCommand,
|
|
|
|
eventsCommand,
|
|
|
|
execCommand,
|
|
|
|
initCommand,
|
|
|
|
killCommand,
|
|
|
|
listCommand,
|
|
|
|
pauseCommand,
|
2017-02-06 20:16:36 +00:00
|
|
|
psCommand,
|
2017-02-01 00:45:59 +00:00
|
|
|
restoreCommand,
|
|
|
|
resumeCommand,
|
2017-02-06 20:16:36 +00:00
|
|
|
runCommand,
|
2017-02-01 00:45:59 +00:00
|
|
|
specCommand,
|
|
|
|
startCommand,
|
|
|
|
stateCommand,
|
2017-02-06 20:16:36 +00:00
|
|
|
updateCommand,
|
2017-02-01 00:45:59 +00:00
|
|
|
}
|
|
|
|
app.Before = func(context *cli.Context) error {
|
|
|
|
if context.GlobalBool("debug") {
|
|
|
|
logrus.SetLevel(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, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.SetOutput(f)
|
|
|
|
}
|
|
|
|
switch context.GlobalString("log-format") {
|
|
|
|
case "text":
|
|
|
|
// retain logrus's default.
|
|
|
|
case "json":
|
|
|
|
logrus.SetFormatter(new(logrus.JSONFormatter))
|
|
|
|
default:
|
2017-02-06 20:16:36 +00:00
|
|
|
return fmt.Errorf("unknown log-format %q", context.GlobalString("log-format"))
|
2017-02-01 00:45:59 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-06 20:16:36 +00:00
|
|
|
// 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}
|
2017-02-01 00:45:59 +00:00
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2017-02-06 20:16:36 +00:00
|
|
|
|
|
|
|
type FatalWriter struct {
|
|
|
|
cliErrWriter io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FatalWriter) Write(p []byte) (n int, err error) {
|
|
|
|
logrus.Error(string(p))
|
|
|
|
return f.cliErrWriter.Write(p)
|
|
|
|
}
|