-
Notifications
You must be signed in to change notification settings - Fork 30
/
DockerHelper.py
executable file
·369 lines (316 loc) · 13.1 KB
/
DockerHelper.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env python
"""Docker Helper functions
It contains docker tag query functions and Dockerfile update function.
Run ./DockerHelper --help or ./DockerHelper --help <command> for
detail help."""
from typing import List, Any # pylint: disable=unused-import
# We need to import 'List' and 'Any' for mypy to work
import argparse
import fileinput
import json
import logging
import re
import subprocess # nosec
import sys
import urllib2
class GitHelper(object):
"Helper functions for git"
GIT_CMD = '/bin/git'
@staticmethod
def remote_get_latest_tag(repo_url, tag_glob='*'):
# type: (str, str) -> str
"""Get latest (newest) tag from git remote
Known Bug: If the tag 4.4.4 is newer than 4.5.0,
then the 'latest' tag will be 4.4.4"""
tag = subprocess.check_output( # nosec
[GitHelper.GIT_CMD, 'ls-remote', '--tags', repo_url,
'refs/tags/' + tag_glob + '[^^{}]']
).split()[-1].split('/')[-1]
logging.info("latest tag of %s is %s", repo_url, tag)
return tag
@staticmethod
def branch_get_current():
# type: () -> str
"""Return either current branch, or 'HEAD' if in detached mode
This method assume you are already in correct directory"""
return subprocess.check_output( # nosec
[GitHelper.GIT_CMD, 'rev-parse', '--abbrev-ref', 'HEAD'])
@staticmethod
def branch_merge_detached(target_branch):
# type: (str) -> None
"""Merge to target_branch if in git detached mode, or no-op
This method assume you are already in correct directory"""
commit_sha = subprocess.check_output( # nosec
[GitHelper.GIT_CMD, 'rev-parse', 'HEAD'])
tmp_branch = 'br-' + commit_sha
if GitHelper.branch_get_current() == 'HEAD':
subprocess.check_call( # nosec
[GitHelper.GIT_CMD, 'branch', tmp_branch, commit_sha])
subprocess.check_call( # nosec
[GitHelper.GIT_CMD, 'checkout', target_branch])
subprocess.check_call( # nosec
[GitHelper.GIT_CMD, 'merge', tmp_branch])
subprocess.check_call( # nosec
[GitHelper.GIT_CMD, 'branch', '-D', tmp_branch])
@staticmethod
def push(target_branch):
# type: (str) -> None
"Git push to master if there is unpushed commits"
if subprocess.call( # nosec
[GitHelper.GIT_CMD, 'diff', '--exit-code', 'origin', 'HEAD']
) != 0:
# branch_merge_detached is needed in Jenkins
# as it checkout as detached
GitHelper.branch_merge_detached(target_branch)
subprocess.check_call([ # nosec
GitHelper.GIT_CMD, 'push', '--follow-tags',
'origin', 'master'])
def __init__(self, repo_url, tag_prefix=''):
# type: (str, str) -> None
self.repo_url = repo_url
self.latest_tag = GitHelper.remote_get_latest_tag(
self.repo_url, tag_prefix+'*')
self.latest_version = self.latest_tag[len(tag_prefix):]
def __getitem__(self, key):
# type: (str) -> str
return self[key]
class DockerImage(object):
"""This class contains information about Docker image tag and dockerfile
tag: version-postrelease, version, or simply 'latest'
dockerfile: path"""
# pylint: disable=too-many-instance-attributes
# Cmd full path
DOCKER_CMD = '/bin/docker'
DOCKER_NAME_DB = {
'server': {
'dir': 'zanata-server',
'url': 'https://github.com/zanata/zanata-platform',
'tag_prefix': 'platform-',
}
}
@staticmethod
def list_tags(repo_name, docker_name):
# type: (str, str) -> List[str]
"""Return the name of tags as list"""
tag_url = 'https://%s/%s/%s/tags/' % (
'registry.hub.docker.com/v2/repositories',
repo_name, docker_name)
result = [] # type: List[str]
while tag_url:
logging.info("Getting tags from %s", tag_url)
# Audited urlopen URL
response = urllib2.urlopen(tag_url) # nosec
logging.debug(response.info())
tags_json = json.load(response)
result += [elem["name"] for elem in tags_json["results"]]
tag_url = tags_json.get("next", "")
return result
@staticmethod
def has_tag(repo_name, docker_name, tag):
# type: (str, str, str) -> int
"""Whether the docker image has the given tag"""
tags_list = DockerImage.list_tags(repo_name, docker_name)
return tag in tags_list
@staticmethod
def next_postrelease(repo_name, docker_name, version):
# type: (str, str, str) -> int
"Next unused postrelease"
prog = re.compile("^%s-([0-9]+)" % version)
current_release = 0
for tag in DockerImage.list_tags(repo_name, docker_name):
matched = re.match(prog, tag)
if not matched:
continue
if int(matched.group(1)) > current_release:
current_release = int(matched.group(1))
return current_release + 1
def __init__(self, repo_name, docker_name, tag_param):
# type: (str, str, str) -> None
# pylint: disable=too-many-instance-attributes
"""Properties:
repo_name: repository name. e.g. 'zanata'
docker_name: docker image name. e.g. 'server'
tag_param: The tag parameter, it accepts either x.y.z-r or 'auto'
version: e.g. 4.4.3
prerelesae: e.g. alpha-1
postrelease: e.g. 2
final_tag: Final resolved tag, like 4.4.3-2"""
self.repo_name = repo_name
# Dockerfile
self.docker_name = docker_name
if docker_name in DockerImage.DOCKER_NAME_DB:
dockerfile_dir = DockerImage.DOCKER_NAME_DB[docker_name]['dir']
else:
dockerfile_dir = docker_name
self.dockerfile_name = "%s/Dockerfile" % dockerfile_dir
self.tag_param = tag_param
# Docker Tag
match = re.match('^([0-9.]+)(-(.*))?', self.tag_param)
if match:
self.version = match.group(1)
extra = match.group(3)
if extra:
if extra.isdigit():
self.postrelease = int(extra)
self.final_tag = "%s-%d" % (
self.version, self.postrelease)
else:
# alpha or rc
self.prerelease = extra
self.final_tag = "%s-%s" % (
self.version, self.prerelease)
else:
self.final_tag = self.version
elif self.tag_param == 'auto':
if self.docker_name in DockerImage.DOCKER_NAME_DB:
git_helper = GitHelper(
DockerImage.DOCKER_NAME_DB[docker_name]['url'],
DockerImage.DOCKER_NAME_DB[docker_name]['tag_prefix'])
self.version = git_helper.latest_version
self.postrelease = DockerImage.next_postrelease(
repo_name, docker_name, self.version)
else:
logger.error("Unsupported docker_name %s", docker_name)
sys.exit(EXIT_FATAL_INVALID_ARGUMENTS)
self.final_tag = "%s-%d" % (self.version, self.postrelease)
else:
self.final_tag = self.tag_param
self.image_name = "%s/%s:%s" % (
self.repo_name, self.docker_name, self.final_tag)
def __getitem__(self, key):
# type: (str) -> str
return self[key]
def dockerfile_update(self):
# type: () -> None
"""Update the Dockerfile"""
release = 1 if not hasattr(self, 'postrelease') else int(self.postrelease)
dockerfile_file = fileinput.FileInput(
[self.dockerfile_name], inplace=True, backup='.bak')
for line in iter(dockerfile_file):
if re.match(r'^ARG ZANATA_VERSION=.*$', line):
sys.stdout.write(
re.sub(r'^ARG ZANATA_VERSION=.*$',
'ARG ZANATA_VERSION=%s' % self.version, line))
elif re.match(r'\s*Release="([0-9]+)"', line):
sys.stdout.write(
re.sub(r'Release=".*"',
'Release="%d"' % release, line))
else:
sys.stdout.write(line)
sys.stdout.flush()
dockerfile_file.close()
# Exit Status
EXIT_OK = 0
EXIT_FATAL_INVALID_OPTIONS = 3
EXIT_FATAL_INVALID_ARGUMENTS = 7
EXIT_RETURN_FALSE = 40
# Set logging
logging.basicConfig(format='%(asctime)-15s [%(levelname)s] %(message)s')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def get_tags():
# type: () -> None
"""Return the name of tags as list"""
logging.info("get-tags for %s/%s", args.repo_name, args.docker_name)
tags_list = DockerImage.list_tags(args.repo_name, args.docker_name)
print('\n'.join(str(tag) for tag in tags_list))
def has_tag():
# type: () -> None
"""Whether the docker image has the given tag"""
if DockerImage.has_tag(args.repo_name, args.docker_name, args.tag):
print("yes")
sys.exit(EXIT_OK)
else:
print("no")
sys.exit(EXIT_RETURN_FALSE)
def dockerfile_update():
# type: () -> None
"Update the Dockerfile according to given tag"
image = DockerImage(args.repo_name, args.docker_name, args.tag)
image.dockerfile_update()
def publish():
# type: () -> None
"""Publish docker image to docker hub, as well as git commit"""
if DockerImage.has_tag(args.repo_name, args.docker_name, args.tag):
logger.error("Tag %s already exists", args.tag)
sys.exit(EXIT_FATAL_INVALID_ARGUMENTS)
else:
logging.info("docker tag to be publish: %s", args.tag)
# Update Docker file
image = DockerImage(args.repo_name, args.docker_name, args.tag)
image.dockerfile_update()
if subprocess.call([GitHelper.GIT_CMD, 'diff', '--exit-code']) != 0: # nosec
subprocess.check_call([ # nosec
GitHelper.GIT_CMD, 'commit', '-a', '-m',
"[release] Image %s is released" % image.image_name])
# Building docker image
logging.info("Building image %s", image.image_name)
subprocess.check_call([ # nosec
DockerImage.DOCKER_CMD, 'build',
'-t', image.image_name, image.dockerfile_name])
# Tag and push docker image
for current_tag in [image.final_tag, 'latest']:
subprocess.check_call([ # nosec
DockerImage.DOCKER_CMD, 'tag', image.image_name,
"%s/%s/%s:%s" % (
args.registry, args.repo_name,
args.docker_name, current_tag)])
subprocess.check_call([ # nosec
DockerImage.DOCKER_CMD, 'push',
"%s/%s/%s:%s" % (
args.registry, args.repo_name,
args.docker_name, current_tag)])
# Git push to master if there is unpushed commits
GitHelper.push('master')
def parse():
# type () -> None
"""Parse options and arguments"""
parser = argparse.ArgumentParser(description='Docker helper functions')
parser.add_argument(
'-r', '--repo-name', type=str,
default='zanata',
help='Docker Repository name. Default: zanata')
parser.add_argument(
'-R', '--registry', type=str,
default='docker.io',
help='Docker Registry name. Default: docker.io')
subparsers = parser.add_subparsers(
title='Command', description='Valid commands',
help='Command help')
has_tag_parser = subparsers.add_parser(
'has-tag',
help='Whether the docker image has the given tag')
has_tag_parser.add_argument(
'docker_name', type=str,
help='Docker name, e.g. "server"')
has_tag_parser.add_argument('tag', type=str, help='tag')
has_tag_parser.set_defaults(func=has_tag)
get_tags_parser = subparsers.add_parser(
'get-tags',
help='Get all existing tags')
get_tags_parser.add_argument(
'docker_name', type=str,
help='Docker name, e.g. "server"')
get_tags_parser.set_defaults(func=get_tags)
dockerfile_update_parser = subparsers.add_parser(
'dockerfile-update',
help='Update the Dockerfile according to tag')
dockerfile_update_parser.add_argument(
'docker_name', type=str,
help='Docker name, e.g. "server"')
dockerfile_update_parser.add_argument(
'tag', type=str,
help='tag to be publish')
dockerfile_update_parser.set_defaults(func=dockerfile_update)
publish_parser = subparsers.add_parser(
'publish',
help='publish docker image according to the tag')
publish_parser.add_argument(
'docker_name', type=str,
help='Docker name, e.g. "server"')
publish_parser.add_argument('tag', type=str, help='tag to be publish')
publish_parser.set_defaults(func=publish)
return parser.parse_args()
if __name__ == '__main__':
args = parse()
args.func()