-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathdockerfiles.py
47 lines (32 loc) · 1.48 KB
/
dockerfiles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import dataclasses
import datetime
import json
import logging
from collections.abc import Mapping
from typing import Any
from jinja2 import Environment, FileSystemLoader, select_autoescape
from .settings import DOCKERFILES_PATH
from .versions import BuildVersion
logger = logging.getLogger("dpn")
env = Environment(loader=FileSystemLoader("./templates"), autoescape=select_autoescape())
def _render_template(template_name: str, context: Mapping[str, Any]) -> str:
template = env.get_template(template_name)
return template.render(context)
def render_dockerfile(version: BuildVersion) -> str:
distro = "debian" if version.distro != "alpine" else version.distro
context = dataclasses.asdict(version) | {
"now": datetime.datetime.now(datetime.UTC).isoformat()[:-7],
"distro": "bookworm" if version.distro == "slim" else version.distro, # slim is an image variant
"distro_variant": "slim" if version.distro == "slim" else "full",
}
return _render_template(f"{distro}.Dockerfile", context)
def render_dockerfile_with_context(config_json: str, dry_run: bool = False) -> None:
version = BuildVersion(**json.loads(config_json))
dockerfile = render_dockerfile(version)
filename = f"{version.key}.Dockerfile"
logger.debug(f"Writing {filename}")
if not dry_run:
if not DOCKERFILES_PATH.exists():
DOCKERFILES_PATH.mkdir()
dockerfile_path = DOCKERFILES_PATH / filename
dockerfile_path.write_text(dockerfile)