-
Notifications
You must be signed in to change notification settings - Fork 5
/
use-states.sh
165 lines (136 loc) · 4.79 KB
/
use-states.sh
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
#!/bin/bash
set -eu
git config apply.whitespace nowarn
git config core.filemode false
get_nginx_pointing() {
local project_name=$1
local nginx_config
local blue_exists
local green_exists
local nginx_pointing
nginx_config=$(docker exec "${project_name}-nginx" cat /etc/nginx/conf.d/nginx.conf || echo "failed")
if echo "$nginx_config" | grep -Eq "^[^#]*proxy_pass http[s]*://${project_name}-blue"; then
blue_exists="blue"
else
blue_exists="failed"
fi
if echo "$nginx_config" | grep -Eq "^[^#]*proxy_pass http[s]*://${project_name}-green"; then
green_exists="green"
else
green_exists="failed"
fi
if [[ $blue_exists == "blue" ]] && [[ $green_exists == "green" ]]; then
nginx_pointing="error"
elif [[ $blue_exists == "blue" ]]; then
nginx_pointing="blue"
elif [[ $green_exists == "green" ]]; then
nginx_pointing="green"
else
nginx_pointing="failed"
fi
echo "$nginx_pointing"
}
cache_all_states() {
echo '[NOTICE] Checking which container, blue or green, is running. (Priority : Where Nginx Pointing > Which Container Running > Which Container Restarting)'
## Calculation
# 1. Nginx pointing
local nginx_pointing
nginx_pointing=$(get_nginx_pointing "$project_name")
# 2. Container status
local blue_status
blue_status=$(docker inspect --format='{{.State.Status}}' ${project_name}-blue 2>/dev/null || echo "unknown")
local green_status
green_status=$(docker inspect --format='{{.State.Status}}' ${project_name}-green 2>/dev/null || echo "unknown")
echo "[DEBUG] ! Checking which (Blue OR Green) is currently running... (Base Check) : nginx_pointing(${nginx_pointing}), blue_status(${blue_status}), green_status(${green_status})"
local blue_score=1 # Base score
local green_score=0
## Give scores
# 1. Nginx pointing
if [[ "$nginx_pointing" == "blue" ]]; then
blue_score=$((blue_score + 30))
elif [[ "$nginx_pointing" == "green" ]]; then
green_score=$((green_score + 30))
fi
# 2. Container status
case "$blue_status" in
"running")
blue_score=$((blue_score + 30))
;;
"restarting")
blue_score=$((blue_score + 28))
;;
"created")
blue_score=$((blue_score + 25))
;;
"exited")
blue_score=$((blue_score + 5))
;;
"paused")
blue_score=$((blue_score + 3))
;;
"dead")
blue_score=$((blue_score + 1))
;;
*)
;;
esac
case "$green_status" in
"running")
green_score=$((green_score + 30))
;;
"restarting")
green_score=$((green_score + 28))
;;
"created")
green_score=$((green_score + 25))
;;
"exited")
green_score=$((green_score + 5))
;;
"paused")
green_score=$((green_score + 3))
;;
"dead")
green_score=$((green_score + 1))
;;
*)
;;
esac
# Check creation times and award 5 points to the most recently created container
local blue_created green_created
blue_created=$(docker inspect --format='{{.Created}}' ${project_name}-blue 2>/dev/null || echo "unknown")
green_created=$(docker inspect --format='{{.Created}}' ${project_name}-green 2>/dev/null || echo "unknown")
if [[ "$blue_created" != "unknown" && "$green_created" != "unknown" ]]; then
if [[ "$blue_created" > "$green_created" ]]; then
blue_score=$((blue_score + 5))
elif [[ "$green_created" > "$blue_created" ]]; then
green_score=$((green_score + 5))
fi
fi
# Final
if [[ $blue_score -gt $green_score ]]; then
state='blue'
if [[ ("$blue_status" == "unknown" || "$blue_status" == "exited" || "$blue_status" == "paused" || "$blue_status" == "dead") && "$green_status" == "running" ]]; then
state_for_emergency='green'
else
state_for_emergency=${state}
fi
new_state='green'
new_upstream=${green_upstream}
elif [[ $green_score -gt $blue_score ]]; then
state='green'
if [[ ("$green_status" == "unknown" || "$green_status" == "exited" || "$green_status" == "paused" || "$green_status" == "dead") && "$blue_status" == "running" ]]; then
state_for_emergency='blue'
else
state_for_emergency=${state}
fi
new_state='blue'
new_upstream=${blue_upstream}
else
state='green'
state_for_emergency=${state}
new_state='blue'
new_upstream=${blue_upstream}
fi
echo "[DEBUG] ! Checked which (Blue OR Green) is currently running... (Final Check) : blue_score : ${blue_score}, green_score : ${green_score}, state : ${state}, new_state : ${new_state}, state_for_emergency : ${state_for_emergency}, new_upstream : ${new_upstream}."
}