#!/usr/bin/env bash # scripts/update_token_server_env.sh # Safe helper to update LIVEKIT_API_KEY / LIVEKIT_API_SECRET in a .env used by docker-compose and recreate the token-server service. # Usage: # ./scripts/update_token_server_env.sh --env-file /path/to/.env --compose-file /path/to/docker-compose.yml --service token-server --livekit-key KEY --livekit-secret SECRET # The script will backup the env file before editing and then run `docker compose -f up -d --no-deps --force-recreate ` set -euo pipefail show_usage(){ cat <&2 show_usage exit 2 fi if [[ ! -f "$ENV_FILE" ]]; then echo "ERROR: env file not found: $ENV_FILE" >&2 exit 2 fi if [[ ! -f "$COMPOSE_FILE" ]]; then echo "ERROR: compose file not found: $COMPOSE_FILE" >&2 exit 2 fi BACKUP="${ENV_FILE}.bak.$(date +%Y%m%d%H%M%S)" cp "$ENV_FILE" "$BACKUP" echo "Backed up $ENV_FILE -> $BACKUP" # Replace or add keys grep -q '^LIVEKIT_API_KEY=' "$ENV_FILE" && sed -i "s/^LIVEKIT_API_KEY=.*/LIVEKIT_API_KEY=${LIVEKIT_KEY}/" "$ENV_FILE" || echo "LIVEKIT_API_KEY=${LIVEKIT_KEY}" >> "$ENV_FILE" grep -q '^LIVEKIT_API_SECRET=' "$ENV_FILE" && sed -i "s/^LIVEKIT_API_SECRET=.*/LIVEKIT_API_SECRET=${LIVEKIT_SECRET}/" "$ENV_FILE" || echo "LIVEKIT_API_SECRET=${LIVEKIT_SECRET}" >> "$ENV_FILE" echo "Updated LIVEKIT_API_KEY and LIVEKIT_API_SECRET in $ENV_FILE" echo "Recreating service $SERVICE_NAME using docker compose..." # Use docker compose if available if command -v docker-compose >/dev/null 2>&1; then docker-compose -f "$COMPOSE_FILE" up -d --no-deps --force-recreate "$SERVICE_NAME" elif command -v docker >/dev/null 2>&1 && docker compose version >/dev/null 2>&1; then docker compose -f "$COMPOSE_FILE" up -d --no-deps --force-recreate "$SERVICE_NAME" else echo "ERROR: docker compose not found. Please restart the service manually (docker compose or docker-compose)" >&2 exit 2 fi echo "Service recreated. Waiting 5s for health checks..." sleep 5 echo "Run the verify script to check token-server and LiveKit connectivity: ./scripts/verify_token_server_after_update.sh --session-room e2e_room --session-user e2e_user --token-server-url https://avanzacast-servertokens.bfzqqk.easypanel.host" exit 0