#!/bin/bash set -e # Script to start Immich containers, create admin user, and generate API key # This script is used for integration testing cd "$(dirname "$0")/.." # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Check dependencies check_dependency() { if ! command -v "$1" &> /dev/null; then echo -e "${RED}Error: $1 is not installed${NC}" exit 1 fi } echo "Checking dependencies..." check_dependency "podman" check_dependency "curl" check_dependency "jq" # Check if podman-compose is available if ! podman compose version &> /dev/null && ! command -v podman-compose &> /dev/null; then echo -e "${RED}Error: podman-compose is not available${NC}" echo "Please install podman-compose or ensure 'podman compose' is available" exit 1 fi # Start containers echo "Starting Immich containers..." podman compose -f docker/podman-compose.yml up -d # Wait for health check (max 120s) echo "Waiting for Immich to be ready..." READY=false for i in {1..60}; do if curl -s http://localhost:2283/api/server/ping > /dev/null 2>&1; then echo -e "${GREEN}Immich is ready!${NC}" READY=true break fi echo "Attempt $i/60 - waiting for Immich..." sleep 2 done if [ "$READY" = false ]; then echo -e "${RED}Error: Immich failed to become ready within 120 seconds${NC}" exit 1 fi # Admin credentials ADMIN_EMAIL="admin@example.com" ADMIN_PASSWORD="admin123" ADMIN_NAME="Admin User" # Create admin user echo "Creating admin user..." curl -s -X POST http://localhost:2283/api/auth/admin-sign-up \ -H "Content-Type: application/json" \ -d "{\"email\":\"$ADMIN_EMAIL\",\"password\":\"$ADMIN_PASSWORD\",\"name\":\"$ADMIN_NAME\"}" \ || echo -e "${YELLOW}Admin may already exist, continuing...${NC}" # Login to get access token echo "Logging in to get access token..." LOGIN_RESPONSE=$(curl -s -X POST http://localhost:2283/api/auth/login \ -H "Content-Type: application/json" \ -d "{\"email\":\"$ADMIN_EMAIL\",\"password\":\"$ADMIN_PASSWORD\"}") ACCESS_TOKEN=$(echo "$LOGIN_RESPONSE" | jq -r '.accessToken') if [ -z "$ACCESS_TOKEN" ] || [ "$ACCESS_TOKEN" = "null" ]; then echo -e "${RED}Error: Failed to get access token${NC}" echo "Response: $LOGIN_RESPONSE" exit 1 fi # Create API key echo "Creating API key..." API_KEY_RESPONSE=$(curl -s -X POST http://localhost:2283/api/api-keys \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $ACCESS_TOKEN" \ -d '{"name":"sdk-test-key","permissions":["all"]}') API_KEY=$(echo "$API_KEY_RESPONSE" | jq -r '.secret') if [ -z "$API_KEY" ] || [ "$API_KEY" = "null" ]; then echo -e "${RED}Error: Failed to create API key${NC}" echo "Response: $API_KEY_RESPONSE" exit 1 fi # Save to .env.test echo "Saving credentials to .env.test..." cat > .env.test << EOF IMMICH_URL=http://localhost:2283 IMMICH_API_KEY=$API_KEY EOF # Success message echo -e "${GREEN}=======================================${NC}" echo -e "${GREEN}Immich setup complete!${NC}" echo -e "${GREEN}=======================================${NC}" echo "" echo "Credentials saved to .env.test:" echo " IMMICH_URL: http://localhost:2283" echo " IMMICH_API_KEY: ${API_KEY:0:10}... (truncated)" echo "" echo "Admin user:" echo " Email: $ADMIN_EMAIL" echo " Password: $ADMIN_PASSWORD" echo "" echo "You can now access Immich at: http://localhost:2283" echo "" echo "To stop the containers, run:" echo " podman compose -f docker/podman-compose.yml down"