-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·337 lines (296 loc) · 14.6 KB
/
deploy.sh
File metadata and controls
executable file
·337 lines (296 loc) · 14.6 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/bin/bash
set -euo pipefail
# ═══════════════════════════════════════════════════════════════════════════════
# DreamFactory — Google Cloud Run Deployment
# ═══════════════════════════════════════════════════════════════════════════════
#
# Usage:
# ./deploy.sh # Build and deploy
# ./deploy.sh --project=my-proj --region=us-central1
# ./deploy.sh --no-build --image=REGISTRY/IMG # Use a pre-built image
#
# Flags:
# --no-build Skip build; use --image instead
# --image=XXX Pre-built image URL (only with --no-build)
# --project=XXX GCP project ID
# --region=XXX GCP region
# --service-name=XXX Cloud Run service name (default: dreamfactory)
# --db-tier=XXX Cloud SQL tier (default: db-g1-small)
# --yes Skip confirmation prompts
# ═══════════════════════════════════════════════════════════════════════════════
# ── Defaults ─────────────────────────────────────────────────────────────────
BUILD_MODE=true
CUSTOM_IMAGE=""
PROJECT=""
REGION=""
SERVICE_NAME="dreamfactory"
DB_INSTANCE_NAME="dreamfactory-db"
DB_NAME="dreamfactory"
DB_USER="dreamfactory"
DB_TIER="db-g1-small"
AR_REPO="dreamfactory"
YES=false
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
info() { echo -e "${BLUE}[INFO]${NC} $*"; }
ok() { echo -e "${GREEN}[OK]${NC} $*"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
err() { echo -e "${RED}[ERROR]${NC} $*" >&2; }
fatal() { err "$@"; exit 1; }
# ── Parse arguments ──────────────────────────────────────────────────────────
for arg in "$@"; do
case $arg in
--build) BUILD_MODE=true ;;
--no-build) BUILD_MODE=false ;;
--image=*) CUSTOM_IMAGE="${arg#*=}" ;;
--project=*) PROJECT="${arg#*=}" ;;
--region=*) REGION="${arg#*=}" ;;
--service-name=*) SERVICE_NAME="${arg#*=}" ;;
--db-tier=*) DB_TIER="${arg#*=}" ;;
--yes) YES=true ;;
--help|-h)
head -25 "$0" | tail -18
exit 0
;;
*) warn "Unknown argument: $arg" ;;
esac
done
# ── Prerequisites ────────────────────────────────────────────────────────────
command -v gcloud >/dev/null 2>&1 || fatal "gcloud CLI not found. Install: https://cloud.google.com/sdk/docs/install"
prompt() {
local var_name="$1" prompt_text="$2" default="${3:-}"
local current_val="${!var_name:-}"
if [ -n "$current_val" ]; then return; fi
if [ -n "$default" ]; then
read -rp "$prompt_text [$default]: " input
printf -v "$var_name" '%s' "${input:-$default}"
else
while [ -z "${!var_name:-}" ]; do
read -rp "$prompt_text: " input
printf -v "$var_name" '%s' "$input"
done
fi
}
prompt_secret() {
local var_name="$1" prompt_text="$2"
local current_val="${!var_name:-}"
if [ -n "$current_val" ]; then return; fi
while [ -z "${!var_name:-}" ]; do
read -srp "$prompt_text: " input
echo
printf -v "$var_name" '%s' "$input"
done
}
confirm() {
if [ "$YES" = true ]; then return 0; fi
read -rp "$1 [y/N]: " response
[[ "$response" =~ ^[Yy]$ ]]
}
# ── Gather inputs ────────────────────────────────────────────────────────────
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo " DreamFactory — Google Cloud Run Deployment"
echo "═══════════════════════════════════════════════════════════════"
echo ""
prompt PROJECT "GCP Project ID"
prompt REGION "GCP Region" "us-central1"
prompt DF_LICENSE_KEY "DreamFactory License Key"
prompt ADMIN_EMAIL "Admin email"
prompt_secret ADMIN_PASSWORD "Admin password"
# ── Summary ──────────────────────────────────────────────────────────────────
echo ""
info "Deployment summary:"
echo " Project: $PROJECT"
echo " Region: $REGION"
echo " Service: $SERVICE_NAME"
echo " DB Instance: $DB_INSTANCE_NAME ($DB_TIER)"
echo " Mode: $([ "$BUILD_MODE" = true ] && echo 'Build from source' || echo 'Pre-built image')"
echo " Admin: $ADMIN_EMAIL"
echo ""
if ! confirm "Proceed with deployment?"; then
echo "Aborted."
exit 0
fi
# ── Set project ──────────────────────────────────────────────────────────────
gcloud config set project "$PROJECT" --quiet
# ── Enable APIs ──────────────────────────────────────────────────────────────
info "Enabling required GCP APIs..."
APIS=(run.googleapis.com sqladmin.googleapis.com secretmanager.googleapis.com)
if [ "$BUILD_MODE" = true ]; then
APIS+=(cloudbuild.googleapis.com artifactregistry.googleapis.com)
fi
gcloud services enable "${APIS[@]}" --quiet
ok "APIs enabled"
# ── Cloud SQL ────────────────────────────────────────────────────────────────
info "Setting up Cloud SQL instance: $DB_INSTANCE_NAME"
if gcloud sql instances describe "$DB_INSTANCE_NAME" --quiet >/dev/null 2>&1; then
ok "Cloud SQL instance already exists"
else
info "Creating Cloud SQL instance (this takes 5-10 minutes)..."
gcloud sql instances create "$DB_INSTANCE_NAME" \
--database-version=MYSQL_8_0 \
--tier="$DB_TIER" \
--region="$REGION" \
--storage-auto-increase \
--storage-size=10GB \
--backup-start-time=03:00 \
--availability-type=zonal \
--quiet
ok "Cloud SQL instance created"
fi
# Database
if gcloud sql databases describe "$DB_NAME" --instance="$DB_INSTANCE_NAME" --quiet >/dev/null 2>&1; then
ok "Database '$DB_NAME' already exists"
else
info "Creating database: $DB_NAME"
gcloud sql databases create "$DB_NAME" --instance="$DB_INSTANCE_NAME" --quiet
ok "Database created"
fi
# DB user + password
DB_PASSWORD=$(openssl rand -base64 32 | tr -dc 'a-zA-Z0-9' | head -c 32)
if gcloud sql users list --instance="$DB_INSTANCE_NAME" --format="value(name)" | grep -q "^${DB_USER}$"; then
info "DB user '$DB_USER' exists — updating password"
gcloud sql users set-password "$DB_USER" --instance="$DB_INSTANCE_NAME" --password="$DB_PASSWORD" --quiet
else
info "Creating DB user: $DB_USER"
gcloud sql users create "$DB_USER" --instance="$DB_INSTANCE_NAME" --password="$DB_PASSWORD" --quiet
fi
ok "Database user configured"
# Connection name for Cloud SQL proxy
CONNECTION_NAME=$(gcloud sql instances describe "$DB_INSTANCE_NAME" --format="value(connectionName)")
info "Cloud SQL connection: $CONNECTION_NAME"
# ── Secrets ──────────────────────────────────────────────────────────────────
info "Setting up Secret Manager secrets..."
create_or_update_secret() {
local name="$1" value="$2"
if gcloud secrets describe "$name" --quiet >/dev/null 2>&1; then
echo -n "$value" | gcloud secrets versions add "$name" --data-file=- --quiet
ok "Secret '$name' updated"
else
echo -n "$value" | gcloud secrets create "$name" --data-file=- --replication-policy=automatic --quiet
ok "Secret '$name' created"
fi
}
# Generate APP_KEY
APP_KEY="base64:$(openssl rand -base64 32)"
create_or_update_secret "dreamfactory-app-key" "$APP_KEY"
create_or_update_secret "dreamfactory-db-password" "$DB_PASSWORD"
create_or_update_secret "dreamfactory-license-key" "$DF_LICENSE_KEY"
create_or_update_secret "dreamfactory-admin-password" "$ADMIN_PASSWORD"
# Grant Cloud Run service account access to secrets
PROJECT_NUMBER=$(gcloud projects describe "$PROJECT" --format="value(projectNumber)")
SA="${PROJECT_NUMBER}-compute@developer.gserviceaccount.com"
for secret in dreamfactory-app-key dreamfactory-db-password dreamfactory-license-key dreamfactory-admin-password; do
gcloud secrets add-iam-policy-binding "$secret" \
--member="serviceAccount:${SA}" \
--role="roles/secretmanager.secretAccessor" \
--quiet >/dev/null 2>&1
done
ok "Secret access granted to Cloud Run service account"
# Grant Cloud SQL client role
gcloud projects add-iam-policy-binding "$PROJECT" \
--member="serviceAccount:${SA}" \
--role="roles/cloudsql.client" \
--quiet >/dev/null 2>&1
ok "Cloud SQL client role granted"
# ── Image ────────────────────────────────────────────────────────────────────
if [ "$BUILD_MODE" = true ]; then
info "Building image from source..."
# Artifact Registry repo
AR_LOCATION="${REGION}"
AR_IMAGE="${AR_LOCATION}-docker.pkg.dev/${PROJECT}/${AR_REPO}/${SERVICE_NAME}:latest"
if gcloud artifacts repositories describe "$AR_REPO" --location="$AR_LOCATION" --quiet >/dev/null 2>&1; then
ok "Artifact Registry repo already exists"
else
info "Creating Artifact Registry repo: $AR_REPO"
gcloud artifacts repositories create "$AR_REPO" \
--repository-format=docker \
--location="$AR_LOCATION" \
--quiet
ok "Artifact Registry repo created"
fi
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Commercial composer files are optional — if absent, builds as OSS
if [ ! -f "${SCRIPT_DIR}/composer.json" ]; then
warn "No composer.json found — building OSS edition."
warn "For commercial features, place composer.json + composer.lock in this directory."
if ! confirm "Continue with OSS build?"; then
echo "Aborted. Add your commercial composer files and re-run."
exit 0
fi
else
ok "Commercial composer files found"
fi
info "Submitting build to Cloud Build..."
gcloud builds submit "${SCRIPT_DIR}" \
--config="${SCRIPT_DIR}/cloudbuild.yaml" \
--substitutions="_AR_IMAGE=${AR_IMAGE}" \
--timeout=1800 \
--quiet
ok "Image built and pushed: $AR_IMAGE"
IMAGE="$AR_IMAGE"
else
if [ -z "$CUSTOM_IMAGE" ]; then
fatal "No image specified. Use --image=REGISTRY/IMAGE with --no-build."
fi
IMAGE="$CUSTOM_IMAGE"
info "Using pre-built image: $IMAGE"
fi
# ── Deploy to Cloud Run ──────────────────────────────────────────────────────
info "Deploying to Cloud Run: $SERVICE_NAME"
# First deploy with max-instances=1 to prevent concurrent migration/seed races,
# then scale up after the revision is healthy.
gcloud run deploy "$SERVICE_NAME" \
--image="$IMAGE" \
--region="$REGION" \
--platform=managed \
--add-cloudsql-instances="$CONNECTION_NAME" \
--set-secrets="APP_KEY=dreamfactory-app-key:latest,DB_PASSWORD=dreamfactory-db-password:latest,DF_LICENSE_KEY=dreamfactory-license-key:latest,ADMIN_PASSWORD=dreamfactory-admin-password:latest" \
--set-env-vars="DB_CONNECTION=mysql,DB_HOST=localhost,DB_PORT=3306,DB_DATABASE=${DB_NAME},DB_USERNAME=${DB_USER},DB_SOCKET=/cloudsql/${CONNECTION_NAME},CACHE_DRIVER=database,CACHE_STORE=database,SESSION_DRIVER=database,ADMIN_EMAIL=${ADMIN_EMAIL},APP_ENV=production,APP_DEBUG=false,LOG_CHANNEL=stderr" \
--min-instances=0 \
--max-instances=1 \
--memory=2Gi \
--cpu=2 \
--timeout=300 \
--allow-unauthenticated \
--port=8080 \
--quiet
ok "Cloud Run service deployed"
# Scale up now that first revision is healthy
info "Scaling to min=1, max=10 instances..."
gcloud run services update "$SERVICE_NAME" \
--region="$REGION" \
--min-instances=1 \
--max-instances=10 \
--quiet
ok "Scaling complete"
# ── Output ───────────────────────────────────────────────────────────────────
SERVICE_URL=$(gcloud run services describe "$SERVICE_NAME" --region="$REGION" --format="value(status.url)")
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo -e " ${GREEN}DreamFactory deployed successfully!${NC}"
echo "═══════════════════════════════════════════════════════════════"
echo ""
echo " URL: $SERVICE_URL"
echo " Admin login: $ADMIN_EMAIL"
echo ""
echo " Cloud SQL: $CONNECTION_NAME"
echo " DB Name: $DB_NAME"
echo " DB User: $DB_USER"
echo ""
echo " Secrets stored in Secret Manager:"
echo " - dreamfactory-app-key"
echo " - dreamfactory-db-password"
echo " - dreamfactory-license-key"
echo " - dreamfactory-admin-password"
echo ""
echo " Next steps:"
echo " 1. Open $SERVICE_URL and log in"
echo " 2. Verify commercial features in Admin > Config"
echo " 3. (Optional) Set up a custom domain:"
echo " gcloud run domain-mappings create --service=$SERVICE_NAME --domain=YOUR_DOMAIN --region=$REGION"
echo ""