|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -eo pipefail |
| 3 | + |
| 4 | +USAGE="Given a manifest file with docker images, this script populates the Singularity cache with those images. |
| 5 | +Usage: $0 <image_manifest_file> <miniwdl_singularity_cache_dir>" |
| 6 | + |
| 7 | + |
| 8 | +# Check if the first argument is -h or --help |
| 9 | +if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then |
| 10 | + echo -e "${USAGE}" |
| 11 | + exit 0 |
| 12 | +fi |
| 13 | +# Check if at least two arguments are provided |
| 14 | +if [ $# -lt 2 ]; then |
| 15 | + echo -e "${USAGE}" |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | + |
| 19 | +image_manifest_file=$1 |
| 20 | +miniwdl_singularity_cache_dir=$2 |
| 21 | +# Check if the image manifest file exists and is readable, and if the cache directory exists and is writable |
| 22 | +[ -r "${image_manifest_file}" ] || (echo "${image_manifest_file} is not readable." >&2 && exit 1) |
| 23 | +if [ ! -d "${miniwdl_singularity_cache_dir}" ]; then |
| 24 | + echo "${miniwdl_singularity_cache_dir} does not exist. Creating it now..." |
| 25 | + mkdir -p "${miniwdl_singularity_cache_dir}" || (echo "Could not create ${miniwdl_singularity_cache_dir}/." >&2 && exit 1) |
| 26 | +fi |
| 27 | +[ -w "${miniwdl_singularity_cache_dir}" ] || (echo "${miniwdl_singularity_cache_dir}/ is not writable" >&2 && exit 1) |
| 28 | +singularity --version || (echo "singularity is not in path. Please install Singularity to use this script." >&2 && exit 1) |
| 29 | + |
| 30 | +# manifest file should contain one image per line, with no empty lines |
| 31 | +# image lines should be in the format: <image_name>:<tag> or <image_name>@sha256:<digest> |
| 32 | +# e.g., google/deepvariant:1.9.0 or quay.io/pacbio/some_image@sha256:abc123... |
| 33 | +while read -r image; do |
| 34 | + if [[ -n "${image}" ]]; then |
| 35 | + image_url="docker://${image}" |
| 36 | + # miniwdl singularity backend replaces ':' and '/' with '_' in the SIF file name |
| 37 | + sif_path="${image_url//:/_}" |
| 38 | + sif_path="${sif_path//\//_}" |
| 39 | + sif_path="${miniwdl_singularity_cache_dir}/${sif_path}.sif" |
| 40 | + if [ -f "${sif_path}" ]; then |
| 41 | + echo "Singularity image already exists: ${sif_path}" >&2 |
| 42 | + else |
| 43 | + echo "Pulling Singularity image: ${image_url}" |
| 44 | + singularity pull "${sif_path}" "${image_url}" |
| 45 | + fi |
| 46 | + fi |
| 47 | +done < "${image_manifest_file}" |
0 commit comments