Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow run/exec -e with equals sign in value #860

Merged
merged 3 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions podman_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -2455,6 +2455,18 @@ async def compose_run(compose, args):
)
await compose.commands["build"](compose, build_args)

compose_run_update_container_from_args(compose, cnt, args)
# run podman
podman_args = await container_to_args(compose, cnt, args.detach)
if not args.detach:
podman_args.insert(1, "-i")
if args.rm:
podman_args.insert(1, "--rm")
p = await compose.podman.run([], "run", podman_args)
sys.exit(p)


def compose_run_update_container_from_args(compose, cnt, args):
# adjust one-off container options
name0 = "{}_{}_tmp{}".format(compose.project_name, args.service, random.randrange(0, 65536))
cnt["name"] = args.name or name0
Expand All @@ -2466,7 +2478,7 @@ async def compose_run(compose, args):
cnt["working_dir"] = args.workdir
env = dict(cnt.get("environment", {}))
if args.env:
additional_env_vars = dict(map(lambda each: each.split("="), args.env))
additional_env_vars = dict(map(lambda each: each.split("=", maxsplit=1), args.env))
env.update(additional_env_vars)
cnt["environment"] = env
if not args.service_ports:
Expand All @@ -2486,14 +2498,6 @@ async def compose_run(compose, args):
# can't restart and --rm
if args.rm and "restart" in cnt:
del cnt["restart"]
# run podman
podman_args = await container_to_args(compose, cnt, args.detach)
if not args.detach:
podman_args.insert(1, "-i")
if args.rm:
podman_args.insert(1, "--rm")
p = await compose.podman.run([], "run", podman_args)
sys.exit(p)


@cmd_run(podman_compose, "exec", "execute a command in a running container")
Expand All @@ -2502,6 +2506,12 @@ async def compose_exec(compose, args):
container_names = compose.container_names_by_service[args.service]
container_name = container_names[args.index - 1]
cnt = compose.container_by_name[container_name]
podman_args = compose_exec_args(cnt, container_name, args)
p = await compose.podman.run([], "exec", podman_args)
sys.exit(p)


def compose_exec_args(cnt, container_name, args):
podman_args = ["--interactive"]
if args.privileged:
podman_args += ["--privileged"]
Expand All @@ -2514,16 +2524,15 @@ async def compose_exec(compose, args):
env = dict(cnt.get("environment", {}))
if args.env:
additional_env_vars = dict(
map(lambda each: each.split("=") if "=" in each else (each, None), args.env)
map(lambda each: each.split("=", maxsplit=1) if "=" in each else (each, None), args.env)
)
env.update(additional_env_vars)
for name, value in env.items():
podman_args += ["--env", f"{name}" if value is None else f"{name}={value}"]
podman_args += [container_name]
if args.cnt_command is not None and len(args.cnt_command) > 0:
podman_args += args.cnt_command
p = await compose.podman.run([], "exec", podman_args)
sys.exit(p)
return podman_args


async def transfer_service_status(compose, args, action):
Expand Down
46 changes: 46 additions & 0 deletions pytests/test_compose_exec_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# SPDX-License-Identifier: GPL-2.0

import argparse
import unittest

from podman_compose import compose_exec_args


class TestExecArgs(unittest.TestCase):
def test_minimal(self):
cnt = get_minimal_container()
args = get_minimal_args()

result = compose_exec_args(cnt, "container_name", args)
expected = ["--interactive", "--tty", "container_name"]
self.assertEqual(result, expected)

def test_additional_env_value_equals(self):
cnt = get_minimal_container()
args = get_minimal_args()
args.env = ["key=valuepart1=valuepart2"]

result = compose_exec_args(cnt, "container_name", args)
expected = [
"--interactive",
"--tty",
"--env",
"key=valuepart1=valuepart2",
"container_name",
]
self.assertEqual(result, expected)


def get_minimal_container():
return {}


def get_minimal_args():
return argparse.Namespace(
T=None,
cnt_command=None,
env=None,
privileged=None,
user=None,
workdir=None,
)
60 changes: 60 additions & 0 deletions pytests/test_compose_run_update_container_from_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# SPDX-License-Identifier: GPL-2.0

import argparse
import unittest

from podman_compose import PodmanCompose
from podman_compose import compose_run_update_container_from_args


class TestRunArgs(unittest.TestCase):
def test_minimal(self):
cnt = get_minimal_container()
compose = get_minimal_compose()
args = get_minimal_args()

compose_run_update_container_from_args(compose, cnt, args)

expected_cnt = {"name": "default_name", "tty": True}
self.assertEqual(cnt, expected_cnt)

def test_additional_env_value_equals(self):
cnt = get_minimal_container()
compose = get_minimal_compose()
args = get_minimal_args()
args.env = ["key=valuepart1=valuepart2"]

compose_run_update_container_from_args(compose, cnt, args)

expected_cnt = {
"environment": {
"key": "valuepart1=valuepart2",
},
"name": "default_name",
"tty": True,
}
self.assertEqual(cnt, expected_cnt)


def get_minimal_container():
return {}


def get_minimal_compose():
return PodmanCompose()


def get_minimal_args():
return argparse.Namespace(
T=None,
cnt_command=None,
entrypoint=None,
env=None,
name="default_name",
rm=None,
service=None,
service_ports=None,
user=None,
volume=None,
workdir=None,
)
Loading