|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Unified container build script |
| 4 | +# Usage: |
| 5 | +# ./build_containers.sh # Build both Docker and Singularity (default) |
| 6 | +# ./build_containers.sh --docker # Build only Docker images |
| 7 | +# ./build_containers.sh --singularity # Build only Singularity images |
| 8 | +# ./build_containers.sh --all # Build both (explicit) |
| 9 | +# ./build_containers.sh --github_action # Build both for GitHub Actions |
| 10 | +# ./build_containers.sh --docker github_action # Build Docker for GitHub Actions |
| 11 | +# ./build_containers.sh --singularity github_action # Build Singularity for GitHub Actions |
| 12 | + |
| 13 | +set -e |
| 14 | + |
| 15 | +# Parse arguments |
| 16 | +build_docker=false |
| 17 | +build_singularity=false |
| 18 | +github_action_mode="" |
| 19 | +auto_detect=false |
| 20 | + |
| 21 | +# If no arguments provided, auto-detect available tools |
| 22 | +if [ $# -eq 0 ]; then |
| 23 | + auto_detect=true |
| 24 | +fi |
| 25 | + |
| 26 | +# Parse all arguments |
| 27 | +while [ $# -gt 0 ]; do |
| 28 | + case "$1" in |
| 29 | + --docker) |
| 30 | + build_docker=true |
| 31 | + ;; |
| 32 | + --singularity|--apptainer) |
| 33 | + build_singularity=true |
| 34 | + ;; |
| 35 | + --all) |
| 36 | + build_docker=true |
| 37 | + build_singularity=true |
| 38 | + ;; |
| 39 | + --github_action) |
| 40 | + github_action_mode="github_action" |
| 41 | + # If --github_action is used without --docker or --singularity, enable auto-detect |
| 42 | + if [ "$build_docker" = false ] && [ "$build_singularity" = false ]; then |
| 43 | + auto_detect=true |
| 44 | + fi |
| 45 | + ;; |
| 46 | + -h|--help) |
| 47 | + echo "Usage: $0 [OPTIONS] [github_action]" |
| 48 | + echo "" |
| 49 | + echo "Options:" |
| 50 | + echo " --docker Build Docker images only" |
| 51 | + echo " --singularity Build Singularity/Apptainer images only" |
| 52 | + echo " --apptainer Alias for --singularity" |
| 53 | + echo " --all Build both Docker and Singularity images" |
| 54 | + echo " (no options) Build both by default" |
| 55 | + echo "" |
| 56 | + echo "Additional arguments:" |
| 57 | + echo " github_action Enable GitHub Actions mode (saves to archive)" |
| 58 | + echo "" |
| 59 | + echo "Examples:" |
| 60 | + echo " $0 # Build both (default)" |
| 61 | + echo " $0 --docker # Build only Docker" |
| 62 | + echo " $0 --singularity github_action # Build Singularity for CI" |
| 63 | + exit 0 |
| 64 | + ;; |
| 65 | + *) |
| 66 | + echo "Unknown option: $1" |
| 67 | + echo "Use --help for usage information" |
| 68 | + exit 1 |
| 69 | + ;; |
| 70 | + esac |
| 71 | + shift |
| 72 | +done |
| 73 | + |
| 74 | +# Auto-detect available tools if no specific option was provided |
| 75 | +if [ "$auto_detect" = true ]; then |
| 76 | + echo "Auto-detecting available container tools..." |
| 77 | + |
| 78 | + if command -v docker &> /dev/null; then |
| 79 | + echo " ✓ Docker found" |
| 80 | + build_docker=true |
| 81 | + else |
| 82 | + echo " ✗ Docker not found" |
| 83 | + fi |
| 84 | + |
| 85 | + if command -v singularity &> /dev/null; then |
| 86 | + echo " ✓ Singularity/Apptainer found" |
| 87 | + build_singularity=true |
| 88 | + else |
| 89 | + echo " ✗ Singularity/Apptainer not found" |
| 90 | + fi |
| 91 | + |
| 92 | + # Check if at least one tool is available |
| 93 | + if [ "$build_docker" = false ] && [ "$build_singularity" = false ]; then |
| 94 | + echo "" |
| 95 | + echo "❌ Error: Neither Docker nor Singularity/Apptainer found on this system." |
| 96 | + echo "Please install at least one container tool to proceed." |
| 97 | + exit 1 |
| 98 | + fi |
| 99 | + |
| 100 | + echo "" |
| 101 | +fi |
| 102 | + |
| 103 | +# Save original working directory |
| 104 | +wd=$(pwd) |
| 105 | + |
| 106 | +# Get architecture for Docker builds |
| 107 | +arch=$(uname -m) |
| 108 | + |
| 109 | +# Build Docker images if requested |
| 110 | +if [ "$build_docker" = true ]; then |
| 111 | + echo "════════════════════════════════════════════════════════════════" |
| 112 | + echo " Building Docker images..." |
| 113 | + echo "════════════════════════════════════════════════════════════════" |
| 114 | + |
| 115 | + # List of image names |
| 116 | + image_list=( ) |
| 117 | + |
| 118 | + for dir in docker/*; do |
| 119 | + cd "${dir}" |
| 120 | + imgname=$(echo $dir | rev | cut -d/ -f1 | rev) |
| 121 | + image_list+=(${imgname}) |
| 122 | + |
| 123 | + echo ██████████████████▓▒░ Building ${imgname} ░▒▓██████████████████ |
| 124 | + |
| 125 | + # Set architecture to docker buildx |
| 126 | + docker_arch_option="" |
| 127 | + |
| 128 | + # Reditools2 and Pluviometer do not compile on arm64, force using amd64 compilation |
| 129 | + if [[ "$arch" == arm* || "$arch" == "aarch64" ]]; then |
| 130 | + if [[ $dir =~ "reditools2" ]] || [[ $dir =~ "pluviometer" ]]; then |
| 131 | + echo "$imgname does not compile on arm64, force using amd64 compilation" |
| 132 | + docker_arch_option=" --platform linux/amd64" |
| 133 | + fi |
| 134 | + fi |
| 135 | + |
| 136 | + docker build ${docker_arch_option} -t ${imgname} . |
| 137 | + |
| 138 | + # Back to the original working directory |
| 139 | + cd "$wd" |
| 140 | + done |
| 141 | + |
| 142 | + if [[ ${github_action_mode} == 'github_action' ]]; then |
| 143 | + echo "Saving docker images to cache..." |
| 144 | + docker save ${image_list[@]} -o docker-images.tar |
| 145 | + echo Archive size: $(stat --printf="%s" docker-images.tar) |
| 146 | + fi |
| 147 | + |
| 148 | + echo "" |
| 149 | +fi |
| 150 | + |
| 151 | +# Build Singularity images if requested |
| 152 | +if [ "$build_singularity" = true ]; then |
| 153 | + echo "════════════════════════════════════════════════════════════════" |
| 154 | + echo " Building Singularity/Apptainer images..." |
| 155 | + echo "════════════════════════════════════════════════════════════════" |
| 156 | + |
| 157 | + # Create output directory for .sif images |
| 158 | + mkdir -p sif_images |
| 159 | + |
| 160 | + # List to store built images |
| 161 | + sif_list=( ) |
| 162 | + |
| 163 | + # Loop through all .def files in singularity/ subdirectories |
| 164 | + for def_file in singularity/*/*.def; do |
| 165 | + # Extract image name from directory |
| 166 | + imgname=$(basename "$(dirname "$def_file")") |
| 167 | + sif_output="sif_images/${imgname}.sif" |
| 168 | + sif_list+=("$sif_output") |
| 169 | + |
| 170 | + echo ██████████████████▓▒░ Building ${imgname} ░▒▓██████████████████ |
| 171 | + |
| 172 | + # Build with fakeroot if available |
| 173 | + if command -v singularity &> /dev/null; then |
| 174 | + singularity build --fakeroot "$sif_output" "$def_file" |
| 175 | + else |
| 176 | + echo "❌ Singularity not found! Aborting." |
| 177 | + exit 1 |
| 178 | + fi |
| 179 | + done |
| 180 | + |
| 181 | + # If used in GitHub Actions, create an archive |
| 182 | + if [[ ${github_action_mode} == 'github_action' ]]; then |
| 183 | + echo "Saving SIF images to cache archive..." |
| 184 | + tar -cf singularity-images.tar "${sif_list[@]}" |
| 185 | + echo Archive size: $(stat --printf="%s" singularity-images.tar) |
| 186 | + fi |
| 187 | + |
| 188 | + echo "" |
| 189 | +fi |
| 190 | + |
| 191 | +echo "════════════════════════════════════════════════════════════════" |
| 192 | +echo " ✓ Build complete!" |
| 193 | +echo "════════════════════════════════════════════════════════════════" |
0 commit comments