From 5927de06ed3607123b73a4aa79730ddfb851b22f Mon Sep 17 00:00:00 2001 From: Asa Rentschler Date: Fri, 24 May 2024 14:26:33 -0400 Subject: [PATCH] moved nameing images to backend --- lib/backends.py | 26 ++++++++++++++++++++++++++ lib/build.py | 32 +++++++++++--------------------- vtmp.vim | 2 +- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/lib/backends.py b/lib/backends.py index abc7d64..cd169ae 100644 --- a/lib/backends.py +++ b/lib/backends.py @@ -42,10 +42,24 @@ def __init__(self, name: str, variables: dict = None) -> None: @abstractmethod def generate_script(self, file: Path, variables: dict) -> list: + """ + Generate a build script e.g. .dockerfile/.def + """ pass @abstractmethod def generate_build_cmd(self, src: str, dest: str, args: list = None) -> str: + """ + Generate CLI command to build image + """ + pass + + @abstractmethod + def format_image_name(self, path: Path, tag: str) -> str: + pass + + @abstractmethod + def clean_up_old_image_cmd(self, name: str) -> str: pass def _get_sections(self, template: list) -> dict: @@ -218,6 +232,12 @@ def generate_build_cmd(self, src: str, dest: str, args: list = None) -> str: cmd = ['podman build', arguments, script, destination, end] return ''.join(_ for _ in cmd) + def format_image_name(self, path: Path, tag: str) -> str: + return f"{'localhost/' if '/' not in tag else ''}{tag}{':latest' if ':' not in tag else ''}" + + def clean_up_old_image_cmd(self, name: str) -> str: + return f'podman untag {name}' + class Apptainer(Backend): @@ -324,6 +344,12 @@ def generate_build_cmd(self, src: str, dest: str, args: list = None) -> str: cmd = ['apptainer build', arguments, destination, script, end] return ''.join(_ for _ in cmd) + def format_image_name(self, path: Path, tag: str) -> str: + return f"{Path.joinpath(path, tag)}{'.sif' if '.sif' not in tag else ''}" + + def clean_up_old_image_cmd(self, name: str) -> str: + return 'echo' + def get_backend(name: str, variables: dict) -> Backend: if name == 'podman': diff --git a/lib/build.py b/lib/build.py index 9d265cd..df9635d 100644 --- a/lib/build.py +++ b/lib/build.py @@ -134,29 +134,19 @@ def build(self): last = None # last image that was built for u in self.build_units: - if self.backend == 'podman': - if u == self.build_units[-1]: # if this is the last image - name = str(self.build_name if self.build_name is not None else - f'{u.node.name}__{u.node.tag}__{self.system}__{self.distro}') - if '/' not in name and ':' not in name: - name = f'localhost/{name}:latest' - else: - name = f'localhost/{u.build_id}:latest' - elif self.backend == 'apptainer': - if u == self.build_units[-1]: # if this is the last image - name = str(Path.joinpath(pwd.absolute(), - self.build_name if self.build_name is not None else - f'{u.node.name}__{u.node.tag}__{self.system}__{self.distro}')) - if '.sif' not in name: - name += '.sif' - else: - name = str(Path.joinpath(self.build_dir, u.build_id, f'{u.build_id}.sif')) + + if u == self.build_units[-1]: + sorted_specs = [(bu.node.name, bu.node.tag) for bu in self.build_units] + sorted_specs.sort() + tag = str(self.build_name if self.build_name is not None else + f"{'_'.join(f'{bu[0]}-{bu[1]}' for bu in sorted_specs)}__{self.system}-{self.distro}") + name = self.backend_engine.format_image_name(Path(pwd.absolute()), tag) else: - raise BackendNotSupported + name = self.backend_engine.format_image_name(Path.joinpath(self.build_dir, u.build_id), u.build_id) self._build_image(u, last, name) - if self.backend == 'podman' and not self.dry_run and not self.leave_tags and last is not None: - run(f'podman untag {last}') + if not self.dry_run and not self.leave_tags and last is not None: + run(self.backend_engine.clean_up_old_image_cmd(last)) last = name # go back to the starting dir @@ -286,4 +276,4 @@ def _build_image(self, unit: BuildUnit, src_image: str, name: str): TextBlock(f"{datetime.timedelta(seconds=round(end - start))}", fore=Fore.MAGENTA, style=Style.BRIGHT), TextBlock(']') ]) - print() # new line + print() # new line diff --git a/vtmp.vim b/vtmp.vim index c864cd2..923fb85 100644 --- a/vtmp.vim +++ b/vtmp.vim @@ -16,7 +16,7 @@ syn match codeBrackets ')' syn match variableOperator '%' " velocity variables -syn keyword velocityVariable __backend__ __base__ __distro__ __hash__ __name__ __system__ __tag__ __timestamp__ +syn keyword velocityVariable __backend__ __base__ __distro__ __hash__ __name__ __system__ __tag__ __timestamp__ __threads__ " velocity operatives syn match velocityOperative '@'