-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpublish.py
More file actions
executable file
·85 lines (62 loc) · 2.49 KB
/
publish.py
File metadata and controls
executable file
·85 lines (62 loc) · 2.49 KB
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
#!/usr/bin/env python
import os.path
import sys
import subprocess
import copy
scriptdir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(scriptdir)
from update import UpdateCLI, Suite, dockerfile_path
from string import maketrans
class PublishCLI(UpdateCLI):
CLIDESC = """Generate Dockerfile(s) from suite template files.
"""
def parse(self):
self.parser.add_argument('--rm', action='store_true',
help='Remove intermediate containers after a successful build.')
self.parser.add_argument('--no-cache', action='store_true',
help='Do not use cache when building the image.')
self.parser.add_argument('--no-push', action='store_true',
help='Do not use cache when building the image.')
super(PublishCLI, self).parse()
def shell_out(command):
rs = subprocess.call(command, shell=True)
if rs != 0:
sys.exit(rs)
def convert_to_dash(s):
dashtbl = maketrans('_', '-')
return s.translate(dashtbl)
def get_build_opts(option_list, args):
build_opts = ('--{}'.format(convert_to_dash(a)) for a in option_list if args[a])
return ' '.join(build_opts)
# String formats
TAG = '{registry}{image}:{suite}{variant}'
BUILD = 'docker build {options} -f {path} -t {tag} .'
TAG_LATEST = 'docker tag -f {tag} {registry}{image}:latest'
PUSH = 'docker push {tag}'
def main():
"""Reads suite Dockerfile files
"""
cli = PublishCLI()
opts = cli.process_options(abort_on_missing_template=False)
args = cli.arguments
suite = Suite(workdir=opts['image_dir'], suites=opts['suites'])
build_opts = ['no_cache', 'rm']
build_opts = get_build_opts(build_opts, args)
for ctx in suite.process():
fmt = ctx.copy()
fmt['path'] = dockerfile_path(suite=fmt['suite'], variant=fmt['variant'])
fmt['variant'] = '-{}'.format(fmt['variant']) if fmt['variant'] else ''
fmt['tag'] = TAG.format(**fmt)
fmt['options'] = build_opts
# Run docker build
shell_out(BUILD.format(**fmt))
# Tag latestest if suite.yml contains latest option
if fmt['suite'] + fmt['variant'] == suite.latest:
shell_out(TAG_LATEST.format(**fmt))
if not args['no_push']:
shell_out(PUSH.format(tag='{registry}{image}:latest'.format(**fmt)))
# Push image
if not args['no_push']:
shell_out(PUSH.format(**fmt))
if __name__ == '__main__':
main()