-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovision-cloud.sh
More file actions
executable file
·275 lines (233 loc) · 9.57 KB
/
provision-cloud.sh
File metadata and controls
executable file
·275 lines (233 loc) · 9.57 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
#!/bin/bash
# This script provisions the cloud infrastructure for n8n on GCP.
# Exit on error
set -e
# --- Configuration ---
# Project Selection
echo "Fetching your GCP projects..."
PROJECTS=$(gcloud projects list --format="value(projectId)" 2>/dev/null)
if [ -z "$PROJECTS" ]; then
echo "No projects found or error fetching projects."
read -p "Enter your GCP Project ID manually: " PROJECT_ID
else
PROJECT_ARRAY=($PROJECTS)
if [ ${#PROJECT_ARRAY[@]} -eq 1 ]; then
PROJECT_ID="${PROJECT_ARRAY[0]}"
echo "Using project: $PROJECT_ID"
else
echo ""
echo "Available GCP Projects:"
select PROJECT_ID in "${PROJECT_ARRAY[@]}"; do
if [ -n "$PROJECT_ID" ]; then
break
else
echo "Invalid selection. Please try again."
fi
done
fi
fi
# Set project first to ensure subsequent commands work
gcloud config set project "$PROJECT_ID" --quiet
# Region Selection
echo ""
echo "Fetching available GCP regions..."
REGIONS=$(gcloud compute regions list --format="value(name)" 2>/dev/null | sort)
if [ -z "$REGIONS" ]; then
echo "Error fetching regions. Please ensure Compute Engine API is enabled."
read -p "Enter the GCP Region manually (e.g., us-central1): " REGION
else
REGION_ARRAY=($REGIONS)
echo ""
echo "Select a GCP Region (recommended: regions close to your users):"
echo "Tip: Europe regions start with 'europe-', US with 'us-', Asia with 'asia-'"
echo ""
select REGION in "${REGION_ARRAY[@]}"; do
if [ -n "$REGION" ]; then
break
else
echo "Invalid selection. Please try again."
fi
done
fi
# Zone Selection
echo ""
echo "Fetching available zones in $REGION..."
ZONES=$(gcloud compute zones list --filter="region:$REGION" --format="value(name)" 2>/dev/null | sort)
if [ -z "$ZONES" ]; then
echo "Error fetching zones for region $REGION."
read -p "Enter the GCP Zone manually (e.g., $REGION-a): " ZONE
else
ZONE_ARRAY=($ZONES)
echo ""
echo "Select a Zone in $REGION:"
select ZONE in "${ZONE_ARRAY[@]}"; do
if [ -n "$ZONE" ]; then
break
else
echo "Invalid selection. Please try again."
fi
done
fi
# Machine Type Selection
echo ""
echo "Select a machine type for your n8n instance:"
echo ""
echo "Machine Type vCPUs Memory Est. Cost/Month Recommended For"
echo "--------------------------------------------------------------------------------"
echo "1) e2-micro 0.25 1 GB ~$7-9 Testing only (may be slow)"
echo "2) e2-small 0.5 2 GB ~$14-16 Light usage, few workflows"
echo "3) e2-medium 1 4 GB ~$24-30 Recommended: Most users"
echo "4) e2-standard-2 2 8 GB ~$49-60 Heavy usage, many workflows"
echo "5) e2-standard-4 4 16 GB ~$97-120 Very heavy usage, complex workflows"
echo "6) n2-standard-2 2 8 GB ~$73-90 Better performance than e2"
echo "7) n2-standard-4 4 16 GB ~$146-180 High performance needs"
echo ""
echo "Note: Costs vary by region. Prices shown are estimates for US/Europe regions."
echo ""
PS3="Select machine type (1-7) [default=3]: "
MACHINE_TYPES=("e2-micro" "e2-small" "e2-medium" "e2-standard-2" "e2-standard-4" "n2-standard-2" "n2-standard-4")
MACHINE_NAMES=("e2-micro (0.25 vCPU, 1GB)" "e2-small (0.5 vCPU, 2GB)" "e2-medium (1 vCPU, 4GB)" "e2-standard-2 (2 vCPU, 8GB)" "e2-standard-4 (4 vCPU, 16GB)" "n2-standard-2 (2 vCPU, 8GB)" "n2-standard-4 (4 vCPU, 16GB)")
select opt in "${MACHINE_NAMES[@]}"; do
case $REPLY in
1) MACHINE_TYPE="${MACHINE_TYPES[0]}"; break;;
2) MACHINE_TYPE="${MACHINE_TYPES[1]}"; break;;
3) MACHINE_TYPE="${MACHINE_TYPES[2]}"; break;;
4) MACHINE_TYPE="${MACHINE_TYPES[3]}"; break;;
5) MACHINE_TYPE="${MACHINE_TYPES[4]}"; break;;
6) MACHINE_TYPE="${MACHINE_TYPES[5]}"; break;;
7) MACHINE_TYPE="${MACHINE_TYPES[6]}"; break;;
"") MACHINE_TYPE="e2-medium"; echo "Using default: e2-medium"; break;;
*) echo "Invalid selection. Please try again.";;
esac
done
# Domain Name
echo ""
read -p "Enter the domain name for n8n (e.g., n8n.example.com): " DOMAIN_NAME
# Validate domain name
if [ -z "$DOMAIN_NAME" ]; then
echo "Error: Domain name cannot be empty."
exit 1
fi
# Basic domain name validation
if ! echo "$DOMAIN_NAME" | grep -qE '^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*\.[a-zA-Z]{2,}$'; then
echo "Error: '$DOMAIN_NAME' does not appear to be a valid domain name."
echo "Please use a format like: n8n.example.com"
exit 1
fi
# Display selected configuration
echo ""
echo "========================================="
echo "Deployment Configuration:"
echo "========================================="
echo "Project: $PROJECT_ID"
echo "Region: $REGION"
echo "Zone: $ZONE"
echo "Machine Type: $MACHINE_TYPE"
echo "Domain: $DOMAIN_NAME"
echo "========================================="
echo ""
read -p "Continue with this configuration? (y/N): " confirm
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
echo "Deployment cancelled."
exit 0
fi
INSTANCE_NAME="n8n-instance"
STATIC_IP_NAME="n8n-static-ip"
FIREWALL_RULE_NAME="allow-web-traffic"
NETWORK_TAG="n8n-web"
# --- Helper Functions ---
check_dns_propagation() {
local domain=$1
local expected_ip=$2
local max_attempts=10
local attempt=1
echo "Checking DNS propagation for $domain..."
while [ $attempt -le $max_attempts ]; do
echo "Attempt $attempt/$max_attempts: Checking if $domain resolves to $expected_ip..."
# Try to resolve the domain
resolved_ip=$(dig +short "$domain" @8.8.8.8 | tail -n1)
if [ -z "$resolved_ip" ]; then
resolved_ip=$(nslookup "$domain" 8.8.8.8 2>/dev/null | grep -A1 "Name:" | tail -n1 | awk '{print $2}')
fi
if [ "$resolved_ip" = "$expected_ip" ]; then
echo "✓ DNS propagation confirmed! $domain resolves to $expected_ip"
return 0
fi
if [ $attempt -lt $max_attempts ]; then
echo "DNS not propagated yet (currently resolves to: ${resolved_ip:-'nothing'}). Waiting 10 seconds..."
sleep 10
fi
attempt=$((attempt + 1))
done
echo "⚠ Warning: DNS may not be fully propagated yet."
echo "Current resolution: ${resolved_ip:-'no result'}"
echo "Expected: $expected_ip"
read -p "Continue anyway? (y/N): " continue_anyway
if [ "$continue_anyway" != "y" ] && [ "$continue_anyway" != "Y" ]; then
echo "Deployment cancelled. Please wait for DNS to propagate and try again."
exit 1
fi
}
# --- GCP CLI Checks ---
if ! command -v gcloud &> /dev/null
then
echo "gcloud CLI not found. Please install it and configure it."
exit 1
fi
# --- Provisioning ---
echo ""
echo "Configuring gcloud CLI..."
gcloud config set compute/region "$REGION" --quiet
gcloud config set compute/zone "$ZONE" --quiet
# Check if static IP already exists
if gcloud compute addresses describe "$STATIC_IP_NAME" --region="$REGION" &>/dev/null; then
echo "Static IP '$STATIC_IP_NAME' already exists. Using existing IP..."
STATIC_IP=$(gcloud compute addresses describe "$STATIC_IP_NAME" --region="$REGION" --format='get(address)')
else
echo "Reserving a static IP address..."
gcloud compute addresses create "$STATIC_IP_NAME" --region="$REGION"
STATIC_IP=$(gcloud compute addresses describe "$STATIC_IP_NAME" --region="$REGION" --format='get(address)')
fi
# Check if firewall rule already exists
if gcloud compute firewall-rules describe "$FIREWALL_RULE_NAME" &>/dev/null; then
echo "Firewall rule '$FIREWALL_RULE_NAME' already exists. Skipping creation..."
else
echo "Creating firewall rules..."
gcloud compute firewall-rules create "$FIREWALL_RULE_NAME" \
--allow tcp:80,tcp:443,tcp:22 \
--source-ranges=0.0.0.0/0 \
--target-tags="$NETWORK_TAG"
fi
# Check if instance already exists
if gcloud compute instances describe "$INSTANCE_NAME" --zone="$ZONE" &>/dev/null; then
echo "Error: Instance '$INSTANCE_NAME' already exists in zone '$ZONE'."
echo "Please delete the existing instance or choose a different name."
exit 1
fi
echo "Creating the Compute Engine instance..."
gcloud compute instances create "$INSTANCE_NAME" \
--machine-type="$MACHINE_TYPE" \
--image-family=ubuntu-2204-lts \
--image-project=ubuntu-os-cloud \
--address="$STATIC_IP_NAME" \
--tags="$NETWORK_TAG"
# --- DNS Instructions ---
echo "-----------------------------------------------------"
echo "Action Required:"
echo "Please create a DNS A record for '$DOMAIN_NAME' pointing to the IP address: $STATIC_IP"
echo "-----------------------------------------------------"
read -p "Press [Enter] to continue after updating the DNS record..."
# Check DNS propagation
check_dns_propagation "$DOMAIN_NAME" "$STATIC_IP"
# --- Remote Configuration ---
echo "Waiting for the instance to be ready..."
sleep 30 # Wait for the instance to boot up
echo "Copying setup files to the instance..."
gcloud compute scp --recurse ./templates "$INSTANCE_NAME":~
gcloud compute scp ./setup-instance.sh "$INSTANCE_NAME":~
echo "Executing the setup script on the instance..."
gcloud compute ssh "$INSTANCE_NAME" --command="./setup-instance.sh \"$DOMAIN_NAME\""
echo "-----------------------------------------------------"
echo "Deployment complete!"
echo "You can now access your n8n instance at: https://$DOMAIN_NAME"
echo "-----------------------------------------------------"