File size: 12,017 Bytes
c334927
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07bc07f
aa554d0
07bc07f
 
 
 
195496c
 
07bc07f
 
 
 
 
 
 
 
195496c
aa554d0
07bc07f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deed508
07bc07f
 
 
 
 
 
 
deed508
07bc07f
 
 
deed508
07bc07f
 
 
 
deed508
07bc07f
 
 
 
 
 
 
deed508
07bc07f
 
deed508
07bc07f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deed508
 
07bc07f
 
 
 
 
deed508
 
07bc07f
deed508
07bc07f
 
 
 
 
 
deed508
07bc07f
 
 
 
 
c334927
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/bin/bash
# =============================================================
# HACKATHON SETUP: STEP 1 OF 2 - TEAM LEAD RUNS FIRST
# =============================================================
# EXECUTION ORDER (IMPORTANT):
# 1. The TEAM LEAD configures and runs THIS script first.
#    Run: source hackathon_setup.sh
#    Wait until it finishes successfully.
# 2. ONLY AFTER THAT, each remaining team member configures
#    and runs teammate.sh from their own JURECA account.
#    Run: source teammate.sh
#
# The team lead runs only this script. Teammates run only
# teammate.sh. Do not run both scripts from the same account.
#
# Before running, set TEAM_LEAD to your Juelich username,
# TEAMMATE_1 through TEAMMATE_3 to your teammates' Juelich
# usernames, and TEAM_FOLDER to the agreed team name.
# =============================================================

# =============================================================
# CONFIGURATION - TEAM LEAD EDITS THESE VALUES
# =============================================================
TEAM_LEAD=""    # Your Juelich username
TEAMMATE_1=""   # Teammate's Juelich username; leave blank if unused
TEAMMATE_2=""
TEAMMATE_3=""
TEAM_FOLDER=""  # Your team name; teammates must use the exact same value
# =============================================================

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

_log()   { echo -e "${BLUE}[INFO]${NC}  $1"; }
_ok()    { echo -e "${GREEN}[OK]${NC}    $1"; }
_warn()  { echo -e "${YELLOW}[WARN]${NC}  $1"; }
_error() { echo -e "${RED}[ERROR]${NC} $1"; }

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    _error "This script must be sourced: source hackathon_setup.sh"
    exit 1
fi

if [[ -z "$TEAM_LEAD" || -z "$TEAM_FOLDER" || "$TEAM_LEAD" == */* || "$TEAM_FOLDER" == */* || "$TEAM_FOLDER" == "." || "$TEAM_FOLDER" == ".." ]]; then
    _error "Set valid TEAM_LEAD and TEAM_FOLDER values before running."
    return 1
fi

if [[ "$TEAM_LEAD" != "$(id -un)" ]]; then
    _error "TEAM_LEAD must match the username running this script: $(id -un)"
    return 1
fi

# Hugging Face datasets to download (org/repo format)
HF_DATASETS=(
    "SprintML/MGI"
    "SprintML/FL_Data_Reconstruction"
    "SprintML/watermark_localization"
)

PROJECT_NAME="training2625"
SCRATCH_BASE="/p/scratch/$PROJECT_NAME"
TEAM_LEAD_DIR="$SCRATCH_BASE/$TEAM_LEAD"
TEAM_DIR="$TEAM_LEAD_DIR/$TEAM_FOLDER"

# Build teammates array, skipping blank entries
TEAMMATES=()
for t in "$TEAMMATE_1" "$TEAMMATE_2" "$TEAMMATE_3"; do
    [[ -n "$t" ]] && TEAMMATES+=("$t")
done

if [[ "${#TEAMMATES[@]}" -eq 0 ]]; then
    _error "No teammates specified. Add at least one."
    return 1
fi

