command

package
v0.0.0-...-28e6f37 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2019 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CreateCommand = cli.Command{
	Name:  "create",
	Usage: "create a container",
	Flags: []cli.Flag{
		cli.StringFlag{
			Name:  "bundle, b",
			Value: "",
			Usage: `path to the root of the bundle directory, defaults to the current directory`,
		},
		cli.StringFlag{
			Name:  "network, net",
			Usage: `network connected by container`,
		},
		cli.StringSliceFlag{
			Name:  "port, p",
			Usage: `port mappings, example: host port:container port`,
		},
	},
	Action: func(ctx *cli.Context) error {
		if err := util.CheckArgs(ctx, 1, util.ExactArgs); err != nil {
			return err
		}

		spec, err := facade.LoadSpec(ctx.String("bundle"))
		if err != nil {
			return err
		}
		if err := facade.CreateOrRunContainer(ctx.GlobalString("root"), ctx.Args().First(), ctx.String("bundle"), spec, facade.ContainerActCreate, false, ctx.String("network"), ctx.StringSlice("port")); err != nil {
			return err
		}
		return nil
	},
}
View Source
var DeleteCommand = cli.Command{
	Name:  "delete",
	Usage: "delete a container",
	Flags: []cli.Flag{
		cli.BoolFlag{
			Name:  "force, f",
			Usage: "Force delete the container even it is still running (uses SIGKILL)",
		},
	},
	Action: func(ctx *cli.Context) error {
		if err := util.CheckArgs(ctx, 1, util.ExactArgs); err != nil {
			return err
		}
		container, err := facade.GetContainer(ctx.GlobalString("root"), ctx.Args().First())
		if err != nil {
			return err
		}
		if ctx.Bool("force") {
			_ = container.Signal(unix.SIGKILL)
			for i := 0; i < 100; i++ {
				time.Sleep(100 * time.Millisecond)
				if err := container.Signal(syscall.Signal(0)); err != nil {

					return container.Destroy()
				}
			}
			return fmt.Errorf("waiting container dead timed out")
		} else {
			return container.Destroy()
		}
	},
}
View Source
var ExecCommand = cli.Command{
	Name:  "exec",
	Usage: "exec command in a container",
	Flags: []cli.Flag{
		cli.BoolFlag{
			Name:  "detach, d",
			Usage: "detach from the container's process",
		},
		cli.StringSliceFlag{
			Name:  "env, e",
			Usage: "set environment variables",
		},
		cli.StringFlag{
			Name:  "cwd",
			Usage: "current work directory of exec process",
		},
	},
	Action: func(ctx *cli.Context) error {
		if err := util.CheckArgs(ctx, 2, util.MinArgs); err != nil {
			return err
		}
		args := ctx.Args()[1:]
		if len(args) == 1 && strings.Contains(args[0], " ") {
			args = strings.Split(args[0], " ")
		}
		execId, err := facade.ExecContainer(
			ctx.GlobalString("root"),
			ctx.Args().First(),
			ctx.Bool("detach"),
			args,
			ctx.String("cwd"),
			ctx.StringSlice("env"))
		if err != nil {
			return err
		}
		if ctx.Bool("detach") {
			fmt.Printf("exec id is %s\n", execId)
		}
		return nil
	},
}
View Source
var ImagesCommand = cli.Command{
	Name:  "image",
	Usage: "image management",
	Subcommands: []cli.Command{
		imageCreateCommand,
		imageDeleteCommand,
		imageListCommand,
		imageGetCommand,
		imageRunContainerCommand,
		imageDestroyContainerCommand,
	},
}
View Source
var InitCommand = cli.Command{
	Name:  "init",
	Usage: "init a container(execute init/exec process)",
	Action: func(ctx *cli.Context) error {
		factory, err := libcapsule.NewFactory(ctx.GlobalString("root"), false)
		if err != nil {
			return err
		}
		if err := factory.StartInitialization(); err != nil {
			logrus.WithField("init", true).Errorf("init failed, err: %s", err.Error())
		}
		return nil
	},
}

