mirror of https://github.com/containers/podman.git
Add variable for global flags to runlabel
use $GLOBAL_OPTS to pass global flags to the runlabel command. Signed-off-by: Qi Wang <qiwan@redhat.com>
This commit is contained in:
parent
7d05ff3fc7
commit
a477a8ff75
|
@ -12,6 +12,7 @@ import (
|
|||
"github.com/containers/libpod/cmd/podman/shared"
|
||||
"github.com/containers/libpod/libpod"
|
||||
"github.com/containers/libpod/libpod/image"
|
||||
"github.com/containers/libpod/pkg/util"
|
||||
"github.com/containers/libpod/utils"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
@ -145,7 +146,8 @@ func runlabelCmd(c *cliconfig.RunlabelValues) error {
|
|||
return errors.Errorf("%s does not have a label of %s", runlabelImage, label)
|
||||
}
|
||||
|
||||
cmd, env, err := shared.GenerateRunlabelCommand(runLabel, imageName, c.Name, opts, extraArgs)
|
||||
globalOpts := util.GetGlobalOpts(c)
|
||||
cmd, env, err := shared.GenerateRunlabelCommand(runLabel, imageName, c.Name, opts, extraArgs, globalOpts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -883,7 +883,7 @@ func GetRunlabel(label string, runlabelImage string, ctx context.Context, runtim
|
|||
}
|
||||
|
||||
// GenerateRunlabelCommand generates the command that will eventually be execucted by podman
|
||||
func GenerateRunlabelCommand(runLabel, imageName, name string, opts map[string]string, extraArgs []string) ([]string, []string, error) {
|
||||
func GenerateRunlabelCommand(runLabel, imageName, name string, opts map[string]string, extraArgs []string, globalOpts string) ([]string, []string, error) {
|
||||
// If no name is provided, we use the image's basename instead
|
||||
if name == "" {
|
||||
baseName, err := image.GetImageBaseName(imageName)
|
||||
|
@ -896,7 +896,7 @@ func GenerateRunlabelCommand(runLabel, imageName, name string, opts map[string]s
|
|||
if len(extraArgs) > 0 {
|
||||
runLabel = fmt.Sprintf("%s %s", runLabel, strings.Join(extraArgs, " "))
|
||||
}
|
||||
cmd, err := GenerateCommand(runLabel, imageName, name)
|
||||
cmd, err := GenerateCommand(runLabel, imageName, name, globalOpts)
|
||||
if err != nil {
|
||||
return nil, nil, errors.Wrapf(err, "unable to generate command")
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ func substituteCommand(cmd string) (string, error) {
|
|||
}
|
||||
|
||||
// GenerateCommand takes a label (string) and converts it to an executable command
|
||||
func GenerateCommand(command, imageName, name string) ([]string, error) {
|
||||
func GenerateCommand(command, imageName, name, globalOpts string) ([]string, error) {
|
||||
var (
|
||||
newCommand []string
|
||||
)
|
||||
|
@ -79,6 +79,8 @@ func GenerateCommand(command, imageName, name string) ([]string, error) {
|
|||
newArg = fmt.Sprintf("NAME=%s", name)
|
||||
case "$NAME":
|
||||
newArg = name
|
||||
case "$GLOBAL_OPTS":
|
||||
newArg = globalOpts
|
||||
default:
|
||||
newArg = arg
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ var (
|
|||
func TestGenerateCommand(t *testing.T) {
|
||||
inputCommand := "docker run -it --name NAME -e NAME=NAME -e IMAGE=IMAGE IMAGE echo \"hello world\""
|
||||
correctCommand := "/proc/self/exe run -it --name bar -e NAME=bar -e IMAGE=foo foo echo hello world"
|
||||
newCommand, err := GenerateCommand(inputCommand, "foo", "bar")
|
||||
newCommand, err := GenerateCommand(inputCommand, "foo", "bar", "")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "hello world", newCommand[11])
|
||||
assert.Equal(t, correctCommand, strings.Join(newCommand, " "))
|
||||
|
@ -83,7 +83,7 @@ func TestGenerateCommandCheckSubstitution(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, test := range tests {
|
||||
newCommand, err := GenerateCommand(test.input, "foo", "bar")
|
||||
newCommand, err := GenerateCommand(test.input, "foo", "bar", "")
|
||||
if test.shouldFail {
|
||||
assert.NotNil(t, err)
|
||||
} else {
|
||||
|
@ -96,14 +96,14 @@ func TestGenerateCommandCheckSubstitution(t *testing.T) {
|
|||
func TestGenerateCommandPath(t *testing.T) {
|
||||
inputCommand := "docker run -it --name NAME -e NAME=NAME -e IMAGE=IMAGE IMAGE echo install"
|
||||
correctCommand := "/proc/self/exe run -it --name bar -e NAME=bar -e IMAGE=foo foo echo install"
|
||||
newCommand, _ := GenerateCommand(inputCommand, "foo", "bar")
|
||||
newCommand, _ := GenerateCommand(inputCommand, "foo", "bar", "")
|
||||
assert.Equal(t, correctCommand, strings.Join(newCommand, " "))
|
||||
}
|
||||
|
||||
func TestGenerateCommandNoSetName(t *testing.T) {
|
||||
inputCommand := "docker run -it --name NAME -e NAME=NAME -e IMAGE=IMAGE IMAGE echo install"
|
||||
correctCommand := "/proc/self/exe run -it --name foo -e NAME=foo -e IMAGE=foo foo echo install"
|
||||
newCommand, err := GenerateCommand(inputCommand, "foo", "")
|
||||
newCommand, err := GenerateCommand(inputCommand, "foo", "", "")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, correctCommand, strings.Join(newCommand, " "))
|
||||
}
|
||||
|
@ -111,7 +111,7 @@ func TestGenerateCommandNoSetName(t *testing.T) {
|
|||
func TestGenerateCommandNoName(t *testing.T) {
|
||||
inputCommand := "docker run -it -e IMAGE=IMAGE IMAGE echo install"
|
||||
correctCommand := "/proc/self/exe run -it -e IMAGE=foo foo echo install"
|
||||
newCommand, err := GenerateCommand(inputCommand, "foo", "")
|
||||
newCommand, err := GenerateCommand(inputCommand, "foo", "", "")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, correctCommand, strings.Join(newCommand, " "))
|
||||
}
|
||||
|
@ -119,7 +119,7 @@ func TestGenerateCommandNoName(t *testing.T) {
|
|||
func TestGenerateCommandAlreadyPodman(t *testing.T) {
|
||||
inputCommand := "podman run -it --name NAME -e NAME=NAME -e IMAGE=IMAGE IMAGE echo install"
|
||||
correctCommand := "/proc/self/exe run -it --name bar -e NAME=bar -e IMAGE=foo foo echo install"
|
||||
newCommand, err := GenerateCommand(inputCommand, "foo", "bar")
|
||||
newCommand, err := GenerateCommand(inputCommand, "foo", "bar", "")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, correctCommand, strings.Join(newCommand, " "))
|
||||
}
|
||||
|
|
|
@ -10,10 +10,12 @@ import (
|
|||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/containers/image/types"
|
||||
"github.com/containers/libpod/cmd/podman/cliconfig"
|
||||
"github.com/containers/storage"
|
||||
"github.com/containers/storage/pkg/idtools"
|
||||
"github.com/opencontainers/image-spec/specs-go/v1"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/pflag"
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
)
|
||||
|
||||
|
@ -252,3 +254,32 @@ func ParseInputTime(inputTime string) (time.Time, error) {
|
|||
}
|
||||
return time.Now().Add(-duration), nil
|
||||
}
|
||||
|
||||
// GetGlobalOpts checks all global flags and generates the command string
|
||||
func GetGlobalOpts(c *cliconfig.RunlabelValues) string {
|
||||
globalFlags := map[string]bool{
|
||||
"cgroup-manager": true, "cni-config-dir": true, "conmon": true, "default-mounts-file": true,
|
||||
"hooks-dir": true, "namespace": true, "root": true, "runroot": true,
|
||||
"runtime": true, "storage-driver": true, "storage-opt": true, "syslog": true,
|
||||
"trace": true, "network-cmd-path": true, "config": true, "cpu-profile": true,
|
||||
"log-level": true, "tmpdir": true}
|
||||
const stringSliceType string = "stringSlice"
|
||||
|
||||
var optsCommand []string
|
||||
c.PodmanCommand.Command.Flags().VisitAll(func(f *pflag.Flag) {
|
||||
if !f.Changed {
|
||||
return
|
||||
}
|
||||
if _, exist := globalFlags[f.Name]; exist {
|
||||
if f.Value.Type() == stringSliceType {
|
||||
flagValue := strings.TrimSuffix(strings.TrimPrefix(f.Value.String(), "["), "]")
|
||||
for _, value := range strings.Split(flagValue, ",") {
|
||||
optsCommand = append(optsCommand, fmt.Sprintf("--%s %s", f.Name, value))
|
||||
}
|
||||
} else {
|
||||
optsCommand = append(optsCommand, fmt.Sprintf("--%s %s", f.Name, f.Value.String()))
|
||||
}
|
||||
}
|
||||
})
|
||||
return strings.Join(optsCommand, " ")
|
||||
}
|
||||
|
|
|
@ -728,7 +728,7 @@ func (i *LibpodAPI) ContainerRunlabel(call iopodman.VarlinkCall, input iopodman.
|
|||
return call.ReplyErrorOccurred(fmt.Sprintf("%s does not contain the label %s", input.Image, input.Label))
|
||||
}
|
||||
|
||||
cmd, env, err := shared.GenerateRunlabelCommand(runLabel, imageName, input.Name, input.Opts, input.ExtraArgs)
|
||||
cmd, env, err := shared.GenerateRunlabelCommand(runLabel, imageName, input.Name, input.Opts, input.ExtraArgs, "")
|
||||
if err != nil {
|
||||
return call.ReplyErrorOccurred(err.Error())
|
||||
}
|
||||
|
|
|
@ -18,6 +18,11 @@ var LsDockerfile = `
|
|||
FROM alpine:latest
|
||||
LABEL RUN ls -la`
|
||||
|
||||
var GlobalDockerfile = `
|
||||
FROM alpine:latest
|
||||
LABEL RUN echo \$GLOBAL_OPTS
|
||||
`
|
||||
|
||||
var _ = Describe("podman container runlabel", func() {
|
||||
var (
|
||||
tempdir string
|
||||
|
@ -78,4 +83,18 @@ var _ = Describe("podman container runlabel", func() {
|
|||
Expect(result.ExitCode()).ToNot(Equal(0))
|
||||
|
||||
})
|
||||
|
||||
It("podman container runlabel global options", func() {
|
||||
image := "podman-global-test:ls"
|
||||
podmanTest.BuildImage(GlobalDockerfile, image, "false")
|
||||
result := podmanTest.Podman([]string{"--syslog", "--log-level", "debug", "container", "runlabel", "RUN", image})
|
||||
result.WaitWithDefaultTimeout()
|
||||
Expect(result.ExitCode()).To(Equal(0))
|
||||
|
||||
Expect(result.OutputToString()).To(ContainSubstring("--syslog true"))
|
||||
Expect(result.OutputToString()).To(ContainSubstring("--log-level debug"))
|
||||
result = podmanTest.Podman([]string{"rmi", image})
|
||||
result.WaitWithDefaultTimeout()
|
||||
Expect(result.ExitCode()).To(Equal(0))
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue