From ea6a10c4125de5fdd7482ef4eb54683db8d770a1 Mon Sep 17 00:00:00 2001 From: Lantao Liu Date: Wed, 28 Feb 2018 19:47:20 +0000 Subject: [PATCH] Add cri subcommand and add ctr/command package Signed-off-by: Lantao Liu --- cmd/containerd/builtins_cri_linux.go | 2 +- cmd/ctr/builtins_cri_linux.go | 25 ++++++ cmd/ctr/command/main.go | 113 +++++++++++++++++++++++++++ cmd/ctr/{ => command}/main_unix.go | 2 +- cmd/ctr/main.go | 88 +-------------------- 5 files changed, 144 insertions(+), 86 deletions(-) create mode 100644 cmd/ctr/builtins_cri_linux.go create mode 100644 cmd/ctr/command/main.go rename cmd/ctr/{ => command}/main_unix.go (97%) diff --git a/cmd/containerd/builtins_cri_linux.go b/cmd/containerd/builtins_cri_linux.go index c57b38617..2dbf81253 100644 --- a/cmd/containerd/builtins_cri_linux.go +++ b/cmd/containerd/builtins_cri_linux.go @@ -18,4 +18,4 @@ package main -import _ "github.com/containerd/cri-containerd" +import _ "github.com/containerd/cri" diff --git a/cmd/ctr/builtins_cri_linux.go b/cmd/ctr/builtins_cri_linux.go new file mode 100644 index 000000000..384dcefb9 --- /dev/null +++ b/cmd/ctr/builtins_cri_linux.go @@ -0,0 +1,25 @@ +// +build !no_cri + +/* + Copyright The containerd Authors. + + 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 "github.com/containerd/cri/cli" + +func init() { + pluginCmds = append(pluginCmds, cli.Command) +} diff --git a/cmd/ctr/command/main.go b/cmd/ctr/command/main.go new file mode 100644 index 000000000..8fd1a5a51 --- /dev/null +++ b/cmd/ctr/command/main.go @@ -0,0 +1,113 @@ +/* + Copyright The containerd Authors. + + 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 command + +import ( + "fmt" + "io/ioutil" + "log" + + "github.com/containerd/containerd/cmd/ctr/commands/containers" + "github.com/containerd/containerd/cmd/ctr/commands/content" + "github.com/containerd/containerd/cmd/ctr/commands/events" + "github.com/containerd/containerd/cmd/ctr/commands/images" + namespacesCmd "github.com/containerd/containerd/cmd/ctr/commands/namespaces" + "github.com/containerd/containerd/cmd/ctr/commands/plugins" + "github.com/containerd/containerd/cmd/ctr/commands/pprof" + "github.com/containerd/containerd/cmd/ctr/commands/run" + "github.com/containerd/containerd/cmd/ctr/commands/snapshots" + "github.com/containerd/containerd/cmd/ctr/commands/tasks" + versionCmd "github.com/containerd/containerd/cmd/ctr/commands/version" + "github.com/containerd/containerd/defaults" + "github.com/containerd/containerd/namespaces" + "github.com/containerd/containerd/version" + "github.com/sirupsen/logrus" + "github.com/urfave/cli" + "google.golang.org/grpc/grpclog" +) + +var extraCmds = []cli.Command{} + +func init() { + // Discard grpc logs so that they don't mess with our stdio + grpclog.SetLogger(log.New(ioutil.Discard, "", log.LstdFlags)) + + cli.VersionPrinter = func(c *cli.Context) { + fmt.Println(c.App.Name, version.Package, c.App.Version) + } +} + +// App returns a *cli.App instance. +func App() *cli.App { + app := cli.NewApp() + app.Name = "ctr" + app.Version = version.Version + app.Usage = ` + __ + _____/ /______ + / ___/ __/ ___/ +/ /__/ /_/ / +\___/\__/_/ + +containerd CLI +` + app.Flags = []cli.Flag{ + cli.BoolFlag{ + Name: "debug", + Usage: "enable debug output in logs", + }, + cli.StringFlag{ + Name: "address, a", + Usage: "address for containerd's GRPC server", + Value: defaults.DefaultAddress, + }, + cli.DurationFlag{ + Name: "timeout", + Usage: "total timeout for ctr commands", + }, + cli.DurationFlag{ + Name: "connect-timeout", + Usage: "timeout for connecting to containerd", + }, + cli.StringFlag{ + Name: "namespace, n", + Usage: "namespace to use with commands", + Value: namespaces.Default, + EnvVar: namespaces.NamespaceEnvVar, + }, + } + app.Commands = append([]cli.Command{ + plugins.Command, + versionCmd.Command, + containers.Command, + content.Command, + events.Command, + images.Command, + namespacesCmd.Command, + pprof.Command, + run.Command, + snapshots.Command, + tasks.Command, + }, extraCmds...) + app.Before = func(context *cli.Context) error { + if context.GlobalBool("debug") { + logrus.SetLevel(logrus.DebugLevel) + } + return nil + } + return app +} diff --git a/cmd/ctr/main_unix.go b/cmd/ctr/command/main_unix.go similarity index 97% rename from cmd/ctr/main_unix.go rename to cmd/ctr/command/main_unix.go index 4ea9cfd18..80f2f41dc 100644 --- a/cmd/ctr/main_unix.go +++ b/cmd/ctr/command/main_unix.go @@ -16,7 +16,7 @@ limitations under the License. */ -package main +package command import "github.com/containerd/containerd/cmd/ctr/commands/shim" diff --git a/cmd/ctr/main.go b/cmd/ctr/main.go index ec41c59a2..dd9a1fc43 100644 --- a/cmd/ctr/main.go +++ b/cmd/ctr/main.go @@ -18,97 +18,17 @@ package main import ( "fmt" - "io/ioutil" - "log" "os" - "github.com/containerd/containerd/cmd/ctr/commands/containers" - "github.com/containerd/containerd/cmd/ctr/commands/content" - "github.com/containerd/containerd/cmd/ctr/commands/events" - "github.com/containerd/containerd/cmd/ctr/commands/images" - namespacesCmd "github.com/containerd/containerd/cmd/ctr/commands/namespaces" - "github.com/containerd/containerd/cmd/ctr/commands/plugins" - "github.com/containerd/containerd/cmd/ctr/commands/pprof" - "github.com/containerd/containerd/cmd/ctr/commands/run" - "github.com/containerd/containerd/cmd/ctr/commands/snapshots" - "github.com/containerd/containerd/cmd/ctr/commands/tasks" - versionCmd "github.com/containerd/containerd/cmd/ctr/commands/version" - "github.com/containerd/containerd/defaults" - "github.com/containerd/containerd/namespaces" - "github.com/containerd/containerd/version" - "github.com/sirupsen/logrus" + "github.com/containerd/containerd/cmd/ctr/command" "github.com/urfave/cli" - "google.golang.org/grpc/grpclog" ) -var extraCmds = []cli.Command{} - -func init() { - // Discard grpc logs so that they don't mess with our stdio - grpclog.SetLogger(log.New(ioutil.Discard, "", log.LstdFlags)) - - cli.VersionPrinter = func(c *cli.Context) { - fmt.Println(c.App.Name, version.Package, c.App.Version) - } -} +var pluginCmds = []cli.Command{} func main() { - app := cli.NewApp() - app.Name = "ctr" - app.Version = version.Version - app.Usage = ` - __ - _____/ /______ - / ___/ __/ ___/ -/ /__/ /_/ / -\___/\__/_/ - -containerd CLI -` - app.Flags = []cli.Flag{ - cli.BoolFlag{ - Name: "debug", - Usage: "enable debug output in logs", - }, - cli.StringFlag{ - Name: "address, a", - Usage: "address for containerd's GRPC server", - Value: defaults.DefaultAddress, - }, - cli.DurationFlag{ - Name: "timeout", - Usage: "total timeout for ctr commands", - }, - cli.DurationFlag{ - Name: "connect-timeout", - Usage: "timeout for connecting to containerd", - }, - cli.StringFlag{ - Name: "namespace, n", - Usage: "namespace to use with commands", - Value: namespaces.Default, - EnvVar: namespaces.NamespaceEnvVar, - }, - } - app.Commands = append([]cli.Command{ - plugins.Command, - versionCmd.Command, - containers.Command, - content.Command, - events.Command, - images.Command, - namespacesCmd.Command, - pprof.Command, - run.Command, - snapshots.Command, - tasks.Command, - }, extraCmds...) - app.Before = func(context *cli.Context) error { - if context.GlobalBool("debug") { - logrus.SetLevel(logrus.DebugLevel) - } - return nil - } + app := command.App() + app.Commands = append(app.Commands, pluginCmds...) if err := app.Run(os.Args); err != nil { fmt.Fprintf(os.Stderr, "ctr: %s\n", err) os.Exit(1)