summaryrefslogtreecommitdiff
path: root/cmd/podman/pod.go
blob: 9a9c7a7021ea3880e2ea04118f5afd04881abab6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main

import (
	"os"

	"github.com/containers/libpod/cmd/podman/cliconfig"
	"github.com/containers/libpod/pkg/adapter"
	"github.com/containers/libpod/pkg/rootless"
	"github.com/pkg/errors"
	"github.com/spf13/cobra"
)

var (
	podDescription = `Pods are a group of one or more containers sharing the same network, pid and ipc namespaces.`
)
var podCommand = cliconfig.PodmanCommand{
	Command: &cobra.Command{
		Use:   "pod",
		Short: "Manage pods",
		Long:  podDescription,
		RunE:  commandRunE(),
	},
}

//podSubCommands are implemented both in local and remote clients
var podSubCommands = []*cobra.Command{
	_podCreateCommand,
	_podExistsCommand,
	_podInspectCommand,
	_podKillCommand,
	_podPauseCommand,
	_podPsCommand,
	_podRestartCommand,
	_podRmCommand,
	_podStartCommand,
	_podStatsCommand,
	_podStopCommand,
	_podTopCommand,
	_podUnpauseCommand,
}

func joinPodNS(runtime *adapter.LocalRuntime, all, latest bool, inputArgs []string) ([]string, bool, bool, error) {
	if rootless.IsRootless() {
		if os.Geteuid() == 0 {
			return []string{rootless.Argument()}, false, false, nil
		} else {
			var err error
			var pods []*adapter.Pod
			if all {
				pods, err = runtime.GetAllPods()
				if err != nil {
					return nil, false, false, errors.Wrapf(err, "unable to get pods")
				}
			} else if latest {
				pod, err := runtime.GetLatestPod()
				if err != nil {
					return nil, false, false, errors.Wrapf(err, "unable to get latest pod")
				}
				pods = append(pods, pod)
			} else {
				for _, i := range inputArgs {
					pod, err := runtime.LookupPod(i)
					if err != nil {
						return nil, false, false, errors.Wrapf(err, "unable to lookup pod %s", i)
					}
					pods = append(pods, pod)
				}
			}
			for _, p := range pods {
				_, ret, err := runtime.JoinOrCreateRootlessPod(p)
				if err != nil {
					return nil, false, false, err
				}
				if ret != 0 {
					os.Exit(ret)
				}
			}
			os.Exit(0)
		}
	}
	return inputArgs, all, latest, nil
}

func init() {
	podCommand.AddCommand(podSubCommands...)
	podCommand.SetHelpTemplate(HelpTemplate())
	podCommand.SetUsageTemplate(UsageTemplate())
}