Skip to content

Commit 6c9e21c

Browse files
committed
Improvements for building one-off Docker images with cb-docker-tool
* Add --docker-load to build an image in the local docker * Allow --write-context to honor --from-latestbuilds Change-Id: I570f25a876be1cc305d83808ab3457acb7af1f90 Reviewed-on: https://review.couchbase.org/c/build-tools/+/193688 Tested-by: Chris Hillery <[email protected]> Reviewed-by: Blair Watt <[email protected]>
1 parent 5d715dc commit 6c9e21c

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

dockerhub/cb-docker-tool

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ class DockerTool:
2424
platform: str
2525
imagenames: List[str]
2626
buildargs: List[str]
27+
templateoverrides: List[str]
2728
gitrepo: Optional[str]
2829
gitdir: Optional[pathlib.Path]
2930
contextdir: Optional[pathlib.Path]
3031
do_docker_build: bool
3132
do_docker_push: bool
33+
do_docker_load: bool
3234
do_docker_save: bool
3335
do_update_github: bool
3436
force_update_github: bool
@@ -48,11 +50,13 @@ class DockerTool:
4850
self.platform = self._setup_platform(platform)
4951
self.imagenames = []
5052
self.buildargs = []
53+
self.templateoverrides = []
5154
self.gitrepo = githubrepo
5255
self.gitdir = None
5356
self.contextdir = None
5457
self.do_docker_build = False
5558
self.do_docker_push = False
59+
self.do_docker_load = False
5660
self.do_docker_save = False
5761
self.do_update_github = False
5862
self.force_update_github = False
@@ -105,6 +109,11 @@ class DockerTool:
105109
self.do_docker_push = True
106110

107111

112+
def will_docker_load(self) -> None:
113+
self.do_docker_build = True
114+
self.do_docker_load = True
115+
116+
108117
def will_docker_save(self, tarfile: str) -> None:
109118
# Want to keep the absolute path
110119
self.docker_save_file = str(pathlib.Path(tarfile).resolve())
@@ -207,14 +216,14 @@ class DockerTool:
207216
return
208217
version, bldnum = self.version.split('-')
209218
if self.product == "sync-gateway":
210-
self.add_buildargs([
219+
self.add_templateoverrides([
211220
"SGW_PACKAGE=http://latestbuilds.service.couchbase.com/builds/"
212221
f"latestbuilds/{self.product.replace('-','_')}/{version}/"
213222
f"{bldnum}/couchbase-{self.product}-{self.edition}_{version}"
214223
f"-{bldnum}_@@ARCH@@.deb"
215224
])
216225
else:
217-
self.add_buildargs([
226+
self.add_templateoverrides([
218227
"CB_SKIP_CHECKSUM=true",
219228
"CB_RELEASE_URL=http://latestbuilds.service.couchbase.com/builds/"
220229
f"latestbuilds/{self.product}/zz-versions/{version}/{bldnum}"
@@ -229,6 +238,14 @@ class DockerTool:
229238
self.buildargs.extend(buildargs)
230239

231240

241+
def add_templateoverrides(self, templateargs: List[str]) -> None:
242+
"""
243+
Specify overrides to Dockerfile template arguments
244+
"""
245+
246+
self.templateoverrides.extend(templateargs)
247+
248+
232249
def check_ready(self) -> Optional[str]:
233250
"""
234251
Performs additional argument validation depending on the chosen
@@ -246,6 +263,7 @@ class DockerTool:
246263

247264
if not any([
248265
self.do_docker_push,
266+
self.do_docker_load,
249267
self.do_docker_save,
250268
self.do_update_github,
251269
self.contextdir is not None
@@ -254,13 +272,17 @@ class DockerTool:
254272

255273
if len(self.imagenames) == 0 and any([
256274
self.do_docker_save,
275+
self.do_docker_load,
257276
self.do_docker_push
258277
]):
259278
return "No image names specified for Docker push/save action!"
260279

261280
if self.do_update_github and self.contextdir is not None:
262281
return "--update-github may not be combined with --write-context"
263282

283+
if self.do_docker_load and ',' in self.platform:
284+
return "--docker-load can only work with a single platform"
285+
264286
if self.do_docker_save and ',' in self.platform and '%P' not in self.docker_save_file:
265287
return (
266288
"This product/version supports multiple platforms; either specify a "
@@ -460,6 +482,8 @@ class DockerTool:
460482
"--edition", self.edition,
461483
"-o", self.contextdir
462484
]
485+
for override in self.templateoverrides:
486+
gencmd.extend(["-t", override])
463487
logging.info(
464488
f"Running Docker context generation into {self.contextdir}"
465489
)
@@ -507,7 +531,8 @@ class DockerTool:
507531
else:
508532
logging.info("...Will push images to registries")
509533
buildcmd.append("--push")
510-
534+
if self.do_docker_load:
535+
buildcmd.extend(["--load"])
511536
self.run(buildcmd)
512537
logging.info(f"'docker buildx build' complete!")
513538

@@ -617,6 +642,10 @@ if __name__ == "__main__":
617642
"--docker-push", action="store_true",
618643
help="Push all resulting images to their registries"
619644
)
645+
actions.add_argument(
646+
"--docker-load", action="store_true",
647+
help="Load resulting image into local docker (must be single-arch)"
648+
)
620649
actions.add_argument(
621650
"--docker-save", type=str, metavar="TARFILE",
622651
help="Save the resulting image to a tarfile (includes .sha256 file); %%P "
@@ -743,6 +772,8 @@ if __name__ == "__main__":
743772
# Prepare actions
744773
if args.docker_push:
745774
tool.will_docker_push()
775+
if args.docker_load:
776+
tool.will_docker_load()
746777
if args.docker_save is not None:
747778
tool.will_docker_save(args.docker_save)
748779
if args.update_github:

0 commit comments

Comments
 (0)