Skip to content

Commit

Permalink
moved nameing images to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Asa Rentschler committed May 24, 2024
1 parent c19d3ef commit 5927de0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
26 changes: 26 additions & 0 deletions lib/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):

Expand Down Expand Up @@ -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':
Expand Down
32 changes: 11 additions & 21 deletions lib/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion vtmp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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 '@'
Expand Down

0 comments on commit 5927de0

Please sign in to comment.