for t in "${TEAMMATES[@]}"; do
    if [[ "$t" == */* || "$t" == "$TEAM_LEAD" ]]; then
        _error "Invalid teammate username: $t"
        return 1
    fi
done

echo ""
echo "============================================="
echo "   Hackathon Setup - Team Lead (Step 1/2)"
echo "============================================="
echo ""
echo "  Team lead   : $TEAM_LEAD"
echo "  Teammates   : ${TEAMMATES[*]}"
echo "  Lead folder : $TEAM_LEAD_DIR"
echo "  Team folder : $TEAM_DIR"
echo ""

# -------------------------------------------------------------
# STEP 1: Check cluster
# -------------------------------------------------------------
_log "Step 1/6 - Checking cluster environment..."

if ! command -v jutil &> /dev/null; then
    _error "jutil not found. Are you on JURECA?"
    echo "       SSH in: ssh <username>@jureca.fz-juelich.de"
    return 1
fi
_ok "jutil found - on JURECA."

# -------------------------------------------------------------
# STEP 2: Activate project
# -------------------------------------------------------------
_log "Step 2/6 - Activating project $PROJECT_NAME..."

if ! jutil env activate -p "$PROJECT_NAME"; then
    _error "Could not activate project $PROJECT_NAME."
    return 1
fi

if [[ -z "$PROJECT" ]]; then
    _error '\$PROJECT empty after activation.'
    echo "       Try manually: jutil env activate -p $PROJECT_NAME"
    return 1
fi
_ok "Project activated. \$PROJECT = $PROJECT"

# -------------------------------------------------------------
# STEP 3: Create folders
# -------------------------------------------------------------
_log "Step 3/6 - Creating folders in scratch..."

cd "$SCRATCH_BASE" || { _error "Cannot cd to $SCRATCH_BASE"; return 1; }

if [[ -d "$TEAM_LEAD_DIR" ]]; then
    _warn "Team lead folder $TEAM_LEAD already exists. Skipping mkdir."
else
    mkdir "$TEAM_LEAD_DIR" || { _error "Could not create $TEAM_LEAD_DIR"; return 1; }
    _ok "Created $TEAM_LEAD_DIR"
fi

if [[ -d "$TEAM_DIR" ]]; then
    _warn "Team folder $TEAM_FOLDER already exists. Skipping mkdir."
else
    mkdir "$TEAM_DIR" || { _error "Could not create $TEAM_DIR"; return 1; }
    _ok "Created $TEAM_FOLDER inside $TEAM_LEAD."
fi

chmod g+s "$TEAM_LEAD_DIR" || { _error "Could not set directory group inheritance."; return 1; }

# -------------------------------------------------------------
# STEP 4: Set ACLs - team lead + teammates get full access
# -------------------------------------------------------------
_log "Step 4/6 - Setting ACLs on $TEAM_LEAD_DIR ..."

_lock_down_tree() {
    local dir="$1"

    # Restrict the top-level directory itself and all existing contents
    setfacl -m g::--- "$dir" || return 1
    setfacl -m o::--- "$dir" || return 1
    setfacl -R -m g::--- "$dir" || return 1
    setfacl -R -m o::--- "$dir" || return 1

    # Restrict default access on existing and future subdirectories
    find "$dir" -type d -exec setfacl -d -m g::--- {} + || return 1
    find "$dir" -type d -exec setfacl -d -m o::--- {} + || return 1
}

_grant_user_access() {
    local uname="$1"
    local dir="$2"

    # Existing directory tree
    setfacl -R -m "u:$uname:rwx" "$dir" || return 1

    # Default ACLs for new files and subdirectories
    find "$dir" -type d -exec setfacl -d -m "u:$uname:rwx" {} + || return 1
}

if ! _lock_down_tree "$TEAM_LEAD_DIR"; then
    _error "Could not restrict the team lead directory."
    return 1
fi

for uname in "$TEAM_LEAD" "${TEAMMATES[@]}"; do
    _log "  Granting access to: $uname"
    if ! _grant_user_access "$uname" "$TEAM_LEAD_DIR"; then
        _error "Could not grant ACL permissions to $uname."
        return 1
    fi
    _ok "  ACL set for $uname"
done

# Ensure ACL named-user entries have an effective rwx mask.
setfacl -R -m m:rwx "$TEAM_LEAD_DIR" || return 1
find "$TEAM_LEAD_DIR" -type d -exec setfacl -d -m m:rwx {} + || return 1

_log "Final ACL for team lead folder:"
getfacl "$TEAM_LEAD_DIR"

_log "Final ACL for team folder:"
getfacl "$TEAM_DIR"

# -------------------------------------------------------------
# STEP 5: Install uv + configure shared cache
# -------------------------------------------------------------
_log "Step 5/6 - Installing uv and configuring shared cache..."

if command -v uv &> /dev/null; then
    _warn "uv already installed. Skipping."
else
    if ! curl -LsSf https://astral.sh/uv/install.sh | sh; then
        _error "uv installation failed."
        return 1
    fi
    if [[ -f "$HOME/.local/bin/env" ]]; then
        source "$HOME/.local/bin/env"
    fi
    export PATH="$HOME/.local/bin:$PATH"
    if ! command -v uv &> /dev/null; then
        _error "uv is still unavailable after installation."
        return 1
    fi
    _ok "uv installed."
fi

UV_BASE="$TEAM_DIR/.uv"
HF_CACHE="$TEAM_DIR/.cache"
mkdir -p "$UV_BASE" "$HF_CACHE" || return 1

# The marker makes the configuration check exact and repeat-safe.
if grep -Fq "TEAM SHARED UV/HF CONFIG: $TEAM_DIR" "$HOME/.bashrc" 2>/dev/null; then
    _warn "uv/HF cache config already in ~/.bashrc. Skipping."
else
    cat <<EOF >> "$HOME/.bashrc"

# TEAM SHARED UV/HF CONFIG: $TEAM_DIR
export PATH="\$HOME/.local/bin:\$PATH"
export UV_CACHE_DIR="$UV_BASE/cache"
export UV_TOOL_DIR="$UV_BASE/tools"
export UV_PYTHON_INSTALL_DIR="$UV_BASE/python"
export HF_HOME="$HF_CACHE"
export HUGGINGFACE_HUB_CACHE="$HF_CACHE/hub"
EOF
    _ok "uv + HF cache paths written to ~/.bashrc"
fi

export UV_CACHE_DIR="$UV_BASE/cache"
export UV_TOOL_DIR="$UV_BASE/tools"
export UV_PYTHON_INSTALL_DIR="$UV_BASE/python"
export HF_HOME="$HF_CACHE"
export HUGGINGFACE_HUB_CACHE="$HF_CACHE/hub"

# -------------------------------------------------------------
# STEP 6: Download each dataset then create its per-task uv env
# -------------------------------------------------------------
_log "Step 6/6 - Downloading datasets and creating per-task environments..."

if [[ -n "$HF_TOKEN" ]]; then
    _ok "HF_TOKEN found - will be used for authentication."
else
    _log "No HF_TOKEN set - proceeding without (fine for public datasets)."
fi

BOOTSTRAP_VENV="$TEAM_DIR/.bootstrap"
if [[ ! -d "$BOOTSTRAP_VENV" ]]; then
    _log "Creating bootstrap venv for downloads..."
    uv venv -p 3.12 "$BOOTSTRAP_VENV" || return 1
    VIRTUAL_ENV="$BOOTSTRAP_VENV" uv pip install huggingface_hub || return 1
    _ok "Bootstrap venv ready."
else
    _warn "Bootstrap venv already exists. Skipping."
fi

BOOTSTRAP_PYTHON="$BOOTSTRAP_VENV/bin/python"

for dataset in "${HF_DATASETS[@]}"; do
    dataset_name="${dataset##*/}"
    dest="$TEAM_DIR/$dataset_name"
    VENV_DIR="$dest/.venv"
    REQ="$dest/requirements.txt"

    echo ""
    _log "--- Task: $dataset_name ---"

    if [[ -d "$dest" ]]; then
        _warn "  Dataset directory already exists. Skipping download."
    else
        _log "  Downloading $dataset..."
        "$BOOTSTRAP_PYTHON" - "$dataset" "$dest" <<'PYEOF'
import os
import sys
from huggingface_hub import snapshot_download

snapshot_download(
    repo_id=sys.argv[1],
    repo_type="dataset",
    local_dir=sys.argv[2],
    token=os.environ.get("HF_TOKEN"),
)
PYEOF
        if [[ $? -ne 0 ]]; then
            _error "  Download failed - skipping venv setup."
            continue
        fi
        _ok "  Downloaded $dataset_name."
    fi

    mkdir -p "$dest/output" || { _error "Cannot create output directory"; continue; }
    setfacl -m m:rwx "$dest/output"
    setfacl -d -m m:rwx "$dest/output"

    if [[ -d "$VENV_DIR" ]]; then
        _warn "  Venv already exists. Skipping."
        continue
    fi

    if [[ ! -f "$REQ" ]]; then
        _warn "  No requirements.txt found in $dataset_name - skipping venv."
        continue
    fi

    _log "  Setting up uv project for $dataset_name..."
    cd "$dest" || { _error "  Cannot cd into $dest"; continue; }

    if [[ ! -f pyproject.toml ]]; then
        uv init -p 3.12 --no-workspace || { _error "  uv init failed"; cd "$TEAM_DIR"; continue; }
    fi

    uv venv -p 3.12 "$VENV_DIR" || { _error "  uv venv failed"; cd "$TEAM_DIR"; continue; }

    # Remove standalone comments and blank lines from requirements.
    # Original behavior uses uv add to create pyproject.toml and uv.lock.
    mapfile -t DEPS < <(grep -vE '^[[:space:]]*(#|$)' "$REQ")

    if [[ "${#DEPS[@]}" -eq 0 ]]; then
        _warn "  requirements.txt is empty - skipping uv add."
    else
        if VIRTUAL_ENV="$VENV_DIR" uv add "${DEPS[@]}"; then
            _ok "  Done: pyproject.toml + uv.lock + .venv ready."
        else
            _error "  Failed to install requirements for $dataset_name."
        fi
    fi

    cd "$TEAM_DIR" || return 1
done

if [[ -d "$BOOTSTRAP_VENV" ]]; then
    rm -rf "$BOOTSTRAP_VENV"
    _ok "Bootstrap venv removed."
fi

echo ""
echo "============================================="
echo -e "${GREEN}   Team lead setup finished!${NC}"
echo "============================================="
echo "  Team access : $TEAM_LEAD + ${TEAMMATES[*]}"
echo "  uv          : $(uv --version 2>&1)"
echo ""
echo "  NEXT: Each remaining team member must now run:"
echo "        source teammate.sh"
echo "  with TEAM_LEAD=$TEAM_LEAD and TEAM_FOLDER=$TEAM_FOLDER"
echo ""
echo "  Activate a task environment:"
echo "  source $TEAM_DIR/<dataset-name>/.venv/bin/activate"
echo ""
echo "  Submit a job: sbatch job.sh"
echo "============================================="
echo ""