-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsetup_containers.sh
More file actions
executable file
·351 lines (289 loc) · 11.2 KB
/
Copy pathsetup_containers.sh
File metadata and controls
executable file
·351 lines (289 loc) · 11.2 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/bin/bash
#
# setup_containers.sh
#
# Downloads and converts NeMo-Skills containers to .sqsh format in parallel using Slurm.
#
# Usage:
# sbatch --account=<account> scripts/setup_containers.sh --config FILE [output_dir] [--platform PLATFORM] [--force]
#
# Options:
# --config FILE Container definitions YAML file (required)
# output_dir Directory to save .sqsh files (default: ./containers)
# --platform PLATFORM Target platform: amd64 | arm64 (default: auto-detect from host)
# --force Force re-download existing containers
#
# Examples:
# sbatch --account=llmservice_modelalignment_sft scripts/setup_containers.sh --config cluster_configs/my_containers.yaml ./containers
# sbatch --account=llmservice_modelalignment_sft scripts/setup_containers.sh --config cluster_configs/my_containers.yaml ./containers --platform arm64
# sbatch --account=llmservice_modelalignment_sft scripts/setup_containers.sh --config cluster_configs/my_containers.yaml ./containers --force
#
# Platform support:
# - Some containers are multi-arch (same tag for amd64/arm64)
# - Some containers have platform-specific tags defined in containers.yaml
# - Containers without support for the target platform are skipped
#SBATCH --partition=cpu
#SBATCH --nodes=1
#SBATCH --ntasks=9
#SBATCH --time=04:00:00
#SBATCH --job-name=nemo-containers
#SBATCH --output=outputs/logs/slurm-containers-%j.out
set -e
# =============================================================================
# Configuration
# =============================================================================
# Resolve project root (works for both sbatch and direct execution)
if [[ -n "$SLURM_SUBMIT_DIR" ]]; then
PROJECT_ROOT="$SLURM_SUBMIT_DIR"
else
SCRIPT_PATH="$(readlink -f "${BASH_SOURCE[0]}")"
PROJECT_ROOT="$(dirname "$(dirname "$SCRIPT_PATH")")"
fi
cd "$PROJECT_ROOT" || { echo "ERROR: Could not cd to $PROJECT_ROOT"; exit 1; }
# =============================================================================
# Output Helpers
# =============================================================================
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; BLUE='\033[0;34m'; NC='\033[0m'
print_header() { echo -e "\n${BLUE}=== $1 ===${NC}\n"; }
print_success() { echo -e "${GREEN}✓ $1${NC}"; }
print_error() { echo -e "${RED}✗ $1${NC}"; }
print_warning() { echo -e "${YELLOW}! $1${NC}"; }
print_info() { echo -e "${BLUE}→ $1${NC}"; }
# =============================================================================
# Utility Functions
# =============================================================================
# Detect host architecture, normalized to amd64 or arm64
get_host_arch() {
case "$(uname -m)" in
x86_64) echo "amd64" ;;
aarch64) echo "arm64" ;;
*) echo "" ;; # Unsupported
esac
}
# Validate and normalize platform
validate_platform() {
case "$1" in
amd64|x86_64) echo "amd64" ;;
arm64|aarch64) echo "arm64" ;;
*) echo "" ;; # Invalid
esac
}
# Convert platform to enroot arch format (uname -m style)
platform_to_enroot_arch() {
case "$1" in
amd64) echo "x86_64" ;;
arm64) echo "aarch64" ;;
esac
}
# Extract tag from image (e.g., "repo:v1.0" -> "v1.0")
get_image_tag() {
local tag="${1##*:}"
[[ "$tag" == "$1" ]] && echo "latest" || echo "$tag"
}
# =============================================================================
# Dependency Check
# =============================================================================
check_dependencies() {
print_header "Checking Dependencies"
local missing=()
if ! command -v yq &>/dev/null; then missing+=("yq"); fi
if ! command -v enroot &>/dev/null; then missing+=("enroot"); fi
if [[ ${#missing[@]} -gt 0 ]]; then
print_error "Missing: ${missing[*]}"
echo " yq: brew install yq (macOS) or https://github.com/mikefarah/yq"
echo " enroot: module load enroot (or contact cluster admin)"
exit 1
fi
print_success "All dependencies found"
}
# =============================================================================
# Container Parsing
# =============================================================================
parse_containers() {
print_header "Parsing Containers"
CONTAINER_NAMES=()
CONTAINER_IMAGES=()
local skipped=()
# Read each container from YAML
local names=$(yq -r '.containers | keys | .[]' "$YAML_FILE")
for name in $names; do
local value_type=$(yq -r ".containers.${name} | type" "$YAML_FILE")
# Handle both yq formats: "string" (v4) and "!!str" (older/different versions)
if [[ "$value_type" == "string" || "$value_type" == "!!str" ]]; then
# Multi-arch image (single string)
CONTAINER_NAMES+=("$name")
CONTAINER_IMAGES+=("$(yq -r ".containers.${name}" "$YAML_FILE")")
else
# Platform-specific (nested object)
local image=$(yq -r ".containers.${name}.${PLATFORM} // \"\"" "$YAML_FILE")
if [[ -n "$image" && "$image" != "null" ]]; then
CONTAINER_NAMES+=("$name")
CONTAINER_IMAGES+=("$image")
else
skipped+=("$name")
fi
fi
done
if [[ ${#CONTAINER_NAMES[@]} -eq 0 ]]; then
print_error "No containers found for platform: $PLATFORM"
exit 1
fi
print_success "Found ${#CONTAINER_NAMES[@]} containers for $PLATFORM"
if [[ ${#skipped[@]} -gt 0 ]]; then
print_warning "Skipped (no $PLATFORM support): ${skipped[*]}"
fi
}
# =============================================================================
# Download
# =============================================================================
download_containers() {
local output_dir="$1"
local enroot_arch=$(platform_to_enroot_arch "$PLATFORM")
print_header "Downloading Containers"
print_info "Architecture: $PLATFORM ($enroot_arch)"
local launched=0
for i in "${!CONTAINER_NAMES[@]}"; do
local name="${CONTAINER_NAMES[$i]}"
local image="${CONTAINER_IMAGES[$i]}"
local tag=$(get_image_tag "$image")
local outfile="${output_dir}/${name}-${tag}.sqsh"
if [[ -f "$outfile" && "$FORCE" == false ]]; then
print_warning "Skipping $name (exists)"
continue
fi
if [[ "$FORCE" == true && -f "$outfile" ]]; then rm -f "$outfile"; fi
print_info "Downloading: $name"
srun --ntasks=1 --exclusive enroot import --arch "$enroot_arch" --output "$outfile" "docker://$image" &
launched=$((launched + 1))
done
if [[ $launched -gt 0 ]]; then
print_info "Waiting for $launched downloads..."
wait
else
print_info "No new containers to download"
fi
}
# =============================================================================
# Config Snippet Generator
# =============================================================================
generate_config_snippet() {
local output_dir=$(readlink -f "$1")
print_header "Configuration Snippet"
echo "Add to your cluster config after you create it: <REPO_ROOT>/cluster_configs/my_cluster.yaml"
echo ""
echo "containers:"
local found=0
for i in "${!CONTAINER_NAMES[@]}"; do
local name="${CONTAINER_NAMES[$i]}"
local tag=$(get_image_tag "${CONTAINER_IMAGES[$i]}")
local sqsh_file="$output_dir/${name}-${tag}.sqsh"
# Only include containers that actually exist
if [[ -f "$sqsh_file" ]]; then
echo " $name: $sqsh_file"
found=$((found + 1))
fi
done
echo ""
if [[ $found -lt ${#CONTAINER_NAMES[@]} ]]; then
print_warning "Some containers are missing - re-run to download them"
fi
}
# =============================================================================
# Argument Parsing
# =============================================================================
parse_arguments() {
OUTPUT_DIR="./containers"
PLATFORM=""
FORCE=false
CONFIG_FILE=""
while [[ $# -gt 0 ]]; do
case "$1" in
--config)
[[ -z "$2" || "$2" =~ ^-- ]] && { print_error "--config requires a file path"; exit 1; }
CONFIG_FILE="$2"; shift 2 ;;
--platform)
[[ -z "$2" || "$2" =~ ^-- ]] && { print_error "--platform requires a value (amd64 or arm64)"; exit 1; }
PLATFORM="$2"; shift 2 ;;
--force)
FORCE=true; shift ;;
-*)
print_error "Unknown option: $1"; exit 1 ;;
*)
OUTPUT_DIR="$1"; shift ;;
esac
done
# Require --config
if [[ -z "$CONFIG_FILE" ]]; then
print_error "--config is required."
echo ""
echo " Usage: sbatch --account=<acct> scripts/setup_containers.sh --config <FILE> [output_dir]"
echo ""
echo " Create a config from the template if you haven't already:"
echo " cp cluster_configs/containers.yaml cluster_configs/my_containers.yaml"
echo " # Edit my_containers.yaml with your registry paths"
exit 1
fi
# Auto-detect platform if not specified
if [[ -z "$PLATFORM" ]]; then
PLATFORM=$(get_host_arch)
if [[ -z "$PLATFORM" ]]; then
print_error "Unsupported host architecture: $(uname -m). Use --platform to specify amd64 or arm64"
exit 1
fi
else
PLATFORM=$(validate_platform "$PLATFORM")
if [[ -z "$PLATFORM" ]]; then
print_error "Invalid platform. Supported: amd64, arm64"
exit 1
fi
fi
}
# =============================================================================
# Main
# =============================================================================
mkdir -p outputs/logs
parse_arguments "$@"
YAML_FILE="$CONFIG_FILE"
[[ -f "$YAML_FILE" ]] || { print_error "$YAML_FILE not found"; exit 1; }
check_dependencies
# Warn if cross-platform download
HOST_ARCH=$(get_host_arch)
if [[ -n "$HOST_ARCH" && "$HOST_ARCH" != "$PLATFORM" ]]; then
print_warning "Downloading $PLATFORM containers on $HOST_ARCH host (won't run locally)"
elif [[ -z "$HOST_ARCH" ]]; then
HOST_ARCH="unknown"
fi
parse_containers
mkdir -p "$OUTPUT_DIR"
print_header "Configuration"
cat <<EOF
Config: $YAML_FILE
Output: $OUTPUT_DIR
Platform: $PLATFORM (host: $HOST_ARCH)
Force: $FORCE
Containers: ${#CONTAINER_NAMES[@]}
EOF
download_containers "$OUTPUT_DIR"
print_header "Summary"
# Check which containers exist vs. expected
FAILED_DOWNLOADS=()
for i in "${!CONTAINER_NAMES[@]}"; do
name="${CONTAINER_NAMES[$i]}"
tag=$(get_image_tag "${CONTAINER_IMAGES[$i]}")
sqsh_file="$OUTPUT_DIR/${name}-${tag}.sqsh"
if [[ ! -f "$sqsh_file" ]]; then
FAILED_DOWNLOADS+=("$name")
fi
done
ls -lh "$OUTPUT_DIR"/*.sqsh 2>/dev/null || print_warning "No .sqsh files found"
if [[ ${#FAILED_DOWNLOADS[@]} -gt 0 ]]; then
echo ""
print_error "Missing: ${FAILED_DOWNLOADS[*]}"
fi
generate_config_snippet "$OUTPUT_DIR"
if [[ ${#FAILED_DOWNLOADS[@]} -gt 0 ]]; then
print_warning "Completed with errors - ${#FAILED_DOWNLOADS[@]} container(s) failed"
exit 1
else
print_success "Done!"
fi