-
Notifications
You must be signed in to change notification settings - Fork 5
/
generate_dockerfiles.py
executable file
·326 lines (254 loc) · 13.1 KB
/
generate_dockerfiles.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env python3
import requests
import json
import os
def get_repository_tags(url):
response = requests.get(url)
response.raise_for_status()
tags = json.loads(response.text)
return list(tags.keys())
def str2bool(arg):
return arg.lower() in ["true", "1", "yes"]
class Registry:
url = None
version = None
verify_certs = True
def __init__(self, registry_url, verify_certs=True):
self.url = registry_url
self.verify_certs = verify_certs
def __repr__(self):
return "<Registry ({}) {}>".format(self.version, self.url)
def get_tags(self, repository):
raise NotImplementedError()
@classmethod
def create(cls, registry_url, verify_certs=True):
version_lookup = [
{"class": Registry_v2, "url": "https://{}/v2/"},
{"class": Registry_v1, "url": "https://{}/v1/_ping"},
]
for version in version_lookup:
url = version["url"].format(registry_url)
response = requests.get(url, verify=verify_certs)
if response.status_code == 200:
return version["class"](registry_url, verify_certs)
return NotImplemented("registry not recognized")
class Registry_v1(Registry):
def __init__(self, registry_url, verify_certs):
Registry.__init__(self, registry_url, verify_certs)
self.version = "v1"
def get_tags(self, repo):
url = "https://{}/v1/repositories/{}/tags".format(self.url, repo)
response = requests.get(url, verify=self.verify_certs)
response.raise_for_status()
tags = json.loads(response.text)
return list(tags.keys())
class Registry_v2(Registry):
def __init__(self, registry_url, verify_certs):
Registry.__init__(self, registry_url, verify_certs)
self.version = "v2"
def get_tags(self, repo):
url = "https://{}/v2/{}/tags/list".format(self.url, repo)
response = requests.get(url, verify=self.verify_certs)
response.raise_for_status()
tags = json.loads(response.text)["tags"]
return tags
class Repository:
host = ''
namespace = ''
name = ''
tag = ''
def __init__(self, repository_identifier):
domain_split_point = repository_identifier.find('.')
host_split_point = repository_identifier.find('/')
if domain_split_point != -1 and domain_split_point < host_split_point:
self.host = repository_identifier[:host_split_point]
rest = repository_identifier[host_split_point+1:]
else:
self.host = ''
rest = repository_identifier
name_split_point = rest.rfind('/')
if name_split_point == -1:
self.namespace = ''
else:
self.namespace = rest[:name_split_point]
rest = rest[name_split_point+1:]
tag_split_point = rest.find(':')
if tag_split_point == -1:
self.name = rest
self.tag = ''
else:
self.name = rest[:tag_split_point]
self.tag = rest[tag_split_point+1:]
assert self.name != ''
def get_repository_identifier(self):
result = ''
if self.host:
result += self.host + '/'
if self.namespace:
result += self.namespace + '/'
if self.name:
result += self.name
if self.tag:
result += ':' + self.tag
return result
def get_image_full_name(self):
if self.namespace:
return self.namespace + '/' + self.name
else:
return self.name
def get_host_prefix(self):
if self.host:
return self.host + '/'
else:
return ''
def get_image_path(self):
return self.get_host_prefix() + self.get_image_full_name()
def test_class_repository():
repository_identifier = 'debian'
repository = Repository(repository_identifier)
assert repository.host == ''
assert repository.namespace == ''
assert repository.name == 'debian'
assert repository.tag == ''
assert repository.get_repository_identifier() == repository_identifier
assert repository.get_image_full_name() == 'debian'
assert repository.get_image_path() == 'debian'
repository_identifier = 'debian:latest'
repository = Repository(repository_identifier)
assert repository.host == ''
assert repository.namespace == ''
assert repository.name == 'debian'
assert repository.tag == 'latest'
assert repository.get_repository_identifier() == repository_identifier
assert repository.get_image_full_name() == 'debian'
assert repository.get_image_path() == 'debian'
repository_identifier = 'library/debian:latest'
repository = Repository(repository_identifier)
assert repository.host == ''
assert repository.namespace == 'library'
assert repository.name == 'debian'
assert repository.tag == 'latest'
assert repository.get_repository_identifier() == repository_identifier
assert repository.get_image_full_name() == 'library/debian'
assert repository.get_image_path() == 'library/debian'
repository_identifier = 'docker.dev/szn-jessie:latest'
repository = Repository(repository_identifier)
assert repository.host == 'docker.dev'
assert repository.namespace == ''
assert repository.name == 'szn-jessie'
assert repository.tag == 'latest'
assert repository.get_repository_identifier() == repository_identifier
assert repository.get_image_full_name() == 'szn-jessie'
assert repository.get_image_path() == 'docker.dev/szn-jessie'
repository_identifier = 'docker.dev/library/szn-jessie:latest'
repository = Repository(repository_identifier)
assert repository.host == 'docker.dev'
assert repository.namespace == 'library'
assert repository.name == 'szn-jessie'
assert repository.tag == 'latest'
assert repository.get_repository_identifier() == repository_identifier
assert repository.get_image_full_name() == 'library/szn-jessie'
assert repository.get_image_path() == 'docker.dev/library/szn-jessie'
def convert_docker_tag_to_makefile_target(docker_tag):
return docker_tag.replace('/', '__').replace(':', '~')
def generate_dockerfiles(output_dir, configuration_files, tag_separator="_"):
import os
import yaml
import jinja2
import copy
if not os.path.exists(output_dir):
os.makedirs(output_dir)
makefile = ("#!/usr/bin/make -f\n"
"all: build\n\t\n"
".PHONY: build push all\n"
"DOCKER_PUSH_CMD :=docker push\n"
"DOCKER_BUILD_CMD :=docker build --pull --no-cache --build-arg build_date=$(shell date --iso-8601=seconds) --build-arg vcs_ref=$(shell git describe --always --dirty='.m')\n"
"DOCKER_BUILD_CONTEXT :=../\n\n\n")
processed_names = {}
for configuration_file in configuration_files:
with open(configuration_file, 'r') as configuration_f:
config = yaml.load(configuration_f)
dbuilder_namespace_mapping = config['dbuilder-namespace-mapping'] if 'dbuilder-namespace-mapping' in config else {}
repo_group = []
for repository_identifier, settings in config['packages'].items():
repository = Repository(repository_identifier)
if not repository.host:
repository.host = 'docker.io'
if repository.host in dbuilder_namespace_mapping:
dbuilder_namespace = dbuilder_namespace_mapping[repository.host]
else:
dbuilder_namespace = 'library'
name = repository.name if repository.namespace == '' else repository.namespace + '__' + repository.name
docker_tag_repo = repository.get_host_prefix() + dbuilder_namespace + '/dbuilder'
repo_group.append(convert_docker_tag_to_makefile_target(docker_tag_repo))
name_group = []
for template_settings in settings['templates']:
template_file_abspath = os.path.abspath(os.path.join(os.path.dirname(configuration_file), template_settings['file']))
template_file_jinja_relpath = os.path.relpath(template_file_abspath, os.path.dirname(template_file_abspath))
tags = template_settings['tags']
if isinstance(tags, str):
if tags.lower() == 'all':
verify_certs = str2bool(os.getenv('VERIFY_CERTS', '1'))
registry = Registry.create(repository.host, verify_certs)
tags = registry.get_tags(repository.get_image_full_name())
suffix = '_' + template_settings['suffix'] if 'suffix' in template_settings else ''
jinja_env = template_settings['jinja_env'] if 'jinja_env' in template_settings else {}
docker_tag_name = docker_tag_repo + ':' + name + suffix
name_group.append(convert_docker_tag_to_makefile_target(docker_tag_name))
tag_group = []
for tag in tags:
template_loader = jinja2.FileSystemLoader(os.path.dirname(template_file_abspath))
template_environment = jinja2.Environment(loader=template_loader)
template = template_environment.get_template(template_file_jinja_relpath)
dockerfile_data = template.render(name=repository.get_image_path(), tag=tag, jinja_env=jinja_env)
docker_tag = docker_tag_name + tag_separator + str(tag)
makefile_target = convert_docker_tag_to_makefile_target(docker_tag)
tag_group.append(makefile_target)
dockerfile_filename = docker_tag.replace('/', '__') + '.dockerfile'
# Docker hub is special so you cannot use it's hostname
# as with other repositories or push will fail
docker_tag = docker_tag.replace('docker.io/', '')
# Docker at local machine differentiates between library/<name> and <name>
# so we will simplify it
docker_tag = docker_tag.replace('library/', '')
makefile += 'build-{}:\n\t$(DOCKER_BUILD_CMD) -f {} -t {} $(DOCKER_BUILD_CONTEXT)\n'.format(
makefile_target, dockerfile_filename, docker_tag)
makefile += 'push-{}:\n\t$(DOCKER_PUSH_CMD) {}\n\n'.format(
makefile_target, docker_tag)
if dockerfile_filename in processed_names:
raise RuntimeError(dockerfile_filename + " has already been processed")
else:
processed_names[dockerfile_filename] = None
with open(os.path.join(output_dir, dockerfile_filename), 'w+') as dockerfile_f:
dockerfile_f.write(dockerfile_data)
makefile_target_name = convert_docker_tag_to_makefile_target(docker_tag_name)
makefile += 'build-{}: {}\n\n'.format(makefile_target_name, ' '.join(
map(lambda x: 'build-' + x, tag_group)))
makefile += 'push-{}: {}\n\n'.format(makefile_target_name, ' '.join(
map(lambda x: 'push-' + x, tag_group)))
makefile += '.PHONY: build-{} push-{}\n\n'.format(makefile_target_name, makefile_target_name)
makefile_target_repo = convert_docker_tag_to_makefile_target(docker_tag_repo)
makefile += 'build-{}: {}\n\n'.format(makefile_target_repo, ' '.join(
map(lambda x: 'build-' + x, name_group)))
makefile += 'push-{}: {}\n\n'.format(makefile_target_repo, ' '.join(
map(lambda x: 'push-' + x, name_group)))
makefile += '.PHONY: build-{} push-{}\n\n'.format(makefile_target_repo, makefile_target_repo)
makefile += 'build: {}\n\n'.format(' '.join(map(lambda x: 'build-' + x, repo_group)))
makefile += 'push: {}\n\n'.format(' '.join(map(lambda x: 'push-' + x, repo_group)))
with open(os.path.join(output_dir, 'Makefile'), 'w+') as makefile_f:
makefile_f.write(makefile)
def main():
from optparse import OptionParser
parser = OptionParser()
parser.add_option('-c', '--configuration-file', dest='configuration_file', default='config.yaml',
help="Configuration file path [%default]")
parser.add_option('-o', '--output-dir', dest='output_dir', default='./dockerfiles/',
help="Output directory [%default]")
parser.add_option('-t', '--tag-separator', dest='tag_separator', default='_',
help="Separator of a docker image name and tags which will used to separate output docker image names[%default]")
parser.set_defaults(verbose=True)
(options, args) = parser.parse_args()
generate_dockerfiles(options.output_dir, [options.configuration_file], options.tag_separator)
if __name__ == '__main__':
test_class_repository()
main()