mirror of https://github.com/containers/podman.git
Merge pull request #9689 from boaz0/boaz-1
add restart-policy to container filters & --filter to podman start
This commit is contained in:
commit
034470e5be
|
@ -3,6 +3,7 @@ package containers
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/containers/podman/v3/cmd/podman/common"
|
"github.com/containers/podman/v3/cmd/podman/common"
|
||||||
"github.com/containers/podman/v3/cmd/podman/registry"
|
"github.com/containers/podman/v3/cmd/podman/registry"
|
||||||
|
@ -42,7 +43,9 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
startOptions entities.ContainerStartOptions
|
startOptions = entities.ContainerStartOptions{
|
||||||
|
Filters: make(map[string][]string),
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func startFlags(cmd *cobra.Command) {
|
func startFlags(cmd *cobra.Command) {
|
||||||
|
@ -56,6 +59,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.StringSliceVarP(&filters, "filter", "f", []string{}, "Filter output based on conditions given")
|
||||||
|
_ = cmd.RegisterFlagCompletionFunc("filter", common.AutocompletePsFilters)
|
||||||
|
|
||||||
flags.BoolVar(&startOptions.All, "all", false, "Start all containers regardless of their state or configuration")
|
flags.BoolVar(&startOptions.All, "all", false, "Start all containers regardless of their state or configuration")
|
||||||
|
|
||||||
|
@ -116,7 +121,18 @@ func start(cmd *cobra.Command, args []string) error {
|
||||||
startOptions.Stdout = os.Stdout
|
startOptions.Stdout = os.Stdout
|
||||||
}
|
}
|
||||||
|
|
||||||
responses, err := registry.ContainerEngine().ContainerStart(registry.GetContext(), args, startOptions)
|
var containers []string = args
|
||||||
|
if len(filters) > 0 {
|
||||||
|
for _, f := range filters {
|
||||||
|
split := strings.SplitN(f, "=", 2)
|
||||||
|
if len(split) == 1 {
|
||||||
|
return errors.Errorf("invalid filter %q", f)
|
||||||
|
}
|
||||||
|
startOptions.Filters[split[0]] = append(startOptions.Filters[split[0]], split[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
responses, err := registry.ContainerEngine().ContainerStart(registry.GetContext(), containers, startOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,31 @@ Proxy received signals to the process (non-TTY mode only). SIGCHLD, SIGSTOP, and
|
||||||
|
|
||||||
Start all the containers created by Podman, default is only running containers.
|
Start all the containers created by Podman, default is only running containers.
|
||||||
|
|
||||||
|
#### **\-\-filter**, **-f**
|
||||||
|
|
||||||
|
Filter what containers are going to be started from the given arguments.
|
||||||
|
Multiple filters can be given with multiple uses of the --filter flag.
|
||||||
|
Filters with the same key work inclusive with the only exception being
|
||||||
|
`label` which is exclusive. Filters with different keys always work exclusive.
|
||||||
|
|
||||||
|
Valid filters are listed below:
|
||||||
|
|
||||||
|
| **Filter** | **Description** |
|
||||||
|
| --------------- | -------------------------------------------------------------------------------- |
|
||||||
|
| id | [ID] Container's ID (accepts regex) |
|
||||||
|
| name | [Name] Container's name (accepts regex) |
|
||||||
|
| label | [Key] or [Key=Value] Label assigned to a container |
|
||||||
|
| exited | [Int] Container's exit code |
|
||||||
|
| status | [Status] Container's status: 'created', 'exited', 'paused', 'running', 'unknown' |
|
||||||
|
| ancestor | [ImageName] Image or descendant used to create container |
|
||||||
|
| before | [ID] or [Name] Containers created before this container |
|
||||||
|
| since | [ID] or [Name] Containers created since this container |
|
||||||
|
| volume | [VolumeName] or [MountpointDestination] Volume mounted in container |
|
||||||
|
| health | [Status] healthy or unhealthy |
|
||||||
|
| pod | [Pod] name or full or partial ID of pod |
|
||||||
|
| network | [Network] name or full ID of network |
|
||||||
|
|
||||||
|
|
||||||
## EXAMPLE
|
## EXAMPLE
|
||||||
|
|
||||||
podman start mywebserver
|
podman start mywebserver
|
||||||
|
|
|
@ -17,3 +17,12 @@ const (
|
||||||
// handling of system restart, which Podman does not yet support.
|
// handling of system restart, which Podman does not yet support.
|
||||||
RestartPolicyUnlessStopped = "unless-stopped"
|
RestartPolicyUnlessStopped = "unless-stopped"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// RestartPolicyMap maps between restart-policy valid values to restart policy types
|
||||||
|
var RestartPolicyMap = map[string]string{
|
||||||
|
"none": RestartPolicyNone,
|
||||||
|
RestartPolicyNo: RestartPolicyNo,
|
||||||
|
RestartPolicyAlways: RestartPolicyAlways,
|
||||||
|
RestartPolicyOnFailure: RestartPolicyOnFailure,
|
||||||
|
RestartPolicyUnlessStopped: RestartPolicyUnlessStopped,
|
||||||
|
}
|
||||||
|
|
|
@ -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 {
|
||||||
|
Filters map[string][]string
|
||||||
All bool
|
All bool
|
||||||
Attach bool
|
Attach bool
|
||||||
DetachKeys string
|
DetachKeys string
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package filters
|
package filters
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -226,6 +227,32 @@ func GenerateContainerFilterFuncs(filter string, filterValues []string, r *libpo
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}, nil
|
}, nil
|
||||||
|
case "restart-policy":
|
||||||
|
invalidPolicyNames := []string{}
|
||||||
|
for _, policy := range filterValues {
|
||||||
|
if _, ok := define.RestartPolicyMap[policy]; !ok {
|
||||||
|
invalidPolicyNames = append(invalidPolicyNames, policy)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var filterValueError error = nil
|
||||||
|
if len(invalidPolicyNames) > 0 {
|
||||||
|
errPrefix := "invalid restart policy"
|
||||||
|
if len(invalidPolicyNames) > 1 {
|
||||||
|
errPrefix = "invalid restart policies"
|
||||||
|
}
|
||||||
|
filterValueError = fmt.Errorf("%s %s", strings.Join(invalidPolicyNames, ", "), errPrefix)
|
||||||
|
}
|
||||||
|
return func(c *libpod.Container) bool {
|
||||||
|
for _, policy := range filterValues {
|
||||||
|
if policy == "none" && c.RestartPolicy() == define.RestartPolicyNone {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if c.RestartPolicy() == policy {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}, filterValueError
|
||||||
}
|
}
|
||||||
return nil, errors.Errorf("%s is an invalid filter", filter)
|
return nil, errors.Errorf("%s is an invalid filter", filter)
|
||||||
}
|
}
|
||||||
|
|
|
@ -694,7 +694,33 @@ 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(options.All, options.Latest, namesOrIds, ic.Libpod)
|
containersNamesOrIds := namesOrIds
|
||||||
|
if len(options.Filters) > 0 {
|
||||||
|
filterFuncs := make([]libpod.ContainerFilter, 0, len(options.Filters))
|
||||||
|
if len(options.Filters) > 0 {
|
||||||
|
for k, v := range options.Filters {
|
||||||
|
generatedFunc, err := dfilters.GenerateContainerFilterFuncs(k, v, ic.Libpod)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
filterFuncs = append(filterFuncs, generatedFunc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
candidates, err := ic.Libpod.GetContainers(filterFuncs...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
containersNamesOrIds = []string{}
|
||||||
|
for _, candidate := range candidates {
|
||||||
|
for _, nameOrID := range namesOrIds {
|
||||||
|
if nameOrID == candidate.ID() || nameOrID == candidate.Name() {
|
||||||
|
containersNamesOrIds = append(containersNamesOrIds, nameOrID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ctrs, rawInputs, err := getContainersAndInputByContext(options.All, options.Latest, containersNamesOrIds, ic.Libpod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -506,7 +506,30 @@ 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, options.All, false, namesOrIds)
|
containersNamesOrIds := namesOrIds
|
||||||
|
if len(options.Filters) > 0 {
|
||||||
|
containersNamesOrIds = []string{}
|
||||||
|
opts := new(containers.ListOptions).WithFilters(options.Filters).WithAll(true)
|
||||||
|
candidates, listErr := containers.List(ic.ClientCtx, opts)
|
||||||
|
if listErr != nil {
|
||||||
|
return nil, listErr
|
||||||
|
}
|
||||||
|
for _, candidate := range candidates {
|
||||||
|
for _, nameOrID := range namesOrIds {
|
||||||
|
if nameOrID == candidate.ID {
|
||||||
|
containersNamesOrIds = append(containersNamesOrIds, nameOrID)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, containerName := range candidate.Names {
|
||||||
|
if containerName == nameOrID {
|
||||||
|
containersNamesOrIds = append(containersNamesOrIds, nameOrID)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ctrs, err := getContainersByContext(ic.ClientCtx, options.All, false, containersNamesOrIds)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,4 +40,21 @@ load helpers
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "podman start --filter - start only containers that match the filter" {
|
||||||
|
run_podman run -d $IMAGE /bin/true
|
||||||
|
cid="$output"
|
||||||
|
run_podman start --filter restart-policy=always $cid "CID of restart-policy=always container"
|
||||||
|
is "$output" ""
|
||||||
|
|
||||||
|
run_podman start --filter restart-policy=none $cid "CID of restart-policy=none container"
|
||||||
|
is "$output" "$cid"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "podman start --filter invalid-restart-policy - return error" {
|
||||||
|
run_podman run -d $IMAGE /bin/true
|
||||||
|
cid="$output"
|
||||||
|
run_podman 125 start --filter restart-policy=fakepolicy $cid "CID of restart-policy=<not-exists> container"
|
||||||
|
is "$output" "Error: fakepolicy invalid restart policy"
|
||||||
|
}
|
||||||
|
|
||||||
# vim: filetype=sh
|
# vim: filetype=sh
|
||||||
|
|
Loading…
Reference in New Issue