From 10b5740f390d0696939c86497076371d5edbf291 Mon Sep 17 00:00:00 2001 From: YuviPanda Date: Wed, 23 Aug 2023 12:56:56 -0700 Subject: [PATCH] Autogenerate useful tags for various built images --- images/build.py | 94 +++++++++++++++++++++++++++++++++--- images/common-tags-generator | 21 ++++++++ images/qgis/tags-generator | 14 ++++++ 3 files changed, 123 insertions(+), 6 deletions(-) create mode 100755 images/common-tags-generator create mode 100755 images/qgis/tags-generator diff --git a/images/build.py b/images/build.py index eb04a173..0479b7df 100755 --- a/images/build.py +++ b/images/build.py @@ -3,15 +3,19 @@ Build and push all the images we maintain """ import argparse +import secrets import subprocess +from pathlib import Path IMAGES = { "base": None, "qgis": "base", } +HERE = Path(__file__).parent -def build(name: str, tag: str, build_args: dict, platform: str, push: bool): + +def build(name: str, tag: str, build_args: dict, platform: str): cmd = [ 'docker', 'buildx', @@ -24,15 +28,80 @@ def build(name: str, tag: str, build_args: dict, platform: str, push: bool): for key, value in build_args.items(): cmd += ['--build-arg', f'{key}={value}'] - if push: - cmd += ['--push'] - cmd.append(name) print(cmd) subprocess.check_call(cmd) +def get_tags(image: str, base_image_spec: str): + container_name = secrets.token_hex(8) + try: + run_cmd = [ + 'docker', + 'container', + 'run', + '--detach', + '--name', + container_name, + base_image_spec, + ] + subprocess.check_call(run_cmd) + + subprocess.check_call( + [ + 'docker', + 'container', + 'ls', + ] + ) + + tag_generator_scripts = [ + str(f) + for f in [ + HERE / "common-tags-generator", + HERE / image / "tags-generator", + ] + if f.exists() + ] + + tags = set() + + for tgs in tag_generator_scripts: + subprocess.check_call( + [ + 'docker', + 'container', + 'cp', + str(tgs), + f'{container_name}:/tmp/tags-generator', + ] + ) + + tags.update( + set( + subprocess.check_output( + [ + 'docker', + 'container', + 'exec', + container_name, + '/tmp/tags-generator', + ] + ) + .decode() + .strip() + .split('\n') + ) + ) + + print(tags) + return tags + + finally: + subprocess.check_call(['docker', 'container', 'stop', container_name]) + + def images_to_build(name: str, image_dependencies_graph: dict[str, str]) -> list[str]: parent = image_dependencies_graph[name] @@ -80,9 +149,22 @@ def main(): to_build = images_to_build(args.image, IMAGES) for image in to_build: - tag = f"{args.image_prefix}{image}" + base_image_spec = f"{args.image_prefix}{image}" + + build(image, base_image_spec, build_args, args.platforms) + + tags = get_tags(image, base_image_spec) + + for t in tags: + image_spec = f"{base_image_spec}:{t}" + subprocess.check_call( + ['docker', 'image', 'tag', base_image_spec, image_spec] + ) + print(f'Tagged {image_spec}') - build(image, tag, build_args, args.platforms, args.push) + if args.push: + subprocess.check_call(['docker', 'image', 'push', image_spec]) + print(f'Pushed {image_spec}') main() diff --git a/images/common-tags-generator b/images/common-tags-generator new file mode 100755 index 00000000..c1e2cff0 --- /dev/null +++ b/images/common-tags-generator @@ -0,0 +1,21 @@ +#!/usr/bin/env python +import subprocess +import sys +from datetime import date + +# Today's date should be a tag +print(date.today().isoformat()) + +# Python version inside should be a tag +python_version_string = f"{sys.version_info.major}.{sys.version_info.minor}" +print(f"python-{python_version_string}") + +# Ubuntu version should be a tag +ubuntu_version = ( + subprocess.check_output( + ["/bin/bash", "-c", "source /etc/os-release; echo $VERSION_ID"] + ) + .decode() + .strip() +) +print(f"ubuntu-{ubuntu_version}") diff --git a/images/qgis/tags-generator b/images/qgis/tags-generator new file mode 100755 index 00000000..42fd0574 --- /dev/null +++ b/images/qgis/tags-generator @@ -0,0 +1,14 @@ +#!/usr/bin/env python +import json +import subprocess + +# Ubuntu version should be a tag +qgis_version = json.loads( + subprocess.check_output(["mamba", "list", "qgis", "--json"]).decode().strip() +)[0]['version'] + + +print(f"qgis-{qgis_version}") +# Provide a shortened, major.minor version too +shortened_version = '.'.join(qgis_version.split('.')[0:2]) +print(f"qgis-{shortened_version}")