Skip to content

Commit

Permalink
Autogenerate useful tags for various built images
Browse files Browse the repository at this point in the history
  • Loading branch information
yuvipanda committed Aug 23, 2023
1 parent 7211885 commit 10b5740
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 6 deletions.
94 changes: 88 additions & 6 deletions images/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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]

Expand Down Expand Up @@ -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()
21 changes: 21 additions & 0 deletions images/common-tags-generator
Original file line number Diff line number Diff line change
@@ -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}")
14 changes: 14 additions & 0 deletions images/qgis/tags-generator
Original file line number Diff line number Diff line change
@@ -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}")

0 comments on commit 10b5740

Please sign in to comment.