-
Notifications
You must be signed in to change notification settings - Fork 3
/
template.py
executable file
·122 lines (108 loc) · 3.39 KB
/
template.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
"""Build and optionally push images."""
import argparse
import re
import shutil
import subprocess
from pathlib import Path
from ocflib.misc.shell import bg_green
from ocflib.misc.shell import bg_yellow
from ocflib.misc.whoami import current_user
from jinja2 import Environment
from jinja2 import FileSystemLoader
def report(line, color=bg_yellow):
print(color(line))
DEFAULT_IMAGE_OPTIONS = {
'dumb_init_version': '1.2.5',
'packages': {
'ca-certificates',
'curl',
'gnupg',
'heimdal-clients',
'htop',
'ldap-utils',
'less',
'libnss-ldap',
'libsasl2-modules',
'libsasl2-modules-gssapi-mit',
'locales',
'net-tools',
'man-db',
'manpages',
'procps',
'ssmtp',
'sudo',
'vim-tiny',
},
'ldap': True,
'kerberos': True,
'mail': True,
'utils': True,
}
IMAGES = {
'debian:bullseye': {
'base': 'debian:bullseye',
'ocf_apt_repo_dist': 'bullseye',
'backport_dist': 'bullseye-backports',
},
'debian:bullseye-py': {
'base': 'python:3.7-slim-bullseye',
'ocf_apt_repo_dist': 'bullseye',
'backport_dist': 'bullseye-backports',
},
'debian:bookworm': {
'base': 'debian:bookworm',
'ocf_apt_repo_dist': 'bookworm',
'backport_dist': 'bookworm-backports',
},
}
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
'--push',
default=False,
action='store_true',
help=(
"Push images to DockerHub and OCF's internal registry. "
'With this option, --no-cache is used to ensure we avoid stale layers. '
'This will require you to be logged in with `docker login`.'
),
)
args = parser.parse_args()
env = Environment(loader=FileSystemLoader('.'))
template = env.get_template('Dockerfile.in')
build = Path('build')
if not build.is_dir():
build.mkdir()
for tag, params in IMAGES.items():
report('Templating out {}'.format(tag))
assert re.match(r'^[a-z\-:]+$', tag), tag
p = build / tag
if p.is_dir():
shutil.rmtree(str(p))
shutil.copytree('include', str(p))
with (p / 'Dockerfile').open('w') as f:
f.write(template.render(**dict(DEFAULT_IMAGE_OPTIONS, **params)) + '\n')
temp_tag = '{}-test-{}'.format(current_user(), tag)
report('Building {} (as {})'.format(tag, temp_tag))
subprocess.check_call(
('docker', 'build') +
(('--no-cache',) if args.push else ()) +
('-t', temp_tag, str(p))
)
report(
'Successfully built {} (as {})'.format(tag, temp_tag),
color=bg_green,
)
if args.push:
for new_tag in ('theocf/{}', 'docker-push.ocf.berkeley.edu/theocf/{}'):
new_tag = new_tag.format(tag)
report('Tagging {} as {} for push'.format(temp_tag, new_tag))
subprocess.check_call((
'docker', 'tag', temp_tag, new_tag,
))
subprocess.check_call((
'docker', 'push', new_tag,
))
report('Sucessfully pushed {}'.format(new_tag), color=bg_green)
report('All images built!', color=bg_green)