From d3469e9b81fdc4661e19a2181fc3f26eda9c5283 Mon Sep 17 00:00:00 2001 From: AbcSxyZ Date: Sun, 12 Dec 2021 00:15:50 +0100 Subject: [PATCH 1/2] Templatization of Dockerfile --- 1.14.5/bullseye/Dockerfile | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/1.14.5/bullseye/Dockerfile b/1.14.5/bullseye/Dockerfile index d249631..213e96c 100644 --- a/1.14.5/bullseye/Dockerfile +++ b/1.14.5/bullseye/Dockerfile @@ -1,9 +1,9 @@ -FROM debian:bullseye-slim AS verify +FROM debian:%{variant} AS verify WORKDIR /verify # Specify release variables -ARG RLS_VERSION=1.14.5 +ARG RLS_VERSION=%{version} ARG RLS_OS=linux ARG RLS_LIB=gnu ARG RLS_ARCH= @@ -27,10 +27,10 @@ ARG REPO_GITIAN_SIGS=https://github.com/dogecoin/gitian.sigs.git ARG REPO_DOGECOIN_CORE=https://github.com/dogecoin/dogecoin.git # Pinned known sha256sums -RUN echo f3bc387f393a0d55b6f653aef24febef6cb6f352fab2cbb0bae420bddcdacd1c dogecoin-1.14.5-aarch64-linux-gnu.tar.gz > SHASUMS \ - && echo dfdcdc6bb36076e7634cc8ed89138ec0383d73ba42b3e7ecfa9279b8949bce6b dogecoin-1.14.5-arm-linux-gnueabihf.tar.gz >> SHASUMS \ - && echo 7e7dd731ecfb2b78d6cc50d013ebf5faceeab50c59ffa2ab7551167b1bb81f08 dogecoin-1.14.5-i686-pc-linux-gnu.tar.gz >> SHASUMS \ - && echo 17a03f019168ec5283947ea6fbf1a073c1d185ea9edacc2b91f360e1c191428e dogecoin-1.14.5-x86_64-linux-gnu.tar.gz >> SHASUMS +RUN echo %{shasum_arm64} %{file_arm64} > SHASUMS \ + && echo %{shasum_armv7} %{file_armv7} >> SHASUMS \ + && echo %{shasum_386} %{file_386} >> SHASUMS \ + && echo %{shasum_amd64} %{file_amd64} >> SHASUMS # install system requirements RUN apt update && apt install -y \ From 0107ae3d62976754da1d6e2e369b0eb25f2ad49a Mon Sep 17 00:00:00 2001 From: AbcSxyZ Date: Sun, 12 Dec 2021 00:19:45 +0100 Subject: [PATCH 2/2] create apply_templates.py script --- 1.14.5/bullseye/Dockerfile => Dockerfile | 0 apply_templates.py | 108 ++++++++++++++++++ .../bullseye/entrypoint.py => entrypoint.py | 0 version.json | 26 +++++ 4 files changed, 134 insertions(+) rename 1.14.5/bullseye/Dockerfile => Dockerfile (100%) create mode 100644 apply_templates.py rename 1.14.5/bullseye/entrypoint.py => entrypoint.py (100%) create mode 100644 version.json diff --git a/1.14.5/bullseye/Dockerfile b/Dockerfile similarity index 100% rename from 1.14.5/bullseye/Dockerfile rename to Dockerfile diff --git a/apply_templates.py b/apply_templates.py new file mode 100644 index 0000000..172e9f4 --- /dev/null +++ b/apply_templates.py @@ -0,0 +1,108 @@ +#!/usr/bin/env python3 + +""" +Clean and generate all files for Dogecoin Core images using Dockerfile +templates, copying entrypoint.py. Use a manifest (version.json) +to create images tree structure. + +Use python variables in templates using DockerfileTemplate.delimiter `%`. +Regular Dockerfile variable subsitution symbol `$` won't be alterate. + +Template instruction example: + +RUN FOO=%{bar} +RUN FILENAME=%{var_1}-%var2 +""" + +from string import Template +import shutil +import json +import os + +class DockerfileTemplate(Template): + """ + Interface to generate Dockerfiles from base templates. + + Enable 2 type of variable in Dockerfiles templates, + variables with `%` are used by python templates, + variable with `$` are regular variable used by Dockerfiles. + + Example: `%version` or `%{version}` + """ + delimiter = "%" + +def release_files(version_info): + """ + Format version release files for all architectures with + respective checksum. + + Prepare dictionnary for template arguments. + """ + archs_list = version_info["arches"] + + files = {} + for architecture, asset in archs_list.items(): + #Remove 'arm/v7' slash, to use armv7 as python variable + architecture = architecture.replace("/", "") + + #Store tar filename and checksum + files[f"file_{architecture}"] = asset['file'] + files[f"shasum_{architecture}"] = asset['shasum'] + + return files + +def load_template(base): + """Load a Dockerfile base template""" + with open(base, encoding="utf8") as dockerfile_stream: + return DockerfileTemplate(dockerfile_stream.read()) + +def render_dockerfile(template_vars): + """ + Generate Dockerfile & entrypoint for a single image. + Save result in `version/variant` directory. + """ + #Load base template used by the variant + template = load_template("Dockerfile") + + #Replace python variable in the base template + dockerfile_content = template.substitute(template_vars) + + #Define destination files + version = template_vars['version'] + variant = template_vars['variant'] + + directory = os.path.join(version, variant) + dockerfile_path = os.path.join(directory, "Dockerfile") + entrypoint_path = os.path.join(directory, "entrypoint.py") + + #Write Dockerfile and entrypoint for the image + os.makedirs(directory) + + shutil.copy("entrypoint.py", entrypoint_path) + with open(dockerfile_path, "w", encoding="utf8") as dockerfile: + dockerfile.write(dockerfile_content) + +def generate_images_files(): + """ + Generate Dockerfile and entrypoint for all images according to + a manifest. + """ + # Get manifest file for images versions/variant/architectures + with open("version.json", encoding="utf8") as versions_json: + manifest = json.load(versions_json) + + for version in manifest: + #Clean previous images + shutil.rmtree(version, ignore_errors=True) + + #Prepate template variables + template_vars = {"version" : version} + template_vars.update(release_files(manifest[version])) + + #Generate image files for each variant of the version + for variant in manifest[version]['variants']: + template_vars['variant'] = variant + render_dockerfile(template_vars) + +if __name__ == "__main__": + generate_images_files() diff --git a/1.14.5/bullseye/entrypoint.py b/entrypoint.py similarity index 100% rename from 1.14.5/bullseye/entrypoint.py rename to entrypoint.py diff --git a/version.json b/version.json new file mode 100644 index 0000000..a9b2ab7 --- /dev/null +++ b/version.json @@ -0,0 +1,26 @@ +{ + "1.14.5": { + "arches" : { + "amd64" : { + "file" : "dogecoin-1.14.5-x86_64-linux-gnu.tar.gz", + "shasum" : "17a03f019168ec5283947ea6fbf1a073c1d185ea9edacc2b91f360e1c191428e" + }, + "386" : { + "file" : "dogecoin-1.14.5-i686-pc-linux-gnu.tar.gz", + "shasum" : "7e7dd731ecfb2b78d6cc50d013ebf5faceeab50c59ffa2ab7551167b1bb81f08" + }, + "arm/v7" : { + "file" : "dogecoin-1.14.5-arm-linux-gnueabihf.tar.gz", + "shasum" : "dfdcdc6bb36076e7634cc8ed89138ec0383d73ba42b3e7ecfa9279b8949bce6b" + }, + "arm64" : { + "file" : "dogecoin-1.14.5-aarch64-linux-gnu.tar.gz", + "shasum" : "f3bc387f393a0d55b6f653aef24febef6cb6f352fab2cbb0bae420bddcdacd1c" + } + }, + "variants": [ + "bullseye-slim", + "buster-slim" + ] + } +}