Files
ansible-mln/infra/finish-portabase.sh
2026-10-26 21:01:06 +01:00

190 lines
5.4 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
BASE="http://10.0.0.4"
AGENT_ID="1f687170-8730-439d-82e6-dc23be878046"
PROJECT_ID="30dbbcc0-6f98-41b8-9a1d-12f0a1cc95e3"
ORG_ID="470f4039-8448-43c4-8aa5-0259e36a5f46"
# Load API key from env
source /home/ubuntu/docker/s3-backup/.env.api
H="x-api-key: $API_KEY"
ORIGIN="Origin: $BASE"
echo "=== [1/5] Attach agent to organization ==="
curl -s -w "\nHTTP %{http_code}\n" -X POST "$BASE/api/v1/organizations/$ORG_ID/agents" \
-H "$H" -H "$ORIGIN" -H "Content-Type: application/json" \
-d "{\"agentId\":\"$AGENT_ID\"}"
echo ""
echo "=== [2/5] Get Edge Key ==="
EDGE_RESP=$(curl -sf "$BASE/api/v1/agents/$AGENT_ID/key" -H "$H" -H "$ORIGIN")
echo "Raw response: $EDGE_RESP"
EDGE_KEY=$(echo "$EDGE_RESP" | python3 -c "
import sys, json
d = json.load(sys.stdin)
if 'data' in d:
print(d['data'].get('edgeKey',''))
elif 'edgeKey' in d:
print(d['edgeKey'])
else:
print('')
" 2>/dev/null || true)
if [ -z "$EDGE_KEY" ]; then
echo "ERROR: Could not get EDGE_KEY"
echo "Response was: $EDGE_RESP"
exit 1
fi
echo "EDGE_KEY=$EDGE_KEY"
echo ""
echo "=== [3/5] Save IDs ==="
cat > /home/ubuntu/docker/s3-backup/portabase-ids.env << EOF
API_KEY=$API_KEY
ORG_ID=$ORG_ID
AGENT_ID=$AGENT_ID
PROJECT_ID=$PROJECT_ID
EDGE_KEY=$EDGE_KEY
EOF
chmod 600 /home/ubuntu/docker/s3-backup/portabase-ids.env
echo " Saved to portabase-ids.env"
echo ""
echo "=== [4/5] Deploy agent on bdd-redis-prod ==="
BDD_HOST="10.0.0.5"
SSH_KEY="/root/.ssh/key_bdd_redis"
ssh -o StrictHostKeyChecking=no -i "$SSH_KEY" root@"$BDD_HOST" bash -s -- "$EDGE_KEY" << 'AGENT_EOF'
EDGE_KEY="$1"
if ! command -v docker &>/dev/null; then
echo " Installing Docker..."
apt-get update -qq
apt-get install -y -qq ca-certificates curl gnupg lsb-release
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list
apt-get update -qq
apt-get install -y -qq docker-ce docker-ce-cli containerd.io docker-compose-plugin
systemctl enable --now docker
fi
mkdir -p /opt/portabase-agent
cd /opt/portabase-agent
cat > docker-compose.yml << 'COMPOSE'
services:
portabase-agent:
image: portabase/agent:latest
container_name: portabase-agent
restart: always
network_mode: host
env_file: .env
environment:
- TZ=Europe/Paris
- DATABASES_CONFIG_FILE=config.toml
volumes:
- ./databases.toml:/config/config.toml:ro
- /var/run/docker.sock:/var/run/docker.sock
- agent-data:/data
- agent-tmp:/tmp
volumes:
agent-data:
agent-tmp:
COMPOSE
cat > .env << ENVEOF
EDGE_KEY=$EDGE_KEY
ENVEOF
chmod 600 .env
cat > databases.toml << 'TOML'
[[databases]]
name = "Production PostgreSQL - gestion_commande_db"
database = "gestion_commande_db"
type = "postgresql"
username = "admin_gestion_commande_db"
password = "1SWDxH20rV7K2Uc2PNlwCaCxfVZEtKomF0CK9OMh"
port = 5432
host = "127.0.0.1"
generated_id = "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
[[databases]]
name = "Production Redis"
type = "redis"
password = "k6UYX9RtuXJVV1HUeefbSukMcSwjvVgRsh2qJGPh"
port = 6379
host = "127.0.0.1"
generated_id = "b2c3d4e5-f6a7-8901-bcde-f12345678901"
TOML
docker compose pull -q 2>/dev/null || true
docker compose up -d
echo " Agent started"
AGENT_EOF
echo ""
echo "=== [5/5] Wait for agent connection & configure backup ==="
echo " Waiting for agent to connect..."
for i in $(seq 1 30); do
AGENT_RESP=$(curl -sf "$BASE/api/v1/agents/$AGENT_ID" -H "$H" -H "$ORIGIN" 2>/dev/null || true)
LAST=$(echo "$AGENT_RESP" | python3 -c "
import sys, json
d = json.load(sys.stdin)
if 'data' in d: d = d['data']
print(d.get('lastContact',''))
" 2>/dev/null || true)
if [ -n "$LAST" ] && [ "$LAST" != "None" ] && [ "$LAST" != "null" ]; then
echo " Agent connected (lastContact=$LAST)"
break
fi
echo " Waiting... ($i/30)"
sleep 5
done
echo " Waiting for database detection..."
sleep 15
DBS=$(curl -sf "$BASE/api/v1/databases" -H "$H" -H "$ORIGIN" 2>/dev/null || echo '{}')
echo " Databases response: $DBS"
PG_DB_ID=$(echo "$DBS" | python3 -c "
import sys, json
d = json.load(sys.stdin)
dbs = d.get('data', d) if isinstance(d, dict) else d
if isinstance(dbs, list):
for db in dbs:
if db.get('type') == 'postgresql':
print(db['id'])
break
" 2>/dev/null || true)
if [ -n "$PG_DB_ID" ]; then
echo " PostgreSQL detected: $PG_DB_ID"
curl -sf -X PATCH "$BASE/api/v1/databases/$PG_DB_ID" \
-H "$H" -H "$ORIGIN" -H "Content-Type: application/json" \
-d "{\"projectId\":\"$PROJECT_ID\"}" > /dev/null 2>&1 || true
echo " PostgreSQL attached to project"
curl -sf -X PUT "$BASE/api/v1/databases/$PG_DB_ID/backup-policy" \
-H "$H" -H "$ORIGIN" -H "Content-Type: application/json" \
-d '{"cron":"0 2 * * *","retentionCount":30}' > /dev/null 2>&1 || true
echo " Schedule: daily backup at 02:00, retention 30"
else
echo " WARNING: PostgreSQL database not found via API"
echo " You may need to configure backup schedule manually in the UI"
fi
echo ""
echo "============================================"
echo " Portabase setup complete!"
echo " Dashboard : $BASE"
echo " Agent : bdd-redis-prod (10.0.0.5)"
echo " Backup : daily at 02:00 UTC"
echo " Retention : 30 backups"
echo "============================================"