-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdruml-inc-utils.sh
executable file
·552 lines (485 loc) · 10.5 KB
/
druml-inc-utils.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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#!/bin/bash
# Run script.
run_script(){
# Get script name.
_SCRIPT=${1}
shift
# Get script dir
CONFIG_DIR=$(get_config_dir)
# Check for custom command.
if [ -f "$CONFIG_DIR/druml-${_SCRIPT}.sh" ];
then
$CONFIG_DIR/druml-${_SCRIPT}.sh $SCRIPT_DIR --config=$(get_config_path) "${@}"
RESULT="$?"
# Check for default command.
elif [ -f "$SCRIPT_DIR/druml-${_SCRIPT}.sh" ];
then
$SCRIPT_DIR/druml-${_SCRIPT}.sh $SCRIPT_DIR --config=$(get_config_path) "${@}"
RESULT="$?"
fi
# Eixt upon an error.
if [[ $RESULT > 0 ]]; then
return 1
fi
}
# Run script and do not output immidiately.
run_script_stashed(){
# Get script name.
_SCRIPT=${1}
shift
# Get script dir
CONFIG_DIR=$(get_config_dir)
# Check for custom command.
if [ -f "$CONFIG_DIR/druml-${_SCRIPT}.sh" ];
then
OUTPUT=$($CONFIG_DIR/druml-${_SCRIPT}.sh $SCRIPT_DIR --config=$(get_config_path) "${@}")
RESULT="$?"
# Check for default command.
elif [ -f "$SCRIPT_DIR/druml-${_SCRIPT}.sh" ];
then
OUTPUT=$($SCRIPT_DIR/druml-${_SCRIPT}.sh $SCRIPT_DIR --config=$(get_config_path) "${@}")
RESULT="$?"
fi
# Echo script output.
echo "$OUTPUT"
# Eixt upon an error.
if [[ $RESULT > 0 ]]; then
return 1
fi
}
# Iterate script for multiple sites.
iterate_script() {
# Get parameters.
_LIST=${1}
shift
_JOBS=${1}
shift
_DELAY=${1}
shift
_COMMAND=${1}
shift
_LISTFILE=$(get_list_file $_LIST)
if [[ -f $_LISTFILE ]]
then
_FAIL_FILE="$CONF_MISC_TEMPORARY/druml-list-failed-$TASK_ID"
if [[ -f $_FAIL_FILE ]]
then
rm $_FAIL_FILE
fi
_I=0
_COUNT=$(cat $_LISTFILE | grep . | wc -l | xargs)
for _SUBSITE in `cat $_LISTFILE`
do
_PROXY_PARAM_SERVER="--server=$_I" # in get_ssh_args and other functions we will get division reminder by the server count
# Generate rnadom number from 0 to 0.5 and sleep that amount of time.
_MULTIPLIER=0.5; _RND=$RANDOM*$_MULTIPLIER/32767; _RND=$(echo "scale=5; $_RND" | bc);
sleep $_RND && {
_OUTPUT="$(run_script_stashed $_COMMAND --site=$_SUBSITE "${@}" $_PROXY_PARAM_SERVER)"
_RESULT="$?"
echo "$_OUTPUT"
echo ""
if [[ $_RESULT > 0 ]]
then
echo $_SUBSITE >> $_FAIL_FILE
fi
} &
let _I+=1
if ! (($_I % $_JOBS))
then
wait;
if [ $_I -ne $_COUNT ]
then
echo "=== $_I / $_COUNT sites are done, iteration ended at $(date)"
fi
if [[ -f $_FAIL_FILE ]]
then
echo "Failed sites: $(cat $_FAIL_FILE | xargs | sed -e 's/ /, /g')."
echo ""
fi
# Delay.
if [[ $_DELAY > 0 ]]
then
echo "Wait $_DELAY seconds"
sleep $_DELAY
fi
echo ""
fi;
done < $_LISTFILE
wait;
echo "=== $_I / $_COUNT sites are done, iteration ended at $(date)"
# Return error if any of the sites failed
if [[ -f $_FAIL_FILE ]]
then
echo "Failed sites: $(cat $_FAIL_FILE | xargs | sed -e 's/ /, /g')."
echo ""
return 1
fi
else
echo "$_LISTFILE file not found";
echo ""
return 1
fi
echo ""
}
# Check if script exists.
script_exists() {
_SCRIPT=${1}
CONFIG_DIR=$(get_config_dir)
# Check for custom command.
if [ -f "$CONFIG_DIR/druml-${_SCRIPT}.sh" ];
then
echo 1
# Check for default command.
elif [ -f "$SCRIPT_DIR/druml-${_SCRIPT}.sh" ];
then
echo 1
else
echo ""
fi
}
# Get a parameter name.
# For string "--parameter=value" returns "parameter".
get_parameter_name() {
if [[ $1 == --* ]]
then
_STR=${1:2}
OIFS="$IFS"
IFS='='
read -a _RET <<< "$_STR"
IFS="$OIFS"
echo ${_RET[0]}
else
echo ""
fi
}
# Get a parameter value.
# For string "--parameter=value" returns "value".
get_parameter_value() {
if [[ $1 == --* ]]
then
OIFS="$IFS"
IFS='='
read -a _RET <<< "$1"
IFS="$OIFS"
echo ${_RET[1]}
else
echo ""
fi
}
# Get config path.
get_config_path() {
echo $CONFIG_PATH
}
# Set config dir.
set_config_path() {
CONFIG_PATH=$1
}
# Get config dir.
get_config_dir() {
dirname $CONFIG_PATH
}
# Get docroot.
get_docroot() {
if [[ -z $PARAM_DOCROOT ]]
then
echo $(get_config_dir)/$CONF_LOCAL_DOCROOT
else
echo $PARAM_DOCROOT
fi
}
# Get real path of the dir relative to docroot
get_real_path_from_docroot_relative_path() {
if [[ $1 == \/* ]] || [[ $1 == \~* ]];
then
echo $1
else
echo $(get_docroot)/$1
fi
}
# Get and process ENV variable.
get_environment() {
_ENV=$(echo $1 | tr '[:lower:]' '[:upper:]')
_V_ALIAS="CONF_ENVIRONMENT_ALIAS_${_ENV}"
if [ -n "${!_V_ALIAS}" ]
then
_ENV=$(echo ${!_V_ALIAS} | tr '[:lower:]' '[:upper:]')
fi
echo $_ENV
}
# Get servers count.
get_server_count() {
_COUNT="CONF_SERVER_COUNT_${1}"
echo "${!_COUNT}"
}
# Get SSH arguments.
get_ssh_args() {
if [ -z $2 ]
then
_I=0
else
_SERVER_COUNT=$(get_server_count $1)
_I=$(($2 % $_SERVER_COUNT))
fi
_V_HOST="CONF_SERVER_DATA_${1}_${_I}_HOST"
_V_USER="CONF_SERVER_DATA_${1}_${_I}_USER"
echo "-o ConnectTimeout=10 ${!_V_USER}@${!_V_HOST}"
}
# Get Rsync arguments.
get_rsync_args() {
if [ -z $2 ]
then
_I=0
else
_SERVER_COUNT=$(get_server_count $1)
_I=$(($2 % $_SERVER_COUNT))
fi
_V_HOST="CONF_SERVER_DATA_${1}_${_I}_HOST"
_V_USER="CONF_SERVER_DATA_${1}_${_I}_USER"
echo "${!_V_USER}@${!_V_HOST}"
}
# Get remote host.
get_remote_host() {
if [ -z $2 ]
then
_I=0
else
_SERVER_COUNT=$(get_server_count $1)
_I=$(($2 % $_SERVER_COUNT))
fi
_V_HOST="CONF_SERVER_DATA_${1}_${_I}_HOST"
echo "${!_V_HOST}"
}
# Get remote docroot.
get_remote_docroot() {
if [ -z $2 ]
then
_I=0
else
_SERVER_COUNT=$(get_server_count $1)
_I=$(($2 % $_SERVER_COUNT))
fi
_V_ROOT="CONF_SERVER_DATA_${1}_${_I}_DOCROOT"
echo "${!_V_ROOT}"
}
# Get remote log.
get_remote_log() {
if [ -z $2 ]
then
_I=0
else
_SERVER_COUNT=$(get_server_count $1)
_I=$(($2 % $_SERVER_COUNT))
fi
_V_LOG="CONF_SERVER_DATA_${1}_${_I}_LOG"
echo "${!_V_LOG}"
}
# Get list file.
get_list_file() {
_LIST=$(echo $1 | tr '[:lower:]' '[:upper:]')
_V_LIST_FILE="CONF_LIST_${_LIST}"
if [[ -n ${!_V_LIST_FILE} ]]
then
echo "$(get_config_dir)/${!_V_LIST_FILE}"
else
echo ""
fi
}
# Get mysql arguments.
get_db_args() {
if [[ -n ${CONF_LOCAL_DB_USER} ]]
then
ARGS="-u${CONF_LOCAL_DB_USER}"
fi
if [[ -n ${CONF_LOCAL_DB_PASS} ]]
then
ARGS="$ARGS -p${CONF_LOCAL_DB_PASS}"
fi
echo $ARGS
}
# Get drush alias.
get_drush_alias() {
_V_DRUSH_ALIAS="CONF_DRUSH_ALIAS_${1}"
echo "${!_V_DRUSH_ALIAS}"
}
# Get drush subsite.
get_drush_subsite_args() {
if [ "$1" == "default" ]
then
echo ""
else
echo "-l $1"
fi
}
# Get drush command.
get_drush_command() {
if [[ -n ${CONF_DRUSH_COMMAND} ]]
then
echo ${CONF_DRUSH_COMMAND}
else
echo "drush"
fi
}
# Get drush subsite.
get_local_db_name() {
if [ "$1" == "default" ]
then
echo "${CONF_LOCAL_DB_PREFIX}${CONF_LOCAL_DB_DEFAULT}"
else
echo "${CONF_LOCAL_DB_PREFIX}$1"
fi
}
# Strip first line in the file if it starts with 'tput'.
fix_file_tput() {
FIRSTLINE=$(head -c 4 $1)
if [ "$FIRSTLINE" == "tput" ]
then
tail -n +2 $1 > $1.tail
mv -f $1.tail $1
fi
}
# Get param that could be proxied to another script.
get_param_proxy() {
PARAM_VAR="PARAM_$(echo $1 | tr '[:lower:]' '[:upper:]')"
if [[ -n ${!PARAM_VAR} ]]
then
echo "--$1=${!PARAM_VAR}"
else
echo ""
fi
}
# Get site alias.
get_site_alias() {
CONF_VAR="CONF_ALIAS_$(echo $1 | tr '[:lower:]' '[:upper:]')"
if [[ -n ${!CONF_VAR} ]]
then
echo "${!CONF_VAR}"
else
echo "$1"
fi
}
# Get default site.
get_default_site() {
CONF_VAR="CONF_DRUSH_DEFAULT"
if [[ -n ${!CONF_VAR} ]]
then
echo "${!CONF_VAR}"
else
echo "default"
fi
}
# Get log file.
log_get_file() {
LOG_FILE=""
if [ ${CONF_MISC_LOG_DIR} ] && [ ${CONF_MISC_LOG_FILE} ]
then
if [ ! -d "${CONF_MISC_LOG_DIR}" ]
then
mkdir ${CONF_MISC_LOG_DIR}
fi
LOG_FILE="${CONF_MISC_LOG_DIR}/${CONF_MISC_LOG_FILE}"
fi
echo $LOG_FILE
}
# Get individual log file for a task.
log_get_task_file() {
LOG_FILE=""
if [ ${CONF_MISC_LOG_DIR} ]
then
if [ ! -d "${CONF_MISC_LOG_DIR}" ]
then
mkdir ${CONF_MISC_LOG_DIR}
fi
LOG_FILE="${CONF_MISC_LOG_DIR}/${TASK_ID}.log"
fi
echo $LOG_FILE
}
# Log Druml command.
log_command() {
_CONFIG_DIR=$(get_config_dir)
OUTPUT=$(echo $(hostname) $USER [${TASK_ID}] \"$_CONFIG_DIR\" \""${@}"\" started)
LOG_FILE=$(log_get_file)
if [[ ${LOG_FILE} ]]
then
echo $OUTPUT >> ${LOG_FILE}
fi
if [[ ${CONF_MISC_LOG_EMAIL} ]]
then
mail -s "Druml script execution" $CONF_MISC_LOG_EMAIL <<< $OUTPUT
fi
}
# Log Druml command.
log_command_succeed() {
_CONFIG_DIR=$(get_config_dir)
OUTPUT=$(echo $(hostname) $USER [${TASK_ID}] \"$_CONFIG_DIR\" \""${@}"\" succeed)
LOG_FILE=$(log_get_file)
if [[ ${LOG_FILE} ]]
then
echo $OUTPUT >> ${LOG_FILE}
fi
if [[ ${CONF_MISC_LOG_EMAIL} ]]
then
# Include Druml log output.
LOG_TASK_FILE=$(log_get_task_file)
OUTPUT="$OUTPUT\n"
while read -r LINE
do
OUTPUT="$OUTPUT\n$LINE"
done < "$LOG_TASK_FILE"
OUTPUT=$(echo -e "$OUTPUT")
mail -s "Druml script execution" $CONF_MISC_LOG_EMAIL <<< "$OUTPUT"
fi
}
# Log Druml command.
log_command_failed() {
_CONFIG_DIR=$(get_config_dir)
OUTPUT=$(echo $(hostname) $USER [${TASK_ID}] \"$_CONFIG_DIR\" \""${@}"\" failed)
LOG_FILE=$(log_get_file)
if [[ ${LOG_FILE} ]]
then
echo $OUTPUT >> ${LOG_FILE}
fi
if [[ ${CONF_MISC_LOG_EMAIL} ]]
then
# Include Druml log output.
LOG_TASK_FILE=$(log_get_task_file)
OUTPUT="$OUTPUT\n"
while read -r LINE
do
OUTPUT="$OUTPUT\n$LINE"
done < "$LOG_TASK_FILE"
OUTPUT=$(echo -e "$OUTPUT")
mail -s "Druml script execution" $CONF_MISC_LOG_EMAIL <<< "$OUTPUT"
fi
}
# Log Druml output.
log_output() {
LOG_TASK_FILE=$(log_get_task_file)
if [[ ${LOG_FILE} ]]
then
exec > >(tee -i $LOG_TASK_FILE)
exec 2>&1
fi
}
# Get log file.
log_get_file_debug() {
LOG_FILE=""
if [ ${CONF_MISC_LOG_DIR} ] && [ ${CONF_MISC_LOG_FILE} ]
then
if [ ! -d "${CONF_MISC_LOG_DIR}" ]
then
mkdir ${CONF_MISC_LOG_DIR}
fi
LOG_FILE="${CONF_MISC_LOG_DIR}/${CONF_MISC_LOG_FILE_DEBUG}"
fi
echo $LOG_FILE
}
# Log Druml command.
log_debug() {
_CONFIG_DIR=$(get_config_dir)
OP=$(echo $(hostname) $USER [${TASK_ID}] \"$_CONFIG_DIR\" "${@}")
LOG_FILE=$(log_get_file_debug)
if [[ ${LOG_FILE} ]]
then
echo $OP >> ${LOG_FILE}
fi
}