mirror of
https://github.com/containers/podman.git
synced 2025-06-24 11:28:24 +08:00
Merge pull request #8828 from boaz0/closes_8779
Add --all to podman start
This commit is contained in:
@ -57,6 +57,8 @@ func startFlags(cmd *cobra.Command) {
|
|||||||
flags.BoolVarP(&startOptions.Interactive, "interactive", "i", false, "Keep STDIN open even if not attached")
|
flags.BoolVarP(&startOptions.Interactive, "interactive", "i", false, "Keep STDIN open even if not attached")
|
||||||
flags.BoolVar(&startOptions.SigProxy, "sig-proxy", false, "Proxy received signals to the process (default true if attaching, false otherwise)")
|
flags.BoolVar(&startOptions.SigProxy, "sig-proxy", false, "Proxy received signals to the process (default true if attaching, false otherwise)")
|
||||||
|
|
||||||
|
flags.BoolVar(&startOptions.All, "all", false, "Start all containers regardless of their state or configuration")
|
||||||
|
|
||||||
if registry.IsRemote() {
|
if registry.IsRemote() {
|
||||||
_ = flags.MarkHidden("sig-proxy")
|
_ = flags.MarkHidden("sig-proxy")
|
||||||
}
|
}
|
||||||
@ -79,7 +81,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func validateStart(cmd *cobra.Command, args []string) error {
|
func validateStart(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) == 0 && !startOptions.Latest {
|
if len(args) == 0 && !startOptions.Latest && !startOptions.All {
|
||||||
return errors.New("start requires at least one argument")
|
return errors.New("start requires at least one argument")
|
||||||
}
|
}
|
||||||
if len(args) > 0 && startOptions.Latest {
|
if len(args) > 0 && startOptions.Latest {
|
||||||
@ -88,6 +90,12 @@ func validateStart(cmd *cobra.Command, args []string) error {
|
|||||||
if len(args) > 1 && startOptions.Attach {
|
if len(args) > 1 && startOptions.Attach {
|
||||||
return errors.Errorf("you cannot start and attach multiple containers at once")
|
return errors.Errorf("you cannot start and attach multiple containers at once")
|
||||||
}
|
}
|
||||||
|
if (len(args) > 0 || startOptions.Latest) && startOptions.All {
|
||||||
|
return errors.Errorf("either start all containers or the container(s) provided in the arguments")
|
||||||
|
}
|
||||||
|
if startOptions.Attach && startOptions.All {
|
||||||
|
return errors.Errorf("you cannot start and attach all containers at once")
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,6 +38,10 @@ to run containers such as CRI-O, the last started container could be from either
|
|||||||
|
|
||||||
Proxy received signals to the process (non-TTY mode only). SIGCHLD, SIGSTOP, and SIGKILL are not proxied. The default is *true* when attaching, *false* otherwise.
|
Proxy received signals to the process (non-TTY mode only). SIGCHLD, SIGSTOP, and SIGKILL are not proxied. The default is *true* when attaching, *false* otherwise.
|
||||||
|
|
||||||
|
#### **\-\-all**
|
||||||
|
|
||||||
|
Start all the containers created by Podman, default is only running containers.
|
||||||
|
|
||||||
## EXAMPLE
|
## EXAMPLE
|
||||||
|
|
||||||
podman start mywebserver
|
podman start mywebserver
|
||||||
|
@ -265,6 +265,7 @@ type ContainerExistsOptions struct {
|
|||||||
// ContainerStartOptions describes the val from the
|
// ContainerStartOptions describes the val from the
|
||||||
// CLI needed to start a container
|
// CLI needed to start a container
|
||||||
type ContainerStartOptions struct {
|
type ContainerStartOptions struct {
|
||||||
|
All bool
|
||||||
Attach bool
|
Attach bool
|
||||||
DetachKeys string
|
DetachKeys string
|
||||||
Interactive bool
|
Interactive bool
|
||||||
|
@ -693,14 +693,17 @@ func (ic *ContainerEngine) ContainerExecDetached(ctx context.Context, nameOrID s
|
|||||||
func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []string, options entities.ContainerStartOptions) ([]*entities.ContainerStartReport, error) {
|
func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []string, options entities.ContainerStartOptions) ([]*entities.ContainerStartReport, error) {
|
||||||
reports := []*entities.ContainerStartReport{}
|
reports := []*entities.ContainerStartReport{}
|
||||||
var exitCode = define.ExecErrorCodeGeneric
|
var exitCode = define.ExecErrorCodeGeneric
|
||||||
ctrs, rawInputs, err := getContainersAndInputByContext(false, options.Latest, namesOrIds, ic.Libpod)
|
ctrs, rawInputs, err := getContainersAndInputByContext(options.All, options.Latest, namesOrIds, ic.Libpod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// There can only be one container if attach was used
|
// There can only be one container if attach was used
|
||||||
for i := range ctrs {
|
for i := range ctrs {
|
||||||
ctr := ctrs[i]
|
ctr := ctrs[i]
|
||||||
rawInput := rawInputs[i]
|
rawInput := ctr.ID()
|
||||||
|
if !options.All {
|
||||||
|
rawInput = rawInputs[i]
|
||||||
|
}
|
||||||
ctrState, err := ctr.State()
|
ctrState, err := ctr.State()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -506,7 +506,7 @@ func startAndAttach(ic *ContainerEngine, name string, detachKeys *string, input,
|
|||||||
func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []string, options entities.ContainerStartOptions) ([]*entities.ContainerStartReport, error) {
|
func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []string, options entities.ContainerStartOptions) ([]*entities.ContainerStartReport, error) {
|
||||||
reports := []*entities.ContainerStartReport{}
|
reports := []*entities.ContainerStartReport{}
|
||||||
var exitCode = define.ExecErrorCodeGeneric
|
var exitCode = define.ExecErrorCodeGeneric
|
||||||
ctrs, err := getContainersByContext(ic.ClientCtx, false, false, namesOrIds)
|
ctrs, err := getContainersByContext(ic.ClientCtx, options.All, false, namesOrIds)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -514,9 +514,13 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri
|
|||||||
// There can only be one container if attach was used
|
// There can only be one container if attach was used
|
||||||
for i, ctr := range ctrs {
|
for i, ctr := range ctrs {
|
||||||
name := ctr.ID
|
name := ctr.ID
|
||||||
|
rawInput := ctr.ID
|
||||||
|
if !options.All {
|
||||||
|
rawInput = namesOrIds[i]
|
||||||
|
}
|
||||||
report := entities.ContainerStartReport{
|
report := entities.ContainerStartReport{
|
||||||
Id: name,
|
Id: name,
|
||||||
RawInput: namesOrIds[i],
|
RawInput: rawInput,
|
||||||
ExitCode: exitCode,
|
ExitCode: exitCode,
|
||||||
}
|
}
|
||||||
ctrRunning := ctr.State == define.ContainerStateRunning.String()
|
ctrRunning := ctr.State == define.ContainerStateRunning.String()
|
||||||
@ -598,9 +602,9 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri
|
|||||||
reports = append(reports, &report)
|
reports = append(reports, &report)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
report.ExitCode = 0
|
||||||
|
reports = append(reports, &report)
|
||||||
}
|
}
|
||||||
report.ExitCode = 0
|
|
||||||
reports = append(reports, &report)
|
|
||||||
}
|
}
|
||||||
return reports, nil
|
return reports, nil
|
||||||
}
|
}
|
||||||
|
43
test/system/045-start.bats
Normal file
43
test/system/045-start.bats
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
#!/usr/bin/env bats -*- bats -*-
|
||||||
|
|
||||||
|
load helpers
|
||||||
|
|
||||||
|
@test "podman start --all - start all containers" {
|
||||||
|
# Run a bunch of short-lived containers, with different --restart settings
|
||||||
|
run_podman run -d $IMAGE /bin/true
|
||||||
|
cid_none_implicit="$output"
|
||||||
|
run_podman run -d --restart=no $IMAGE /bin/false
|
||||||
|
cid_none_explicit="$output"
|
||||||
|
run_podman run -d --restart=on-failure $IMAGE /bin/true
|
||||||
|
cid_on_failure="$output"
|
||||||
|
|
||||||
|
# Run one longer-lived one.
|
||||||
|
run_podman run -d --restart=always $IMAGE sleep 20
|
||||||
|
cid_always="$output"
|
||||||
|
|
||||||
|
run_podman wait $cid_none_implicit $cid_none_explicit $cid_on_failure
|
||||||
|
|
||||||
|
run_podman start --all
|
||||||
|
is "$output" ".*$cid_none_implicit" "started: container with no --restart"
|
||||||
|
is "$output" ".*$cid_none_explicit" "started: container with --restart=no"
|
||||||
|
is "$output" ".*$cid_on_failure" "started: container with --restart=on-failure"
|
||||||
|
if [[ $output =~ $cid_always ]]; then
|
||||||
|
die "podman start --all restarted a running container"
|
||||||
|
fi
|
||||||
|
|
||||||
|
run_podman rm $cid_none_implicit $cid_none_explicit $cid_on_failure
|
||||||
|
run_podman stop -t 1 $cid_always
|
||||||
|
run_podman rm $cid_always
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "podman start --all with incompatible options" {
|
||||||
|
expected="Error: either start all containers or the container(s) provided in the arguments"
|
||||||
|
run_podman 125 start --all 12333
|
||||||
|
is "$output" "$expected" "start --all, with args, throws error"
|
||||||
|
if ! is_remote; then
|
||||||
|
run_podman 125 start --all --latest
|
||||||
|
is "$output" "$expected" "podman start --all --latest"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# vim: filetype=sh
|
Reference in New Issue
Block a user