...
1#!/hint/python3
2
3import os
4import shlex
5import subprocess
6from contextlib import contextmanager
7from typing import Generator, Iterable, List
8
9from .uiutil import run as _run
10from .uiutil import run_txtcapture
11
12
13def run(args: List[str], /) -> None:
14 print("$ " + (" ".join(shlex.quote(arg) for arg in args)))
15 _run(args)
16
17
18@contextmanager
19def gcr_login() -> Generator[None, None, None]:
20 key = run_txtcapture(
21 ['keybase', 'fs', 'read', '/keybase/team/datawireio/secrets/googlecloud.gcr-ci-robot.datawire.json.key'])
22
23 subprocess.run(
24 ['gcloud', 'auth', 'activate-service-account', '--key-file=-'],
25 check=True,
26 text=True,
27 input=key,)
28 subprocess.run(['gcloud', 'auth', 'configure-docker'], check=True)
29 yield
30 subprocess.run(['docker', 'logout', 'https://gcr.io'], check=True)
31
32
33default_repos = {
34 'docker.io/emissaryingress/emissary',
35 'gcr.io/datawire/emissary',
36}
37
38default_source_repo = 'docker.io/emissaryingress/emissary'
39
40
41def enumerate_images(*, repos: Iterable[str] = default_repos, tag: str) -> Iterable[str]:
42 return [f"{repo}:{tag}" for repo in repos]
43
44
45def mirror_images(*, repos: Iterable[str] = default_repos, tag: str, source_repo: str = default_source_repo) -> None:
46 print('Note: This script can be rerun.')
47 print('If pushes to registries fail, you can rerun the command in your terminal to debug.')
48 print('If pushes fail, it might be a credentials problem with gcr or quay.io or an issue with your gcloud installation.')
49
50 with gcr_login():
51 src = f'{source_repo}:{tag}'
52 dsts = enumerate_images(repos=repos, tag=tag)
53
54 run(['docker', 'pull', src])
55 for dst in dsts:
56 if dst == src:
57 continue
58 run(['docker', 'tag', src, dst])
59 run(['docker', 'push', dst])
View as plain text