#!/bin/bash set -e # Portainer Stack Setup Script for OpenClaw # Following the git-linked migration plan # Load credentials if available if [ -f "../.credentials" ]; then source ../.credentials echo "Loaded credentials from .credentials file" fi # Configuration SERVICE_NAME="openclaw" COMPOSE_FILE="openclaw/docker-compose.yml" REPO_URL="${GITEA_REPO_URL:-https://gitea.kolpacksoftware.com/homelab/docker-infrastructure.git}" ENDPOINT_ID="${PORTAINER_ENDPOINT_ID:-2}" # Check required environment variables if [ -z "$PORTAINER_API_TOKEN" ]; then echo "Error: PORTAINER_API_TOKEN not set" echo "Get token from: Portainer UI → User Settings → Access Tokens" echo "Then run: export PORTAINER_API_TOKEN='ptr_...'" exit 1 fi if [ -z "$GITEA_TOKEN" ]; then echo "Error: GITEA_TOKEN not set" echo "Get token from: Gitea → Settings → Applications → Generate New Token" echo "Then run: export GITEA_TOKEN='...'" exit 1 fi if [ -z "$GITEA_USER" ]; then echo "Error: GITEA_USER not set" echo "Then run: export GITEA_USER='your-username'" exit 1 fi # Gateway token from .env file OPENCLAW_TOKEN=$(grep OPENCLAW_GATEWAY_TOKEN .env | cut -d'=' -f2) if [ -z "$OPENCLAW_TOKEN" ]; then echo "Error: OPENCLAW_GATEWAY_TOKEN not found in .env file" exit 1 fi echo "=== Portainer Stack Setup for $SERVICE_NAME ===" echo "" # Step 1: Check for existing stack echo "Step 1: Checking for existing stack..." EXISTING_STACK=$(curl -s "https://localhost:9443/api/stacks" \ -H "X-API-Key: $PORTAINER_API_TOKEN" \ --insecure | grep -o "\"Id\":[0-9]*,\"Name\":\"$SERVICE_NAME\"" || true) if [ -n "$EXISTING_STACK" ]; then STACK_ID=$(echo "$EXISTING_STACK" | grep -o "[0-9]*") echo "Found existing stack with ID: $STACK_ID" read -p "Delete existing stack and recreate? (y/n) " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then echo "Deleting stack $STACK_ID..." curl -X DELETE "https://localhost:9443/api/stacks/$STACK_ID?endpointId=$ENDPOINT_ID" \ -H "X-API-Key: $PORTAINER_API_TOKEN" \ --insecure echo "Stack deleted" sleep 2 else echo "Aborted" exit 0 fi else echo "No existing stack found" fi # Step 2: Create git-linked stack echo "" echo "Step 2: Creating git-linked stack..." RESPONSE=$(curl -X POST "https://localhost:9443/api/stacks/create/standalone/repository?endpointId=$ENDPOINT_ID" \ -H "X-API-Key: $PORTAINER_API_TOKEN" \ -H "Content-Type: application/json" \ --insecure \ -d '{ "name": "'"$SERVICE_NAME"'", "repositoryURL": "'"$REPO_URL"'", "repositoryReferenceName": "refs/heads/main", "composeFile": "'"$COMPOSE_FILE"'", "repositoryAuthentication": true, "repositoryUsername": "'"$GITEA_USER"'", "repositoryPassword": "'"$GITEA_TOKEN"'", "autoUpdate": { "interval": "5m" }, "env": [ { "name": "OPENCLAW_GATEWAY_TOKEN", "value": "'"$OPENCLAW_TOKEN"'" } ] }') if echo "$RESPONSE" | grep -q '"Id"'; then NEW_STACK_ID=$(echo "$RESPONSE" | grep -o '"Id":[0-9]*' | head -1 | cut -d':' -f2) echo "✓ Stack created successfully with ID: $NEW_STACK_ID" else echo "✗ Failed to create stack" echo "Response: $RESPONSE" exit 1 fi # Step 3: Verify echo "" echo "Step 3: Verifying deployment..." sleep 5 if docker ps | grep -q "$SERVICE_NAME"; then echo "✓ Container is running" else echo "⚠ Container not found yet, check Portainer UI" fi echo "" echo "=== Setup Complete ===" echo "Next steps:" echo "1. Configure nginx-proxy-manager:" echo " - Domain: openclaw.kolpacksoftware.com" echo " - Forward to: openclaw:18789" echo " - Add SSL certificate" echo " - Set access list to private network" echo "" echo "2. Run onboarding wizard:" echo " docker exec -it openclaw npx openclaw onboard" echo "" echo "3. Access UI at: https://openclaw.kolpacksoftware.com" echo " Gateway token: $OPENCLAW_TOKEN"