forked from pi-hole/docker-pi-hole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile.py
executable file
·139 lines (119 loc) · 4.5 KB
/
Dockerfile.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
#!/usr/bin/env python
""" Dockerfile.py - generates and build dockerfiles
Usage:
Dockerfile.py [--arch=<arch> ...] [--skip=<arch> ...] [-v] [-t] [--no-build | --no-generate] [--no-cache]
Options:
--no-build Skip building the docker images
--no-cache Build without using any cache data
--no-generate Skip generating Dockerfiles from template
--arch=<arch> What Architecture(s) to build [default: amd64 armel armhf aarch64]
--skip=<arch> What Architectures(s) to skip [default: None]
-v Print docker's command output [default: False]
-t Print docker's build time [default: False]
Examples:
"""
from __future__ import print_function
from docopt import docopt
from jinja2 import Environment, FileSystemLoader
from docopt import docopt
import os
import subprocess
import sys
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
base_vars = {
'name': 'pihole/pihole',
'maintainer' : '[email protected]',
's6_version' : 'v1.21.7.0',
}
os_base_vars = {
'php_env_config': '/etc/lighttpd/conf-enabled/15-fastcgi-php.conf',
'php_error_log': '/var/log/lighttpd/error.log'
}
__version__ = None
dot = os.path.abspath('.')
with open('{}/VERSION'.format(dot), 'r') as v:
raw_version = v.read().strip()
__version__ = raw_version.replace('release/', 'release-')
images = {
__version__: [
{
'base': 'pihole/debian-base:latest',
'arch': 'amd64'
},
{
'base': 'multiarch/debian-debootstrap:armel-stretch-slim',
'arch': 'armel'
},
{
'base': 'multiarch/debian-debootstrap:armhf-stretch-slim',
'arch': 'armhf'
},
{
'base': 'multiarch/debian-debootstrap:arm64-stretch-slim',
'arch': 'aarch64'
}
]
}
def generate_dockerfiles(args):
if args['--no-generate']:
print(" ::: Skipping Dockerfile generation")
return
for version, archs in images.iteritems():
for image in archs:
if image['arch'] not in args['--arch'] or image['arch'] in args['--skip']:
return
s6arch = image['arch']
if image['arch'] == 'armel':
s6arch = 'arm'
merged_data = dict(
{ 'version': version }.items() +
base_vars.items() +
os_base_vars.items() +
image.items() +
{ 's6arch': s6arch }.items()
)
j2_env = Environment(loader=FileSystemLoader(THIS_DIR),
trim_blocks=True)
template = j2_env.get_template('Dockerfile.template')
dockerfile = 'Dockerfile_{}'.format(image['arch'])
with open(dockerfile, 'w') as f:
f.write(template.render(pihole=merged_data))
def build_dockerfiles(args):
if args['--no-build']:
print(" ::: Skipping Dockerfile building")
return
for arch in args['--arch']:
# TODO: include from external .py that can be shared with Dockerfile.py / Tests / deploy scripts '''
if arch == 'armel':
print("Skipping armel, incompatible upstream binaries/broken")
continue
build('pihole', arch, args)
def build(docker_repo, arch, args):
dockerfile = 'Dockerfile_{}'.format(arch)
repo_tag = '{}:{}_{}'.format(docker_repo, __version__, arch)
cached_image = '{}/{}'.format('pihole', repo_tag)
time=''
if args['-t']:
time='time '
no_cache = ''
if args['--no-cache']:
no_cache = '--no-cache'
build_command = '{time}docker build {no_cache} --pull --cache-from="{cache},{create_tag}" -f {dockerfile} -t {create_tag} .'\
.format(time=time, no_cache=no_cache, cache=cached_image, dockerfile=dockerfile, create_tag=repo_tag)
print(" ::: Building {} into {}".format(dockerfile, repo_tag))
if args['-v']:
print(build_command, '\n')
build_result = subprocess.Popen(build_command.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
if args['-v']:
for c in iter(lambda: build_result.stdout.read(1), b''):
sys.stdout.write(c)
build_result.wait()
if build_result.returncode != 0:
print(" ::: Building {} encountered an error".format(dockerfile))
print(build_result.stderr)
assert build_result.returncode == 0
if __name__ == '__main__':
args = docopt(__doc__, version='Dockerfile 1.0')
# print args
generate_dockerfiles(args)
build_dockerfiles(args)