-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.sh
More file actions
91 lines (79 loc) · 3.01 KB
/
entrypoint.sh
File metadata and controls
91 lines (79 loc) · 3.01 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
#!/bin/bash
# Declare an associative array
declare -A services
# Read key-value pairs from JSON and add to the associative array
while IFS="=" read -r key value; do
# Validate and sanitize the key before using it as an array subscript
if [ -z "$key" ]; then
key="_empty_key" # Assign a placeholder key for empty keys
fi
# Add the key-value pair to the services array
services["$key"]="$value"
done < <(jq -r "to_entries | map(\"\(.key)=\(.value)\") | .[]" "${BALANCER_NGINX_SERVICES_FILE}")
# To demonstrate, print the array elements
for key in "${!services[@]}"; do
# Display placeholder for empty keys during debugging
if [ "$key" == "_empty_key" ]; then
echo "(empty) -> ${services[$key]}"
else
echo "$key -> ${services[$key]}"
fi
done
# Load or source additional script modules
update_config() {
source /etc/nginx/modules/initialize_project_conf.sh
source /etc/nginx/modules/generate_certificate.sh
source /etc/nginx/modules/generate_dh_params.sh
source /etc/nginx/modules/create_nginx_server_block.sh
source /etc/nginx/modules/generate_certificates_and_nginx_blocks.sh
nginx -s reload
}
# Function to check DNS availability and use fallbacks
initialize_dns_check_with_fallback() {
for service in "${!services[@]}"; do
local hostname=$(echo "${services[$service]}" | cut -d: -f1)
# Only check DNS if hostname is non-empty
if [ -n "$hostname" ] && ! host "$hostname" > /dev/null 2>&1; then
echo "DNS resolution failed for $hostname, using fallback."
services["$service"]="gitlab-runner:404" # Set fallback host and port
fi
done
}
# Update services, configure NGINX, and start NGINX
initialize_dns_check_with_fallback
update_config
nginx -g "daemon off;" &
# Initialize original settings in a separate array to ensure they are preserved
declare -A original_services
for key in "${!services[@]}"; do
original_services["$key"]="${services[$key]}"
done
update_services_and_reload_nginx() {
local changed=false
for service in "${!services[@]}"; do
local original_setting="${original_services[$service]}"
local hostname=$(echo "$original_setting" | cut -d: -f1)
# Only check DNS if hostname is non-empty
if [ -n "$hostname" ] && ! host "$hostname" > /dev/null 2>&1; then
if [ "${services[$service]}" != "gitlab-runner:404" ]; then
services["$service"]="gitlab-runner:404"
changed=true
fi
else
if [ "${services[$service]}" != "$original_setting" ]; then
services["$service"]="$original_setting"
changed=true
fi
fi
done
if [ "$changed" = true ]; then
update_config
nginx -s reload
echo "*** Detected change in local DNS - config was updated ***"
fi
}
# Periodically check service DNS and update NGINX config as needed
while true; do
update_services_and_reload_nginx
sleep "${BALANCER_SLEEP_BUFFER}"
done