#!/bin/bash # Great Lakes Ice Report Deployment Script for Debian 12 (ARM64/x86_64) # Supports both ARM64 and x86_64 architectures # # Usage: # ./deploy.sh # Downloads from default S3 bucket (ice-puremichigan-lol) # S3_BUCKET_NAME=mybucket ./deploy.sh # Downloads from custom S3 bucket # S3_BUCKET_NAME=none ./deploy.sh # Uses local files from repository # # When using S3, expects files at: # https://{bucket}.s3.amazonaws.com/scripts/icewatch.service # https://{bucket}.s3.amazonaws.com/scripts/Caddyfile set -e echo "🚀 Starting Great Lakes Ice Report deployment..." # Detect architecture ARCH=$(uname -m) echo "🔍 Raw architecture from uname -m: $ARCH" # Also check dpkg architecture as fallback DPKG_ARCH=$(dpkg --print-architecture 2>/dev/null || echo "unknown") echo "🔍 dpkg architecture: $DPKG_ARCH" case $ARCH in x86_64|amd64) GO_ARCH="amd64" echo "📋 Detected x86_64 architecture" ;; aarch64|arm64) GO_ARCH="arm64" echo "📋 Detected ARM64 architecture" ;; armv7l|armhf) echo "❌ Detected 32-bit ARM architecture" echo "This script requires 64-bit architecture (x86_64 or ARM64)." exit 1 ;; *) echo "❌ Unsupported architecture: $ARCH" echo "This script supports x86_64 and ARM64 only." exit 1 ;; esac # Update system echo "📦 Updating system packages..." sudo apt update && sudo apt upgrade -y # Install Node.js and Git echo "📦 Installing Node.js and Git..." curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - sudo apt install -y nodejs build-essential git # Install Go (required for xcaddy) echo "📦 Installing Go for $GO_ARCH architecture..." GO_VERSION="1.21.5" GO_TARBALL="go${GO_VERSION}.linux-${GO_ARCH}.tar.gz" wget -q "https://go.dev/dl/${GO_TARBALL}" sudo rm -rf /usr/local/go sudo tar -C /usr/local -xzf "${GO_TARBALL}" export PATH=$PATH:/usr/local/go/bin echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc # Install xcaddy to build Caddy with plugins echo "📦 Installing xcaddy..." go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest export PATH=$PATH:$(go env GOPATH)/bin # Build Caddy with rate limiting plugin echo "🔧 Building Caddy with rate limiting plugin..." xcaddy build --with github.com/mholt/caddy-ratelimit # Install the custom Caddy binary echo "📦 Installing custom Caddy..." sudo mv caddy /usr/local/bin/caddy sudo chmod +x /usr/local/bin/caddy # Create Caddy user and directories sudo groupadd --system caddy sudo useradd --system --gid caddy --create-home --home-dir /var/lib/caddy --shell /usr/sbin/nologin caddy sudo mkdir -p /etc/caddy /var/log/caddy sudo chown -R caddy:caddy /var/log/caddy # Create systemd service for custom Caddy echo "⚙️ Creating Caddy systemd service..." sudo tee /etc/systemd/system/caddy.service > /dev/null <