| 1 | #!/usr/bin/env bash |
| 2 | # Deploys ClonAgent to utopiaia.com (145.239.65.26) |
| 3 | # Usage: SSH_KEY=~/.ssh/id_xxx ./scripts/deploy-utopia.sh |
| 4 | set -euo pipefail |
| 5 | |
| 6 | HOST="${HOST:-145.239.65.26}" |
| 7 | USER="${USER:-ubuntu}" |
| 8 | SSH_KEY="${SSH_KEY:-$HOME/.ssh/id_ed25519_kikocisbot}" |
| 9 | REMOTE_DIR="${REMOTE_DIR:-/home/ubuntu/clonagent}" |
| 10 | SUBDOMAIN="${SUBDOMAIN:-clonagent.utopiaia.com}" |
| 11 | ANTHROPIC_KEY="${ANTHROPIC_API_KEY:-${ANTHROPIC_KEY:-}}" |
| 12 | |
| 13 | PROJECT_ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 14 | echo "→ project: $PROJECT_ROOT" |
| 15 | echo "→ remote: $USER@$HOST:$REMOTE_DIR" |
| 16 | echo "→ domain: https://$SUBDOMAIN" |
| 17 | |
| 18 | SSH() { ssh -i "$SSH_KEY" -o StrictHostKeyChecking=accept-new "$USER@$HOST" "$@"; } |
| 19 | SCP() { scp -i "$SSH_KEY" -o StrictHostKeyChecking=accept-new "$@"; } |
| 20 | |
| 21 | # 1. Build the client locally so we don't need npm on the server |
| 22 | echo "→ building client..." |
| 23 | ( cd "$PROJECT_ROOT/client" && npm install --no-audit --no-fund --silent && npm run build ) |
| 24 | |
| 25 | # 2. Tar up everything except node_modules |
| 26 | echo "→ packaging..." |
| 27 | TARBALL="/tmp/clonagent-$(date +%s).tgz" |
| 28 | tar --exclude='node_modules' --exclude='.git' --exclude='client/dist/.vite' \ |
| 29 | --exclude='server/data' --exclude='mail' \ |
| 30 | -czf "$TARBALL" -C "$PROJECT_ROOT" . |
| 31 | |
| 32 | # 3. Ship it |
| 33 | echo "→ uploading..." |
| 34 | SSH "mkdir -p $REMOTE_DIR" |
| 35 | SCP "$TARBALL" "$USER@$HOST:/tmp/clonagent.tgz" |
| 36 | SSH "tar -xzf /tmp/clonagent.tgz -C $REMOTE_DIR && rm /tmp/clonagent.tgz" |
| 37 | rm "$TARBALL" |
| 38 | |
| 39 | # 4. Write .env on the server |
| 40 | echo "→ writing .env..." |
| 41 | SSH "cat > $REMOTE_DIR/.env <<EOF |
| 42 | ANTHROPIC_API_KEY=$ANTHROPIC_KEY |
| 43 | PORT=3300 |
| 44 | AGENT_RELAY_URL=http://host.docker.internal:3201 |
| 45 | EOF" |
| 46 | |
| 47 | # 5. Install server deps on the server (Node already there from agent-manager) |
| 48 | echo "→ installing server deps on server..." |
| 49 | SSH "cd $REMOTE_DIR/server && npm install --omit=dev --no-audit --no-fund" |
| 50 | |
| 51 | # 6. Systemd unit |
| 52 | echo "→ installing systemd unit..." |
| 53 | SSH "sudo tee /etc/systemd/system/clonagent.service >/dev/null <<'EOF' |
| 54 | [Unit] |
| 55 | Description=ClonAgent server |
| 56 | After=network.target |
| 57 | |
| 58 | [Service] |
| 59 | Type=simple |
| 60 | User=ubuntu |
| 61 | WorkingDirectory=/home/ubuntu/clonagent/server |
| 62 | EnvironmentFile=/home/ubuntu/clonagent/.env |
| 63 | ExecStart=/usr/bin/node index.js |
| 64 | Restart=on-failure |
| 65 | RestartSec=5 |
| 66 | |
| 67 | [Install] |
| 68 | WantedBy=multi-user.target |
| 69 | EOF" |
| 70 | SSH "sudo systemctl daemon-reload && sudo systemctl enable --now clonagent && sleep 2 && sudo systemctl status clonagent --no-pager | head -10" |
| 71 | |
| 72 | # 7. Nginx subdomain (HTTP only — TLS via existing acme/letsencrypt setup) |
| 73 | echo "→ adding nginx config..." |
| 74 | SSH "sudo tee /etc/nginx/sites-available/clonagent >/dev/null <<'EOF' |
| 75 | server { |
| 76 | listen 80; |
| 77 | server_name $SUBDOMAIN; |
| 78 | |
| 79 | location / { |
| 80 | proxy_pass http://127.0.0.1:3300; |
| 81 | proxy_http_version 1.1; |
| 82 | proxy_set_header Upgrade \$http_upgrade; |
| 83 | proxy_set_header Connection \"upgrade\"; |
| 84 | proxy_set_header Host \$host; |
| 85 | proxy_set_header X-Real-IP \$remote_addr; |
| 86 | proxy_buffering off; # SSE streams |
| 87 | proxy_read_timeout 3600s; # SSE long connections |
| 88 | } |
| 89 | } |
| 90 | EOF" |
| 91 | SSH "sudo ln -sf /etc/nginx/sites-available/clonagent /etc/nginx/sites-enabled/clonagent && sudo nginx -t && sudo systemctl reload nginx" |
| 92 | |
| 93 | # 8. Smoke test |
| 94 | echo "→ smoke test..." |
| 95 | SSH "curl -sf http://localhost:3300/api/health" && echo |
| 96 | echo "✅ Done. Open https://$SUBDOMAIN" |
| 97 | echo " (DNS already wildcard-resolves *.utopiaia.com → 145.239.65.26)" |