-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.sh
executable file
·334 lines (285 loc) · 8.41 KB
/
setup.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
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
#!/usr/bin/env bash
set -euo pipefail
# Load common functions
base_dir="$(cd "$(dirname "$0")" && pwd)"
source "$base_dir"/scripts/common.sh
env_file="$base_dir"/.env
DOCKER_VERSION=""
DOCKER_COMPOSER=""
DOCKER_COMPOSE_VERSION=""
check_system_requirements() {
local distro
distro="$(get_distribution)"
if [[ "$distro" != "ubuntu" && "$distro" != "debian" ]]; then
ERROR "Unsupported Linux distribution: ${distro}, only support Ubuntu."
exit 1
fi
}
network_connectivity_check() {
INFO "Checking network connectivity..."
INFO "Checking ping..."
if ping -c 1 8.8.8.8 >/dev/null 2>&1; then
echo 'PONG!'
else
echo 'Ping failed.'
ERROR "Please check your network settings."
exit 1
fi
INFO "Checking DNS..."
if ping -c 1 -W 1 google.com &>/dev/null; then
echo 'DNS resolved successfully.'
else
echo 'DNS resolve failed.'
ERROR "Please check your DNS settings."
exit 1
fi
INFO "Network connectivity check passed!"
}
get_docker_version() {
if command_exists docker; then
DOCKER_VERSION="$(docker version --format '{{.Server.Version}}')"
else
return 0
fi
if docker compose version &>/dev/null; then
# Docker Compose version v2.19.1
DOCKER_COMPOSER="docker compose"
DOCKER_COMPOSE_VERSION="$(docker compose version | grep -oP '(?<=version )[^,]+')"
elif docker-compose --version &>/dev/null; then
# docker-compose version X.Y.Z, build <identifier>
DOCKER_COMPOSER="docker-compose"
DOCKER_COMPOSE_VERSION="$(docker-compose --version | grep -oP '(?<=version )[^,]+')"
fi
# If DOCKER_COMPOSER is empty, failed the script
if [ -z "$DOCKER_COMPOSER" ]; then
ERROR "CAN NOT detect docker compose command, please check your docker installation."
exit 1
fi
print_docker_version
}
print_docker_version() {
INFO "============= ${YELLOW}DOCKER INFO${NOFORMAT} ============="
INFO "Docker version: ${GREEN}${DOCKER_VERSION}${NOFORMAT}"
INFO "Docker compose version: ${GREEN}${DOCKER_COMPOSE_VERSION}${NOFORMAT}"
INFO "Docker compose command: ${GREEN}${DOCKER_COMPOSER}${NOFORMAT}"
INFO "======================================="
}
check_docker_exist_or_install() {
if ! command_exists docker; then
WARN "Docker is not installed, auto install docker..."
if sudo_timeout_check; then
WARN "Please enter your sudo password to grant docker installation permission!"
sudo -v
fi
"$base_dir"/scripts/install-docker.sh
fi
}
load_env() {
if [ ! -f "$env_file" ]; then
cp "$base_dir"/.env.example "$env_file"
fi
set -a
# shellcheck source=/dev/null
source "$env_file"
set +a
}
check_environs() {
local need_reload=false
port_validator() {
local port="$1"
if [[ "$port" =~ ^[0-9]+$ ]]; then
if [[ "$port" -lt 1 || "$port" -gt 65535 ]]; then
ERROR "Port number must be between 1 and 65535."
return 1
fi
else
ERROR "Port number must be a number."
return 1
fi
}
password_validator() {
local password="$1"
if [ ${#password} -lt 8 ]; then
ERROR "Password must be at least 8 characters."
return 1
fi
}
string_validator() {
local string="$1"
if [ ${#string} -lt 2 ]; then
ERROR "String must be at least 2 characters."
return 1
fi
}
ask_port() {
local port_name="$1"
local default_port="$2"
local port
while true; do
read -rp "Please enter ${port_name} port [${default_port}]: " port
if [ -z "$port" ]; then
port="$default_port"
fi
if port_validator "$port"; then
break
fi
done
echo "$port"
}
ask_password() {
local password_name="$1"
local default_password
default_password="$(generate_random_string 20)"
local password=""
while true; do
# read -rp "Please enter ${password_name} password (default: random string): " password
if [ -z "$password" ]; then
password="$default_password"
fi
if password_validator "$password"; then
break
fi
done
echo "$password"
}
ask_string() {
local string_name="$1"
local default_string="$2"
local string
while true; do
read -rp "Please enter ${string_name} [${default_string}]: " string
if [ -z "$string" ]; then
string="$default_string"
fi
if string_validator "$string"; then
break
fi
done
echo "$string"
}
if [ -z "${SPORT_DB_PORT:-}" ]; then
SPORT_DB_PORT="$(ask_port "database" "5432")"
write_back_data "SPORT_DB_PORT" "$SPORT_DB_PORT"
need_reload=true
else
if ! port_validator "$SPORT_DB_PORT"; then
SPORT_DB_PORT="$(ask_port "database" "5432")"
write_back_data "SPORT_DB_PORT" "$SPORT_DB_PORT"
need_reload=true
fi
fi
if [ -z "${POSTGRES_PASSWORD:-}" ]; then
POSTGRES_PASSWORD="$(ask_password "database")"
write_back_data "POSTGRES_PASSWORD" "$POSTGRES_PASSWORD" true
need_reload=true
else
if ! password_validator "$POSTGRES_PASSWORD"; then
POSTGRES_PASSWORD="$(ask_password "database")"
write_back_data "POSTGRES_PASSWORD" "$POSTGRES_PASSWORD" true
need_reload=true
fi
fi
# Mongo
if [ -z "${MONGO_PORT:-}" ]; then
MONGO_PORT="$(ask_port "mongo" "27017")"
write_back_data "MONGO_PORT" "$MONGO_PORT"
need_reload=true
else
if ! port_validator "$MONGO_PORT"; then
MONGO_PORT="$(ask_port "mongo" "27017")"
write_back_data "MONGO_PORT" "$MONGO_PORT"
need_reload=true
fi
fi
if [ -z "${MONGO_ROOT:-}" ]; then
MONGO_ROOT="$(ask_string "mongo root" "mongouser")"
write_back_data "MONGO_ROOT" "$MONGO_ROOT"
need_reload=true
else
if ! string_validator "$MONGO_ROOT"; then
MONGO_ROOT="$(ask_string "mongo root" "mongouser")"
write_back_data "MONGO_ROOT" "$MONGO_ROOT"
need_reload=true
fi
fi
if [ -z "${MONGO_PASSWORD:-}" ]; then
MONGO_PASSWORD="$(ask_password "mongo")"
write_back_data "MONGO_PASSWORD" "$MONGO_PASSWORD" true
need_reload=true
else
if ! password_validator "$MONGO_PASSWORD"; then
MONGO_PASSWORD="$(ask_password "mongo")"
write_back_data "MONGO_PASSWORD" "$MONGO_PASSWORD" true
need_reload=true
fi
fi
# Login service
if [ -z "${SPORT_PORT:-}" ]; then
SPORT_PORT="$(ask_port "login" "10009")"
write_back_data "SPORT_PORT" "$SPORT_PORT"
need_reload=true
else
if ! port_validator "$SPORT_PORT"; then
SPORT_PORT="$(ask_port "login" "10009")"
write_back_data "SPORT_PORT" "$SPORT_PORT"
need_reload=true
fi
fi
if $need_reload; then
INFO "Reloading environs..."
load_env
fi
}
print_environs() {
INFO "============== ${YELLOW}ENV INFO${NOFORMAT} ==============="
INFO "Database port: ${GREEN}${SPORT_DB_PORT}${NOFORMAT}"
INFO "Database password: ${GREEN}${POSTGRES_PASSWORD}${NOFORMAT}"
INFO "Mongo DB port: ${GREEN}${MONGO_PORT}${NOFORMAT}"
INFO "Mongo DB user: ${GREEN}${MONGO_ROOT}${NOFORMAT}"
INFO "Mongo DB password: ${GREEN}${MONGO_PASSWORD}${NOFORMAT}"
INFO "Login port: ${GREEN}${SPORT_PORT}${NOFORMAT}"
INFO "Debug use url: ${GREEN}${MASTER_URL}${NOFORMAT}"
INFO "======================================="
}
redis_check() {
INFO "Redis requirements check..."
# Check vm.overcommit_memory is enabled
if [ "$(sudo sysctl -n vm.overcommit_memory)" -ne 1 ]; then
INFO "vm.overcommit_memory is enabled"
INFO "Executing command to set vm.overcommit_memory to 1..."
sudo sysctl -w vm.overcommit_memory=1
INFO "Persisting vm.overcommit_memory to ${PURPLE}/etc/sysctl.d/99-redis.conf${NOFORMAT}"
echo "vm.overcommit_memory=1" | sudo tee -a /etc/sysctl.d/99-redis.conf
fi
INFO "Redis requirements check finished!"
}
start_service() {
local IP_ADDRESS
if sudo_timeout_check; then
INFO "We need sudo permission to check requirements, please enter your sudo password!"
sudo -v
fi
redis_check
INFO "Starting services..."
IP_ADDRESS="$(ip -o route get to 8.8.8.8 | sed -n 's/.*src \([0-9.]\+\).*/\1/p')"
write_back_data "IP_ADDRESS" $IP_ADDRESS
$DOCKER_COMPOSER up -d
}
post_message() {
local IP_ADDRESS
IP_ADDRESS="$(ip -o route get to 8.8.8.8 | sed -n 's/.*src \([0-9.]\+\).*/\1/p')"
INFO "Service started!"
INFO "You can visit ${GREEN}http://${IP_ADDRESS}${NOFORMAT} to login"
}
main() {
check_runas_root
check_system_requirements
network_connectivity_check
check_docker_exist_or_install
get_docker_version
load_env
check_environs
start_service
post_message
}
main "$@"