Remove urfave cli dep from shim

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2017-11-07 10:04:33 -05:00
parent a70b95b202
commit 13c7c3ef10
2 changed files with 78 additions and 97 deletions

View File

@ -4,6 +4,7 @@ package main
import ( import (
"context" "context"
"flag"
"fmt" "fmt"
"net" "net"
"os" "os"
@ -20,12 +21,10 @@ import (
"github.com/containerd/containerd/linux/shim" "github.com/containerd/containerd/linux/shim"
shimapi "github.com/containerd/containerd/linux/shim/v1" shimapi "github.com/containerd/containerd/linux/shim/v1"
"github.com/containerd/containerd/reaper" "github.com/containerd/containerd/reaper"
"github.com/containerd/containerd/version"
"github.com/containerd/typeurl" "github.com/containerd/typeurl"
google_protobuf "github.com/golang/protobuf/ptypes/empty" google_protobuf "github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/urfave/cli"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"google.golang.org/grpc" "google.golang.org/grpc"
) )
@ -40,53 +39,40 @@ const usage = `
shim for container lifecycle and reconnection shim for container lifecycle and reconnection
` `
var (
debugFlag bool
namespaceFlag string
socketFlag string
addressFlag string
workdirFlag string
runtimeRootFlag string
criuFlag string
systemdCgroupFlag bool
)
func init() {
flag.BoolVar(&debugFlag, "debug", false, "enable debug output in logs")
flag.StringVar(&namespaceFlag, "namespace", "", "namespace that owns the shim")
flag.StringVar(&socketFlag, "socket", "", "abstract socket path to serve")
flag.StringVar(&addressFlag, "address", "", "grpc address back to main containerd")
flag.StringVar(&workdirFlag, "workdir", "", "path used to storge large temporary data")
flag.StringVar(&runtimeRootFlag, "runtime-root", shim.RuncRoot, "root directory for the runtime")
flag.StringVar(&criuFlag, "criu", "", "path to criu binary")
flag.BoolVar(&systemdCgroupFlag, "systemd-cgroup", false, "set runtime to use systemd-cgroup")
flag.Parse()
}
func main() { func main() {
app := cli.NewApp() if debugFlag {
app.Name = "containerd-shim"
app.Version = version.Version
app.Usage = usage
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug",
Usage: "enable debug output in logs",
},
cli.StringFlag{
Name: "namespace,n",
Usage: "namespace that owns the task",
},
cli.StringFlag{
Name: "socket,s",
Usage: "abstract socket path to serve on",
},
cli.StringFlag{
Name: "address,a",
Usage: "grpc address back to containerd",
},
cli.StringFlag{
Name: "workdir,w",
Usage: "path used to store large temporary data",
},
cli.StringFlag{
Name: "runtime-root",
Usage: "root directory for the runtime",
Value: shim.RuncRoot,
},
cli.StringFlag{
Name: "criu,c",
Usage: "path to criu",
},
cli.BoolFlag{
Name: "systemd-cgroup",
Usage: "set runtime to use systemd-cgroup",
},
}
app.Before = func(context *cli.Context) error {
if context.GlobalBool("debug") {
logrus.SetLevel(logrus.DebugLevel) logrus.SetLevel(logrus.DebugLevel)
} }
return nil if err := executeShim(); err != nil {
fmt.Fprintf(os.Stderr, "containerd-shim: %s\n", err)
os.Exit(1)
} }
app.Action = func(context *cli.Context) error { }
func executeShim() error {
// start handling signals as soon as possible so that things are properly reaped // start handling signals as soon as possible so that things are properly reaped
// or if runtime exits before we hit the handler // or if runtime exits before we hit the handler
signals, err := setupSignals() signals, err := setupSignals()
@ -98,18 +84,18 @@ func main() {
return err return err
} }
server := newServer() server := newServer()
e, err := connectEvents(context.GlobalString("address")) e, err := connectEvents(addressFlag)
if err != nil { if err != nil {
return err return err
} }
sv, err := shim.NewService( sv, err := shim.NewService(
shim.Config{ shim.Config{
Path: path, Path: path,
Namespace: context.GlobalString("namespace"), Namespace: namespaceFlag,
WorkDir: context.GlobalString("workdir"), WorkDir: workdirFlag,
Criu: context.GlobalString("criu"), Criu: criuFlag,
SystemdCgroup: context.GlobalBool("systemd-cgroup"), SystemdCgroup: systemdCgroupFlag,
RuntimeRoot: context.GlobalString("runtime-root"), RuntimeRoot: runtimeRootFlag,
}, },
&remoteEventsPublisher{client: e}, &remoteEventsPublisher{client: e},
) )
@ -118,21 +104,16 @@ func main() {
} }
logrus.Debug("registering grpc server") logrus.Debug("registering grpc server")
shimapi.RegisterShimServer(server, sv) shimapi.RegisterShimServer(server, sv)
socket := context.GlobalString("socket") socket := socketFlag
if err := serve(server, socket); err != nil { if err := serve(server, socket); err != nil {
return err return err
} }
logger := logrus.WithFields(logrus.Fields{ logger := logrus.WithFields(logrus.Fields{
"pid": os.Getpid(), "pid": os.Getpid(),
"path": path, "path": path,
"namespace": context.GlobalString("namespace"), "namespace": namespaceFlag,
}) })
return handleSignals(logger, signals, server, sv) return handleSignals(logger, signals, server, sv)
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "containerd-shim: %s\n", err)
os.Exit(1)
}
} }
// serve serves the grpc API over a unix socket at the provided path // serve serves the grpc API over a unix socket at the provided path

View File

@ -86,22 +86,22 @@ func WithStart(binary, address, daemonAddress, cgroup string, nonewns, debug boo
func newCommand(binary, daemonAddress string, nonewns, debug bool, config Config, socket *os.File) *exec.Cmd { func newCommand(binary, daemonAddress string, nonewns, debug bool, config Config, socket *os.File) *exec.Cmd {
args := []string{ args := []string{
"--namespace", config.Namespace, "-namespace", config.Namespace,
"--workdir", config.WorkDir, "-workdir", config.WorkDir,
"--address", daemonAddress, "-address", daemonAddress,
} }
if config.Criu != "" { if config.Criu != "" {
args = append(args, "--criu-path", config.Criu) args = append(args, "-criu-path", config.Criu)
} }
if config.RuntimeRoot != "" { if config.RuntimeRoot != "" {
args = append(args, "--runtime-root", config.RuntimeRoot) args = append(args, "-runtime-root", config.RuntimeRoot)
} }
if config.SystemdCgroup { if config.SystemdCgroup {
args = append(args, "--systemd-cgroup") args = append(args, "-systemd-cgroup")
} }
if debug { if debug {
args = append(args, "--debug") args = append(args, "-debug")
} }
cmd := exec.Command(binary, args...) cmd := exec.Command(binary, args...)