仅限内部调用,有可能是init process,也有可能是exec process。

View Source
var KillCommand = cli.Command{
	Name:  "kill",
	Usage: "kill a container",
	Action: func(ctx *cli.Context) error {
		if err := util.CheckArgs(ctx, 1, util.MinArgs); err != nil {
			return err
		}
		if err := util.CheckArgs(ctx, 2, util.MaxArgs); err != nil {
			return err
		}
		container, err := facade.GetContainer(ctx.GlobalString("root"), ctx.Args().First())
		if err != nil {
			return err
		}

		sigStr := ctx.Args().Get(1)
		if sigStr == "" {
			sigStr = "SIGTERM"
		}
		signal, err := parseSignal(sigStr)
		if err != nil {
			return err
		}
		return container.Signal(signal)
	},
}

如果杀死容器的init process,那么容器的exec process也会被杀死 暂不清楚原因。

View Source
var ListCommand = cli.Command{
	Name:  "list",
	Usage: "list all containers",
	Action: func(ctx *cli.Context) error {
		ids, err := facade.GetContainerIds(ctx.GlobalString("root"))
		if err != nil {
			return err
		}
		vos, err := facade.GetContainerStateVOs(ctx.GlobalString("root"), ids)
		if err != nil {
			return err
		}
		w := tabwriter.NewWriter(os.Stdout, 12, 1, 3, ' ', 0)
		fmt.Fprint(w, "ID\tPID\tSTATUS\tIP\tBUNDLE\tCREATED\n")
		for _, item := range vos {
			fmt.Fprintf(w, "%s\t%d\t%s\t%s\t%s\t%s\n",
				item.ID,
				item.InitProcessPid,
				item.Status,
				item.IP,
				item.Bundle,
				item.Created.Format(time.RFC3339Nano))
		}
		if err := w.Flush(); err != nil {
			return err
		}
		return nil
	},
}
View Source
var LogCommand = cli.Command{
	Name:  "log",
	Usage: "get a container's log",
	Flags: []cli.Flag{
		cli.StringFlag{
			Name:  "exec",
			Usage: "get a container's exec log",
		},
	},
	Action: func(ctx *cli.Context) error {
		if err := util.CheckArgs(ctx, 1, util.ExactArgs); err != nil {
			return err
		}
		containerId := ctx.Args().First()
		_, err := facade.GetContainer(ctx.GlobalString("root"), containerId)
		if err != nil {
			return err
		}
		var logFilename string
		logrus.Infof("exec param: %s", ctx.String("exec"))
		if ctx.String("exec") != "" {

			logFilename = path.Join(ctx.GlobalString("root"), constant.ContainerDir, containerId, fmt.Sprintf(constant.ContainerExecLogFilenamePattern, ctx.String("exec")))
		} else {

			logFilename = path.Join(ctx.GlobalString("root"), constant.ContainerDir, containerId, constant.ContainerInitLogFilename)
		}
		file, err := os.Open(logFilename)
		if err != nil {
			return err
		}
		bytes, err := ioutil.ReadAll(file)
		if err != nil {
			return err
		}
		fmt.Print(string(bytes))
		return nil
	},
}
View Source
var NetworkCommand = cli.Command{
	Name:  "network",
	Usage: "container network commands",
	Before: func(ctx *cli.Context) error {
		logrus.Infof("init network drivers...")
		if err := network.InitNetworkDrivers(ctx.GlobalString("root")); err != nil {
			return err
		}
		return nil
	},
	Subcommands: []cli.Command{
		networkCreateCommand,
		networkDeleteCommand,
		networkListCommand,
		networkShowCommand,
	},
}
View Source
var PsCommand = cli.Command{
	Name:  "ps",
	Usage: "show a container's process info",
	Action: func(ctx *cli.Context) error {
		if err := util.CheckArgs(ctx, 1, util.ExactArgs); err != nil {
			return err
		}
		if _, err := facade.ExecContainer(
			ctx.GlobalString("root"),
			ctx.Args().First(),
			false,
			[]string{"ps", "-ef"},
			"",
			nil); err != nil {
			return err
		}
		return nil
	},
}

