2017-10-19 04:41:23 +02:00
|
|
|
#!/usr/bin/env python
|
2017-10-17 06:45:39 +02:00
|
|
|
|
2017-10-19 04:41:23 +02:00
|
|
|
""" Dockerfile.py - generates and build dockerfiles
|
|
|
|
|
|
|
|
Usage:
|
2018-10-30 06:20:45 +01:00
|
|
|
Dockerfile.py [--arch=<arch> ...] [--skip=<arch> ...] [-v] [-t] [--no-build | --no-generate] [--no-cache]
|
2017-10-19 04:41:23 +02:00
|
|
|
|
|
|
|
Options:
|
|
|
|
--no-build Skip building the docker images
|
2017-11-15 05:33:05 +01:00
|
|
|
--no-cache Build without using any cache data
|
2017-10-19 04:41:23 +02:00
|
|
|
--no-generate Skip generating Dockerfiles from template
|
2018-07-17 06:43:55 +02:00
|
|
|
--arch=<arch> What Architecture(s) to build [default: amd64 armel armhf aarch64]
|
|
|
|
--skip=<arch> What Architectures(s) to skip [default: None]
|
2017-10-19 04:41:23 +02:00
|
|
|
-v Print docker's command output [default: False]
|
2018-10-30 06:20:45 +01:00
|
|
|
-t Print docker's build time [default: False]
|
2017-10-19 04:41:23 +02:00
|
|
|
|
|
|
|
Examples:
|
|
|
|
"""
|
|
|
|
|
|
|
|
from docopt import docopt
|
2017-10-02 06:25:57 +02:00
|
|
|
from jinja2 import Environment, FileSystemLoader
|
2017-10-19 04:41:23 +02:00
|
|
|
from docopt import docopt
|
2017-10-02 06:25:57 +02:00
|
|
|
import os
|
2017-10-17 06:45:39 +02:00
|
|
|
import testinfra
|
2017-10-02 06:25:57 +02:00
|
|
|
|
|
|
|
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
|
|
|
base_vars = {
|
2018-08-04 18:40:31 +02:00
|
|
|
'name': 'pihole/pihole',
|
2017-10-02 06:25:57 +02:00
|
|
|
'maintainer' : 'adam@diginc.us',
|
2018-07-01 00:16:16 +02:00
|
|
|
's6_version' : 'v1.21.4.0',
|
2017-10-02 06:25:57 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 18:05:27 +02:00
|
|
|
os_base_vars = {
|
2018-08-04 18:40:31 +02:00
|
|
|
'php_env_config': '/etc/lighttpd/conf-enabled/15-fastcgi-php.conf',
|
|
|
|
'php_error_log': '/var/log/lighttpd/error.log'
|
2017-10-04 18:05:27 +02:00
|
|
|
}
|
|
|
|
|
2017-10-02 06:25:57 +02:00
|
|
|
images = {
|
2018-08-04 18:40:31 +02:00
|
|
|
'v4.0': [
|
2017-10-04 18:05:27 +02:00
|
|
|
{
|
2018-11-01 04:40:00 +01:00
|
|
|
'base': 'pihole/debian-base:latest',
|
2017-10-02 06:25:57 +02:00
|
|
|
'arch': 'amd64'
|
2017-10-04 18:05:27 +02:00
|
|
|
},
|
2018-07-17 06:43:55 +02:00
|
|
|
{
|
|
|
|
'base': 'multiarch/debian-debootstrap:armel-stretch-slim',
|
|
|
|
'arch': 'armel'
|
|
|
|
},
|
2017-10-04 18:05:27 +02:00
|
|
|
{
|
2018-01-26 22:45:15 +01:00
|
|
|
'base': 'multiarch/debian-debootstrap:armhf-stretch-slim',
|
2017-10-02 06:25:57 +02:00
|
|
|
'arch': 'armhf'
|
2017-10-04 18:05:27 +02:00
|
|
|
},
|
|
|
|
{
|
2018-01-26 22:45:15 +01:00
|
|
|
'base': 'multiarch/debian-debootstrap:arm64-stretch-slim',
|
2017-10-02 06:25:57 +02:00
|
|
|
'arch': 'aarch64'
|
2017-10-04 18:05:27 +02:00
|
|
|
}
|
2017-10-02 06:25:57 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2017-10-19 04:41:23 +02:00
|
|
|
def generate_dockerfiles(args):
|
|
|
|
if args['--no-generate']:
|
|
|
|
print " ::: Skipping Dockerfile generation"
|
|
|
|
return
|
|
|
|
|
2018-08-04 18:40:31 +02:00
|
|
|
for version, archs in images.iteritems():
|
2017-10-02 06:25:57 +02:00
|
|
|
for image in archs:
|
2018-07-17 06:59:44 +02:00
|
|
|
if image['arch'] not in args['--arch'] or image['arch'] in args['--skip']:
|
2018-07-17 06:43:55 +02:00
|
|
|
return
|
|
|
|
s6arch = image['arch']
|
|
|
|
if image['arch'] == 'armel':
|
|
|
|
s6arch = 'arm'
|
2017-10-04 18:05:27 +02:00
|
|
|
merged_data = dict(
|
2018-08-04 18:40:31 +02:00
|
|
|
{ 'version': version }.items() +
|
2017-10-04 18:05:27 +02:00
|
|
|
base_vars.items() +
|
2018-08-04 18:40:31 +02:00
|
|
|
os_base_vars.items() +
|
2018-07-17 06:43:55 +02:00
|
|
|
image.items() +
|
|
|
|
{ 's6arch': s6arch }.items()
|
2017-10-04 18:05:27 +02:00
|
|
|
)
|
2017-10-02 06:25:57 +02:00
|
|
|
j2_env = Environment(loader=FileSystemLoader(THIS_DIR),
|
|
|
|
trim_blocks=True)
|
2017-10-04 18:05:27 +02:00
|
|
|
template = j2_env.get_template('Dockerfile.template')
|
2017-10-03 05:52:02 +02:00
|
|
|
|
2018-08-04 18:40:31 +02:00
|
|
|
dockerfile = 'Dockerfile_{}'.format(image['arch'])
|
2017-10-03 05:52:02 +02:00
|
|
|
with open(dockerfile, 'w') as f:
|
2017-10-04 18:05:27 +02:00
|
|
|
f.write(template.render(pihole=merged_data))
|
|
|
|
|
2017-10-02 06:25:57 +02:00
|
|
|
|
2017-10-19 04:41:23 +02:00
|
|
|
def build_dockerfiles(args):
|
|
|
|
if args['--no-build']:
|
2017-12-08 07:04:01 +01:00
|
|
|
print " ::: Skipping Dockerfile building"
|
2017-10-19 04:41:23 +02:00
|
|
|
return
|
|
|
|
|
2018-07-17 06:59:44 +02:00
|
|
|
for arch in args['--arch']:
|
2018-08-04 20:31:58 +02:00
|
|
|
# TODO: include from external .py that can be shared with Dockerfile.py / Tests / deploy scripts '''
|
2018-11-01 04:40:00 +01:00
|
|
|
if arch == 'armel':
|
|
|
|
print "Skipping armel, incompatible upstream binaries/broken"
|
|
|
|
continue
|
2018-08-04 20:31:58 +02:00
|
|
|
build('pihole', 'v4.0', arch, args)
|
2017-10-17 06:45:39 +02:00
|
|
|
|
2017-10-19 04:41:23 +02:00
|
|
|
|
2018-08-04 18:40:31 +02:00
|
|
|
def build(docker_repo, version, arch, args):
|
2017-10-17 06:45:39 +02:00
|
|
|
run_local = testinfra.get_backend(
|
|
|
|
"local://"
|
|
|
|
).get_module("Command").run
|
|
|
|
|
2018-08-04 18:40:31 +02:00
|
|
|
dockerfile = 'Dockerfile_{}'.format(arch)
|
|
|
|
repo_tag = '{}:{}_{}'.format(docker_repo, version, arch)
|
2018-08-04 18:44:19 +02:00
|
|
|
cached_image = '{}/{}'.format('pihole', repo_tag)
|
2018-10-30 06:20:45 +01:00
|
|
|
time=''
|
|
|
|
if args['-t']:
|
|
|
|
time='time '
|
2017-11-15 05:33:05 +01:00
|
|
|
no_cache = ''
|
|
|
|
if args['--no-cache']:
|
|
|
|
no_cache = '--no-cache'
|
2018-10-30 06:20:45 +01:00
|
|
|
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)
|
2017-10-19 04:41:23 +02:00
|
|
|
print " ::: Building {} into {}".format(dockerfile, repo_tag)
|
|
|
|
if args['-v']:
|
2017-11-15 05:33:05 +01:00
|
|
|
print build_command, '\n'
|
|
|
|
build_result = run_local(build_command)
|
|
|
|
if args['-v']:
|
|
|
|
print build_result.stdout
|
2018-07-01 00:16:16 +02:00
|
|
|
print build_result.stderr
|
2017-11-15 05:33:05 +01:00
|
|
|
if build_result.rc != 0:
|
2017-10-19 04:41:23 +02:00
|
|
|
print " ::: Building {} encountered an error".format(dockerfile)
|
2017-11-15 05:33:05 +01:00
|
|
|
print build_result.stderr
|
|
|
|
assert build_result.rc == 0
|
2017-10-17 06:45:39 +02:00
|
|
|
|
|
|
|
|
2017-10-02 06:25:57 +02:00
|
|
|
if __name__ == '__main__':
|
2018-08-04 18:40:31 +02:00
|
|
|
args = docopt(__doc__, version='Dockerfile 1.0')
|
2017-10-19 04:41:23 +02:00
|
|
|
# print args
|
|
|
|
|
|
|
|
generate_dockerfiles(args)
|
|
|
|
build_dockerfiles(args)
|