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:
|
2017-11-15 05:33:05 +01:00
|
|
|
Dockerfile.py [--os=<os> ...] [--arch=<arch> ...] [-v] [--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
|
|
|
|
--os=<os> What OS(s) to build [default: alpine debian]
|
|
|
|
--arch=<arch> What Architecture(s) to build [default: amd64 armhf aarch64]
|
|
|
|
-v Print docker's command output [default: False]
|
|
|
|
|
|
|
|
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 = {
|
2017-10-04 18:05:27 +02:00
|
|
|
'name': 'diginc/pi-hole',
|
2017-10-02 06:25:57 +02:00
|
|
|
'maintainer' : 'adam@diginc.us',
|
2017-12-08 02:57:09 +01:00
|
|
|
's6_version' : 'v1.21.2.1',
|
2017-10-02 06:25:57 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 18:05:27 +02:00
|
|
|
os_base_vars = {
|
|
|
|
'debian': {
|
|
|
|
'php_env_config': '/etc/lighttpd/conf-enabled/15-fastcgi-php.conf',
|
|
|
|
'php_error_log': '/var/log/lighttpd/error.log'
|
|
|
|
},
|
|
|
|
'alpine': {
|
|
|
|
'php_env_config': '/etc/php5/fpm.d/envs.conf',
|
|
|
|
'php_error_log': '/var/log/nginx/error.log'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-02 06:25:57 +02:00
|
|
|
images = {
|
|
|
|
'debian': [
|
2017-10-04 18:05:27 +02:00
|
|
|
{
|
2017-10-02 06:25:57 +02:00
|
|
|
'base': 'debian:jessie',
|
|
|
|
'arch': 'amd64'
|
2017-10-04 18:05:27 +02:00
|
|
|
},
|
|
|
|
{
|
2017-10-02 06:25:57 +02:00
|
|
|
'base': 'multiarch/debian-debootstrap:armhf-jessie-slim',
|
|
|
|
'arch': 'armhf'
|
2017-10-04 18:05:27 +02:00
|
|
|
},
|
|
|
|
{
|
2017-10-02 06:25:57 +02:00
|
|
|
'base': 'multiarch/debian-debootstrap:arm64-jessie-slim',
|
|
|
|
'arch': 'aarch64'
|
2017-10-04 18:05:27 +02:00
|
|
|
}
|
2017-10-02 06:25:57 +02:00
|
|
|
],
|
|
|
|
'alpine': [
|
2017-10-04 18:05:27 +02:00
|
|
|
{
|
2017-10-02 06:25:57 +02:00
|
|
|
'base': 'alpine:edge',
|
|
|
|
'arch': 'amd64'
|
2017-10-04 18:05:27 +02:00
|
|
|
},
|
|
|
|
{
|
2017-10-02 06:25:57 +02:00
|
|
|
'base': 'multiarch/alpine:armhf-edge',
|
|
|
|
'arch': 'armhf'
|
2017-10-04 18:05:27 +02:00
|
|
|
},
|
2017-11-06 04:30:07 +01:00
|
|
|
# Impossible combo :(
|
|
|
|
# {
|
|
|
|
# 'base': 'multiarch/alpine:aarch64-edge',
|
|
|
|
# 'arch': 'aarch64'
|
|
|
|
# }
|
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
|
|
|
|
|
2017-10-02 06:25:57 +02:00
|
|
|
for os, archs in images.iteritems():
|
|
|
|
for image in archs:
|
2017-10-19 04:41:23 +02:00
|
|
|
if os not in args['--os'] and image['arch'] not in args['--arch']:
|
|
|
|
return
|
2017-10-04 18:05:27 +02:00
|
|
|
merged_data = dict(
|
|
|
|
{ 'os': os }.items() +
|
|
|
|
base_vars.items() +
|
|
|
|
os_base_vars[os].items() +
|
|
|
|
image.items()
|
|
|
|
)
|
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
|
|
|
|
|
|
|
dockerfile = 'Dockerfile_{}_{}'.format(os, image['arch'])
|
|
|
|
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']:
|
|
|
|
print " ::: Skipping Dockerfile generation"
|
|
|
|
return
|
|
|
|
|
|
|
|
for os in args['--os']:
|
|
|
|
for arch in args['--arch']:
|
|
|
|
docker_repo = 'pi-hole-multiarch'
|
|
|
|
if arch == 'amd64':
|
|
|
|
docker_repo = 'pi-hole'
|
2017-10-17 06:45:39 +02:00
|
|
|
|
2017-10-19 04:41:23 +02:00
|
|
|
build(docker_repo, os, arch, args)
|
2017-10-17 06:45:39 +02:00
|
|
|
|
2017-10-19 04:41:23 +02:00
|
|
|
|
|
|
|
def build(docker_repo, os, arch, args):
|
2017-10-17 06:45:39 +02:00
|
|
|
run_local = testinfra.get_backend(
|
|
|
|
"local://"
|
|
|
|
).get_module("Command").run
|
|
|
|
|
|
|
|
dockerfile = 'Dockerfile_{}_{}'.format(os, arch)
|
2017-10-19 04:41:23 +02:00
|
|
|
repo_tag = '{}:{}_{}'.format(docker_repo, os, arch)
|
2017-11-15 05:33:05 +01:00
|
|
|
cached_image = '{}/{}'.format('diginc', repo_tag)
|
|
|
|
no_cache = ''
|
|
|
|
if args['--no-cache']:
|
|
|
|
no_cache = '--no-cache'
|
|
|
|
build_command = 'docker build {no_cache} --pull --cache-from="{cache},{create_tag}" -f {dockerfile} -t {create_tag} .'\
|
|
|
|
.format(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
|
|
|
|
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__':
|
2017-10-19 04:41:23 +02:00
|
|
|
args = docopt(__doc__, version='Dockerfile 0.1')
|
|
|
|
# print args
|
|
|
|
|
|
|
|
generate_dockerfiles(args)
|
|
|
|
build_dockerfiles(args)
|