-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.py
executable file
·83 lines (71 loc) · 3.49 KB
/
publish.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
import sys, subprocess, tempfile
from pathlib import Path
if len(sys.argv) < 2:
sys.exit("publish.sh must be called by make, not run directly.")
import env
def run(cmd, **kwargs):
return subprocess.run(cmd, check=True, shell=True, **kwargs)
def get_node_models_of_image(image):
result = run(
"""docker inspect --format '{{ index .Config.Labels "walt.node.models"}}' """ + image,
stdout=subprocess.PIPE).stdout
return result.decode('ascii').strip().split(',')
def get_base_image_description(os_type, model_type, desc_type):
os_type_label = env.OS_TYPE_LABEL[os_type]
board_models_label = env.LABELS_PER_BOARD_MODEL[model_type][desc_type]
return env.BASE_IMAGE_SHORT_DESC_TEMPLATES[desc_type].format(
os_type=os_type_label,
board_models=board_models_label)
def get_featured_image_description(image_name, desc_type):
return env.FEATURED_IMAGE_SHORT_DESC_TEMPLATES[image_name][desc_type]
# This function updates the short description and the long one (README section)
# of an image on the docker hub by running a docker container chko/docker-pushrm
# designed for this purpose.
def publish_description(hub_repo_name, short_desc, long_desc):
with tempfile.TemporaryDirectory() as tmp_d:
readme_text = env.IMAGE_OVERALL_TEMPLATE.format(image_description = long_desc)
readme_path = Path(tmp_d) / 'README.md'
readme_path.write_text(readme_text)
result = run(
f"""docker run --rm -t -v {readme_path}:/tmp/README.md \
-e DOCKER_USER="$DOCKER_USER" -e DOCKER_PASS="$DOCKER_PASSWORD" \
chko/docker-pushrm:1 \
--file /tmp/README.md \
--short "{short_desc}" \
--debug \
docker.io/{hub_repo_name}""")
image_descriptor = sys.argv[1].split('.')
if image_descriptor[0] == 'featured':
name = image_descriptor[1]
run(f'docker push waltplatform/{name}:latest')
publish_description(f'waltplatform/{name}',
get_featured_image_description(name, 'short'),
get_featured_image_description(name, 'long'))
else:
os_type, model_type = image_descriptor
os_version = env.OS_VERSIONS[os_type]
hub_repo_name = f"waltplatform/{model_type}-{os_type}"
image_latest = f"{hub_repo_name}:latest"
image_version = f"{hub_repo_name}:{os_version}"
# publish
run(f'docker push {image_latest}')
run(f'docker push {image_version}')
publish_description(hub_repo_name,
get_base_image_description(os_type, model_type, 'short'),
get_base_image_description(os_type, model_type, 'long'))
# if the OS is the default one for a board model, also tag the image
# with 'waltplatform/<model>-default' and push it
if model_type == 'rpi':
models = get_node_models_of_image(image_latest)
else:
models = [ model_type ]
for model in models:
if os_type == env.DEFAULT_OS_TYPE_PER_BOARD_MODEL[model]:
hub_repo_name = f"waltplatform/{model}-default"
image_model_default = f"{hub_repo_name}:latest"
run(f'docker tag {image_latest} {image_model_default}')
run(f'docker push {image_model_default}')
publish_description(hub_repo_name,
get_base_image_description('default', model, 'short'),
get_base_image_description('default', model, 'long'))