build.py 5.86 KB
Newer Older
Philippe Pepiot's avatar
Philippe Pepiot committed
1
2
3
4
5
6
7
8
9
#!/usr/bin/env python3

import itertools
import subprocess
import sys
import logging


LOG = logging.getLogger(__name__)
10
REGISTRY = 'logilab/cubicweb'
11
CWREPO = 'https://forge.extranet.logilab.fr/cubicweb/cubicweb/'
Philippe Pepiot's avatar
Philippe Pepiot committed
12
13
14
MATRIX = [
    (['py27'], ['stretch', 'buster'], [None, '3.25', '3.26'],
        [None, 'onbuild']),
Philippe Pepiot's avatar
Philippe Pepiot committed
15
    (['py35'], ['stretch'], [None, '3.26', '3.27'], [None, 'onbuild']),
16
    (['py37'], ['buster'], [None, '3.26', '3.27', '3.28', '3.29', 'dev'], [None, 'onbuild']),
Philippe Pepiot's avatar
Philippe Pepiot committed
17
18
19
20
]
ALIASES = (
    ('3.25', 'py27-buster-3.25'),
    ('3.26', 'py37-buster-3.26'),
Philippe Pepiot's avatar
Philippe Pepiot committed
21
    ('3.27', 'py37-buster-3.27'),
22
    ('3.28', 'py37-buster-3.28'),
23
    ('3.29', 'py37-buster-3.29'),
Philippe Pepiot's avatar
Philippe Pepiot committed
24
    ('dev', 'py37-buster-dev'),
25
    ('latest', 'py37-buster-3.29'),
26
    ('buildpackage', 'buster-buildpackage'),
Philippe Pepiot's avatar
Philippe Pepiot committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
)


def run(*args):
    LOG.info('%s', ' '.join(args))
    return subprocess.run(args, stdout=subprocess.PIPE)


def check_call(*args):
    LOG.info('%s', ' '.join(args))
    return subprocess.check_call(args)


def check_output(*args):
    LOG.info('%s', ' '.join(args))
    return subprocess.check_output(args).decode().strip()


def _cwdev(_cache={}):
    if not _cache:
        rev = check_output('hg', 'id', '-r', 'default', CWREPO)
48
49
        # url anchor is used to invalidate docker cache.
        _cache[None] = '{0}-/archive/branch/default/cubicweb-branch-default.tar.gz#rev={1}'.format(
Philippe Pepiot's avatar
Philippe Pepiot committed
50
51
52
53
54
55
56
57
58
59
60
61
62
            CWREPO, rev)
    return _cache[None]


def _tag(*args, registry=None):
    tag_part = '-'.join([x for x in args if x is not None])
    if tag_part:
        return '{}:{}'.format(registry or REGISTRY, tag_part)
    return REGISTRY


def tag_aliases():
    for alias, source in ALIASES:
63
64
65
        if alias == 'buildpackage':
            check_call('docker', 'tag', _tag(source), _tag(alias))
            continue
Philippe Pepiot's avatar
Philippe Pepiot committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
        for onbuild in [None, 'onbuild']:
            if alias == 'latest' and onbuild == 'onbuild':
                tag = _tag('onbuild')
            else:
                tag = _tag(alias, onbuild)
            src = _tag(source, onbuild)
            check_call('docker', 'tag', src, tag)


def build_image(python, dist, cw, onbuild, no_cache=False, registry=None):
    tag = _tag(python, dist, cw, onbuild, registry=registry)
    args = {}
    dockerfile = 'Dockerfile'
    if onbuild is not None:
        dockerfile = 'Dockerfile.onbuild'
        args['FROM'] = _tag(python, dist, cw)
    elif cw is not None:
        dockerfile = 'Dockerfile.cubicweb'
        args['FROM'] = _tag(python, dist)
