cloud-init.sh

119 lines
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
#!/bin/bash
# Congo Dev — server setup script
# Usage: curl -fsSL https://congo.gg/cloud-init.sh | bash
set -euo pipefail
export DEBIAN_FRONTEND=noninteractive
export PATH=$PATH:/usr/local/go/bin
export GOPATH=/root/go
export GOMODCACHE=/root/go/pkg/mod

echo "=== Congo Dev Setup ==="

# Timezone (override with TZ=America/New_York before running)
TZ=${TZ:-America/Los_Angeles}
timedatectl set-timezone "$TZ" 2>/dev/null || ln -sf "/usr/share/zoneinfo/$TZ" /etc/localtime

# --- System packages ---
apt-get update
apt-get install -y curl git make ufw build-essential

# --- Docker ---
if ! command -v docker &> /dev/null; then
    curl -fsSL https://get.docker.com | sh
    systemctl enable --now docker
fi

# --- Go (for building the dev image) ---
if ! command -v go &> /dev/null; then
    GO_VERSION=$(curl -s https://go.dev/VERSION?m=text | head -1)
    ARCH=$(dpkg --print-architecture)
    curl -fsSL "https://go.dev/dl/${GO_VERSION}.linux-${ARCH}.tar.gz" | tar -C /usr/local -xz
fi

# --- Congo CLI ---
if ! command -v congo &> /dev/null; then
    ARCH=$(dpkg --print-architecture)
    curl -fsSL "https://congo.gg/download/congo-linux-${ARCH}.tar.gz" | tar -C /usr/local/bin -xz
    chmod +x /usr/local/bin/congo
fi

# --- Extract source ---
congo source /opt/congo-src
if [ ! -d /opt/congo-src/dev ]; then
    echo "FATAL: congo source did not extract dev/"
    exit 1
fi

# --- Data directories ---
# /mnt/data/dev is the dev container's data root (matches infra.json HOST_DATA_DIR)
mkdir -p /mnt/data/dev/{workspace/.config,repos}
mkdir -p /mnt/data/dev/certs
chown -R 1000:1000 /mnt/data/dev/workspace /mnt/data/dev/repos

# --- Run setup scripts (from the extracted source) ---
bash /opt/congo-src/res/dev-setup.sh
# --- Build the dev image ---
cd /opt/congo-src
docker build -t congo-dev:latest -f dev/Dockerfile .
if ! docker image inspect congo-dev:latest >/dev/null 2>&1; then
    echo "FATAL: congo-dev image failed to build"
    exit 1
fi

# --- Firewall ---
ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw --force enable

# --- Launch Congo Dev (handles HTTP, HTTPS, and reverse proxy — no Caddy) ---
AUTH_SECRET=$(cat /opt/congo-dev/auth-secret)
docker rm -f dev 2>/dev/null || true
docker run -d --name dev \
  --network internal \
  --restart unless-stopped \
  -p 80:80 -p 443:443 \
  -v /mnt/data/dev:/mnt/data \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v /root/.ssh:/root/.ssh \
  -v /opt/congo-dev-setup-token:/opt/congo-dev-setup-token \
  -e PORT=80 \
  -e ENV=production \
  -e DB_PATH=/mnt/data/congo-dev.db \
  -e DATA_DIR=/mnt/data \
  -e HOST_DATA_DIR=/mnt/data/dev \
  -e CERT_DIR=/mnt/data/certs \
  -e AUTH_SECRET="$AUTH_SECRET" \
  -e TZ="$TZ" \
  congo-dev:latest

# --- Wait for health ---
echo "Waiting for Congo Dev to start..."
for i in $(seq 1 30); do
    if curl -sf http://localhost:80/health >/dev/null 2>&1; then
        break
    fi
    sleep 2
done

# --- Connection info ---
SERVER_IP=$(curl -s http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address 2>/dev/null || curl -s ifconfig.me)
SETUP_TOKEN=$(cat /opt/congo-dev-setup-token)

cat > /etc/motd <<EOF

  Congo Dev is running!

  Dashboard: http://${SERVER_IP}
  Setup token: ${SETUP_TOKEN}

  To activate the AI agent:
    1. Open the terminal in VS Code (dashboard → Code)
    2. Run: claude auth
    3. The agent starts automatically after authentication

EOF

echo "=== Congo Dev setup complete ==="
echo "Dashboard: http://${SERVER_IP}"
echo "Token: ${SETUP_TOKEN}"