-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
stop.go
144 lines (125 loc) · 4.2 KB
/
stop.go
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package containers
import (
"context"
"errors"
"fmt"
"os"
"strings"
"github.com/containers/common/pkg/completion"
"github.com/containers/podman/v5/cmd/podman/common"
"github.com/containers/podman/v5/cmd/podman/registry"
"github.com/containers/podman/v5/cmd/podman/utils"
"github.com/containers/podman/v5/cmd/podman/validate"
"github.com/containers/podman/v5/pkg/domain/entities"
"github.com/spf13/cobra"
)
var (
stopDescription = fmt.Sprintf(`Stops one or more running containers. The container name or ID can be used.
A timeout to forcibly stop the container can also be set but defaults to %d seconds otherwise.`, containerConfig.Engine.StopTimeout)
stopCommand = &cobra.Command{
Use: "stop [options] CONTAINER [CONTAINER...]",
Short: "Stop one or more containers",
Long: stopDescription,
RunE: stop,
Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndIDFile(cmd, args, false, "cidfile")
},
ValidArgsFunction: common.AutocompleteContainersRunning,
Example: `podman stop ctrID
podman stop --time 2 mywebserver 6e534f14da9d`,
}
containerStopCommand = &cobra.Command{
Use: stopCommand.Use,
Short: stopCommand.Short,
Long: stopCommand.Long,
RunE: stopCommand.RunE,
Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndIDFile(cmd, args, false, "cidfile")
},
ValidArgsFunction: stopCommand.ValidArgsFunction,
Example: `podman container stop ctrID
podman container stop --time 2 mywebserver 6e534f14da9d`,
}
)
var (
stopOptions = entities.StopOptions{
Filters: make(map[string][]string),
}
stopCidFiles = []string{}
stopTimeout int
)
func stopFlags(cmd *cobra.Command) {
flags := cmd.Flags()
flags.BoolVarP(&stopOptions.All, "all", "a", false, "Stop all running containers")
flags.BoolVarP(&stopOptions.Ignore, "ignore", "i", false, "Ignore errors when a specified container is missing")
cidfileFlagName := "cidfile"
flags.StringArrayVar(&stopCidFiles, cidfileFlagName, nil, "Read the container ID from the file")
_ = cmd.RegisterFlagCompletionFunc(cidfileFlagName, completion.AutocompleteDefault)
timeFlagName := "time"
flags.IntVarP(&stopTimeout, timeFlagName, "t", int(containerConfig.Engine.StopTimeout), "Seconds to wait for stop before killing the container")
_ = cmd.RegisterFlagCompletionFunc(timeFlagName, completion.AutocompleteNone)
filterFlagName := "filter"
flags.StringArrayVarP(&filters, filterFlagName, "f", []string{}, "Filter output based on conditions given")
_ = cmd.RegisterFlagCompletionFunc(filterFlagName, common.AutocompletePsFilters)
if registry.IsRemote() {
_ = flags.MarkHidden("cidfile")
_ = flags.MarkHidden("ignore")
}
flags.SetNormalizeFunc(utils.TimeoutAliasFlags)
}
func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Command: stopCommand,
})
stopFlags(stopCommand)
validate.AddLatestFlag(stopCommand, &stopOptions.Latest)
registry.Commands = append(registry.Commands, registry.CliCommand{
Command: containerStopCommand,
Parent: containerCmd,
})
stopFlags(containerStopCommand)
validate.AddLatestFlag(containerStopCommand, &stopOptions.Latest)
}
func stop(cmd *cobra.Command, args []string) error {
var (
errs utils.OutputErrors
)
args = utils.RemoveSlash(args)
if cmd.Flag("time").Changed {
timeout := uint(stopTimeout)
stopOptions.Timeout = &timeout
}
for _, cidFile := range stopCidFiles {
content, err := os.ReadFile(cidFile)
if err != nil {
if stopOptions.Ignore && errors.Is(err, os.ErrNotExist) {
continue
}
return fmt.Errorf("reading CIDFile: %w", err)
}
id := strings.Split(string(content), "\n")[0]
args = append(args, id)
}
for _, f := range filters {
fname, filter, hasFilter := strings.Cut(f, "=")
if !hasFilter {
return fmt.Errorf("invalid filter %q", f)
}
stopOptions.Filters[fname] = append(stopOptions.Filters[fname], filter)
}
responses, err := registry.ContainerEngine().ContainerStop(context.Background(), args, stopOptions)
if err != nil {
return err
}
for _, r := range responses {
switch {
case r.Err != nil:
errs = append(errs, r.Err)
case r.RawInput != "":
fmt.Println(r.RawInput)
default:
fmt.Println(r.Id)
}
}
return errs.PrintErrors()
}