85
86
87
88
89
90
91
        args['DIST'] = dist
        args['PACKAGES'] = 'python-cubicweb' if python == 'py27' else 'python3-cubicweb'
        if cw == 'dev':
            args['SOURCE_TARBALL'] = _cwdev()
            args['COMPONENT'] = 'main'
        else:
            args['COMPONENT'] = 'cubicweb-{}'.format(cw)
Philippe Pepiot's avatar
Philippe Pepiot committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    else:
        args['DIST'] = dist
        args['PYTHON'] = 'python' if python == 'py27' else 'python3'
    cmd = [
        'docker', 'build', '-t', tag,
        '-f', dockerfile,
    ]
    if no_cache:
        cmd += ['--no-cache']
    for key, value in args.items():
        cmd += ['--build-arg', '{}={}'.format(key, value)]
    cmd += ['.']
    check_call(*cmd)


def green(text):
    return '\033[0;32m' + text + '\033[0m'


def red(text):
    return '\033[0;31m' + text + '\033[0m'


115
116
117
118
119
120
121
def build_buildpackage(dist):
    tag = '{}:{}-buildpackage'.format(REGISTRY, dist)
    check_call('docker', 'build', '-t', tag,
               '--build-arg', 'DIST={}'.format(dist),
               '-f', 'Dockerfile.buildpackage', '.')


Philippe Pepiot's avatar
Philippe Pepiot committed
122
def build(rebuild=False):
123
124
125
126
    if rebuild:
        # pull base images
        check_call('docker', 'pull', 'debian:stretch-slim')
        check_call('docker', 'pull', 'debian:buster-slim')
127
128
    build_buildpackage('stretch')
    build_buildpackage('buster')
Philippe Pepiot's avatar
Philippe Pepiot committed
129
130
131
    for matrix in MATRIX:
        for python, dist, cw, onbuild in itertools.product(*matrix):
            build_image(python, dist, cw, onbuild)
132
            if rebuild and onbuild is None:
Philippe Pepiot's avatar
Philippe Pepiot committed
133
                tag = _tag(python, dist, cw)
Philippe Pepiot's avatar
Philippe Pepiot committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
                out = run(
                    'docker', 'run', '--rm', '-t',
                    '--user', 'root',
                    '--entrypoint', 'check-docker-updates.sh',
                    tag, 'apt')
                if out.returncode == 1:
                    LOG.warning(red(
                        '%s debian packages updates are available: %s'),
                        tag, out.stdout)
                    build_image(python, dist, cw, onbuild, no_cache=True)
                else:
                    assert (out.returncode, out.stdout) == (0, b''), out
                    LOG.info(green('%s debian packages are up-to-date'), tag)


def push():
    for matrix in MATRIX:
        for python, dist, cw, onbuild in itertools.product(*matrix):
            tag = _tag(python, dist, cw, onbuild)
            check_call('docker', 'push', tag)
    for alias, source in ALIASES:
155
156
157
158
        if alias == "buildpackage":
            check_call('docker', 'push', _tag(source))
            check_call('docker', 'push', _tag(alias))
            continue
Philippe Pepiot's avatar
Philippe Pepiot committed
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
        for onbuild in [None, 'onbuild']:
            if alias == 'latest' and onbuild == 'onbuild':
                tag = _tag('onbuild')
            else:
                tag = _tag(alias, onbuild)
            check_call('docker', 'push', tag)


if __name__ == '__main__':
    import argparse
    logging.basicConfig(format='%(asctime)-15s %(message)s')
    LOG.setLevel(logging.INFO)
    parser = argparse.ArgumentParser(sys.argv[0])
    parser.add_argument('--registry', action='store', default=REGISTRY)
    parser.add_argument(
        '--checkrebuild', action='store_true', default=False,
        help='Rebuild images in case of outdated debian or python package')
    parser.add_argument(
        '--push', action='store_true', default=False,
        help='push images')
    args = parser.parse_args()
    REGISTRY = args.registry
    if args.push:
        push()
    else:
        build(args.checkrebuild)
        tag_aliases()