|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +# Entrypoint script for Mussel container |
| 5 | +# Manages user permissions and runs commands with proper UID/GID |
| 6 | + |
| 7 | +# Get the user ID and group ID from environment or use defaults |
| 8 | +USER_ID="${MUSSEL_UID:-1000}" |
| 9 | +GROUP_ID="${MUSSEL_GID:-1000}" |
| 10 | + |
| 11 | +# Create a user with the specified UID/GID if running as root and custom UID is requested |
| 12 | +if [ "$(id -u)" = "0" ] && [ "$USER_ID" != "0" ]; then |
| 13 | + # Create group if it doesn't exist |
| 14 | + if ! getent group mussel >/dev/null 2>&1; then |
| 15 | + groupadd -g "$GROUP_ID" mussel |
| 16 | + fi |
| 17 | + |
| 18 | + # Create user if it doesn't exist |
| 19 | + if ! id -u mussel >/dev/null 2>&1; then |
| 20 | + useradd -u "$USER_ID" -g "$GROUP_ID" -m -s /bin/bash mussel |
| 21 | + fi |
| 22 | + |
| 23 | + # Ensure writable cache directories |
| 24 | + mkdir -p /.cache /tmp |
| 25 | + chown -R mussel:mussel /.cache /tmp || true |
| 26 | + chmod -R 777 /.cache /tmp |
| 27 | + |
| 28 | + # Ensure output directory exists and is writable (Azure Batch expects this) |
| 29 | + if [ -n "$AZ_BATCH_TASK_WORKING_DIR" ]; then |
| 30 | + mkdir -p "$AZ_BATCH_TASK_WORKING_DIR/output" |
| 31 | + chmod -R 777 "$AZ_BATCH_TASK_WORKING_DIR/output" || true |
| 32 | + fi |
| 33 | + mkdir -p /tmp/output |
| 34 | + chmod 777 /tmp/output || true |
| 35 | + |
| 36 | + # Ensure cache directories are writable by mussel user if they're set |
| 37 | + if [ -n "$TMPDIR" ] && [ -d "$TMPDIR" ]; then |
| 38 | + chown -R mussel:mussel "$TMPDIR" 2>/dev/null || chmod -R 777 "$TMPDIR" 2>/dev/null || true |
| 39 | + fi |
| 40 | + if [ -n "$TORCH_HOME" ] && [ -d "$TORCH_HOME" ]; then |
| 41 | + chown -R mussel:mussel "$TORCH_HOME" 2>/dev/null || chmod -R 777 "$TORCH_HOME" 2>/dev/null || true |
| 42 | + fi |
| 43 | + if [ -n "$HF_HOME" ] && [ -d "$HF_HOME" ]; then |
| 44 | + chown -R mussel:mussel "$HF_HOME" 2>/dev/null || chmod -R 777 "$HF_HOME" 2>/dev/null || true |
| 45 | + fi |
| 46 | + |
| 47 | + # Switch to the created user and execute the command |
| 48 | + exec gosu mussel "$@" |
| 49 | +else |
| 50 | + # Not root or no custom UID requested - run directly |
| 51 | + exec "$@" |
| 52 | +fi |
0 commit comments