相当于exec(ps -ef)

View Source
var RunCommand = cli.Command{
	Name:  "run",
	Usage: "create and start a container",
	Flags: []cli.Flag{
		cli.BoolFlag{
			Name:  "detach, d",
			Usage: "detach from the container's process",
		},
		cli.StringFlag{
			Name:  "bundle, b",
			Value: "",
			Usage: `path to the root of the bundle directory, defaults to the current directory`,
		},
		cli.StringFlag{
			Name:  "network, net",
			Usage: `network connected by container`,
		},
		cli.StringSliceFlag{
			Name:  "port, p",
			Usage: `port mapping, host port:container port`,
		},
	},
	Action: func(ctx *cli.Context) error {
		if err := util.CheckArgs(ctx, 1, util.ExactArgs); err != nil {
			return err
		}

		spec, err := facade.LoadSpec(ctx.String("bundle"))
		if err != nil {
			return err
		}
		if err := facade.CreateOrRunContainer(ctx.GlobalString("root"), ctx.Args().First(), ctx.String("bundle"), spec, facade.ContainerActRun, ctx.Bool("detach"), ctx.String("network"), ctx.StringSlice("port")); err != nil {
			return err
		}
		return nil
	},
}

如果detach=true,则表示将容器的stdio、stdout、stderr设置为os.stdio...等,且等待容器进程结束 如果detach=false,则什么都不做。 并且capsule start时,detach总是为false。

View Source
var SpecCommand = cli.Command{
	Name:  "spec",
	Usage: "create a new specification file",
	Action: func(ctx *cli.Context) error {
		if err := util.CheckArgs(ctx, 0, util.ExactArgs); err != nil {
			return err
		}
		exampleSpec := spec.Example()
		if err := util.CheckNoFile(constant.ContainerConfigFilename); err != nil {
			return err
		}
		data, err := json.MarshalIndent(exampleSpec, "", "\t")
		if err != nil {
			return err
		}
		return ioutil.WriteFile(constant.ContainerConfigFilename, data, 0666)
	},
}
View Source
var StartCommand = cli.Command{
	Name:  "start",
	Usage: "start a container",
	Action: func(ctx *cli.Context) error {
		if err := util.CheckArgs(ctx, 1, util.ExactArgs); err != nil {
			return err
		}
		container, err := facade.GetContainer(ctx.GlobalString("root"), ctx.Args().First())
		if err != nil {
			return err
		}
		status, err := container.Status()
		if err != nil {
			return err
		}
		switch status {
		case libcapsule.Created:
			return container.Start()
		case libcapsule.Stopped:
			return errors.New("cannot start a container that has stopped")
		case libcapsule.Running:
			return errors.New("cannot start an already running container")
		default:
			return fmt.Errorf("cannot start a container in the %s state\n", status)
		}
	},
}
View Source
var StateCommand = cli.Command{
	Name:  "state",
	Usage: "get a container's state",
	Flags: []cli.Flag{
		cli.BoolFlag{
			Name:  "detail, d",
			Usage: "get container's detailed state",
		},
	},
	Action: func(ctx *cli.Context) error {
		if err := util.CheckArgs(ctx, 1, util.ExactArgs); err != nil {
			return err
		}
		vo, err := facade.GetContainerStateVO(ctx.GlobalString("root"), ctx.Args().First())
		if err != nil {
			return err
		}
		if !ctx.Bool("detail") {
			vo.Detail = nil
		}
		data, err := json.MarshalIndent(vo, "", "  ")
		if err != nil {
			return err
		}
		fmt.Println(string(data))
		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL