summaryrefslogtreecommitdiff
path: root/cmd/podman/cleanup.go
blob: d68255aa229464d4f84ed53b564afa7254341831 (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
package main

import (
	"fmt"
	"os"

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

var (
	cleanupCommand     cliconfig.CleanupValues
	cleanupDescription = `
   podman container cleanup

   Cleans up mount points and network stacks on one or more containers from the host. The container name or ID can be used. This command is used internally when running containers, but can also be used if container cleanup has failed when a container exits.
`
	_cleanupCommand = &cobra.Command{
		Use:   "cleanup",
		Short: "Cleanup network and mountpoints of one or more containers",
		Long:  cleanupDescription,
		RunE: func(cmd *cobra.Command, args []string) error {
			cleanupCommand.InputArgs = args
			cleanupCommand.GlobalFlags = MainGlobalOpts
			return cleanupCmd(&cleanupCommand)
		},
		Args: func(cmd *cobra.Command, args []string) error {
			return checkAllAndLatest(cmd, args, false)
		},
		Example: `podman container cleanup --latest
  podman container cleanup ctrID1 ctrID2 ctrID3
  podman container cleanup --all`,
	}
)

func init() {
	cleanupCommand.Command = _cleanupCommand
	cleanupCommand.SetUsageTemplate(UsageTemplate())
	flags := cleanupCommand.Flags()

	flags.BoolVarP(&cleanupCommand.All, "all", "a", false, "Cleans up all containers")
	flags.BoolVarP(&cleanupCommand.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
	flags.BoolVar(&cleanupCommand.Remove, "rm", false, "After cleanup, remove the container entirely")
	markFlagHiddenForRemoteClient("latest", flags)
}

func cleanupCmd(c *cliconfig.CleanupValues) error {
	runtime, err := libpodruntime.GetRuntime(&c.PodmanCommand)
	if err != nil {
		return errors.Wrapf(err, "could not get runtime")
	}
	defer runtime.Shutdown(false)

	cleanupContainers, lastError := getAllOrLatestContainers(&c.PodmanCommand, runtime, -1, "all")

	ctx := getContext()

	for _, ctr := range cleanupContainers {
		hadError := false
		if c.Remove {
			if err := runtime.RemoveContainer(ctx, ctr, false, false); err != nil {
				if lastError != nil {
					fmt.Fprintln(os.Stderr, lastError)
				}
				lastError = errors.Wrapf(err, "failed to cleanup and remove container %v", ctr.ID())
				hadError = true
			}
		} else {
			if err := ctr.Cleanup(ctx); err != nil {
				if lastError != nil {
					fmt.Fprintln(os.Stderr, lastError)
				}
				lastError = errors.Wrapf(err, "failed to cleanup container %v", ctr.ID())
				hadError = true
			}
		}
		if !hadError {
			fmt.Println(ctr.ID())
		}
	}
	return lastError
}