command

package
v0.0.0-...-ae7f4bf Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CommitCommand = cli.Command{
	Name:  "commit",
	Usage: "Commit container to image",
	Action: func(ctx *cli.Context) error {
		if len(ctx.Args()) < 1 {
			return fmt.Errorf("mssing image name")
		}
		containerId := ctx.Args().Get(0)
		imageName := ctx.Args().Get(1)
		return commitContainer(containerId, imageName)
	},
}
View Source
var ExecCommand = cli.Command{
	Name:  "exec",
	Usage: "exec a command into container, mydocker exec [containerId] [command]",
	Action: func(ctx *cli.Context) error {
		if os.Getenv(container.EnvExecPid) != "" {
			logrus.Infof("pid callback, [pid = %d]", os.Getpid())
			return nil
		}

		if len(ctx.Args()) < 2 {
			return fmt.Errorf("missing containerId or command")
		}
		containerId := ctx.Args().Get(0)
		var cmdArray []string
		cmdArray = append(cmdArray, ctx.Args().Tail()...)
		return execContainer(containerId, cmdArray)
	},
}
View Source
var InitCommand = cli.Command{
	Name:  "init",
	Usage: "Init container process run user's process in container. Do not call it outside",

	Action: func(ctx *cli.Context) error {
		cmd := ctx.Args().Get(0)
		logrus.Infof("[InitCommand] init command %s", cmd)
		return container.RunContainerInitProcess()
	},
}

InitCommand 内部方法,没有暴露给外部使用

View Source
var ListCommand = cli.Command{
	Name:  "ps",
	Usage: "list all of the containers",
	Action: func(ctx *cli.Context) error {
		listContainers()
		return nil
	},
}
View Source
var LogCommand = cli.Command{
	Name:  "logs",
	Usage: "print logs of a container",
	Action: func(ctx *cli.Context) error {
		if len(ctx.Args()) < 1 {
			return fmt.Errorf("please input your container id")
		}
		containerId := ctx.Args().Get(0)
		return logContainer(containerId)
	},
}
View Source
var NetworkCommand = cli.Command{
	Name:  "network",
	Usage: "container network commands",
	Subcommands: []cli.Command{
		{
			Name:  "create",
			Usage: "create a container network",
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "driver",
					Usage: "network driver",
				},
				cli.StringFlag{
					Name:  "subnet",
					Usage: "subnet cidr",
				},
			},
			Action: func(context *cli.Context) error {
				if len(context.Args()) < 1 {
					return fmt.Errorf("missing network name")
				}
				err := network.Init()
				if err != nil {
					return fmt.Errorf("init network error: %+v", err)
				}
				err = network.CreateNetwork(context.String("driver"), context.String("subnet"), context.Args()[0])
				if err != nil {
					return fmt.Errorf("create network error: %+v", err)
				}
				return nil
			},
		},
		{
			Name:  "list",
			Usage: "list container network",
			Action: func(context *cli.Context) error {
				err := network.Init()
				if err != nil {
					return err
				}
				network.ListNetwork()
				return nil
			},
		},
		{
			Name:  "remove",
			Usage: "remove container network",
			Action: func(context *cli.Context) error {
				if len(context.Args()) < 1 {
					return fmt.Errorf("missing network name")
				}
				network.Init()
				err := network.DeleteNetwork(context.Args()[0])
				if err != nil {
					return fmt.Errorf("remove network error: %+v", err)
				}
				return nil
			},
		},
	},
}
View Source
var RemoveCommand = cli.Command{
	Name:  "rm",
	Usage: "remove a stopped container",
	Action: func(ctx *cli.Context) error {
		if len(ctx.Args()) < 1 {
			return fmt.Errorf("missing container id")
		}
		containerId := ctx.Args().Get(0)
		return removeContainer(containerId)
	},
}
View Source
var RunCommand = cli.Command{
	Name: "run",
	Usage: `Create a container with namespace and cgroups limit
			mydocker run -it [command]`,

	Flags: []cli.Flag{
		cli.BoolFlag{
			Name:  "it",
			Usage: "enable tty",
		},
		cli.BoolFlag{
			Name:  "d",
			Usage: "detach container",
		},
		cli.StringFlag{

			Name:  "mem",
			Usage: "memory limit, e.g.: -mem 100m",
		},
		cli.StringFlag{

			Name:  "cpushare",
			Usage: "cpu quota, e.g.: -cpushare 100",
		},
		cli.StringFlag{

			Name:  "cpuset",
			Usage: "cpuset limit, e.g.: -cpuset 2,4",
		},
		cli.StringFlag{

			Name:  "v",
			Usage: "volume, e.g.: -v /etc/conf:/etc/conf",
		},
		cli.StringFlag{
			Name:  "name",
			Usage: "container name",
		},
		cli.StringSliceFlag{
			Name:  "e",
			Usage: "set environment",
		},
	},

	Action: func(ctx *cli.Context) error {
		if len(ctx.Args()) < 1 {
			return fmt.Errorf("missing container command")
		}

		imageName := ctx.Args().First()
		var cmdArray []string
		cmdArray = append(cmdArray, ctx.Args().Tail()...)
		tty := ctx.Bool("it")
		detach := ctx.Bool("d")

		if tty && detach {
			return fmt.Errorf("it and d paramter can not both provided")
		}

		resConf := &subsystems.ResourceConfig{
			MemoryLimit: ctx.String("mem"),
			CpuShare:    ctx.String("cpushare"),
			CpuSet:      ctx.String("cpuset"),
		}
		logrus.Infof("run cmd = %s", strings.Join(cmdArray, " "))
		volume := ctx.String("v")
		containerName := ctx.String("name")
		environSlice := ctx.StringSlice("e")
		run(tty, cmdArray, resConf, volume, containerName, imageName, environSlice)
		return nil
	},
}
View Source
var StopCommand = cli.Command{
	Name:  "stop",
	Usage: "stop a container",
	Action: func(ctx *cli.Context) error {
		if len(ctx.Args()) < 1 {
			return fmt.Errorf("missing container id")
		}
		containerId := ctx.Args().Get(0)
		return stopContainer(containerId)
	},
}

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