image_management_api/scripts/setup_credentials.py
2025-05-24 06:21:38 +02:00

97 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""
Script to set up Firestore credentials for development and deployment
"""
import os
import json
import sys
import argparse
from pathlib import Path
def create_env_file(project_id, credentials_file="firestore-credentials.json"):
"""Create a .env file with the necessary environment variables"""
env_content = f"""# Firestore Settings
FIRESTORE_PROJECT_ID={project_id}
FIRESTORE_CREDENTIALS_FILE={credentials_file}
# Google Cloud Storage Settings
GCS_BUCKET_NAME={project_id}-storage
GCS_CREDENTIALS_FILE={credentials_file}
# Security settings
API_KEY_SECRET=development-secret-key-change-in-production
API_KEY_EXPIRY_DAYS=365
# Vector Database Settings
VECTOR_DB_API_KEY=
VECTOR_DB_ENVIRONMENT=
VECTOR_DB_INDEX_NAME=image-embeddings
# Other Settings
ENVIRONMENT=development
LOG_LEVEL=INFO
RATE_LIMIT_PER_MINUTE=100
"""
with open(".env", "w") as f:
f.write(env_content)
print("Created .env file with Firestore settings")
def main():
parser = argparse.ArgumentParser(description='Set up Firestore credentials')
parser.add_argument('--key-file', type=str, help='Path to the service account key file')
parser.add_argument('--project-id', type=str, help='Google Cloud project ID')
parser.add_argument('--create-env', action='store_true', help='Create .env file')
args = parser.parse_args()
# Ensure we have a project ID
project_id = args.project_id
if not project_id:
if args.key_file and os.path.exists(args.key_file):
try:
with open(args.key_file, 'r') as f:
key_data = json.load(f)
project_id = key_data.get('project_id')
if project_id:
print(f"Using project ID from key file: {project_id}")
except Exception as e:
print(f"Error reading key file: {e}")
sys.exit(1)
if not project_id:
print("Error: Project ID is required")
parser.print_help()
sys.exit(1)
# Handle key file
target_key_file = "firestore-credentials.json"
if args.key_file and os.path.exists(args.key_file):
# Copy the key file to the target location
try:
with open(args.key_file, 'r') as src, open(target_key_file, 'w') as dst:
key_data = json.load(src)
json.dump(key_data, dst, indent=2)
print(f"Copied service account key to {target_key_file}")
except Exception as e:
print(f"Error copying key file: {e}")
sys.exit(1)
else:
print("Warning: No service account key file provided")
print(f"You need to place your service account key in {target_key_file}")
# Create .env file if requested
if args.create_env:
create_env_file(project_id, target_key_file)
print("\nSetup complete!")
print("\nFor development:")
print(f"1. Make sure {target_key_file} exists in the project root")
print("2. Ensure environment variables are set in .env file")
print("\nFor deployment:")
print("1. For Cloud Run, set environment variables in deployment config")
print("2. Make sure to securely manage service account key")
if __name__ == "__main__":
main()