Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templatization of Dockerfile #40

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions 1.14.5/bullseye/Dockerfile → Dockerfile
Original file line number Diff line number Diff line change
@@ -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=
Expand All @@ -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 \
Expand Down
108 changes: 108 additions & 0 deletions apply_templates.py
Original file line number Diff line number Diff line change
@@ -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()
File renamed without changes.
26 changes: 26 additions & 0 deletions version.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}