diff --git a/deployment/cleanup-images.sh b/deployment/cleanup-images.sh new file mode 100644 index 0000000..5475721 --- /dev/null +++ b/deployment/cleanup-images.sh @@ -0,0 +1,62 @@ +#!/bin/bash +set -e + +# Container Image Cleanup Script +# This script cleans up container images from Google Container Registry +# Images are not managed by Terraform, so this provides a manual cleanup option + +PROJECT_ID=$(gcloud config get-value project) +IMAGE_NAME="sereact-api" + +if [ -z "$PROJECT_ID" ]; then + echo "ERROR: No Google Cloud project is set. Run 'gcloud config set project YOUR_PROJECT_ID'" + exit 1 +fi + +echo "Cleaning up container images for project: $PROJECT_ID" +echo "Image repository: gcr.io/$PROJECT_ID/$IMAGE_NAME" +echo "" + +# Check if repository exists +if ! gcloud container images list-tags "gcr.io/$PROJECT_ID/$IMAGE_NAME" > /dev/null 2>&1; then + echo "No container images found for $IMAGE_NAME" + exit 0 +fi + +echo "Found container images. Listing current images:" +gcloud container images list-tags "gcr.io/$PROJECT_ID/$IMAGE_NAME" +echo "" + +read -p "Do you want to delete ALL images for $IMAGE_NAME? (yes/no): " confirm + +if [ "$confirm" != "yes" ]; then + echo "Cleanup cancelled." + exit 0 +fi + +echo "Deleting container images..." + +# Get all image digests and delete them +DIGESTS=$(gcloud container images list-tags "gcr.io/$PROJECT_ID/$IMAGE_NAME" --format="get(digest)" --filter="tags:*" 2>/dev/null || true) +UNTAGGED_DIGESTS=$(gcloud container images list-tags "gcr.io/$PROJECT_ID/$IMAGE_NAME" --format="get(digest)" --filter="-tags:*" 2>/dev/null || true) + +# Delete tagged images +if [ ! -z "$DIGESTS" ]; then + echo "Deleting tagged images..." + for digest in $DIGESTS; do + gcloud container images delete "gcr.io/$PROJECT_ID/$IMAGE_NAME@$digest" --force-delete-tags --quiet || echo "Failed to delete $digest" + done +fi + +# Delete untagged images +if [ ! -z "$UNTAGGED_DIGESTS" ]; then + echo "Deleting untagged images..." + for digest in $UNTAGGED_DIGESTS; do + gcloud container images delete "gcr.io/$PROJECT_ID/$IMAGE_NAME@$digest" --quiet || echo "Failed to delete $digest" + done +fi + +echo "Container image cleanup completed." +echo "" +echo "Note: The repository gcr.io/$PROJECT_ID/$IMAGE_NAME may still exist but should be empty." +echo "You can verify with: gcloud container images list-tags gcr.io/$PROJECT_ID/$IMAGE_NAME" \ No newline at end of file diff --git a/deployment/deploy.sh b/deployment/deploy.sh index 3bd3807..e3e9d21 100644 --- a/deployment/deploy.sh +++ b/deployment/deploy.sh @@ -2,21 +2,34 @@ set -e # Configuration -PROJECT_ID=$(gcloud config get-value project) IMAGE_NAME="sereact-api" REGION="us-central1" SERVICE_NAME="sereact" +# Get project ID from terraform.tfvars if it exists, otherwise use gcloud +if [ -f "$(dirname "$0")/terraform/terraform.tfvars" ]; then + PROJECT_ID=$(grep '^project_id' "$(dirname "$0")/terraform/terraform.tfvars" | cut -d'"' -f2) +fi + +# Fallback to gcloud if not found in tfvars +if [ -z "$PROJECT_ID" ]; then + PROJECT_ID=$(gcloud config get-value project 2>/dev/null || echo "") +fi + # Help function function show_help { echo "Usage: $0 [options]" echo "Options:" echo " --provision Run Terraform to provision cloud resources" echo " --build Build and push Docker image" - echo " --deploy Deploy to Cloud Run" + echo " --deploy Deploy to Cloud Run via Terraform" echo " --destroy Destroy cloud resources with Terraform" + echo " --list List all Cloud Run services in the project" echo " --all Do all of the above (except destroy)" echo " --help Show this help message" + echo "" + echo "Additional scripts:" + echo " ./deployment/cleanup-images.sh Clean up container images (not managed by Terraform)" exit 0 } @@ -30,6 +43,7 @@ PROVISION=false BUILD=false DEPLOY=false DESTROY=false +LIST=false while [[ $# -gt 0 ]]; do case "$1" in @@ -49,6 +63,10 @@ while [[ $# -gt 0 ]]; do DESTROY=true shift ;; + --list) + LIST=true + shift + ;; --all) PROVISION=true BUILD=true @@ -65,15 +83,141 @@ while [[ $# -gt 0 ]]; do esac done -# Ensure gcloud is configured +# Ensure project ID is available if [ -z "$PROJECT_ID" ]; then - echo "ERROR: No Google Cloud project is set. Run 'gcloud config set project YOUR_PROJECT_ID'" + echo "ERROR: No Google Cloud project ID found." + echo "Either run 'gcloud config set project YOUR_PROJECT_ID' or ensure terraform.tfvars contains project_id" exit 1 fi echo "Using Google Cloud project: $PROJECT_ID" echo "===================================" +# List Cloud Run services +if [ "$LIST" = true ]; then + echo "Listing all services and resources in project: $PROJECT_ID" + echo "=================================================" + + # Check if gcloud is available + if ! command -v gcloud &> /dev/null; then + echo "ERROR: gcloud CLI is not installed or not in PATH" + exit 1 + fi + + echo "=== COMPUTE SERVICES ===" + + echo "Cloud Run Services:" + if gcloud run services list --project="$PROJECT_ID" --format="value(metadata.name)" --limit=1 &>/dev/null; then + gcloud run services list --project="$PROJECT_ID" --format="table(metadata.name,status.url,metadata.labels.region)" 2>/dev/null + else + echo " None found or Cloud Run API not enabled" + fi + + echo "" + echo "Compute Engine Instances:" + if gcloud compute instances list --project="$PROJECT_ID" --format="value(name)" --limit=1 &>/dev/null; then + gcloud compute instances list --project="$PROJECT_ID" --format="table(name,zone,status,machineType.basename())" 2>/dev/null + else + echo " None found or Compute Engine API not enabled" + fi + + echo "" + echo "App Engine Services:" + if gcloud app describe --project="$PROJECT_ID" &>/dev/null; then + gcloud app services list --project="$PROJECT_ID" --format="table(id,versions)" 2>/dev/null || echo " None found" + else + echo " App Engine not configured" + fi + + echo "" + echo "=== DATABASE SERVICES ===" + + echo "Firestore Databases:" + if gcloud firestore databases list --project="$PROJECT_ID" --format="table(name,type,locationId)" 2>/dev/null | grep -v "Listed 0 items"; then + echo " Found databases above" + else + echo " None found or Firestore API not enabled" + fi + + echo "" + echo "Cloud SQL Instances:" + if gcloud sql instances list --project="$PROJECT_ID" --format="table(name,databaseVersion,region,tier)" 2>/dev/null | grep -v "Listed 0 items"; then + echo " Found instances above" + else + echo " None found or Cloud SQL API not enabled" + fi + + echo "" + echo "=== STORAGE SERVICES ===" + + echo "Cloud Storage Buckets:" + if gsutil ls -p "$PROJECT_ID" 2>/dev/null; then + echo " Found buckets above" + else + echo " None found or Cloud Storage API not enabled" + fi + + echo "" + echo "=== SECURITY & SECRETS ===" + + echo "Secret Manager Secrets:" + if gcloud secrets list --project="$PROJECT_ID" --format="table(name,createTime)" 2>/dev/null | grep -v "Listed 0 items"; then + echo " Found secrets above" + else + echo " None found or Secret Manager API not enabled" + fi + + echo "" + echo "IAM Service Accounts:" + if gcloud iam service-accounts list --project="$PROJECT_ID" --format="table(email,displayName)" 2>/dev/null | grep -v "Listed 0 items"; then + echo " Found service accounts above" + else + echo " None found" + fi + + echo "" + echo "=== NETWORKING ===" + + echo "VPC Networks:" + if gcloud compute networks list --project="$PROJECT_ID" --format="table(name,subnet_mode,bgp_routing_mode)" 2>/dev/null | grep -v "Listed 0 items"; then + echo " Found networks above" + else + echo " None found or using default network" + fi + + echo "" + echo "=== APIS & SERVICES ===" + + echo "Enabled APIs:" + if gcloud services list --enabled --project="$PROJECT_ID" --format="table(name,title)" --limit=10 2>/dev/null; then + echo " (Showing first 10 enabled APIs)" + else + echo " Unable to list APIs" + fi + + echo "" + echo "=== MONITORING & LOGGING ===" + + echo "Cloud Functions:" + if gcloud functions list --project="$PROJECT_ID" --format="table(name,status,trigger.eventTrigger.eventType)" 2>/dev/null | grep -v "Listed 0 items"; then + echo " Found functions above" + else + echo " None found or Cloud Functions API not enabled" + fi + + echo "" + echo "Pub/Sub Topics:" + if gcloud pubsub topics list --project="$PROJECT_ID" --format="table(name)" 2>/dev/null | grep -v "Listed 0 items"; then + echo " Found topics above" + else + echo " None found or Pub/Sub API not enabled" + fi + + echo "" + echo "Instance listing completed." + exit 0 +fi + # Destroy resources with Terraform if [ "$DESTROY" = true ]; then echo "WARNING: This will destroy all cloud resources managed by Terraform!" @@ -141,8 +285,9 @@ fi if [ "$BUILD" = true ]; then echo "Building and pushing Docker image..." - # Enable Docker to authenticate to GCR - gcloud auth configure-docker gcr.io + # Note: Docker authentication should be configured externally via: + # gcloud auth configure-docker gcr.io + # or by using service account key files # Build the image with timestamp tag TAG=$(date +%Y%m%d-%H%M%S) @@ -185,10 +330,10 @@ if [ "$DEPLOY" = true ]; then terraform init terraform apply -auto-approve - cd - > /dev/null + # Get service URL from Terraform output + SERVICE_URL=$(terraform output -raw cloud_run_url 2>/dev/null || echo "URL not available") - # Get service URL - SERVICE_URL=$(gcloud run services describe "sereact" --region="$REGION" --format='value(status.url)') + cd - > /dev/null echo "Deployment completed successfully." echo "Service URL: $SERVICE_URL" diff --git a/deployment/terraform/main.tf b/deployment/terraform/main.tf index 78f59be..d69a7c7 100644 --- a/deployment/terraform/main.tf +++ b/deployment/terraform/main.tf @@ -4,6 +4,11 @@ provider "google" { zone = var.zone } +# Get current project information +data "google_project" "current" { + project_id = var.project_id +} + # Enable required APIs resource "google_project_service" "services" { for_each = toset([