-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick-start.sh
executable file
·267 lines (191 loc) · 6.47 KB
/
quick-start.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
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
#!/bin/bash
COLOR_OFF='\033[0m'
COLOR_RED='\033[0;91m'
COLOR_CYAN='\033[0;96m'
COLOR_GREEN='\033[0;92m'
COLOR_YELLOW='\033[0;93m'
current_step=1
database_host=''
database_provider='postgres'
run_from_step=''
run_in_strict_mode_without_pipefall() {
set -T # inherit DEBUG and RETURN trap for functions
set -C # prevent file overwrite by > &> <>
set -E # inherit -e
set -e # exit immediately on errors
set -u # exit on not assigned variables
}
load_args() {
while [ $# -gt 0 ]; do
case "$1" in
--from-step=*)
run_from_step="${1#*=}"
;;
*)
log_error "Invalid argument passed to script!"
;;
esac
shift
done
}
move_to_script_directory_if_needed() {
local dirname=$(dirname $0)
if [ "${dirname}" != "." ]; then
cd $dirname
fi
}
precheck() {
# &>/dev/null - suppress stdout and stderr
if ! command ansible --version &>/dev/null; then
log_error "Ansible not found!"
fi
if ! command terraform --version &>/dev/null; then
log_error "Terraform not found!"
fi
if ! command docker --version &>/dev/null; then
log_error "Docker not found!"
fi
if ! command kubectl version &>/dev/null; then
log_error "kubectl not found!"
fi
is_file_available "./database/inventory"
}
extract_database_host_from_inventory() {
log_note "Attempting to extract [${database_provider}] database host from inventory.."
database_host=$(grep -A2 "\[${database_provider}\]" ./database/inventory | grep -o -P '(?<=ansible_host=).*(?=( |$))')
if [ -z "${database_host}" ]; then
log_error "Extraction failed! Make sure that inventory includes [${database_provider}] entry with ansible_host definition below it!"
fi
sleep 0.2
log_note "Host: ${database_host}"
}
log() {
echo -e "$1[$2]: $3${COLOR_OFF}"
}
log_warning() {
log $COLOR_YELLOW "WARNING" "${1}"
}
log_error() {
log $COLOR_RED "ERROR" "${1}"
exit 1
}
log_success() {
log $COLOR_GREEN "SUCCESS" "${1}"
}
log_note() {
log $COLOR_CYAN "NOTE" "${1}"
}
execute_step() {
local is_skipped=1
local text="Step ${current_step}: $1"
if [ ! -z "${run_from_step}" ] && [ $current_step -lt $run_from_step ]; then
is_skipped=0
text+=" ${COLOR_YELLOW}(skipped)${COLOR_OFF}"
fi
log_note "${text}"
if [ $is_skipped -eq 1 ]; then
$2
fi
((current_step++))
}
is_file_available() {
if ! test -f "$1"; then
log_error "File ${1} does not exist but is required to run script!"
exit 1
fi
}
is_action_confirmed() {
local text=$1
read -p "$(echo -e $text $COLOR_YELLOW"(y/n)"$COLOR_OFF) " choice
case "$choice" in
y | Y) return 0 ;;
n | N) return 1 ;;
*) is_action_confirmed "${text}" ;;
esac
}
require_confirmation() {
local text=$1
read -p "$(echo -e $text $COLOR_YELLOW"(y/n)"$COLOR_OFF) " choice
case "$choice" in
y) ;;
n) ;;
*) is_action_confirmed "${text}" ;;
esac
if [ "${choice}" != 'y' ]; then
log_note "Exiting..."
exit 0
fi
}
step_connection_test() {
local result=$(bash -c "ansible "${database_provider}" -m ping")
if grep -qi "unreachable" <<<"$result"; then
log_error "Ping of [${database_provider}] includes unreachable host(s)! Check if specified host(s) have openssh-server installed and include public key of id_cass in ~/.ssh/authorized_keys file"
fi
log_success "Connection OK"
}
step_database_setup() {
local result=$(ansible-playbook setup.yaml --extra-vars "role=${database_provider}" -K 2>&1 | tee /dev/tty)
if grep -qE "(unreachable|failed)=[1-9]+[0-9]{0,}" <<<"$result"; then
log_error "Running playbook failed!"
fi
}
step_init_envs() {
local example_env_filename='.env.example'
local env_filename='.env'
cp ${example_env_filename} ${env_filename}
cp ./api/${example_env_filename} ./api/${env_filename}
cp ./client/${example_env_filename} ./client/${env_filename}
}
step_adjust_database_connection_url() {
sed -i "s/REPLACE_WITH_VALID_HOST/${database_host}/g" ./api/.env
}
step_build_images() {
local result=$(docker compose build 2>&1 | tee /dev/tty)
if grep -iq "error" <<<"$result"; then
log_error "Failed to build Docker images!"
fi
}
step_apply_infrastructure() {
local result=$(terraform apply -auto-approve 2>&1 | tee /dev/tty)
if ! grep -iq "Apply complete!" <<<"$result"; then
log_error "Failed to apply infrastructure!"
fi
}
step_migrate_database() {
local pod_name=$(kubectl get pods -o=name | grep -o "api-.*" 2>&1)
if [ -z $pod_name ]; then
log_error "Failed to get api pod name!"
fi
local result=$(kubectl exec -it ${pod_name} -- bash -c "npm run db:migrate:dev" 2>&1 | tee /dev/tty)
if grep -iq "error" <<<"$result"; then
log_error "Failed to perform database migration!"
fi
}
# ---------------------------------------------------------------------------------------
run_in_strict_mode_without_pipefall
move_to_script_directory_if_needed
precheck
load_args "$@"
if [ -z $run_from_step ]; then
log_note "(ノ◕ヮ◕)ノ*:・゚✧"
log_note "This script performs quick start of 'simple-stack-playground' infrastructure in K8S cluster using Ansible and Terraform on preconfigured data. It does not have rollback feature. In case of any error, try to re-run from particular step."
log_warning "Script does not have rollback feature. In case of any error, try to re-run from particular step, e.g.
./quick-start.sh --from-step=4
"
require_confirmation "Do you want to run it?"
fi
# @TMP hardcoded because currently no other provider is possible to use for data center
extract_database_host_from_inventory "postgres"
require_confirmation "Is host correct?"
cd ./database
execute_step "Testing connection.." "step_connection_test"
execute_step "Preparing ${database_provider}.." "step_database_setup"
cd ..
execute_step "Creating env files.." "step_init_envs"
execute_step "Adjusting DATABASE_URL/SHADOW_DATABASE_URL.." "step_adjust_database_connection_url"
execute_step "Building Docker images.." "step_build_images"
cd ./infrastructure-as-code
execute_step "Applying infrastructure.." "step_apply_infrastructure"
execute_step "Migrating database.." "step_migrate_database"
log_success "Script completed. Access app at http://localhost:31000 or http://127.0.0.1:31000"
log_note "To destroy infrastructure, use 'terraform destroy'"