From be328b2c7ba3abde42b7e21b53718e364d11be76 Mon Sep 17 00:00:00 2001 From: Vishnu Ks Date: Tue, 30 Jan 2018 19:17:30 +0000 Subject: [PATCH] circleci: Create script for generating Dockerfiles. [greg: updated Dockerfile comment] --- .gitignore | 3 ++ .../Dockerfile => Dockerfile.template} | 22 ++++++------ tools/circleci/generate-dockerfiles | 35 +++++++++++++++++++ tools/circleci/images.yml | 2 ++ 4 files changed, 52 insertions(+), 10 deletions(-) rename tools/circleci/{images/Dockerfile => Dockerfile.template} (88%) create mode 100755 tools/circleci/generate-dockerfiles create mode 100644 tools/circleci/images.yml diff --git a/.gitignore b/.gitignore index b3b3f8e5fe..122b713c67 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,9 @@ package-lock.json /.vagrant /var +# Dockerfiles generated for CircleCI +/tools/circleci/images + # Static build *.mo npm-debug.log diff --git a/tools/circleci/images/Dockerfile b/tools/circleci/Dockerfile.template similarity index 88% rename from tools/circleci/images/Dockerfile rename to tools/circleci/Dockerfile.template index ac56e5c78a..0d87807742 100644 --- a/tools/circleci/images/Dockerfile +++ b/tools/circleci/Dockerfile.template @@ -1,4 +1,5 @@ -# Dockerfile for a generic Ubuntu 14.04 image with just the basics we need +{generated_file_warning} +# Dockerfile for a generic Ubuntu image with just the basics we need # to make it suitable for CI. In particular: # * a non-root user to run as (a pain to try to do in setup, # because by then we've already cloned the repo); @@ -20,14 +21,15 @@ # (but much simpler, with a couple of packages from the distro), and # then borrows from the CircleCI Dockerfile. -# To rebuild from this file: -# 0. pick a new image name, like `gregprice/circleci:trusty-python-3.test` -# 1. $ sudo docker build tools/circleci/images/ --tag $NAME -# 2. $ sudo docker push $NAME -# 3. update .circleci/config.yml to refer to the new name +# To rebuild from this file for a given release, say trusty: +# 0. $ tools/circleci/generate-dockerfiles +# 1. pick a new image name, like `gregprice/circleci:trusty-python-3.test` +# 2. $ sudo docker build tools/circleci/images/$RELEASE/ --tag $NAME +# 3. $ sudo docker push $NAME +# 4. update .circleci/config.yml to refer to the new name # TODO add xenial -FROM buildpack-deps:trusty-scm +FROM {base_image} RUN echo 'APT::Get::Assume-Yes "true";' > /etc/apt/apt.conf.d/90circleci \ && echo 'DPkg::Options "--force-confnew";' >> /etc/apt/apt.conf.d/90circleci @@ -40,7 +42,7 @@ RUN apt-get update \ netcat unzip zip jq \ python3-pip \ && ln -sf /usr/share/zoneinfo/Etc/UTC /etc/localtime \ - && { locale-gen en_US.UTF-8 || true; } \ + && {{ locale-gen en_US.UTF-8 || true; }} \ && echo "LC_ALL=en_US.UTF-8" | sudo tee -a /etc/default/locale # Set the locale, together with the locale-related steps above. @@ -61,9 +63,9 @@ ENV LC_ALL en_US.UTF-8 # Docker core... RUN set -e \ && export DOCKER_VERSION=$(curl --silent --fail --retry 3 https://download.docker.com/linux/static/stable/x86_64/ | grep -o -e 'docker-[.0-9]*-ce\.tgz' | sort -r | head -n 1) \ - && DOCKER_URL="https://download.docker.com/linux/static/stable/x86_64/${DOCKER_VERSION}" \ + && DOCKER_URL="https://download.docker.com/linux/static/stable/x86_64/${{DOCKER_VERSION}}" \ && echo Docker URL: $DOCKER_URL \ - && curl --silent --show-error --location --fail --retry 3 --output /tmp/docker.tgz "${DOCKER_URL}" \ + && curl --silent --show-error --location --fail --retry 3 --output /tmp/docker.tgz "${{DOCKER_URL}}" \ && ls -lha /tmp/docker.tgz \ && tar -xz -C /tmp -f /tmp/docker.tgz \ && mv /tmp/docker/* /usr/bin \ diff --git a/tools/circleci/generate-dockerfiles b/tools/circleci/generate-dockerfiles new file mode 100755 index 0000000000..555584b945 --- /dev/null +++ b/tools/circleci/generate-dockerfiles @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 + +import errno +import os + +import yaml + +def generate_dockerfile_directories(dockerfile_path: str) -> None: + if not os.path.exists(os.path.dirname(dockerfile_path)): + try: + os.makedirs(os.path.dirname(dockerfile_path)) + except OSError as e: + if e.errno != errno.EEXIST: + raise + +if __name__ == "__main__": + os.chdir(os.path.abspath(os.path.dirname(__file__))) + + with open("Dockerfile.template") as f: + docker_template = f.read() + + with open("images.yml") as f: + dockerfile_settings = yaml.safe_load(f) + + for distro in dockerfile_settings: + dockerfile_settings[distro]["generated_file_warning"] = """\ +# THIS IS A GENERATED FILE. DO NOT EDIT. +# See template: tools/circleci/Dockerfile.template +""" + + dockerfile_content = docker_template.format_map(dockerfile_settings[distro]) + dockerfile_path = "images/{}/Dockerfile".format(distro) + generate_dockerfile_directories(dockerfile_path) + with open(dockerfile_path, "w") as f: + f.write(dockerfile_content) diff --git a/tools/circleci/images.yml b/tools/circleci/images.yml new file mode 100644 index 0000000000..53addd4ca8 --- /dev/null +++ b/tools/circleci/images.yml @@ -0,0 +1,2 @@ +trusty: + base_image: buildpack-deps:trusty-scm