Skip to content

Commit a1f3bef

Browse files
authored
Merge pull request #1295 from lisongmin/fix-pull-can-not-set-to-pull-policy
fix: `--pull` option in `build` and `up` command can not set to pull policy
2 parents a38a34a + 909355d commit a1f3bef

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
@@ -32,6 +32,7 @@
3232
from typing import Any
3333
from typing import Callable
3434
from typing import Iterable
35+
from typing import Sequence
3536
from typing import Union
3637
from typing import overload
3738

@@ -1249,7 +1250,7 @@ async def container_to_args(
12491250
podman_args.extend(["--pid", cnt["pid"]])
12501251
pull_policy = cnt.get("pull_policy")
12511252
if pull_policy is not None and pull_policy != "build":
1252-
podman_args.extend(["--pull", pull_policy])
1253+
podman_args.append(f"--pull={pull_policy}")
12531254
if cnt.get("restart") is not None:
12541255
podman_args.extend(["--restart", cnt["restart"]])
12551256
container_to_ulimit_args(cnt, podman_args)
@@ -3011,10 +3012,11 @@ def cleanup_temp_dockfile() -> None:
30113012
container_to_ulimit_build_args(cnt, build_args)
30123013
if getattr(args, "no_cache", None):
30133014
build_args.append("--no-cache")
3014-
if getattr(args, "pull_always", None):
3015-
build_args.append("--pull-always")
3016-
elif getattr(args, "pull", None):
3017-
build_args.append("--pull")
3015+
3016+
pull_policy = getattr(args, "pull", None)
3017+
if pull_policy:
3018+
build_args.append(f"--pull={pull_policy}")
3019+
30183020
args_list = norm_as_list(build_desc.get("args", {}))
30193021
for build_arg in args_list + args.build_arg:
30203022
build_args.extend((
@@ -4215,18 +4217,40 @@ def compose_ps_parse(parser: argparse.ArgumentParser) -> None:
42154217
parser.add_argument("-q", "--quiet", help="Only display container IDs", action="store_true")
42164218

42174219

4220+
class PullPolicyAction(argparse.Action):
4221+
def __call__(
4222+
self,
4223+
parser: argparse.ArgumentParser,
4224+
namespace: argparse.Namespace,
4225+
values: str | Sequence[str] | None,
4226+
option_string: str | None = None,
4227+
) -> None:
4228+
if option_string == "--pull-always":
4229+
if values in (None, "true"):
4230+
namespace.pull = "always"
4231+
4232+
return
4233+
4234+
namespace.pull = "newer" if values is None else values
4235+
4236+
42184237
@cmd_parse(podman_compose, ["build", "up"])
42194238
def compose_build_up_parse(parser: argparse.ArgumentParser) -> None:
42204239
parser.add_argument(
42214240
"--pull",
4222-
help="attempt to pull a newer version of the image",
4223-
action="store_true",
4241+
help="Pull image policy (always|missing|never|newer)."
4242+
" Set to 'newer' if specify --pull without a value."
4243+
" default (pull_policy in compose file).",
4244+
action=PullPolicyAction,
4245+
nargs="?",
4246+
choices=["always", "missing", "never", "newer"],
42244247
)
42254248
parser.add_argument(
42264249
"--pull-always",
4227-
help="attempt to pull a newer version of the image, Raise an error even if the image is "
4228-
"present locally.",
4229-
action="store_true",
4250+
help="Deprecated, use --pull=always instead",
4251+
action=PullPolicyAction,
4252+
nargs="?",
4253+
choices=["true", "false"],
42304254
)
42314255
parser.add_argument(
42324256
"--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)