Explorar o código

Add temporary swap for Pi Zero 2W builds (512MB RAM)

Detect low-memory boards (<1GB) and create a 1GB swap file before
npm ci / vite build, then remove it after. Without this, npm ci
hangs or gets OOM-killed on the Pi Zero 2W.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
tuanchris hai 4 meses
pai
achega
3533fcddad
Modificáronse 2 ficheiros con 53 adicións e 1 borrados
  1. 28 0
      dw
  2. 25 1
      setup-pi.sh

+ 28 - 0
dw

@@ -28,6 +28,26 @@ YELLOW='\033[1;33m'
 BLUE='\033[0;34m'
 NC='\033[0m'
 
+# Temporary swap for memory-constrained boards (e.g. Pi Zero 2W)
+SWAP_FILE="/tmp/dw-build-swap"
+enable_swap() {
+    local total_mb
+    total_mb=$(awk '/MemTotal/ {printf "%d", $2/1024}' /proc/meminfo)
+    if [[ "$total_mb" -lt 1024 ]]; then
+        echo "Low memory detected (${total_mb}MB). Enabling temporary swap..."
+        sudo dd if=/dev/zero of="$SWAP_FILE" bs=1M count=1024 status=none 2>/dev/null || true
+        sudo chmod 600 "$SWAP_FILE"
+        sudo mkswap "$SWAP_FILE" > /dev/null 2>&1 || true
+        sudo swapon "$SWAP_FILE" 2>/dev/null || true
+    fi
+}
+disable_swap() {
+    if [[ -f "$SWAP_FILE" ]]; then
+        sudo swapoff "$SWAP_FILE" 2>/dev/null || true
+        sudo rm -f "$SWAP_FILE"
+    fi
+}
+
 # Find dune-weaver directory
 find_install_dir() {
     # Check common locations
@@ -118,6 +138,8 @@ cmd_update() {
         exec /usr/local/bin/dw update --continue
     fi
 
+    enable_swap
+
     echo "Updating Python dependencies..."
     source .venv/bin/activate
     pip install -r requirements.txt
@@ -128,6 +150,8 @@ cmd_update() {
     npx vite build
     cd "$INSTALL_DIR"
 
+    disable_swap
+
     # Update nginx config
     echo "Updating nginx config..."
     sudo cp "$INSTALL_DIR/nginx/dune-weaver.conf" /etc/nginx/sites-available/dune-weaver.conf
@@ -309,6 +333,8 @@ cmd_checkout() {
 
     echo -e "Switched to branch: ${GREEN}${branch}${NC}"
 
+    enable_swap
+
     # Rebuild frontend
     echo "Rebuilding frontend..."
     cd "$INSTALL_DIR/frontend"
@@ -321,6 +347,8 @@ cmd_checkout() {
     source .venv/bin/activate
     pip install -r requirements.txt
 
+    disable_swap
+
     # Update dw CLI from the new branch
     sudo cp "$INSTALL_DIR/dw" /usr/local/bin/dw
     sudo chmod +x /usr/local/bin/dw

+ 25 - 1
setup-pi.sh

@@ -82,6 +82,28 @@ print_success() {
     echo -e "${GREEN}$1${NC}"
 }
 
+# Temporary swap for memory-constrained boards (e.g. Pi Zero 2W with 512MB)
+SWAP_FILE="/tmp/dw-build-swap"
+
+enable_swap() {
+    local total_mb
+    total_mb=$(awk '/MemTotal/ {printf "%d", $2/1024}' /proc/meminfo)
+    if [[ "$total_mb" -lt 1024 ]]; then
+        echo "Low memory detected (${total_mb}MB). Enabling temporary swap..."
+        sudo dd if=/dev/zero of="$SWAP_FILE" bs=1M count=1024 status=none 2>/dev/null || true
+        sudo chmod 600 "$SWAP_FILE"
+        sudo mkswap "$SWAP_FILE" > /dev/null 2>&1 || true
+        sudo swapon "$SWAP_FILE" 2>/dev/null || true
+    fi
+}
+
+disable_swap() {
+    if [[ -f "$SWAP_FILE" ]]; then
+        sudo swapoff "$SWAP_FILE" 2>/dev/null || true
+        sudo rm -f "$SWAP_FILE"
+    fi
+}
+
 # Install system dependencies
 install_system_deps() {
     print_step "Installing system dependencies..."
@@ -282,12 +304,14 @@ deploy_native() {
     pip install --upgrade pip
     pip install -r requirements.txt
 
-    # Build frontend
+    # Build frontend (swap helps Pi Zero 2W survive npm ci)
+    enable_swap
     print_step "Building frontend..."
     cd "$INSTALL_DIR/frontend"
     npm ci --loglevel=error
     npx vite build
     cd "$INSTALL_DIR"
+    disable_swap
 
     # Ensure nginx (www-data) can traverse to static files
     # chmod o+x grants traversal only, not directory listing