diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2021-04-14 10:47:50 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-14 10:47:50 -0400 |
commit | 4ee203dde7c9e8de2484f00c7f251c0f32ed0ad2 (patch) | |
tree | d40e6b560c77bf8f17386ee19034f295e51644c3 | |
parent | b130dced648350f547767d07bb0b055472ab79bd (diff) | |
parent | 8f16742187ce02dbbd5a798763bad01a263bc476 (diff) | |
download | podman-4ee203dde7c9e8de2484f00c7f251c0f32ed0ad2.tar.gz podman-4ee203dde7c9e8de2484f00c7f251c0f32ed0ad2.tar.bz2 podman-4ee203dde7c9e8de2484f00c7f251c0f32ed0ad2.zip |
Merge pull request #9945 from rhatdan/runlabel
Fix handling of $NAME and $IMAGE in runlabel
-rw-r--r-- | docs/source/markdown/podman-container-runlabel.1.md | 2 | ||||
-rw-r--r-- | pkg/domain/infra/abi/containers_runlabel.go | 25 | ||||
-rw-r--r-- | test/system/037-runlabel.bats | 32 |
3 files changed, 45 insertions, 14 deletions
diff --git a/docs/source/markdown/podman-container-runlabel.1.md b/docs/source/markdown/podman-container-runlabel.1.md index 84d283cf8..9557303b6 100644 --- a/docs/source/markdown/podman-container-runlabel.1.md +++ b/docs/source/markdown/podman-container-runlabel.1.md @@ -13,7 +13,7 @@ exist, `podman container runlabel` will just exit. If the container image has a LABEL INSTALL instruction like the following: -`LABEL INSTALL /usr/bin/podman run -t -i --rm \${OPT1} --privileged -v /:/host --net=host --ipc=host --pid=host -e HOST=/host -e NAME=\${NAME} -e IMAGE=\${IMAGE} -e CONFDIR=\/etc/${NAME} -e LOGDIR=/var/log/\${NAME} -e DATADIR=/var/lib/\${NAME} \${IMAGE} \${OPT2} /bin/install.sh \${OPT3}` +`LABEL INSTALL /usr/bin/podman run -t -i --rm \${OPT1} --privileged -v /:/host --net=host --ipc=host --pid=host -e HOST=/host -e NAME=\${NAME} -e IMAGE=\${IMAGE} -e CONFDIR=/etc/\${NAME} -e LOGDIR=/var/log/\${NAME} -e DATADIR=/var/lib/\${NAME} \${IMAGE} \${OPT2} /bin/install.sh \${OPT3}` `podman container runlabel` will set the following environment variables for use in the command: diff --git a/pkg/domain/infra/abi/containers_runlabel.go b/pkg/domain/infra/abi/containers_runlabel.go index 8de383926..2cabab988 100644 --- a/pkg/domain/infra/abi/containers_runlabel.go +++ b/pkg/domain/infra/abi/containers_runlabel.go @@ -177,6 +177,16 @@ func generateRunlabelCommand(runlabel string, img *image.Image, args []string, o return cmd, env, nil } +func replaceName(arg, name string) string { + newarg := strings.ReplaceAll(arg, "$NAME", name) + return strings.ReplaceAll(newarg, "${NAME}", name) +} + +func replaceImage(arg, image string) string { + newarg := strings.ReplaceAll(arg, "$IMAGE", image) + return strings.ReplaceAll(newarg, "${IMAGE}", image) +} + // generateCommand takes a label (string) and converts it to an executable command func generateCommand(command, imageName, name, globalOpts string) ([]string, error) { if name == "" { @@ -196,26 +206,15 @@ func generateCommand(command, imageName, name, globalOpts string) ([]string, err for _, arg := range cmd[1:] { var newArg string switch arg { - case "IMAGE": - newArg = imageName - case "$IMAGE": - newArg = imageName case "IMAGE=IMAGE": newArg = fmt.Sprintf("IMAGE=%s", imageName) - case "IMAGE=$IMAGE": - newArg = fmt.Sprintf("IMAGE=%s", imageName) - case "NAME": - newArg = name case "NAME=NAME": newArg = fmt.Sprintf("NAME=%s", name) - case "NAME=$NAME": - newArg = fmt.Sprintf("NAME=%s", name) - case "$NAME": - newArg = name case "$GLOBAL_OPTS": newArg = globalOpts default: - newArg = arg + newArg = replaceName(arg, name) + newArg = replaceImage(newArg, imageName) } newCommand = append(newCommand, newArg) } diff --git a/test/system/037-runlabel.bats b/test/system/037-runlabel.bats new file mode 100644 index 000000000..8e18f40d3 --- /dev/null +++ b/test/system/037-runlabel.bats @@ -0,0 +1,32 @@ +#!/usr/bin/env bats + +load helpers + +@test "podman container runlabel test" { + skip_if_remote "container runlabel is not supported for remote" + tmpdir=$PODMAN_TMPDIR/runlabel-test + mkdir -p $tmpdir + containerfile=$tmpdir/Containerfile + rand1=$(random_string 30) + rand2=$(random_string 30) + rand3=$(random_string 30) + cat >$containerfile <<EOF +FROM $IMAGE +LABEL INSTALL /usr/bin/podman run -t -i --rm \\\${OPT1} --privileged -v /:/host --net=host --ipc=host --pid=host -e HOST=/host -e NAME=\\\${NAME} -e IMAGE=\\\${IMAGE} -e CONFDIR=/etc/\\\${NAME} -e LOGDIR=/var/log/\\\${NAME} -e DATADIR=/var/lib/\\\${NAME} \\\${IMAGE} \\\${OPT2} /bin/install.sh \\\${OPT3} +EOF + + run_podman build -t runlabel_image $tmpdir + + run_podman container runlabel --opt1=${rand1} --opt2=${rand2} --opt3=${rand3} --name test1 --display install runlabel_image + is "$output" "command: ${PODMAN} run -t -i --rm ${rand1} --privileged -v /:/host --net=host --ipc=host --pid=host -e HOST=/host -e NAME=test1 -e IMAGE=localhost/runlabel_image:latest -e CONFDIR=/etc/test1 -e LOGDIR=/var/log/test1 -e DATADIR=/var/lib/test1 localhost/runlabel_image:latest ${rand2} /bin/install.sh ${rand3}" "generating runlabel install command" + + run_podman container runlabel --opt3=${rand3} --display install runlabel_image + is "$output" "command: ${PODMAN} run -t -i --rm --privileged -v /:/host --net=host --ipc=host --pid=host -e HOST=/host -e NAME=runlabel_image -e IMAGE=localhost/runlabel_image:latest -e CONFDIR=/etc/runlabel_image -e LOGDIR=/var/log/runlabel_image -e DATADIR=/var/lib/runlabel_image localhost/runlabel_image:latest /bin/install.sh ${rand3}" "generating runlabel without name and --opt1, --opt2" + + run_podman 125 container runlabel --opt1=${rand1} --opt2=${rand2} --opt3=${rand3} --name test1 --display run runlabel_image + is "$output" "Error: cannot find the value of label: run in image: runlabel_image" "generating runlabel run command" + + run_podman rmi -f runlabel_image +} + +# vim: filetype=sh |