Skip to content

Commit c12ff85

Browse files
committed
fix: --pull option in build and up command can not set to pull policy.
Signed-off-by: Songmin Li <lisongmin@protonmail.com>
1 parent e8db551 commit c12ff85

File tree

3 files changed

+56
-22
lines changed

3 files changed

+56
-22
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed `--pull` option in `build` and `up` command can not set to pull policy explicitly.

podman_compose.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from typing import Any
3232
from typing import Callable
3333
from typing import Iterable
34+
from typing import Sequence
3435
from typing import Union
3536
from typing import overload
3637

@@ -1248,7 +1249,7 @@ async def container_to_args(
12481249
podman_args.extend(["--pid", cnt["pid"]])
12491250
pull_policy = cnt.get("pull_policy")
12501251
if pull_policy is not None and pull_policy != "build":
1251-
podman_args.extend(["--pull", pull_policy])
1252+
podman_args.append(f"--pull={pull_policy}")
12521253
if cnt.get("restart") is not None:
12531254
podman_args.extend(["--restart", cnt["restart"]])
12541255
container_to_ulimit_args(cnt, podman_args)
@@ -2948,10 +2949,11 @@ def cleanup_temp_dockfile() -> None:
29482949
container_to_ulimit_build_args(cnt, build_args)
29492950
if getattr(args, "no_cache", None):
29502951
build_args.append("--no-cache")
2951-
if getattr(args, "pull_always", None):
2952-
build_args.append("--pull-always")
2953-
elif getattr(args, "pull", None):
2954-
build_args.append("--pull")
2952+
2953+
pull_policy = getattr(args, "pull", None)
2954+
if pull_policy:
2955+
build_args.append(f"--pull={pull_policy}")
2956+
29552957
args_list = norm_as_list(build_desc.get("args", {}))
29562958
for build_arg in args_list + args.build_arg:
29572959
build_args.extend((
@@ -4138,18 +4140,40 @@ def compose_ps_parse(parser: argparse.ArgumentParser) -> None:
41384140
parser.add_argument("-q", "--quiet", help="Only display container IDs", action="store_true")
41394141

41404142

4143+
class PullPolicyAction(argparse.Action):
4144+
def __call__(
4145+
self,
4146+
parser: argparse.ArgumentParser,
4147+
namespace: argparse.Namespace,
4148+
values: str | Sequence[str] | None,
4149+
option_string: str | None = None,
4150+
) -> None:
4151+
if option_string == "--pull-always":
4152+
if values in (None, "true"):
4153+
namespace.pull = "always"
4154+
4155+
return
4156+
4157+
namespace.pull = "newer" if values is None else values
4158+
4159+
41414160
@cmd_parse(podman_compose, ["build", "up"])
41424161
def compose_build_up_parse(parser: argparse.ArgumentParser) -> None:
41434162
parser.add_argument(
41444163
"--pull",
4145-
help="attempt to pull a newer version of the image",
4146-
action="store_true",
4164+
help="Pull image policy (always|missing|never|newer)."
4165+
" Set to 'newer' if specify --pull without a value."
4166+
" default (pull_policy in compose file).",
4167+
action=PullPolicyAction,
4168+
nargs="?",
4169+
choices=["always", "missing", "never", "newer"],
41474170
)
41484171
parser.add_argument(
41494172
"--pull-always",
4150-
help="attempt to pull a newer version of the image, Raise an error even if the image is "
4151-
"present locally.",
4152-
action="store_true",
4173+
help="Deprecated, use --pull=always instead",
4174+
action=PullPolicyAction,
4175+
nargs="?",
4176+
choices=["true", "false"],
41534177
)
41544178
parser.add_argument(
41554179
"--build-arg",

tests/unit/test_container_to_build_args.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def get_minimal_container():
3131
def get_minimal_args():
3232
args = mock.Mock()
3333
args.build_arg = []
34+
args.pull = None
3435
return args
3536

3637

@@ -50,7 +51,6 @@ def test_minimal(self):
5051
'-t',
5152
'new-image',
5253
'--no-cache',
53-
'--pull-always',
5454
'.',
5555
],
5656
)
@@ -73,7 +73,6 @@ def test_platform(self):
7373
'--platform',
7474
'linux/amd64',
7575
'--no-cache',
76-
'--pull-always',
7776
'.',
7877
],
7978
)
@@ -98,7 +97,6 @@ def test_tags(self):
9897
'-t',
9998
'some-tag2:2',
10099
'--no-cache',
101-
'--pull-always',
102100
'.',
103101
],
104102
)
@@ -123,7 +121,6 @@ def test_labels(self):
123121
'--label',
124122
'some-label2.2',
125123
'--no-cache',
126-
'--pull-always',
127124
'.',
128125
],
129126
)
@@ -145,7 +142,6 @@ def test_caches(self):
145142
'-t',
146143
'new-image',
147144
'--no-cache',
148-
'--pull-always',
149145
'--cache-from',
150146
'registry/image1',
151147
'--cache-from',
@@ -195,7 +191,6 @@ def test_context_git_url(self):
195191
'-t',
196192
'new-image',
197193
'--no-cache',
198-
'--pull-always',
199194
'https://github.com/test_repo.git',
200195
],
201196
)
@@ -228,7 +223,6 @@ def test_build_ssh_absolute_path(self):
228223
'--ssh',
229224
'id1=/test1',
230225
'--no-cache',
231-
'--pull-always',
232226
'.',
233227
],
234228
)
@@ -251,7 +245,6 @@ def test_build_ssh_relative_path(self):
251245
'--ssh',
252246
'id1=test_dirname/id1/test1',
253247
'--no-cache',
254-
'--pull-always',
255248
'.',
256249
],
257250
)
@@ -274,7 +267,6 @@ def test_build_ssh_working_dir(self):
274267
'--ssh',
275268
'id1=test_dirname/./test1',
276269
'--no-cache',
277-
'--pull-always',
278270
'.',
279271
],
280272
)
@@ -298,7 +290,6 @@ def test_build_ssh_path_home_dir(self):
298290
'--ssh',
299291
'id1=/home/user/test1',
300292
'--no-cache',
301-
'--pull-always',
302293
'.',
303294
],
304295
)
@@ -323,7 +314,6 @@ def test_build_ssh_map(self):
323314
'--ssh',
324315
'id2=test_dirname/test2',
325316
'--no-cache',
326-
'--pull-always',
327317
'.',
328318
],
329319
)
@@ -348,7 +338,26 @@ def test_build_ssh_array(self):
348338
'--ssh',
349339
'id2=test_dirname/test2',
350340
'--no-cache',
351-
'--pull-always',
341+
'.',
342+
],
343+
)
344+
345+
def test_pull_always(self):
346+
c = create_compose_mock()
347+
cnt = get_minimal_container()
348+
args = get_minimal_args()
349+
args.pull = 'always'
350+
351+
args = container_to_build_args(c, cnt, args, lambda path: True)
352+
self.assertEqual(
353+
args,
354+
[
355+
'-f',
356+
'Containerfile',
357+
'-t',
358+
'new-image',
359+
'--no-cache',
360+
'--pull=always',
352361
'.',
353362
],
354363
)

0 commit comments

Comments
 (0)