-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·119 lines (103 loc) · 3.95 KB
/
deploy.sh
File metadata and controls
executable file
·119 lines (103 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash
# ArXiv Paper Collector - Google Cloud Deployment Script
set -e
PROJECT_ID="zesameri"
SERVICE_NAME="arxiv-paper-collector"
REGION="us-central1"
DB_INSTANCE_NAME="arxiv-papers-db"
DB_NAME="arxiv_papers"
DB_USER="postgres"
echo "🚀 Deploying ArXiv Paper Collector to Google Cloud"
echo "=================================================="
echo "Project: $PROJECT_ID"
echo "Region: $REGION"
echo ""
# Set project
echo "📋 Setting up project configuration..."
gcloud config set project $PROJECT_ID
# Enable required APIs
echo "🔌 Enabling required APIs..."
gcloud services enable \
cloudbuild.googleapis.com \
run.googleapis.com \
sql-component.googleapis.com \
sqladmin.googleapis.com \
artifactregistry.googleapis.com
# Create Cloud SQL instance (if it doesn't exist)
echo "🗄️ Setting up Cloud SQL PostgreSQL..."
if ! gcloud sql instances describe $DB_INSTANCE_NAME --quiet 2>/dev/null; then
echo "Creating new Cloud SQL instance..."
gcloud sql instances create $DB_INSTANCE_NAME \
--database-version=POSTGRES_15 \
--tier=db-f1-micro \
--region=$REGION \
--storage-size=10GB \
--storage-type=SSD \
--no-backup
echo "Creating database..."
gcloud sql databases create $DB_NAME --instance=$DB_INSTANCE_NAME
echo "Setting database password..."
echo "Please set a password for the database user:"
gcloud sql users set-password $DB_USER --instance=$DB_INSTANCE_NAME --prompt-for-password
else
echo "Cloud SQL instance already exists: $DB_INSTANCE_NAME"
fi
# Get database connection string
echo "📡 Getting database connection info..."
CONNECTION_NAME=$(gcloud sql instances describe $DB_INSTANCE_NAME --format="value(connectionName)")
echo "Connection name: $CONNECTION_NAME"
# Build and deploy to Cloud Run
echo "🏗️ Building and deploying to Cloud Run..."
gcloud builds submit --tag gcr.io/$PROJECT_ID/$SERVICE_NAME --file docker/Dockerfile .
# Generate random secret key
SECRET_KEY=$(python3 -c "import secrets; print(secrets.token_urlsafe(50))")
# Deploy to Cloud Run
echo "☁️ Deploying to Cloud Run..."
gcloud run deploy $SERVICE_NAME \
--image gcr.io/$PROJECT_ID/$SERVICE_NAME \
--platform managed \
--region $REGION \
--allow-unauthenticated \
--port 8080 \
--memory 2Gi \
--cpu 2 \
--set-env-vars="
DEBUG=False,
SECRET_KEY=$SECRET_KEY,
ALLOWED_HOSTS=*,
PUBMED_EMAIL=your-email@example.com
" \
--set-secrets="DATABASE_PASSWORD=db-password:latest" \
--update-env-vars="DATABASE_URL=postgresql://$DB_USER:\$DATABASE_PASSWORD@//cloudsql/$CONNECTION_NAME/$DB_NAME" \
--add-cloudsql-instances $CONNECTION_NAME \
--max-instances 10
# Run migrations
echo "🔄 Running database migrations..."
gcloud run jobs create migrate-job \
--image gcr.io/$PROJECT_ID/$SERVICE_NAME \
--region $REGION \
--set-env-vars="
DEBUG=False,
DATABASE_URL=postgresql://$DB_USER:PASSWORD@//cloudsql/$CONNECTION_NAME/$DB_NAME
" \
--add-cloudsql-instances $CONNECTION_NAME \
--command python,backend/manage.py,migrate
echo ""
echo "🎉 Deployment Complete!"
echo "======================="
# Get service URL
SERVICE_URL=$(gcloud run services describe $SERVICE_NAME --region=$REGION --format="value(status.url)")
echo "🌐 Your ArXiv Paper Collector is live at:"
echo " $SERVICE_URL"
echo ""
echo "📋 Next steps:"
echo "1. Update the DATABASE_URL with the actual password"
echo "2. Set your PUBMED_EMAIL in the environment variables"
echo "3. Create a superuser: gcloud run jobs execute migrate-job --region=$REGION"
echo "4. Visit $SERVICE_URL/admin to access Django admin"
echo "5. Use $SERVICE_URL/api/ for the REST API"
echo ""
echo "💡 Useful commands:"
echo " View logs: gcloud run services logs read $SERVICE_NAME --region=$REGION"
echo " Update env: gcloud run services update $SERVICE_NAME --region=$REGION --set-env-vars=KEY=VALUE"
echo " Scale down: gcloud run services update $SERVICE_NAME --region=$REGION --max-instances=0"