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