#!/usr/bin/env python3 """ Dockerfile.py - generates and build dockerfiles Usage: Dockerfile.py [--hub_tag=] [--arch= ...] [--debian= ...] [-v] [-t] [--no-build] [--no-cache] Options: --no-build Skip building the docker images --no-cache Build without using any cache data --hub_tag= What the Docker Hub Image should be tagged as [default: None] --arch= What Architecture(s) to build [default: amd64 armel armhf arm64] --debian= What debian version(s) to build [default: stretch buster] -v Print docker's command output [default: False] -t Print docker's build time [default: False] Examples: """ from docopt import docopt import os import subprocess __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-') def build_dockerfiles(args): if args['-v']: print(args) if args['--no-build']: print(" ::: Skipping Dockerfile building") return for arch in args['--arch']: for debian_version in args['--debian']: build('pihole', arch, debian_version, args['--hub_tag'], args['-t'], args['--no-cache'], args['-v']) def run_and_stream_command_output(command, environment_vars, verbose): print("Running", command) build_result = subprocess.Popen(command.split(), env=environment_vars, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True) if verbose: while build_result.poll() is None: for line in build_result.stdout: print(line, end='') build_result.wait() if build_result.returncode != 0: print(" ::: Error running".format(command)) print(build_result.stderr) def build(docker_repo: str, arch: str, debian_version: str, hub_tag: str, show_time: bool, no_cache: bool, verbose: bool): create_tag = f'{docker_repo}:{__version__}-{arch}-{debian_version}' print(f' ::: Building {create_tag}') time_arg = 'time' if show_time else '' cache_arg = '--no-cache' if no_cache else '' build_env = os.environ.copy() build_env['PIHOLE_VERSION'] = __version__ build_env['DEBIAN_VERSION'] = debian_version build_command = f'{time_arg} docker-compose -f build.yml build {cache_arg} --pull {arch}' print(f' ::: Building {arch} into {create_tag}') run_and_stream_command_output(build_command, build_env, verbose) if verbose: print(build_command, '\n') if hub_tag: hub_tag_command = f'{time_arg} docker tag {create_tag} {hub_tag}' print(f' ::: Tagging {create_tag} into {hub_tag}') run_and_stream_command_output(hub_tag_command, build_env, verbose) if __name__ == '__main__': args = docopt(__doc__, version='Dockerfile 1.1') build_dockerfiles(args)