forked from Vonng/pigsty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure
executable file
·434 lines (392 loc) · 14.7 KB
/
configure
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
#!/bin/bash
#==============================================================#
# File : configure
# Desc : generate pigsty.yml config according to env
# Ctime : 2021-05-17
# Mtime : 2023-02-26
# Path : configure
# Author : Ruohang Feng ([email protected])
# License : AGPLv3
#==============================================================#
VERSION=v2.0.1
#--------------------------------------------------------------#
# Usage
#--------------------------------------------------------------#
# ./configure
# [-i|--ip <ip>]
# [-r|--region <region] [default,china,europe]
# [-m|--mode <mode] [el7,el8,el9,sec,citus,redis...]
# [-n|--non-interactive]
#==============================================================#
# args
# -i --ip <ip addr> : set local primary ip address
# -m --mode : mode, (el7|el8|el9|demo|sec|citus|redis)
# -n --non-interactive : flag, run as non-interactive mode
#==============================================================#
INTERACTIVE=true # run configure with interactive mode
PRIMARY_IP="" # primary IP address (intranet, do not use public IP)
MODE="" # default install mode (el7|el8|el9|demo|sec|citus|redis)
SPEC=tiny # default postgres spec (tiny|mini|small|medium|large|huge)
#--------------------------------------------------------------#
# Param
#--------------------------------------------------------------#
PROG_NAME="$(basename $0)"
PROG_DIR="$(cd $(dirname $0) && pwd)"
PIGSTY_HOME="${PROG_DIR}"
REPO_NAME=pigsty
NGINX_HOME=/www
REPO_DIR=${NGINX_HOME}/${REPO_NAME}
BIN_DIR=${PIGSTY_HOME}/files/bin
# os & region
REGION=""
#--------------------------------------------------------------#
# Utils
#--------------------------------------------------------------#
__CN='\033[0m';__CB='\033[0;30m';__CR='\033[0;31m';__CG='\033[0;32m';
__CY='\033[0;33m';__CB='\033[0;34m';__CM='\033[0;35m';__CC='\033[0;36m';__CW='\033[0;37m';
function log_info() { printf "[${__CG} OK ${__CN}] ${__CG}$*${__CN}\n"; }
function log_warn() { printf "[${__CY}WARN${__CN}] ${__CY}$*${__CN}\n"; }
function log_error() { printf "[${__CR}FAIL${__CN}] ${__CR}$*${__CN}\n"; }
function log_debug() { printf "[${__CB}HINT${__CN}] ${__CB}$*${__CN}\n"; }
function log_input() { printf "[${__CM} IN ${__CN}] ${__CM}$*\n=> ${__CN}"; }
function log_hint() { printf "${__CB}$*${__CN}"; }
ipv4_regexp='(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])'
#----------------------------------------------#
# region
#----------------------------------------------#
function check_region(){
if [ "${REGION}" == "" ]; then
if behind_gfw; then
REGION=china # mainland china is behind GFW
else
REGION=default # otherwise use default mirror
fi
log_info "region = ${REGION}"
fi
}
#----------------------------------------------#
# kernel
#----------------------------------------------#
function check_kernel(){
local kernel_name=$(uname -s)
if [[ "${kernel_name}" == "Linux" ]]; then
log_info "kernel = ${kernel_name}"
return 0
else
log_error "kernel = ${kernel_name}, not supported, Linux only"
exit 1
fi
}
#----------------------------------------------#
# machine
#----------------------------------------------#
function check_machine(){
local machine_name=$(uname -m)
if [[ "${machine_name}" == "x86_64" ]]; then
log_info "machine = ${machine_name}"
return 0
else
log_error "machine = ${machine_name}, not supported, x86_64 only"
exit 2
fi
}
#----------------------------------------------#
# os release (Linux|Darwin etc..)
#----------------------------------------------#
function check_release(){
if [[ ! -f /etc/redhat-release ]]; then
log_error "release = unknown, /etc/redhat-release not exists"
log_error "pigsty works on RHEL and compatible distributions, use with caution!"
# exit 3
fi
local full=`cat /etc/redhat-release | tr -dc '0-9.'`
local major=$(cat /etc/redhat-release | tr -dc '0-9.'|cut -d \. -f1)
local minor=$(cat /etc/redhat-release | tr -dc '0-9.'|cut -d \. -f2)
local asynchronous=$(cat /etc/redhat-release | tr -dc '0-9.'|cut -d \. -f3)
if [[ ${major} != "7" && ${major} != "8" && ${major} != "9" ]]; then
log_error "release = ${full} , only EL 7,8,9 is officially supported, use with caution"
log_error "pigsty works on el 7,8,9, please use with caution!"
# exit 4
fi
return 0
}
#----------------------------------------------#
# sudo
#----------------------------------------------#
function can_nopass_sudo(){
local current_user=$(whoami)
if [[ "${current_user}" == "root" ]]; then
return 0
fi
if sudo -n ls >/dev/null 2>/dev/null; then
return 0
fi
return 1
}
function check_sudo(){
local current_user=$(whoami)
if can_nopass_sudo; then
log_info "sudo = ${current_user} ok"
else
log_error "sudo = ${current_user} missing nopasswd"
log_warn "fix nopass sudo for '${current_user}' with sudo:"
log_hint "echo '%%${current_user} ALL=(ALL) NOPASSWD: ALL' > /etc/sudoers.d/${current_user}"
exit 5
fi
}
#----------------------------------------------#
# ssh
#----------------------------------------------#
# One MUST have nopass to localhost with same user
# configure can fix that for you (via ssh-keygen)
function can_nopass_ssh(){
local current_user=$(whoami)
local user=${1-${current_user}}
local ipaddr=${2-'127.0.0.1'}
if ssh -oBatchMode=yes -o "StrictHostKeyChecking no" ${user}@${ipaddr} 'ls' 1>/dev/null 2>/dev/null; then
return 0
else
return 1
fi
}
function fix_nopass_ssh(){
[[ ! -d ~/.ssh ]] && mkdir -p ~/.ssh && chmod 0700 ~/.ssh; # make sure ssh dir exists
[[ ! -f ~/.ssh/id_rsa ]] && ssh-keygen -b 2048 -t rsa -f ~/.ssh/id_rsa -q -N '' # gen ssh key if not exists
touch ~/.ssh/authorized_keys && chmod 0700 ~/.ssh/{authorized_keys,id_rsa}
local publicKey=$(cat ~/.ssh/id_rsa.pub 2>/dev/null)
if ! grep -q "${publicKey}" ~/.ssh/authorized_keys; then
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
fi
return $(can_nopass_ssh)
}
function check_ssh(){
if can_nopass_ssh ; then
log_info "ssh = $(whoami)@127.0.0.1 ok"
return 0
fi
if fix_nopass_ssh; then
log_warn "ssh = $(whoami)@127.0.0.1 fixed"
return 0
else
log_error "ssh = $(whoami)@127.0.0.1 failed"
exit 6
fi
}
#----------------------------------------------#
# primary ip
#----------------------------------------------#
# One MUST configure a local primary IP address
# local primary ip are fetched in following order
# 1. if ip is given via -i|--ip , just use it
# 2. if only one ip is detected, just use it
# 3. if multiple ip detected, ask user for it (interactive mode)
# 4. if -n|non-interactive is set, abort on error
#----------------------------------------------#
function is_valid_ip(){
if [[ "$1" =~ (([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5]) ]]; then
return 0
else
return 1
fi
}
function get_ip_count(){
echo $(hostname --all-ip-addresses 2>/dev/null | wc -w)
}
function list_ipaddr(){
local ipList=$(hostname --all-ip-addresses)
local i=0
for ip in $ipList
do
i=$((i+1))
local ipDetail=$(ip addr 2>/dev/null | grep "inet ${ip}")
printf " (${__CC}${i}${__CN}) ${__CR}${ip}${__CN}\t${__CY}${ipDetail}${__CN}\n"
done
}
function check_ipaddr(){
local primary_ip=${1-${PRIMARY_IP}}
local interactive=${2-${INTERACTIVE}}
# if ip is given, check it
if [[ ! -z "${primary_ip}" ]]; then
if is_valid_ip ${primary_ip}; then
log_info "primary_ip = ${primary_ip} (from argument)"
PRIMARY_IP=${primary_ip}
return 0
else
log_error "primary_ip = ${primary_ip} invalid (from argument)"
exit 7
fi
fi
local ipCount=$(get_ip_count)
if ((ipCount<1)); then
log_error "primary_ip = probe failed"
exit 8
fi
if ((ipCount==1)); then
log_info "primary_ip = $(hostname --all-ip-addresses) (from probe)"
PRIMARY_IP=$(hostname --all-ip-addresses | egrep -o '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])')
return 0
fi
# multiple IP detected
log_warn "Multiple IP address candidates found:"
list_ipaddr
# special case: demo fixed ip (10.10.10.10), we will choose it directly without asking!
if [[ $(hostname --all-ip-addresses) == *'10.10.10.10'* ]]; then
log_info "primary_ip = 10.10.10.10 (from demo)"
PRIMARY_IP="10.10.10.10"
return 0
fi
# ask for input if in interactive mode, abort on non-interactive mode
if [[ ${interactive} != "true" ]]; then
log_error "primary_ip = dilemma abort"
log_hint "HINT: specify ip with -i|--ip , or disable non-interactive mode"
exit 9
fi
log_input "INPUT primary_ip address (of current meta node, e.g 10.10.10.10):"
read -r
local primary_ip=${REPLY}
if is_valid_ip ${primary_ip}; then
log_info "primary_ip = ${primary_ip} (from input)"
PRIMARY_IP=${primary_ip}
return 0
else
log_error "primary_ip = ${primary_ip} invalid (from input)"
exit 9
fi
}
#----------------------------------------------#
# check admin
#----------------------------------------------#
# check whether current user have nopass ssh
# access to primary ip with sudo privilege set
function check_admin(){
local primary_ip=${1-${PRIMARY_IP}}
local current_user=$(whoami)
if ssh -t -o "StrictHostKeyChecking no" ${primary_ip} 'sudo -n ls' 1>/dev/null 2>/dev/null; then
log_info "admin = ${current_user}@${primary_ip} ok"
else
log_error "admin = ${current_user}@${primary_ip} failed"
log_hint "check ${current_user} in sudoer, @${primary_ip} is ssh accessible"
exit 10
fi
}
#----------------------------------------------#
# check deployment mode
#----------------------------------------------#
function check_mode(){
local mode=${1-${MODE}}
local primary_ip=${2-${PRIMARY_IP}}
# if mode is explicitly set, just use it
if [[ ! -z "${mode}" ]]; then
log_info "mode = ${mode} (manually set)"
return 0
fi
# if repo_complete file exists, use auto template
if [[ -f "${REPO_DIR}/repo_complete" ]]; then
log_info "mode = auto (use local repo)"
MODE=auto
return 0
fi
# otherwise, use el7, el8, el9 according to os version
local os_release=$(rpm -q --qf "%{VERSION}" $(rpm -q --whatprovides redhat-release) | grep -o '^[^.]\+')
if [[ ${os_release} == "9" ]]; then
MODE=el9
return 0
elif [[ ${os_release} == "8" ]]; then
MODE=el8
return 0
elif [[ ${os_release} == "7" ]]; then
MODE=el7
return 0
fi
MODE=auto
return 0
}
# return 0 if behind gfw (inside mainland china), otherwise 1
function behind_gfw() {
local return_code=$(curl -I -s --connect-timeout 1 www.google.com -w %{http_code} | tail -n1)
if [ "${return_code}" = "200" ]; then
return 1
fi
return 0
}
#----------------------------------------------#
# generate config
#----------------------------------------------#
function check_config(){
local primary_ip=${1-${PRIMARY_IP}}
local mode=${2-${MODE}} # use files/pigsty/${mode}.yml as template
local pigsty_home=${3-${PIGSTY_HOME}}
local sed_cmd1="s/10.10.10.10/${primary_ip}/g" # replace 10.10.10.10 with actual ip address
local config_src=${pigsty_home}/files/pigsty/${mode}.yml
local config_dst=${pigsty_home}/pigsty.yml
if [[ ! -f ${config_src} ]]; then
log_error "config mode = ${mode} not exists"
exit 11
fi
# replace node_tune & pg_conf with tiny or oltp according to cpu count
if (($(getconf _NPROCESSORS_ONLN)>=4)); then
local sed_cmd2="s/pg_conf: tiny.yml/pg_conf: oltp.yml/g" # replace pg_conf to oltp mode
local sed_cmd3="s/node_tune: tiny/node_tune: oltp/g" # replace node_tune to oltp mode
cat ${config_src} | sed -e "${sed_cmd1}" | sed -e "${sed_cmd2}" | sed -e "${sed_cmd3}" > ${config_dst}
log_info "config = ${mode} @ ${primary_ip} [tiny -> oltp]"
else
cat ${config_src} | sed -e "${sed_cmd1}" > ${config_dst}
log_info "config = ${mode} @ ${primary_ip}"
fi
if [[ ${REGION} != "default" ]]; then
local sed_cmd4='s/ region: default/ region: '"${REGION}"' /g'
sed -ie "${sed_cmd4}" ${config_dst}
rm -rf "${config_dst}e" || true
fi
return 0
}
#----------------------------------------------#
# check utils
#----------------------------------------------#
# install ansible sshpass unzip wget yum , etc...
function check_utils(){
# check ansible is installed
if command -v ansible-playbook >/dev/null ; then
log_info "ansible = $(ansible --version | head -n1)"
else
log_error "ansible = not found"
exit 20
fi
}
#--------------------------------------------------------------#
# Main
#--------------------------------------------------------------#
function main(){
# arg parsing
while [ $# -gt 0 ]; do
case $1 in
-h|--help)
echo './configure [-n|--non-interactive] [-d|--download] [-i|--ip <ipaddr>] [-m|--mode <mode>]'
exit 0;;
-i|--ip) PRIMARY_IP="$2" ; shift;;
-r|--region) REGION="$2" ; shift;;
-m|--mode) MODE="$2" ; shift;;
-n|--non-interactive) INTERACTIVE=false ;;
(--) shift; break;;
(-*) echo "$0: error - unrecognized option $1" 1>&2; exit 1;;
(*) break;;
esac
shift
done
log_hint "configure pigsty ${VERSION} begin\n"
# check
check_region # region = default
check_kernel # kernel = Linux
check_machine # machine = x86_64
check_release # release = CentOS 7|8|9
check_sudo # current_user = NOPASSWD sudo
check_ssh # current_user = NOPASSWD ssh
check_ipaddr # primary_ip (arg|probe|input) (INTERACTIVE: ask for ip)
check_admin # check current_user@primary_ip nopass ssh sudo
check_mode # check config template
check_config # generate config according to primary_ip and mode
check_utils # check ansible sshpass and other utils installed
log_info "configure pigsty done"
log_hint "proceed with ./install.yml \n"
}
main $@