-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdeploy-script.sh
More file actions
executable file
·488 lines (408 loc) · 15.3 KB
/
deploy-script.sh
File metadata and controls
executable file
·488 lines (408 loc) · 15.3 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#!/bin/bash
set -e
CONFIG_DIR="/usr/share/nginx/html/.well-known/configs"
NGINX_CONFIG="/etc/nginx/conf.d/default.conf"
TEMP_CONFIG="/tmp/nginx_temp.conf"
echo "Fetching all configs from $CONFIG_DIR..."
# Use traditional arrays for bash 3.2 compatibility
CONFIG_FILES=()
PROXY_SOURCES=()
PROXY_TARGETS=()
# Get all config files
if [ -d "$CONFIG_DIR" ]; then
while IFS= read -r -d '' config_file; do
CONFIG_FILES+=("$config_file")
done < <(find "$CONFIG_DIR" -type f -print0 2>/dev/null)
fi
if [ ${#CONFIG_FILES[@]} -eq 0 ]; then
echo "No config files found in $CONFIG_DIR"
exit 1
fi
echo "Found ${#CONFIG_FILES[@]} config files"
# List found config files
for config_file in "${CONFIG_FILES[@]}"; do
echo " - $config_file"
done
# Check if any config has proxy settings and Veeam settings
HAS_PROXY_CONFIG=false
HAS_VEEAM_CONFIG=false
VEEAM_CONFIG=""
for config_file in "${CONFIG_FILES[@]}"; do
if jq -e '.spec.selfConfiguration.proxy' "$config_file" >/dev/null 2>&1; then
HAS_PROXY_CONFIG=true
# Check for Veeam configuration
if jq -e '.spec.selfConfiguration.proxy.veeam' "$config_file" >/dev/null 2>&1; then
cloudserver_endpoint=$(jq -r '.spec.selfConfiguration.proxy.veeam.cloudserverEndpoint // ""' "$config_file")
# Veeam is enabled if: cloudserverEndpoint exists
if [ ! -z "$cloudserver_endpoint" ] && [ "$cloudserver_endpoint" != "null" ]; then
HAS_VEEAM_CONFIG=true
base_path=$(jq -r '.spec.selfConfiguration.basePath // "/"' "$config_file")
if [ ! -z "$cloudserver_endpoint" ] && [ "$cloudserver_endpoint" != "null" ]; then
# Store Veeam configuration
VEEAM_CONFIG="$base_path|$cloudserver_endpoint"
echo "Found Veeam configuration: basePath=$base_path, endpoint=$cloudserver_endpoint"
fi
fi
fi
fi
done
# If no proxy configurations found, skip
if [ "$HAS_PROXY_CONFIG" = false ]; then
echo "No proxy configurations found, skipping nginx update"
exit 0
fi
echo "Generating nginx configuration..."
# Create backup of original nginx config
cp "$NGINX_CONFIG" "${NGINX_CONFIG}.backup"
# Read existing nginx config and prepare for modification
cp "$NGINX_CONFIG" "$TEMP_CONFIG"
# Function to check if a proxy already exists
proxy_exists() {
local source="$1"
local i
for i in "${!PROXY_SOURCES[@]}"; do
if [ "${PROXY_SOURCES[$i]}" = "$source" ]; then
return 0
fi
done
return 1
}
# Process each config file and add proxy locations
for config_file in "${CONFIG_FILES[@]}"; do
if jq -e '.spec.selfConfiguration.proxy' "$config_file" >/dev/null 2>&1; then
echo "Processing proxy config from: $config_file"
# Extract proxy configurations and collect unique ones (excluding veeam)
while IFS='|' read -r service source target; do
if [ ! -z "$service" ] && [ ! -z "$source" ] && [ ! -z "$target" ] && [ "$service" != "veeam" ]; then
echo "Found proxy: $service $source -> $target"
# Skip backend proxy if it conflicts with existing static location
if [ "$service" = "backend" ]; then
if grep -q "location $source" "$NGINX_CONFIG" 2>/dev/null; then
echo "Skipping backend proxy $source (conflicts with existing static location)"
continue
fi
fi
# Add to arrays if not already present (prevent duplicates)
if ! proxy_exists "$source"; then
PROXY_SOURCES+=("$source")
PROXY_TARGETS+=("$target")
fi
fi
done < <(jq -r '.spec.selfConfiguration.proxy | to_entries[] | "\(.key)|\(.value.source)|\(.value.target)"' "$config_file" 2>/dev/null)
fi
done
# Add collected proxy configurations
if [ ${#PROXY_SOURCES[@]} -gt 0 ] || [ "$HAS_VEEAM_CONFIG" = true ]; then
echo "Adding dynamic proxy configurations to server block..."
# Create a temporary file with proxy configurations
PROXY_TEMP="/tmp/proxy_locations.conf"
cat > "$PROXY_TEMP" << 'EOF'
# ===========================================
# Dynamic proxy configurations
# ===========================================
EOF
# Sort proxy locations for better organization
# Create arrays for different types of proxies (store indices)
ROOT_PROXY_INDICES=()
DATA_PROXY_INDICES=()
S3_PROXY_INDICES=()
# Categorize proxy locations (S3 paths get priority)
for i in "${!PROXY_SOURCES[@]}"; do
source_path="${PROXY_SOURCES[$i]}"
if [[ "$source_path" == *"/s3" ]] || [[ "$source_path" == "/s3" ]]; then
S3_PROXY_INDICES+=("$i")
elif [[ "$source_path" == "/data/"* ]]; then
DATA_PROXY_INDICES+=("$i")
else
ROOT_PROXY_INDICES+=("$i")
fi
done
# Function to generate proxy headers based on service type
generate_proxy_headers() {
local source_path="$1"
local headers=""
# Add special headers for specific services (based on production environment)
case "$source_path" in
"/sts"|"*/sts")
headers=" proxy_set_header host \$host;
proxy_set_header proxy_path $source_path;"
;;
"/utilization"|"*/utilization")
headers=" proxy_set_header host \$host;
proxy_set_header proxy_path $source_path;"
;;
esac
echo "$headers"
}
# Add proxy configurations in organized order
# 1. Root path proxies (except S3)
if [ ${#ROOT_PROXY_INDICES[@]} -gt 0 ]; then
echo " # Root path proxies" >> "$PROXY_TEMP"
for i in "${ROOT_PROXY_INDICES[@]}"; do
source_path="${PROXY_SOURCES[$i]}"
target="${PROXY_TARGETS[$i]}"
echo "Adding proxy: $source_path -> $target"
# Generate any special headers for this service
proxy_headers=$(generate_proxy_headers "$source_path")
cat >> "$PROXY_TEMP" << EOF
location $source_path {
proxy_pass $target;
$([ ! -z "$proxy_headers" ] && echo "$proxy_headers")
}
EOF
done
fi
# 2. Data path proxies (except S3)
if [ ${#DATA_PROXY_INDICES[@]} -gt 0 ]; then
echo "" >> "$PROXY_TEMP"
echo " # Data path proxies" >> "$PROXY_TEMP"
for i in "${DATA_PROXY_INDICES[@]}"; do
source_path="${PROXY_SOURCES[$i]}"
target="${PROXY_TARGETS[$i]}"
echo "Adding proxy: $source_path -> $target"
# Generate any special headers for this service
proxy_headers=$(generate_proxy_headers "$source_path")
cat >> "$PROXY_TEMP" << EOF
location $source_path {
proxy_pass $target;
$([ ! -z "$proxy_headers" ] && echo "$proxy_headers")
}
EOF
done
fi
# 3. Veeam location block (when S3 proxies with Veeam are enabled)
if [ "$HAS_VEEAM_CONFIG" = true ] && [ ${#S3_PROXY_INDICES[@]} -gt 0 ]; then
echo "" >> "$PROXY_TEMP"
echo " # Veeam proxy" >> "$PROXY_TEMP"
IFS='|' read -r base_path cloudserver_endpoint <<<"$VEEAM_CONFIG"
cat >> "$PROXY_TEMP" << EOF
location ~ "/s3/([a-z0-9][a-z0-9-.]{1,61}[a-z0-9]\/\.system-d26a9498-cb7c-4a87-a44a-8ae204f5ba6c\/(system\.xml|capacity\.xml).*?)" {
set \$escaped_proxied_path \$1;
rewrite "/s3/([a-z0-9][a-z0-9-.]{1,61}[a-z0-9]\/\.system-d26a9498-cb7c-4a87-a44a-8ae204f5ba6c\/(system\.xml|capacity\.xml).*?)" /_/veeam/\$1 break;
proxy_pass ${cloudserver_endpoint};
proxy_redirect off;
}
EOF
fi
# 4. S3 proxies (with special Veeam handling)
if [ ${#S3_PROXY_INDICES[@]} -gt 0 ]; then
echo "" >> "$PROXY_TEMP"
echo " # S3 proxies" >> "$PROXY_TEMP"
for i in "${S3_PROXY_INDICES[@]}"; do
source_path="${PROXY_SOURCES[$i]}"
target="${PROXY_TARGETS[$i]}"
echo "Adding proxy: $source_path -> $target"
# Check if this is an S3 proxy and we have Veeam enabled
if [ "$HAS_VEEAM_CONFIG" = true ] && [ ! -z "$VEEAM_CONFIG" ]; then
IFS='|' read -r base_path cloudserver_endpoint <<<"$VEEAM_CONFIG"
# Special handling for /s3 path with Veeam
if [ "$source_path" = "/s3" ]; then
if [ "${DEPLOY_SCRIPT_TEST_MODE:-false}" = "true" ]; then
# Simplified configuration for testing
cat >> "$PROXY_TEMP" << EOF
location $source_path {
resolver coredns.kube-system.svc;
# Veeam SOS API: Handle special prefix parameter
if ( \$arg_prefix = ".system-d26a9498-cb7c-4a87-a44a-8ae204f5ba6c%2F" ) {
proxy_pass ${cloudserver_endpoint};
rewrite "${source_path}/(.*)" /_/veeam/\$1 break;
}
# Standard S3 requests
proxy_pass $target;
}
EOF
else
# Full production configuration with advanced features
cat >> "$PROXY_TEMP" << EOF
location $source_path {
resolver coredns.kube-system.svc;
# Veeam SOS API support with S3-compliant bucket name validation
location ~ "${s3_path}/([a-z0-9][a-z0-9-.]{1,61}[a-z0-9])/\\.system-d26a9498-cb7c-4a87-a44a-8ae204f5ba6c/(system\\.xml|capacity\\.xml).*?" {
set \$escaped_proxied_path \$1;
rewrite "${s3_path}/([a-z0-9][a-z0-9-.]{1,61}[a-z0-9])/\\.system-d26a9498-cb7c-4a87-a44a-8ae204f5ba6c/(.*)" /_/veeam/\$1/.system-d26a9498-cb7c-4a87-a44a-8ae204f5ba6c/\$2 break;
proxy_pass ${cloudserver_endpoint};
proxy_redirect off;
}
# Veeam SOS API: Handle special prefix parameter
if ( \$arg_prefix = ".system-d26a9498-cb7c-4a87-a44a-8ae204f5ba6c%2F" ) {
proxy_pass ${cloudserver_endpoint};
rewrite "${source_path}/(.*)" /_/veeam/\$1 break;
}
# URL encoding handling for plus signs in S3 requests
set_by_lua_block \$urlencore_proxy_uri {
local uri = ngx.var.request_uri
local proxy_uri = ngx.re.gsub(uri, "^${source_path}", "")
local encoded_uri = ngx.re.gsub(proxy_uri, "\\\\+", "%2B", "jo")
return encoded_uri
}
# Standard S3 requests with URL encoding
proxy_pass ${target}\$urlencore_proxy_uri;
proxy_redirect off;
}
EOF
fi
else
# Regular S3 proxy with Veeam (not /s3 path)
if [ "${DEPLOY_SCRIPT_TEST_MODE:-false}" = "true" ]; then
# Simplified configuration for testing
cat >> "$PROXY_TEMP" << EOF
location $source_path {
# Standard S3 requests
proxy_pass $target;
}
EOF
else
# Full production configuration with advanced features
cat >> "$PROXY_TEMP" << EOF
location $source_path {
# URL encoding handling for plus signs in S3 requests
set_by_lua_block \$urlencore_proxy_uri {
local uri = ngx.var.request_uri
local proxy_uri = ngx.re.gsub(uri, "^${source_path}", "")
local encoded_uri = ngx.re.gsub(proxy_uri, "\\\\+", "%2B", "jo")
return encoded_uri
}
# Standard S3 requests with URL encoding
proxy_pass ${target}\$urlencore_proxy_uri;
proxy_redirect off;
}
EOF
fi
fi
else
# Regular S3 proxy without Veeam (test mode uses simplified config)
if [ "${DEPLOY_SCRIPT_TEST_MODE:-false}" = "true" ]; then
# Simplified configuration for testing
cat >> "$PROXY_TEMP" << EOF
location $source_path {
proxy_pass $target;
}
EOF
else
# Full production configuration with advanced features
cat >> "$PROXY_TEMP" << EOF
location $source_path {
resolver coredns.kube-system.svc;
# URL encoding handling for plus signs in S3 requests
set_by_lua_block \$urlencore_proxy_uri {
local uri = ngx.var.request_uri
local proxy_uri = ngx.re.gsub(uri, "^${source_path}", "")
local encoded_uri = ngx.re.gsub(proxy_uri, "\\\\+", "%2B", "jo")
return encoded_uri
}
# Standard S3 requests with URL encoding
proxy_pass ${target}\$urlencore_proxy_uri;
proxy_redirect off;
}
EOF
fi
fi
done
fi
# Add closing comment
cat >> "$PROXY_TEMP" << 'EOF'
# ===========================================
# End of dynamic proxy configurations
# ===========================================
EOF
# Find the correct server block to insert proxy configurations using awk
# We want to insert into a server block that handles HTTP traffic (port 80)
# Use awk to find the line number where we should insert proxy config
INSERT_LINE=$(awk '
BEGIN {
in_server = 0;
brace_count = 0;
target_line = 0;
has_listen_80 = 0;
server_end = 0;
last_server_end = 0;
}
# Track server block start
/server *\{/ {
in_server = 1;
brace_count = 1;
has_listen_80 = 0;
next;
}
# If in server block
in_server {
# Count braces
brace_count += gsub(/\{/, "") - gsub(/\}/, "");
# Check for listen 80
if (/listen +80/ || (/listen.*80/ && !/ssl/)) {
has_listen_80 = 1;
}
# Server block ends when brace_count reaches 0
if (brace_count == 0) {
server_end = NR;
last_server_end = NR;
# If this server listens on port 80, use it
if (has_listen_80) {
target_line = NR;
}
in_server = 0;
}
}
END {
# If no port 80 server found, use the last server block
if (target_line == 0 && last_server_end > 0) {
target_line = last_server_end;
}
print target_line;
}
' "$TEMP_CONFIG")
if [ "$INSERT_LINE" -gt 0 ]; then
# Insert proxy config before the closing brace of the target server
head -n $((INSERT_LINE - 1)) "$TEMP_CONFIG" > "${TEMP_CONFIG}.new"
cat "$PROXY_TEMP" >> "${TEMP_CONFIG}.new"
tail -n +$INSERT_LINE "$TEMP_CONFIG" >> "${TEMP_CONFIG}.new"
else
# Fallback: append to end of file
sed '$d' "$TEMP_CONFIG" > "${TEMP_CONFIG}.new"
cat "$PROXY_TEMP" >> "${TEMP_CONFIG}.new"
echo "}" >> "${TEMP_CONFIG}.new"
fi
# Replace the original temp config
mv "${TEMP_CONFIG}.new" "$TEMP_CONFIG"
# Clean up proxy temp file
rm -f "$PROXY_TEMP"
fi
echo "Generated integrated nginx configuration:"
cat "$TEMP_CONFIG"
# Replace the original config with the updated one
cp "$TEMP_CONFIG" "$NGINX_CONFIG"
# Validate the complete nginx configuration
echo "Validating nginx configuration..."
echo "Current nginx config:"
cat "$NGINX_CONFIG"
# Check if we're in test mode (skip validation for testing)
if [ "${DEPLOY_SCRIPT_TEST_MODE:-false}" = "true" ]; then
echo "⚠️ Test mode enabled, skipping nginx validation"
echo "✅ Nginx configuration updated successfully (test mode)"
rm -f "${NGINX_CONFIG}.backup"
elif nginx_output=$(nginx -t 2>&1); then
echo "✅ Nginx configuration is valid"
# Reload nginx (try graceful reload first, then force if needed)
echo "Reloading nginx..."
if pgrep nginx >/dev/null 2>&1; then
if nginx -s reload 2>/dev/null; then
echo "✅ Nginx reloaded successfully"
else
echo "⚠️ Nginx reload failed, but config is valid"
fi
else
echo "⚠️ Nginx is not running, config will be used when nginx starts"
echo "This is normal for container startup or testing environment"
fi
echo "✅ Nginx configuration updated successfully"
# Clean up backup file on success
rm -f "${NGINX_CONFIG}.backup"
else
echo "❌ Generated nginx configuration is invalid!"
echo "Nginx validation output:"
echo "$nginx_output"
echo "Configuration validation failed. exiting..."
exit 1
fi
# Cleanup temp file
rm -f "$TEMP_CONFIG"