diff options
Diffstat (limited to 'cmd/podmanV2')
-rw-r--r-- | cmd/podmanV2/common/create.go | 3 | ||||
-rw-r--r-- | cmd/podmanV2/common/default.go | 2 | ||||
-rw-r--r-- | cmd/podmanV2/containers/attach.go | 60 | ||||
-rw-r--r-- | cmd/podmanV2/containers/exec.go | 93 | ||||
-rw-r--r-- | cmd/podmanV2/containers/ps.go | 370 | ||||
-rw-r--r-- | cmd/podmanV2/containers/run.go | 125 | ||||
-rw-r--r-- | cmd/podmanV2/containers/start.go | 87 | ||||
-rw-r--r-- | cmd/podmanV2/main.go | 1 | ||||
-rw-r--r-- | cmd/podmanV2/pods/inspect.go | 64 | ||||
-rw-r--r-- | cmd/podmanV2/pods/ps.go | 4 | ||||
-rw-r--r-- | cmd/podmanV2/system/version.go | 119 |
11 files changed, 913 insertions, 15 deletions
diff --git a/cmd/podmanV2/common/create.go b/cmd/podmanV2/common/create.go index 724ed2f42..e2eb8cbda 100644 --- a/cmd/podmanV2/common/create.go +++ b/cmd/podmanV2/common/create.go @@ -29,7 +29,6 @@ func getDefaultContainerConfig() *config.Config { } func GetCreateFlags(cf *ContainerCLIOpts) *pflag.FlagSet { - //createFlags := c.Flags() createFlags := pflag.FlagSet{} createFlags.StringSliceVar( &cf.Annotation, @@ -138,7 +137,7 @@ func GetCreateFlags(cf *ContainerCLIOpts) *pflag.FlagSet { ) createFlags.StringVar( &cf.DetachKeys, - "detach-keys", getDefaultDetachKeys(), + "detach-keys", GetDefaultDetachKeys(), "Override the key sequence for detaching a container. Format is a single character `[a-Z]` or a comma separated sequence of `ctrl-<value>`, where `<value>` is one of: `a-cf`, `@`, `^`, `[`, `\\`, `]`, `^` or `_`", ) createFlags.StringSliceVar( diff --git a/cmd/podmanV2/common/default.go b/cmd/podmanV2/common/default.go index fea161edf..b71fcb6f0 100644 --- a/cmd/podmanV2/common/default.go +++ b/cmd/podmanV2/common/default.go @@ -116,6 +116,6 @@ func getDefaultPidsDescription() string { return "Tune container pids limit (set 0 for unlimited)" } -func getDefaultDetachKeys() string { +func GetDefaultDetachKeys() string { return defaultContainerConfig.Engine.DetachKeys } diff --git a/cmd/podmanV2/containers/attach.go b/cmd/podmanV2/containers/attach.go new file mode 100644 index 000000000..d62dcff86 --- /dev/null +++ b/cmd/podmanV2/containers/attach.go @@ -0,0 +1,60 @@ +package containers + +import ( + "os" + + "github.com/containers/libpod/cmd/podmanV2/common" + "github.com/containers/libpod/cmd/podmanV2/registry" + "github.com/containers/libpod/pkg/domain/entities" + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +var ( + attachDescription = "The podman attach command allows you to attach to a running container using the container's ID or name, either to view its ongoing output or to control it interactively." + attachCommand = &cobra.Command{ + Use: "attach [flags] CONTAINER", + Short: "Attach to a running container", + Long: attachDescription, + RunE: attach, + Args: func(cmd *cobra.Command, args []string) error { + if len(args) > 1 || (len(args) == 0 && !cmd.Flag("latest").Changed) { + return errors.Errorf("attach requires the name or id of one running container or the latest flag") + } + return nil + }, + PreRunE: preRunE, + Example: `podman attach ctrID + podman attach 1234 + podman attach --no-stdin foobar`, + } +) + +var ( + attachOpts entities.AttachOptions +) + +func init() { + registry.Commands = append(registry.Commands, registry.CliCommand{ + Mode: []entities.EngineMode{entities.ABIMode}, + Command: attachCommand, + }) + flags := attachCommand.Flags() + flags.StringVar(&attachOpts.DetachKeys, "detach-keys", common.GetDefaultDetachKeys(), "Select the key sequence for detaching a container. Format is a single character `[a-Z]` or a comma separated sequence of `ctrl-<value>`, where `<value>` is one of: `a-z`, `@`, `^`, `[`, `\\`, `]`, `^` or `_`") + flags.BoolVar(&attachOpts.NoStdin, "no-stdin", false, "Do not attach STDIN. The default is false") + flags.BoolVar(&attachOpts.SigProxy, "sig-proxy", true, "Proxy received signals to the process") + flags.BoolVarP(&attachOpts.Latest, "latest", "l", false, "Act on the latest container podman is aware of") + if registry.IsRemote() { + _ = flags.MarkHidden("latest") + } +} + +func attach(cmd *cobra.Command, args []string) error { + attachOpts.Stdin = os.Stdin + if attachOpts.NoStdin { + attachOpts.Stdin = nil + } + attachOpts.Stdout = os.Stdout + attachOpts.Stderr = os.Stderr + return registry.ContainerEngine().ContainerAttach(registry.GetContext(), args[0], attachOpts) +} diff --git a/cmd/podmanV2/containers/exec.go b/cmd/podmanV2/containers/exec.go new file mode 100644 index 000000000..4bff57dbb --- /dev/null +++ b/cmd/podmanV2/containers/exec.go @@ -0,0 +1,93 @@ +package containers + +import ( + "bufio" + "os" + + "github.com/containers/libpod/cmd/podmanV2/common" + "github.com/containers/libpod/cmd/podmanV2/registry" + "github.com/containers/libpod/pkg/domain/entities" + envLib "github.com/containers/libpod/pkg/env" + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +var ( + execDescription = `Execute the specified command inside a running container. +` + execCommand = &cobra.Command{ + Use: "exec [flags] CONTAINER [COMMAND [ARG...]]", + Short: "Run a process in a running container", + Long: execDescription, + PreRunE: preRunE, + RunE: exec, + Example: `podman exec -it ctrID ls + podman exec -it -w /tmp myCtr pwd + podman exec --user root ctrID ls`, + } +) + +var ( + envInput, envFile []string + execOpts entities.ExecOptions +) + +func init() { + registry.Commands = append(registry.Commands, registry.CliCommand{ + Mode: []entities.EngineMode{entities.ABIMode}, + Command: execCommand, + }) + flags := execCommand.Flags() + flags.SetInterspersed(false) + flags.StringVar(&execOpts.DetachKeys, "detach-keys", common.GetDefaultDetachKeys(), "Select the key sequence for detaching a container. Format is a single character [a-Z] or ctrl-<value> where <value> is one of: a-z, @, ^, [, , or _") + flags.StringArrayVarP(&envInput, "env", "e", []string{}, "Set environment variables") + flags.StringSliceVar(&envFile, "env-file", []string{}, "Read in a file of environment variables") + flags.BoolVarP(&execOpts.Interactive, "interactive", "i", false, "Keep STDIN open even if not attached") + flags.BoolVarP(&execOpts.Latest, "latest", "l", false, "Act on the latest container podman is aware of") + flags.BoolVar(&execOpts.Privileged, "privileged", false, "Give the process extended Linux capabilities inside the container. The default is false") + flags.BoolVarP(&execOpts.Tty, "tty", "t", false, "Allocate a pseudo-TTY. The default is false") + flags.StringVarP(&execOpts.User, "user", "u", "", "Sets the username or UID used and optionally the groupname or GID for the specified command") + flags.UintVar(&execOpts.PreserveFDs, "preserve-fds", 0, "Pass N additional file descriptors to the container") + flags.StringVarP(&execOpts.WorkDir, "workdir", "w", "", "Working directory inside the container") + if registry.IsRemote() { + _ = flags.MarkHidden("latest") + _ = flags.MarkHidden("preserve-fds") + } + +} +func exec(cmd *cobra.Command, args []string) error { + var nameOrId string + execOpts.Cmd = args + if !execOpts.Latest { + execOpts.Cmd = args[1:] + nameOrId = args[0] + } + // Validate given environment variables + execOpts.Envs = make(map[string]string) + for _, f := range envFile { + fileEnv, err := envLib.ParseFile(f) + if err != nil { + return err + } + execOpts.Envs = envLib.Join(execOpts.Envs, fileEnv) + } + + cliEnv, err := envLib.ParseSlice(envInput) + if err != nil { + return errors.Wrap(err, "error parsing environment variables") + } + + execOpts.Envs = envLib.Join(execOpts.Envs, cliEnv) + execOpts.Streams.OutputStream = os.Stdout + execOpts.Streams.ErrorStream = os.Stderr + if execOpts.Interactive { + execOpts.Streams.InputStream = bufio.NewReader(os.Stdin) + execOpts.Streams.AttachInput = true + } + execOpts.Streams.AttachOutput = true + execOpts.Streams.AttachError = true + + exitCode, err := registry.ContainerEngine().ContainerExec(registry.GetContext(), nameOrId, execOpts) + registry.SetExitCode(exitCode) + return err +} diff --git a/cmd/podmanV2/containers/ps.go b/cmd/podmanV2/containers/ps.go index ce3d66c51..2397eb8c0 100644 --- a/cmd/podmanV2/containers/ps.go +++ b/cmd/podmanV2/containers/ps.go @@ -1,29 +1,379 @@ package containers import ( + "encoding/json" + "fmt" + "os" + "sort" + "strconv" "strings" + "text/tabwriter" + "text/template" + "time" + tm "github.com/buger/goterm" + "github.com/containers/buildah/pkg/formats" + "github.com/containers/libpod/cmd/podman/shared" "github.com/containers/libpod/cmd/podmanV2/registry" + "github.com/containers/libpod/cmd/podmanV2/report" "github.com/containers/libpod/pkg/domain/entities" + "github.com/cri-o/ocicni/pkg/ocicni" + "github.com/docker/go-units" + "github.com/pkg/errors" "github.com/spf13/cobra" ) var ( - // podman _ps_ - psCmd = &cobra.Command{ - Use: "ps", - Args: cobra.NoArgs, - Short: listCmd.Short, - Long: listCmd.Long, - PersistentPreRunE: preRunE, - RunE: containers, - Example: strings.Replace(listCmd.Example, "container list", "ps", -1), + psDescription = "Prints out information about the containers" + psCommand = &cobra.Command{ + Use: "ps", + Args: checkFlags, + Short: "List containers", + Long: psDescription, + RunE: ps, + PreRunE: preRunE, + Example: `podman ps -a + podman ps -a --format "{{.ID}} {{.Image}} {{.Labels}} {{.Mounts}}" + podman ps --size --sort names`, } ) +var ( + listOpts = entities.ContainerListOptions{ + Filters: make(map[string][]string), + } + filters []string + noTrunc bool + defaultHeaders string = "CONTAINER ID\tIMAGE\tCOMMAND\tCREATED\tSTATUS\tPORTS\tNAMES" + +// CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES + +) func init() { registry.Commands = append(registry.Commands, registry.CliCommand{ Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode}, - Command: psCmd, + Command: psCommand, + }) + flags := psCommand.Flags() + flags.BoolVarP(&listOpts.All, "all", "a", false, "Show all the containers, default is only running containers") + flags.StringSliceVarP(&filters, "filter", "f", []string{}, "Filter output based on conditions given") + flags.StringVar(&listOpts.Format, "format", "", "Pretty-print containers to JSON or using a Go template") + flags.IntVarP(&listOpts.Last, "last", "n", -1, "Print the n last created containers (all states)") + flags.BoolVarP(&listOpts.Latest, "latest", "l", false, "Show the latest container created (all states)") + flags.BoolVar(&listOpts.Namespace, "namespace", false, "Display namespace information") + flags.BoolVar(&listOpts.Namespace, "ns", false, "Display namespace information") + flags.BoolVar(&noTrunc, "no-trunc", false, "Display the extended information") + flags.BoolVarP(&listOpts.Pod, "pod", "p", false, "Print the ID and name of the pod the containers are associated with") + flags.BoolVarP(&listOpts.Quiet, "quiet", "q", false, "Print the numeric IDs of the containers only") + flags.BoolVarP(&listOpts.Size, "size", "s", false, "Display the total file sizes") + flags.StringVar(&listOpts.Sort, "sort", "created", "Sort output by command, created, id, image, names, runningfor, size, or status") + flags.BoolVar(&listOpts.Sync, "sync", false, "Sync container state with OCI runtime") + flags.UintVarP(&listOpts.Watch, "watch", "w", 0, "Watch the ps output on an interval in seconds") + if registry.IsRemote() { + _ = flags.MarkHidden("latest") + } +} +func checkFlags(c *cobra.Command, args []string) error { + // latest, and last are mutually exclusive. + if listOpts.Last >= 0 && listOpts.Latest { + return errors.Errorf("last and latest are mutually exclusive") + } + // Filter on status forces all + for _, filter := range filters { + splitFilter := strings.SplitN(filter, "=", 2) + if strings.ToLower(splitFilter[0]) == "status" { + listOpts.All = true + break + } + } + // Quiet conflicts with size and namespace and is overridden by a Go + // template. + if listOpts.Quiet { + if listOpts.Size || listOpts.Namespace { + return errors.Errorf("quiet conflicts with size and namespace") + } + if c.Flag("format").Changed && listOpts.Format != formats.JSONString { + // Quiet is overridden by Go template output. + listOpts.Quiet = false + } + } + // Size and namespace conflict with each other + if listOpts.Size && listOpts.Namespace { + return errors.Errorf("size and namespace options conflict") + } + + if listOpts.Watch > 0 && listOpts.Latest { + return errors.New("the watch and latest flags cannot be used together") + } + return nil +} + +func jsonOut(responses []entities.ListContainer) error { + b, err := json.MarshalIndent(responses, "", " ") + if err != nil { + return err + } + fmt.Println(string(b)) + return nil +} + +func quietOut(responses []entities.ListContainer) error { + for _, r := range responses { + id := r.ID + if !noTrunc { + id = id[0:12] + } + fmt.Println(id) + } + return nil +} + +func getResponses() ([]entities.ListContainer, error) { + responses, err := registry.ContainerEngine().ContainerList(registry.GetContext(), listOpts) + if err != nil { + return nil, err + } + if len(listOpts.Sort) > 0 { + responses, err = entities.SortPsOutput(listOpts.Sort, responses) + if err != nil { + return nil, err + } + } + return responses, nil +} + +func ps(cmd *cobra.Command, args []string) error { + // []string to map[string][]string + for _, f := range filters { + split := strings.SplitN(f, "=", 2) + if len(split) == 1 { + return errors.Errorf("invalid filter %q", f) + } + listOpts.Filters[split[0]] = append(listOpts.Filters[split[0]], split[1]) + } + responses, err := getResponses() + if err != nil { + return err + } + if len(listOpts.Sort) > 0 { + responses, err = entities.SortPsOutput(listOpts.Sort, responses) + if err != nil { + return err + } + } + if listOpts.Format == "json" { + return jsonOut(responses) + } + if listOpts.Quiet { + return quietOut(responses) + } + headers, row := createPsOut() + if cmd.Flag("format").Changed { + row = listOpts.Format + if !strings.HasPrefix(row, "\n") { + row += "\n" + } + } + format := "{{range . }}" + row + "{{end}}" + if !listOpts.Quiet && !cmd.Flag("format").Changed { + format = headers + format + } + funcs := report.AppendFuncMap(psFuncMap) + tmpl, err := template.New("listPods").Funcs(funcs).Parse(format) + if err != nil { + return err + } + w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0) + if listOpts.Watch > 0 { + for { + tm.Clear() + tm.MoveCursor(1, 1) + tm.Flush() + responses, err := getResponses() + if err != nil { + return err + } + if err := tmpl.Execute(w, responses); err != nil { + return nil + } + if err := w.Flush(); err != nil { + return err + } + time.Sleep(time.Duration(listOpts.Watch) * time.Second) + tm.Clear() + tm.MoveCursor(1, 1) + tm.Flush() + } + } else if listOpts.Watch < 1 { + if err := tmpl.Execute(w, responses); err != nil { + return err + } + return w.Flush() + } + return nil +} + +func createPsOut() (string, string) { + var row string + if listOpts.Namespace { + headers := "CONTAINER ID\tNAMES\tPID\tCGROUPNS\tIPC\tMNT\tNET\tPIDN\tUSERNS\tUTS\n" + row := "{{.ID}}\t{{names .Names}}\t{{.Pid}}\t{{.Namespaces.Cgroup}}\t{{.Namespaces.IPC}}\t{{.Namespaces.MNT}}\t{{.Namespaces.NET}}\t{{.Namespaces.PIDNS}}\t{{.Namespaces.User}}\t{{.Namespaces.UTS}}\n" + return headers, row + } + headers := defaultHeaders + if noTrunc { + row += "{{.ID}}" + } else { + row += "{{slice .ID 0 12}}" + } + row += "\t{{.Image}}\t{{cmd .Command}}\t{{humanDuration .Created}}\t{{state .}}\t{{ports .Ports}}\t{{names .Names}}" + + if listOpts.Pod { + headers += "\tPOD ID\tPODNAME" + if noTrunc { + row += "\t{{.Pod}}" + } else { + row += "\t{{slice .Pod 0 12}}" + } + row += "\t{{.PodName}}" + } + + if listOpts.Size { + headers += "\tSIZE" + row += "\t{{consize .Size}}" + } + if !strings.HasSuffix(headers, "\n") { + headers += "\n" + } + if !strings.HasSuffix(row, "\n") { + row += "\n" + } + return headers, row +} + +var psFuncMap = template.FuncMap{ + "cmd": func(conCommand []string) string { + return strings.Join(conCommand, " ") + }, + "state": func(con entities.ListContainer) string { + var state string + switch con.State { + case "running": + t := units.HumanDuration(time.Since(time.Unix(con.StartedAt, 0))) + state = "Up " + t + " ago" + case "configured": + state = "Created" + case "exited": + t := units.HumanDuration(time.Since(time.Unix(con.ExitedAt, 0))) + state = fmt.Sprintf("Exited (%d) %s ago", con.ExitCode, t) + default: + state = con.State + } + return state + }, + "ports": func(ports []ocicni.PortMapping) string { + if len(ports) == 0 { + return "" + } + return portsToString(ports) + }, + "names": func(names []string) string { + return names[0] + }, + "consize": func(csize shared.ContainerSize) string { + virt := units.HumanSizeWithPrecision(float64(csize.RootFsSize), 3) + s := units.HumanSizeWithPrecision(float64(csize.RwSize), 3) + return fmt.Sprintf("%s (virtual %s)", s, virt) + }, +} + +// portsToString converts the ports used to a string of the from "port1, port2" +// and also groups a continuous list of ports into a readable format. +func portsToString(ports []ocicni.PortMapping) string { + type portGroup struct { + first int32 + last int32 + } + var portDisplay []string + if len(ports) == 0 { + return "" + } + //Sort the ports, so grouping continuous ports become easy. + sort.Slice(ports, func(i, j int) bool { + return comparePorts(ports[i], ports[j]) }) + + // portGroupMap is used for grouping continuous ports. + portGroupMap := make(map[string]*portGroup) + var groupKeyList []string + + for _, v := range ports { + + hostIP := v.HostIP + if hostIP == "" { + hostIP = "0.0.0.0" + } + // If hostPort and containerPort are not same, consider as individual port. + if v.ContainerPort != v.HostPort { + portDisplay = append(portDisplay, fmt.Sprintf("%s:%d->%d/%s", hostIP, v.HostPort, v.ContainerPort, v.Protocol)) + continue + } + + portMapKey := fmt.Sprintf("%s/%s", hostIP, v.Protocol) + + portgroup, ok := portGroupMap[portMapKey] + if !ok { + portGroupMap[portMapKey] = &portGroup{first: v.ContainerPort, last: v.ContainerPort} + // This list is required to traverse portGroupMap. + groupKeyList = append(groupKeyList, portMapKey) + continue + } + + if portgroup.last == (v.ContainerPort - 1) { + portgroup.last = v.ContainerPort + continue + } + } + // For each portMapKey, format group list and appned to output string. + for _, portKey := range groupKeyList { + group := portGroupMap[portKey] + portDisplay = append(portDisplay, formatGroup(portKey, group.first, group.last)) + } + return strings.Join(portDisplay, ", ") +} + +func comparePorts(i, j ocicni.PortMapping) bool { + if i.ContainerPort != j.ContainerPort { + return i.ContainerPort < j.ContainerPort + } + + if i.HostIP != j.HostIP { + return i.HostIP < j.HostIP + } + + if i.HostPort != j.HostPort { + return i.HostPort < j.HostPort + } + + return i.Protocol < j.Protocol +} + +// formatGroup returns the group as <IP:startPort:lastPort->startPort:lastPort/Proto> +// e.g 0.0.0.0:1000-1006->1000-1006/tcp. +func formatGroup(key string, start, last int32) string { + parts := strings.Split(key, "/") + groupType := parts[0] + var ip string + if len(parts) > 1 { + ip = parts[0] + groupType = parts[1] + } + group := strconv.Itoa(int(start)) + if start != last { + group = fmt.Sprintf("%s-%d", group, last) + } + if ip != "" { + group = fmt.Sprintf("%s:%s->%s", ip, group, group) + } + return fmt.Sprintf("%s/%s", group, groupType) } diff --git a/cmd/podmanV2/containers/run.go b/cmd/podmanV2/containers/run.go new file mode 100644 index 000000000..bd90aee2f --- /dev/null +++ b/cmd/podmanV2/containers/run.go @@ -0,0 +1,125 @@ +package containers + +import ( + "fmt" + "os" + "strings" + + "github.com/sirupsen/logrus" + + "github.com/containers/libpod/pkg/domain/entities" + + "github.com/containers/libpod/cmd/podmanV2/common" + "github.com/containers/libpod/cmd/podmanV2/registry" + "github.com/containers/libpod/libpod/define" + "github.com/containers/libpod/pkg/specgen" + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +var ( + runDescription = "Runs a command in a new container from the given image" + runCommand = &cobra.Command{ + Use: "run [flags] IMAGE [COMMAND [ARG...]]", + Short: "Run a command in a new container", + Long: runDescription, + PreRunE: preRunE, + RunE: run, + Example: `podman run imageID ls -alF /etc + podman run --network=host imageID dnf -y install java + podman run --volume /var/hostdir:/var/ctrdir -i -t fedora /bin/bash`, + } +) + +var ( + runOpts = entities.ContainerRunOptions{ + OutputStream: os.Stdout, + InputStream: os.Stdin, + ErrorStream: os.Stderr, + } + runRmi bool +) + +func init() { + registry.Commands = append(registry.Commands, registry.CliCommand{ + Mode: []entities.EngineMode{entities.ABIMode}, + Command: runCommand, + }) + flags := runCommand.Flags() + flags.AddFlagSet(common.GetCreateFlags(&cliVals)) + flags.AddFlagSet(common.GetNetFlags()) + flags.SetNormalizeFunc(common.AliasFlags) + flags.BoolVar(&runOpts.SigProxy, "sig-proxy", true, "Proxy received signals to the process") + flags.BoolVar(&runRmi, "rmi", false, "Remove container image unless used by other containers") + if registry.IsRemote() { + _ = flags.MarkHidden("authfile") + } +} + +func run(cmd *cobra.Command, args []string) error { + var ( + err error + ) + cliVals.Net, err = common.NetFlagsToNetOptions(cmd) + if err != nil { + return err + } + if af := cliVals.Authfile; len(af) > 0 { + if _, err := os.Stat(af); err != nil { + return errors.Wrapf(err, "error checking authfile path %s", af) + } + } + runOpts.Rm = cliVals.Rm + if err := createInit(cmd); err != nil { + return err + } + + // If -i is not set, clear stdin + if !cliVals.Interactive { + runOpts.InputStream = nil + } + + // If attach is set, clear stdin/stdout/stderr and only attach requested + if cmd.Flag("attach").Changed { + runOpts.OutputStream = nil + runOpts.ErrorStream = nil + if !cliVals.Interactive { + runOpts.InputStream = nil + } + + for _, stream := range cliVals.Attach { + switch strings.ToLower(stream) { + case "stdout": + runOpts.OutputStream = os.Stdout + case "stderr": + runOpts.ErrorStream = os.Stderr + case "stdin": + runOpts.InputStream = os.Stdin + default: + return errors.Wrapf(define.ErrInvalidArg, "invalid stream %q for --attach - must be one of stdin, stdout, or stderr", stream) + } + } + } + runOpts.Detach = cliVals.Detach + runOpts.DetachKeys = cliVals.DetachKeys + s := specgen.NewSpecGenerator(args[0]) + if err := common.FillOutSpecGen(s, &cliVals, args); err != nil { + return err + } + runOpts.Spec = s + report, err := registry.ContainerEngine().ContainerRun(registry.GetContext(), runOpts) + if err != nil { + return err + } + if cliVals.Detach { + fmt.Println(report.Id) + } + registry.SetExitCode(report.ExitCode) + if runRmi { + _, err := registry.ImageEngine().Delete(registry.GetContext(), []string{report.Id}, entities.ImageDeleteOptions{}) + if err != nil { + logrus.Errorf("%s", errors.Wrapf(err, "failed removing image")) + } + } + return nil +} diff --git a/cmd/podmanV2/containers/start.go b/cmd/podmanV2/containers/start.go new file mode 100644 index 000000000..0ae2f6d50 --- /dev/null +++ b/cmd/podmanV2/containers/start.go @@ -0,0 +1,87 @@ +package containers + +import ( + "fmt" + "os" + + "github.com/containers/libpod/cmd/podmanV2/common" + "github.com/containers/libpod/cmd/podmanV2/registry" + "github.com/containers/libpod/cmd/podmanV2/utils" + "github.com/containers/libpod/libpod/define" + "github.com/containers/libpod/pkg/domain/entities" + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +var ( + startDescription = `Starts one or more containers. The container name or ID can be used.` + startCommand = &cobra.Command{ + Use: "start [flags] CONTAINER [CONTAINER...]", + Short: "Start one or more containers", + Long: startDescription, + RunE: start, + PreRunE: preRunE, + Args: cobra.MinimumNArgs(1), + Example: `podman start --latest + podman start 860a4b231279 5421ab43b45 + podman start --interactive --attach imageID`, + } +) + +var ( + startOptions entities.ContainerStartOptions +) + +func init() { + registry.Commands = append(registry.Commands, registry.CliCommand{ + Mode: []entities.EngineMode{entities.ABIMode}, + Command: startCommand, + }) + flags := startCommand.Flags() + flags.BoolVarP(&startOptions.Attach, "attach", "a", false, "Attach container's STDOUT and STDERR") + flags.StringVar(&startOptions.DetachKeys, "detach-keys", common.GetDefaultDetachKeys(), "Select the key sequence for detaching a container. Format is a single character `[a-Z]` or a comma separated sequence of `ctrl-<value>`, where `<value>` is one of: `a-z`, `@`, `^`, `[`, `\\`, `]`, `^` or `_`") + flags.BoolVarP(&startOptions.Interactive, "interactive", "i", false, "Keep STDIN open even if not attached") + flags.BoolVarP(&startOptions.Latest, "latest", "l", false, "Act on the latest container podman is aware of") + flags.BoolVar(&startOptions.SigProxy, "sig-proxy", false, "Proxy received signals to the process (default true if attaching, false otherwise)") + if registry.IsRemote() { + _ = flags.MarkHidden("latest") + _ = flags.MarkHidden("sig-proxy") + } + +} + +func start(cmd *cobra.Command, args []string) error { + var errs utils.OutputErrors + if len(args) > 1 && startOptions.Attach { + return errors.Errorf("you cannot start and attach multiple containers at once") + } + + sigProxy := startOptions.SigProxy || startOptions.Attach + if cmd.Flag("sig-proxy").Changed { + sigProxy = startOptions.SigProxy + } + + if sigProxy && !startOptions.Attach { + return errors.Wrapf(define.ErrInvalidArg, "you cannot use sig-proxy without --attach") + } + if startOptions.Attach { + startOptions.Stdin = os.Stdin + startOptions.Stderr = os.Stderr + startOptions.Stdout = os.Stdout + } + + responses, err := registry.ContainerEngine().ContainerStart(registry.GetContext(), args, startOptions) + if err != nil { + return err + } + + for _, r := range responses { + if r.Err == nil { + fmt.Println(r.Id) + } else { + errs = append(errs, r.Err) + } + } + // TODO need to understand an implement exitcodes + return errs.PrintErrors() +} diff --git a/cmd/podmanV2/main.go b/cmd/podmanV2/main.go index 6781a7f06..fe3cd9f16 100644 --- a/cmd/podmanV2/main.go +++ b/cmd/podmanV2/main.go @@ -12,6 +12,7 @@ import ( _ "github.com/containers/libpod/cmd/podmanV2/networks" _ "github.com/containers/libpod/cmd/podmanV2/pods" "github.com/containers/libpod/cmd/podmanV2/registry" + _ "github.com/containers/libpod/cmd/podmanV2/system" _ "github.com/containers/libpod/cmd/podmanV2/volumes" "github.com/containers/libpod/libpod" "github.com/containers/libpod/pkg/domain/entities" diff --git a/cmd/podmanV2/pods/inspect.go b/cmd/podmanV2/pods/inspect.go new file mode 100644 index 000000000..9aab610f2 --- /dev/null +++ b/cmd/podmanV2/pods/inspect.go @@ -0,0 +1,64 @@ +package pods + +import ( + "context" + "fmt" + + "github.com/containers/libpod/cmd/podmanV2/registry" + "github.com/containers/libpod/pkg/domain/entities" + jsoniter "github.com/json-iterator/go" + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +var ( + inspectOptions = entities.PodInspectOptions{} +) + +var ( + inspectDescription = fmt.Sprintf(`Display the configuration for a pod by name or id + + By default, this will render all results in a JSON array.`) + + inspectCmd = &cobra.Command{ + Use: "inspect [flags] POD [POD...]", + Short: "Displays a pod configuration", + Long: inspectDescription, + RunE: inspect, + Example: `podman pod inspect podID`, + } +) + +func init() { + registry.Commands = append(registry.Commands, registry.CliCommand{ + Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode}, + Command: inspectCmd, + Parent: podCmd, + }) + flags := inspectCmd.Flags() + flags.BoolVarP(&inspectOptions.Latest, "latest", "l", false, "Act on the latest pod podman is aware of") + if registry.IsRemote() { + _ = flags.MarkHidden("latest") + } +} + +func inspect(cmd *cobra.Command, args []string) error { + + if len(args) < 1 && !inspectOptions.Latest { + return errors.Errorf("you must provide the name or id of a running pod") + } + + if !inspectOptions.Latest { + inspectOptions.NameOrID = args[0] + } + responses, err := registry.ContainerEngine().PodInspect(context.Background(), inspectOptions) + if err != nil { + return err + } + b, err := jsoniter.MarshalIndent(responses, "", " ") + if err != nil { + return err + } + fmt.Println(string(b)) + return nil +} diff --git a/cmd/podmanV2/pods/ps.go b/cmd/podmanV2/pods/ps.go index 9546dff9e..9875263ac 100644 --- a/cmd/podmanV2/pods/ps.go +++ b/cmd/podmanV2/pods/ps.go @@ -88,7 +88,7 @@ func pods(cmd *cobra.Command, args []string) error { fmt.Println(string(b)) return nil } - headers, row := createPodPsOut(cmd) + headers, row := createPodPsOut() if psInput.Quiet { if noTrunc { row = "{{.Id}}\n" @@ -123,7 +123,7 @@ func pods(cmd *cobra.Command, args []string) error { return nil } -func createPodPsOut(cmd *cobra.Command) (string, string) { +func createPodPsOut() (string, string) { var row string headers := defaultHeaders if noTrunc { diff --git a/cmd/podmanV2/system/version.go b/cmd/podmanV2/system/version.go new file mode 100644 index 000000000..e8002056b --- /dev/null +++ b/cmd/podmanV2/system/version.go @@ -0,0 +1,119 @@ +package system + +import ( + "fmt" + "io" + "os" + "strings" + "text/tabwriter" + "time" + + "github.com/containers/buildah/pkg/formats" + "github.com/containers/libpod/cmd/podmanV2/registry" + "github.com/containers/libpod/libpod/define" + "github.com/containers/libpod/pkg/domain/entities" + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +var ( + versionCommand = &cobra.Command{ + Use: "version", + Args: cobra.NoArgs, + Short: "Display the Podman Version Information", + RunE: version, + PersistentPreRunE: preRunE, + } + format string +) + +type versionStruct struct { + Client define.Version + Server define.Version +} + +func init() { + registry.Commands = append(registry.Commands, registry.CliCommand{ + Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode}, + Command: versionCommand, + }) + flags := versionCommand.Flags() + flags.StringVarP(&format, "format", "f", "", "Change the output format to JSON or a Go template") +} + +func version(cmd *cobra.Command, args []string) error { + var ( + v versionStruct + err error + ) + v.Client, err = define.GetVersion() + if err != nil { + return errors.Wrapf(err, "unable to determine version") + } + // TODO we need to discuss how to implement + // this more. current endpoints dont have a + // version endpoint. maybe we use info? + //if remote { + // v.Server, err = getRemoteVersion(c) + // if err != nil { + // return err + // } + //} else { + v.Server = v.Client + //} + + versionOutputFormat := format + if versionOutputFormat != "" { + if strings.Join(strings.Fields(versionOutputFormat), "") == "{{json.}}" { + versionOutputFormat = formats.JSONString + } + var out formats.Writer + switch versionOutputFormat { + case formats.JSONString: + out = formats.JSONStruct{Output: v} + return out.Out() + default: + out = formats.StdoutTemplate{Output: v, Template: versionOutputFormat} + err := out.Out() + if err != nil { + // On Failure, assume user is using older version of podman version --format and check client + out = formats.StdoutTemplate{Output: v.Client, Template: versionOutputFormat} + if err1 := out.Out(); err1 != nil { + return err + } + } + } + return nil + } + w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) + defer w.Flush() + + if registry.IsRemote() { + if _, err := fmt.Fprintf(w, "Client:\n"); err != nil { + return err + } + formatVersion(w, v.Client) + if _, err := fmt.Fprintf(w, "\nServer:\n"); err != nil { + return err + } + formatVersion(w, v.Server) + } else { + formatVersion(w, v.Client) + } + return nil +} + +func formatVersion(writer io.Writer, version define.Version) { + fmt.Fprintf(writer, "Version:\t%s\n", version.Version) + fmt.Fprintf(writer, "RemoteAPI Version:\t%d\n", version.RemoteAPIVersion) + fmt.Fprintf(writer, "Go Version:\t%s\n", version.GoVersion) + if version.GitCommit != "" { + fmt.Fprintf(writer, "Git Commit:\t%s\n", version.GitCommit) + } + // Prints out the build time in readable format + if version.Built != 0 { + fmt.Fprintf(writer, "Built:\t%s\n", time.Unix(version.Built, 0).Format(time.ANSIC)) + } + + fmt.Fprintf(writer, "OS/Arch:\t%s\n", version.OsArch) +} |