def build(image, push, clean): """Build the docker images.""" builds = images() if image != 'all': builds = {image: images()[image]} # Build docker images. dockerpy = Docker(auth_config()) path_to_script = os.path.dirname(os.path.abspath(__file__)) for name in builds: repository = "{}/{}".format(USER, builds[name]["repository"]) targets = builds[name]["targets"] context = "{path}/{folder}/".format( path=path_to_script, folder=builds[name]["repository"]) dockerfile = "{file}.Dockerfile".format(file=name) labels = {"version": "{date}".format(date=TODAY)} for target in targets: latest_tag = "{name}-{target}".format(name=name, target=target) dated_tag = "{latest}-{date}".format(latest=latest_tag, date=TODAY) dockerpy.build(context=context, dockerfile=dockerfile, repository=repository, tag=latest_tag, target=target, labels=labels) dockerpy.tag(repository=repository, prev_tag=latest_tag, new_tag=dated_tag) if push or docker_push(): dockerpy.push(repository=repository, tag=latest_tag) dockerpy.push(repository=repository, tag=dated_tag) if clean or docker_clean(): dockerpy.rmi(repository=repository, tag=dated_tag) dockerpy.prune()
def generate(log): """Generate the dockerfiles for this repo.""" file_loader = FileSystemLoader('template') env = Environment(loader=file_loader) for name in templates(): template_name = name + ".dockerfile.jinja" dockerfiles = templates()[name] template = env.get_template(template_name) for file in dockerfiles: output = template.render(file) out_file = name + "/" + file['name'] + ".Dockerfile" log.info("Generating {filename}".format(filename=out_file)) dockerfile_out = open(out_file, "w") dockerfile_out.write(output) dockerfile_out.close() log.info("Generating readme for {name}".format(name=name)) readme_template = env.get_template('readme.md.jinja') readme_output = readme_template.render({ "repo_name": name, "dockerfiles": dockerfiles, "images": images() }) readme_file = name + "/README.md" readme_out = open(readme_file, "w") readme_out.write(readme_output) readme_out.close()
dockerpy.rmi(repository=repository, tag=dated_tag) dockerpy.prune() @click.command() @click.option("--generate/--no-generate", default=True, help="Generate docker files.") @click.option("--push/--no-push", default=False, help="Push generated images to DockerHub.") @click.option("--clean/--no-clean", default=True, help="Clean dated content and old images.") @click.argument("image", type=click.Choice(list(images()) + ['all'])) def main(generate, push, clean, image): """Set up logging and trigger build.""" log.setLevel(logging.DEBUG) formatter = logging.Formatter('%(message)s') ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) ch.setFormatter(formatter) log.addHandler(ch) if generate: gen(log) build(image, push, clean)
if clean or docker_clean(): dockerpy.rmi(repository=repository, tag=dated_tag) dockerpy.prune() @click.command() @click.option("--generate/--no-generate", default=True, help="Generate docker files.") @click.option("--push/--no-push", default=False, help="Push generated images to DockerHub.") @click.option("--clean/--no-clean", default=True, help="Clean dated content and old images.") @click.argument("image", type=click.Choice(list(images()) + ['all'])) def main(generate, push, clean, image): """Set up logging and trigger build.""" log.setLevel(logging.DEBUG) formatter = logging.Formatter('%(message)s') ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) ch.setFormatter(formatter) log.addHandler(ch) if generate: gen(log) build(image, push, clean)