mirror of
https://github.com/containers/podman-compose.git
synced 2025-05-29 14:41:21 +02:00
Merge pull request #860 from aripollak/fix-env-vars-with-equals
Allow run/exec -e with equals sign in value
This commit is contained in:
commit
ee013316c0
@ -2455,6 +2455,18 @@ async def compose_run(compose, args):
|
|||||||
)
|
)
|
||||||
await compose.commands["build"](compose, build_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
|
# adjust one-off container options
|
||||||
name0 = "{}_{}_tmp{}".format(compose.project_name, args.service, random.randrange(0, 65536))
|
name0 = "{}_{}_tmp{}".format(compose.project_name, args.service, random.randrange(0, 65536))
|
||||||
cnt["name"] = args.name or name0
|
cnt["name"] = args.name or name0
|
||||||
@ -2466,7 +2478,7 @@ async def compose_run(compose, args):
|
|||||||
cnt["working_dir"] = args.workdir
|
cnt["working_dir"] = args.workdir
|
||||||
env = dict(cnt.get("environment", {}))
|
env = dict(cnt.get("environment", {}))
|
||||||
if args.env:
|
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)
|
env.update(additional_env_vars)
|
||||||
cnt["environment"] = env
|
cnt["environment"] = env
|
||||||
if not args.service_ports:
|
if not args.service_ports:
|
||||||
@ -2486,14 +2498,6 @@ async def compose_run(compose, args):
|
|||||||
# can't restart and --rm
|
# can't restart and --rm
|
||||||
if args.rm and "restart" in cnt:
|
if args.rm and "restart" in cnt:
|
||||||
del cnt["restart"]
|
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")
|
@cmd_run(podman_compose, "exec", "execute a command in a running container")
|
||||||
@ -2502,6 +2506,12 @@ async def compose_exec(compose, args):
|
|||||||
container_names = compose.container_names_by_service[args.service]
|
container_names = compose.container_names_by_service[args.service]
|
||||||
container_name = container_names[args.index - 1]
|
container_name = container_names[args.index - 1]
|
||||||
cnt = compose.container_by_name[container_name]
|
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"]
|
podman_args = ["--interactive"]
|
||||||
if args.privileged:
|
if args.privileged:
|
||||||
podman_args += ["--privileged"]
|
podman_args += ["--privileged"]
|
||||||
@ -2514,7 +2524,7 @@ async def compose_exec(compose, args):
|
|||||||
env = dict(cnt.get("environment", {}))
|
env = dict(cnt.get("environment", {}))
|
||||||
if args.env:
|
if args.env:
|
||||||
additional_env_vars = dict(
|
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)
|
env.update(additional_env_vars)
|
||||||
for name, value in env.items():
|
for name, value in env.items():
|
||||||
@ -2522,8 +2532,7 @@ async def compose_exec(compose, args):
|
|||||||
podman_args += [container_name]
|
podman_args += [container_name]
|
||||||
if args.cnt_command is not None and len(args.cnt_command) > 0:
|
if args.cnt_command is not None and len(args.cnt_command) > 0:
|
||||||
podman_args += args.cnt_command
|
podman_args += args.cnt_command
|
||||||
p = await compose.podman.run([], "exec", podman_args)
|
return podman_args
|
||||||
sys.exit(p)
|
|
||||||
|
|
||||||
|
|
||||||
async def transfer_service_status(compose, args, action):
|
async def transfer_service_status(compose, args, action):
|
||||||
|
46
pytests/test_compose_exec_args.py
Normal file
46
pytests/test_compose_exec_args.py
Normal file
@ -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
pytests/test_compose_run_update_container_from_args.py
Normal file
60
pytests/test_compose_run_update_container_from_args.py
Normal file
@ -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…
x
Reference in New Issue
Block a user