Improve Dockerfile recipe generation (#35187)

- Update default image to Ubuntu 22.04 (previously was still Ubuntu 18.04)
- Optionally use depfiles to install the environment within the container
- Allow extending Dockerfile Jinja2 template
- Allow extending Singularity definition file Jinja2 template
- Deprecate previous options to add extra instructions
This commit is contained in:
Massimiliano Culpo
2023-04-03 21:05:19 +02:00
committed by GitHub
parent 3d149a7db2
commit f91968cf6f
6 changed files with 163 additions and 15 deletions

View File

@@ -39,10 +39,10 @@ def validate(configuration_file):
# Ensure we have a "container" attribute with sensible defaults set
env_dict = ev.config_dict(config)
env_dict.setdefault(
"container", {"format": "docker", "images": {"os": "ubuntu:18.04", "spack": "develop"}}
"container", {"format": "docker", "images": {"os": "ubuntu:22.04", "spack": "develop"}}
)
env_dict["container"].setdefault("format", "docker")
env_dict["container"].setdefault("images", {"os": "ubuntu:18.04", "spack": "develop"})
env_dict["container"].setdefault("images", {"os": "ubuntu:22.04", "spack": "develop"})
# Remove attributes that are not needed / allowed in the
# container recipe

View File

@@ -7,6 +7,7 @@
"""
import collections
import copy
from typing import Optional
import spack.environment as ev
import spack.schema.env
@@ -131,6 +132,9 @@ class PathContext(tengine.Context):
directly via PATH.
"""
# Must be set by derived classes
template_name: Optional[str] = None
def __init__(self, config, last_phase):
self.config = ev.config_dict(config)
self.container_config = self.config["container"]
@@ -146,6 +150,10 @@ def __init__(self, config, last_phase):
# Record the last phase
self.last_phase = last_phase
@tengine.context_property
def depfile(self):
return self.container_config.get("depfile", False)
@tengine.context_property
def run(self):
"""Information related to the run image."""
@@ -280,7 +288,8 @@ def render_phase(self):
def __call__(self):
"""Returns the recipe as a string"""
env = tengine.make_environment()
t = env.get_template(self.template_name)
template_name = self.container_config.get("template", self.template_name)
t = env.get_template(template_name)
return t.render(**self.to_dict())

View File

@@ -63,6 +63,8 @@
},
# Add labels to the image
"labels": {"type": "object"},
# Use a custom template to render the recipe
"template": {"type": "string", "default": None},
# Add a custom extra section at the bottom of a stage
"extra_instructions": {
"type": "object",
@@ -82,6 +84,16 @@
},
},
"docker": {"type": "object", "additionalProperties": False, "default": {}},
"depfile": {"type": "boolean", "default": False},
},
"deprecatedProperties": {
"properties": ["extra_instructions"],
"message": (
"container:extra_instructions has been deprecated and will be removed "
"in Spack v0.21. Set container:template appropriately to use custom Jinja2 "
"templates instead."
),
"error": False,
},
}