Skip to content

Commit 81f6a18

Browse files
committed
refactor: use templates and change CI flow
1 parent 73757e6 commit 81f6a18

File tree

5 files changed

+49
-58
lines changed

5 files changed

+49
-58
lines changed

.github/workflows/main.yml

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Singularity container build
1+
name: Build Singularity images
22

33
on:
44
push:
@@ -14,32 +14,31 @@ jobs:
1414
matrix:
1515
python-version:
1616
- '3.10'
17+
singularity-version:
18+
- '3.8.3'
1719
steps:
1820
- name: Checkout bot code
1921
uses: actions/checkout@v3
20-
- name: Set up Python ${{ matrix.python-version }}
21-
uses: actions/setup-python@v2
22-
with:
23-
python-version: ${{ matrix.python-version }}
2422
- name: Cache downloads
25-
uses: actions/cache@v1
23+
uses: actions/cache@v2
2624
with:
2725
path: |
2826
~/.cache/pip
29-
/usr/local/bin/singularity
30-
/usr/local/go/
31-
key: cache-${{ matrix.python-version }}
32-
- name: Install singularity
33-
run: |
34-
bash install_singularity.sh
27+
key: ${{ runner.os }}_Python-${{ matrix.python-version }}_Singularity-${{ matrix.singularity-version }}
28+
- name: Set up Python ${{ matrix.python-version }}
29+
uses: actions/setup-python@v3
30+
with:
31+
python-version: ${{ matrix.python-version }}
3532
- name: Install Python dependencies
3633
run: |
3734
python -m pip install --requirement requirements.in
38-
- name: Get list of containers to build
35+
- name: Fetch images and generate build script
3936
run: |
40-
python get_container_list.py
41-
- name: Build containers
37+
./populate_build.py
38+
- name: Set up Singularity ${{ matrix.singularity-version }}
39+
uses: eWaterCycle/setup-singularity@v7
40+
with:
41+
singularity-version: ${{ matrix.singularity-version }}
42+
- name: Build images
4243
run: |
43-
echo "${{ secrets.DEPOT_GALAXYPROJECT_ORG }}" > ssh_key && chmod 600 ssh_key
44-
cat build.sh
45-
bash build.sh
44+
./build.sh

build.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
SOURCE='docker://quay.io/biocontainers'
6+
DESTINATION='[email protected]:/srv/nginx/depot.galaxyproject.org/root/singularity/'

image_template.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
singularity build ${img} "$${SOURCE}/${img}" > /dev/null 2>&1 \
3+
&& rsync -azq -e 'ssh -i ssh_key -o StrictHostKeyChecking=no' ./${img} "$${DESTINATION}" \
4+
&& rm ${img} \
5+
&& singularity cache clean --type blob --force \
6+
&& echo 'Container ${img} built (${idx}/${total}).'

install_singularity.sh

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22

33

4-
"""Provide a command line tool for finding new container images and building them."""
4+
"""Provide a command line tool for building Singularity images from BioContainers."""
55

66

77
import argparse
@@ -11,6 +11,7 @@
1111
from functools import partial
1212
from html.parser import HTMLParser
1313
from pathlib import Path
14+
from string import Template
1415
from typing import List, Tuple, Dict, Optional, Iterable
1516

1617
import aiometer
@@ -313,13 +314,13 @@ def parse_denylist(filename: Path) -> List[str]:
313314
return [entry for line in handle.readlines() if (entry := line.strip())]
314315

315316

316-
def generate_build_script(filename: Path, images: List[str]) -> None:
317-
"""Generate a build script with one new image per line."""
318-
with filename.open("w") as handle:
317+
def generate_build_script(filename: Path, images: List[str], template: Path) -> None:
318+
"""Generate a build script from provided templates."""
319+
with template.open() as handle:
320+
img_template = Template(handle.read())
321+
with filename.open("a") as handle:
319322
for idx, img in enumerate(images, start=1):
320-
handle.write(
321-
f"sudo singularity build {img} docker://quay.io/biocontainers/{img} > /dev/null 2>&1 && rsync -azq -e 'ssh -i ssh_key -o StrictHostKeyChecking=no' ./{img} [email protected]:/srv/nginx/depot.galaxyproject.org/root/singularity/ && rm {img} && echo 'Container {img} built ({idx}/{len(images)}).'\n"
322-
)
323+
handle.write(img_template.substitute(img=img, idx=idx, total=len(images)))
323324

324325

325326
def parse_args(argv: Optional[List[str]] = None) -> argparse.Namespace:
@@ -345,6 +346,15 @@ def parse_args(argv: Optional[List[str]] = None) -> argparse.Namespace:
345346
type=Path,
346347
help=f"Output the Singularity build script (default '{default_build_script}').",
347348
)
349+
default_image_template = Path("image_template.sh")
350+
parser.add_argument(
351+
"--image-template",
352+
metavar="PATH",
353+
default=default_image_template,
354+
type=Path,
355+
help=f"The template for building a single Singularity image (default "
356+
f"'{default_image_template}'). Uses Python `string.Template` syntax.",
357+
)
348358
default_quay_api = "https://quay.io/api/v1/"
349359
parser.add_argument(
350360
"--quay-api",
@@ -384,6 +394,8 @@ def main(argv: Optional[List[str]] = None) -> None:
384394
handlers=[RichHandler(markup=True, rich_tracebacks=True)],
385395
)
386396
assert args.denylist.is_file(), f"File not found '{args.denylist}'."
397+
assert args.build_script.is_file(), f"File not found '{args.build_script}'."
398+
assert args.image_template.is_file(), f"File not found '{args.image_template}'."
387399
args.build_script.parent.mkdir(parents=True, exist_ok=True)
388400
logger.info("Fetching quay.io BioContainers images.")
389401
quay_images = asyncio.run(QuayImageFetcher.fetch_all(api_url=args.quay_api))
@@ -400,7 +412,7 @@ def main(argv: Optional[List[str]] = None) -> None:
400412
logger.warning("No new images found.")
401413
return
402414
logger.info(f"{len(images):,} new images found. Generating build script.")
403-
generate_build_script(args.build_script, images)
415+
generate_build_script(args.build_script, images, args.image_template)
404416

405417

406418
if __name__ == "__main__":

0 commit comments

Comments
 (0)