-
Notifications
You must be signed in to change notification settings - Fork 0
/
dscmd.sh
executable file
·695 lines (585 loc) · 18.9 KB
/
dscmd.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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
#!/usr/bin/env bash
#-------------------------------------------------------------------------------------
# Build distribution tool for SenchaCMD
# Anton Fisher <[email protected]>
#
# app1 >-- --> ssh node1 (sencha app build) --
# \ / \
# app2 >--+--> dscmd.sh -- --> local build folder
# / \ /
# app3 >-- --> ssh node2 (sencha app build) --
#
#-------------------------------------------------------------------------------------
echo -e "Build distribution tool for SenchaCMD v0.1.3 [beta]";
# --- config ---
CONFIG_FILE=".dscmd-config";
AGENTS_FILE=".dscmd-agents";
# --- global variables ---
START_TIME=$(date +%s);
CMD_PATH="";
CMD_PATH_DAFAULT="~/bin/Sencha/Cmd/sencha";
APPS_PATH="";
APPS_PATH_DAFAULT="pages";
AGENT_FREE="1";
AGENT_BUSY="2";
#AGENT_ERROR="0";
declare -a AGENTS_ARRAY;
declare -a AGENTS_STATUSES_ARRAY;
declare -a AGENTS_PIDS_ARRAY;
declare -a APPLICATIONS_ARRAY;
AGENTS_ARRAY_COUNT=0;
APPLICATIONS_ARRAY_MAX_ITEM_LENGTH=0;
# --- common functions ---
##
# Get full file/directory path
#
# Examples:
# full_path=$(get_full_path '/tmp'); # /tmp
# full_path=$(get_full_path '~/..'); # /home
# full_path=$(get_full_path '../../../'); # /home
#
# $1 - relative path
#
# Returns: absolute path
#
function get_full_path {
local user_home;
local user_home_sed;
local rel_path;
local result;
user_home="${HOME//\//\\\/}";
user_home_sed="s#~#${user_home}#g";
rel_path=$( echo "${1}" | sed "${user_home_sed}" );
result=$( readlink -e "${rel_path}" );
echo "${result}";
}
##
# Check directory
#
# Example:
# check_directory_exits /tmp 1;
#
# $1 - directory path
# $2 - show error
#
# Returns: 0 - exists / 1 - not exists
#
function check_directory_exits {
if [[ -d "${1}" ]]; then
return 0;
else
if [[ "${2}" == 1 ]]; then
echo -e "Directory '${1}' does not exist. Please try again.";
fi;
return 1;
fi;
}
##
# $1 - directory path
#
function ls_directory {
ls_directory_result=$( ls -m "$1" | sed 's#, #,#g' | tr -d '\n' );
return "${?}";
}
##
# Convert seconds to human format
#
# $1 - duration in seconds
#
function seconds_to_duration {
s="${1}";
h=$((s/60/60%24))
m=$((s/60%60))
s=$((s%60))
local result="";
if [[ "${h}" != 0 ]]; then
result="${h} hour(s) ";
fi;
if [[ "${m}" != 0 || "${h}" != 0 ]]; then
result="${result}${m} minute(s) ";
fi;
echo "${result}${s} second(s)";
}
# --- util functions ---
function read_config_file {
touch "${CONFIG_FILE}";
while IFS='' read -r line || [[ -n "${line}" ]]; do
IFS='=' read -r -a line_array <<< "${line}";
eval "${line_array[0]}=\"${line_array[1]}\"";
done < "${CONFIG_FILE}";
}
##
# $1 - new config string
#
function save_config_file {
touch "${CONFIG_FILE}";
echo -e "${1}" > "${CONFIG_FILE}";
}
function read_agents_list {
touch "${AGENTS_FILE}";
AGENTS_ARRAY_COUNT="0";
while IFS='' read -r line || [[ -n "${line}" ]]; do
AGENTS_ARRAY["${AGENTS_ARRAY_COUNT}"]="${line}";
AGENTS_STATUSES_ARRAY["${AGENTS_ARRAY_COUNT}"]="${AGENT_FREE}";
AGENTS_PIDS_ARRAY["${AGENTS_ARRAY_COUNT}"]=0;
AGENTS_ARRAY_COUNT=$((AGENTS_ARRAY_COUNT+1));
done < "${AGENTS_FILE}";
}
function save_agents_list {
local nl;
local result;
> "${AGENTS_FILE}";
unset nl;
unset result;
for agent in "${AGENTS_ARRAY[@]}"; do
if [[ -n "${agent}" ]]; then
if [[ -n "${result}" ]]; then
nl=$'\n';
fi;
result="${result}${nl}${agent}";
fi;
done;
if [[ -n "${result}" ]]; then
echo "${result}" > "${AGENTS_FILE}";
else
> "${AGENTS_FILE}";
fi;
}
##
# $1 - agent config string (10.0.0.11:user ---> parse_agent_result=['user', '10.0.0.11'])
#
function parse_agent {
IFS=':';
read -r -a parse_agent_result <<< "${1}";
}
##
# $1 - agent address ([email protected])
#
function check_ssh_agent {
ssh -o ConnectTimeout=10 "${1}" "exit"; #> /dev/null;
return "${?}";
}
##
# $1 - agent address ([email protected])
#
function rsync_agent {
rsync \
-az \
--partial \
--delete \
--delete-excluded \
--exclude=/.dscmd-* \
--exclude=dscmd.sh \
--timeout=30 \
./ "${1}:~/dscmd";
#-azvP \
return "${?}";
}
##
# $1 - agent address ([email protected])
# $2 - application folder
#
function rsync_local_folder {
rsync \
-az \
--partial \
--delete \
--delete-excluded \
--exclude=/.dscmd-* \
--exclude=dscmd.sh \
--timeout=30 \
"${1}:~/dscmd/${2}/" ./"${2}";
#-azvP \
return "${?}";
}
##
# Returns: free arent index
#
function get_free_agent {
unset get_free_agent_result;
local i=0;
for agent in "${AGENTS_ARRAY[@]}"; do
if [[ "${AGENTS_STATUSES_ARRAY[$i]}" == "${AGENT_FREE}" ]]; then
get_free_agent_result="${i}";
break;
fi;
i=$((i+1));
done;
}
##
# $1 - agent index
#
function set_agent_free {
AGENTS_PIDS_ARRAY["${1}"]=0;
AGENTS_STATUSES_ARRAY["${1}"]="${AGENT_FREE}";
}
##
# $1 - agent index
# $2 - agent process pid
#
function set_agent_busy {
AGENTS_PIDS_ARRAY["${1}"]="${2}";
AGENTS_STATUSES_ARRAY["${1}"]="${AGENT_BUSY}";
}
##
# $1 - agent index
# $2 - application name
# $3 - application index
#
function run_build_on_agent {
local application;
local index;
local agent;
local agent_exit_code;
local subprocess_exit_code;
parse_agent "${AGENTS_ARRAY[$1]}";
application="${2}"
index="${3}";
agent="${parse_agent_result[1]}@${parse_agent_result[0]}";
agent_exit_code=0;
while read -r line; do
subprocess_exit_code="${line//EXIT_CODE:/}";
if [[ "${subprocess_exit_code}" == "${line}" ]]; then
index_f=$(printf "% 3d" "${index}");
postfix=$(printf -v t "%%0%ds" "${APPLICATIONS_ARRAY_MAX_ITEM_LENGTH}" && printf "${t}");
echo -e "[build ${index_f}/${#APPLICATIONS_ARRAY[@]}: ${application}${postfix:${#application}}] ${line}";
elif [[ "${subprocess_exit_code}" != "0" ]]; then
agent_exit_code="${subprocess_exit_code}";
fi;
done < <(
echo -e "run build '${application}' on ${agent}";
subprocess_exit_code=0;
if [[ "${subprocess_exit_code}" == 0 ]]; then
echo -e "Syncronize local directory with agent...";
rsync_agent "${agent}";
subprocess_exit_code="${?}";
if [[ "${subprocess_exit_code}" != 0 ]]; then
echo -e "ERROR: failed rsync '${2}' from local folder to ${agent} (local --X--> agent).";
fi;
fi;
if [[ "${subprocess_exit_code}" == 0 ]]; then
ssh -Cq "${agent}" "cd ~/dscmd/${APPS_PATH}/${2}; ${CMD_PATH} --plain --quiet --time app build;";
subprocess_exit_code="${?}";
if [[ "${subprocess_exit_code}" != 0 ]]; then
echo -e "ERROR: failed build application '${2}' on ${agent}.";
fi;
fi;
if [[ "${subprocess_exit_code}" == 0 ]]; then
rsync_local_folder "${agent}" "build/production/${2^}";
subprocess_exit_code="${?}";
if [[ "${subprocess_exit_code}" != 0 ]]; then
echo -e "ERROR: failed rsync '${2}' from ${agent} to local folder (local <--X-- agent).";
fi;
fi;
echo "EXIT_CODE:${subprocess_exit_code}";
)
exit "${agent_exit_code}";
}
# --- tool usage functions ---
function f_config {
echo -e "Master initialization.\n";
local text;
local valid_directory;
local apps_path_user;
local cmd_path_user;
read_config_file;
unset valid_directory;
while [[ -z "${valid_directory}" ]]; do
text="Enter path to applications folder";
text="$text (default: '${APPS_PATH_DAFAULT}' or previous uses '${APPS_PATH}') [ENTER]: ";
read -r -e -p "${text}" apps_path_user;
full_path=$(get_full_path "${apps_path_user}");
if [[ -z "${apps_path_user}" ]]; then
valid_directory=1;
if [[ -z "${APPS_PATH}" ]]; then
apps_path_user="${APPS_PATH_DAFAULT}";
else
apps_path_user="${APPS_PATH}";
fi;
elif [[ "${apps_path_user}" == .* || "${apps_path_user}" == /* || "${apps_path_user}" == ~* ]]; then
echo -e "ERROR: only local directories allowed (without './')...";
elif check_directory_exits "${full_path}" 1; then
valid_directory=1;
fi;
done;
apps_path_user="${apps_path_user%/}";
ls_directory "${apps_path_user}";
echo -e "Found applications in '${apps_path_user}': ${ls_directory_result}\n";
unset valid_directory;
while [[ -z "${valid_directory}" ]]; do
text="Enter path to SenchaCMD on agents (default: ${CMD_PATH_DAFAULT} or previous uses) [ENTER]: ";
read -r -e -p "${text}" cmd_path_user;
if [[ -z "${cmd_path_user}" ]]; then
valid_directory=1;
if [[ -z "${CMD_PATH}" ]]; then
cmd_path_user="${CMD_PATH_DAFAULT}";
else
cmd_path_user="${CMD_PATH}";
fi;
else
valid_directory=1;
fi;
done;
save_config_file "APPS_PATH=${apps_path_user}\nCMD_PATH=${cmd_path_user}";
echo -e "\nSaved to ${CONFIG_FILE}:";
cat "${CONFIG_FILE}";
}
function f_applications_list {
echo -e "Applicaitons list will be used for build:";
read_config_file;
ls_directory "${APPS_PATH}";
echo -e "${ls_directory_result}\n";
}
function f_add_agent {
echo -e "Add agent wizard.\n";
unset hosts_list;
while [[ -z "${hosts_list}" ]]; do
read -r -p "Enter agent ip or host (use ',' to add few agents with same username) [ENTER]: " hosts_list;
done;
unset username;
read -r -p "Enter agent username (default: root) [ENTER]: " username;
if [[ -z "${username}" ]]; then
username="root";
fi;
read_agents_list;
unset hosts_array;
IFS=',' read -r -a hosts_array <<< "${hosts_list}";
for host in "${hosts_array[@]}"; do
for agent in "${AGENTS_ARRAY[@]}"; do
if [[ "${agent}" = "${host}:${username}" ]]; then
echo -e "\nERROR: Host '${username}@${host}' already registered.\n";
f_agents_list;
exit 1;
fi;
done;
done;
unset install_script_path;
while [[ -z "${install_script_path}" ]]; do
read -r -e -p "Enter path to SenchaCMD installation script [ENTER]: " install_script_path;
done;
local install_script_basename;
local install_script_extension;
local install_script_realpath;
install_script_basename=$(basename "${install_script_path}");
install_script_extension="${install_script_basename##*.}";
install_script_realpath=$(get_full_path "${install_script_path}");
if [[ "${install_script_extension}" != "sh" ]] ; then
echo -e "ERROR: file ${install_script_realpath} is not executable (*.sh).";
exit 1;
fi;
read -r -p "Copy ssh key to agent using ssh-copy-id (Y/n) [ENTER]: " skip_copy_ssh_key;
if [[ "${skip_copy_ssh_key}" =~ ^([yY][eE][sS]|[yY])$ ]]; then
unset skip_copy_ssh_key;
fi;
read -r -p "Apt-get update and upgrade agent (Y/n) [ENTER]: " skip_apt_get_update;
if [[ "${skip_apt_get_update}" =~ ^([yY][eE][sS]|[yY])$ ]]; then
unset skip_apt_get_update;
fi;
read -r -p "Install Java and Ruby (Y/n) [ENTER]: " skip_install_dependencies;
if [[ "${skip_install_dependencies}" =~ ^([yY][eE][sS]|[yY])$ ]]; then
unset skip_install_dependencies;
fi;
read_config_file;
echo -e "Start...";
for host in "${hosts_array[@]}"; do
read_agents_list;
if [[ -z "${skip_copy_ssh_key}" ]]; then
echo -e "Copy key to agent ${username}@${host}...";
ssh-copy-id "${username}@${host}"; #> /dev/null;
if [[ "${?}" != 0 ]]; then
echo -e "ERROR: failed ssh connection to ${username}@${host}.";
echo -e "How to create ssh key: https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2";
exit 1;
fi;
fi;
if [[ -z "${skip_apt_get_update}" ]]; then
echo -e "Upgrade system on ${username}@${host}...";
ssh -Ct "${username}@${host}" "sudo apt-get update && sudo apt-get -y upgrade";
if [[ "${?}" != 0 ]]; then
echo -e "ERROR: failed upgrade system.";
exit 1;
fi;
fi;
if [[ -z "${skip_install_dependencies}" ]]; then
echo -e "Install 'openjdk-7-jre ruby' on ${username}@${host}...";
ssh -Ct "${username}@${host}" "sudo apt-get -y install openjdk-7-jre ruby";
if [[ "${?}" != 0 ]]; then
echo -e "ERROR: failed install Java and Ruby.";
exit 1;
fi;
fi;
echo -e "Create 'dscmd' folder on ${username}@${host}...";
ssh -Ct "${username}@${host}" "mkdir -p dscmd";
if [[ "${?}" != 0 ]]; then
echo -e "ERROR: failed create folder.";
exit 1;
fi;
echo -e "Copy SenchaCMD installation script (${install_script_realpath}) to ${username}@${host}:~/dscmd ...";
scp "${install_script_realpath}" "${username}@${host}:~/dscmd";
if [[ "${?}" != 0 ]]; then
echo -e "ERROR: failed copy SenchaCMD installation script.";
exit 1;
fi;
echo -e "Run SenchaCMD installation script on ${username}@${host}...";
ssh -Ct "${username}@${host}" "cd ~/dscmd; bash ./${install_script_basename}";
if [[ "${?}" != 0 ]]; then
echo -e "ERROR: failed run SenchaCMD installation script.";
exit 1;
fi;
echo -e "Syncronize directory with ${username}@${host}:/dscmd...";
rsync_agent "${username}@${host}";
if [[ "${?}" != 0 ]]; then
echo -e "ERROR: failed sync with ${username}@${host}:/dscmd.";
exit 1;
fi;
AGENTS_ARRAY["${AGENTS_ARRAY_COUNT}"]="${host}:${username}";
save_agents_list;
done;
echo -e "Done.";
}
##
# $1 - config '--all' or none
#
function f_remove_agent {
echo -e "Remove agent.";
if [[ -z "${1}" ]]; then
echo -e "ERROR: host missed.";
exit 1;
fi;
read_agents_list;
local i=0;
for agent in "${AGENTS_ARRAY[@]}"; do
parse_agent "${agent}";
if [[ "${1}" = "${parse_agent_result[0]}" ]] || [[ "${1}" = "--all" ]]; then
AGENTS_ARRAY["${i}"]="";
fi;
i=$((i+1));
done;
save_agents_list;
}
function f_agents_list {
echo -e "Agents list:";
read_agents_list;
local i=0;
for agent in "${AGENTS_ARRAY[@]}"; do
parse_agent "${agent}";
echo -e "#${i}: ${parse_agent_result[1]}@${parse_agent_result[0]}";
i=$((i+1));
done;
}
function f_agents_test {
echo -e "Test SSH connection to agents:\n";
read_agents_list;
local i=0;
for agent in "${AGENTS_ARRAY[@]}"; do
parse_agent "${agent}";
echo -e "Connect to #${i}: ${parse_agent_result[1]}@${parse_agent_result[0]}...";
check_ssh_agent "${parse_agent_result[1]}@${parse_agent_result[0]}";
if [[ "${?}" != 0 ]]; then
echo -e "... ERROR";
else
echo -e "... OK";
fi;
i=$((i+1));
done;
}
##
# $1 - application list or '--all' flag
#
function f_build {
echo -e "Build applications:\n";
read_config_file;
apps_list="${1}";
if [[ "${1}" = "--all" ]]; then
ls_directory "${APPS_PATH}";
apps_list="${ls_directory_result}";
fi;
if [[ -z "${apps_list}" ]]; then
echo -e "ERROR: application list missed / no application found.";
echo -e "Usage: ./dscmd.sh build [--all] <application1,application2,...>";
exit 1;
fi;
IFS=',' read -r -a APPLICATIONS_ARRAY <<< "${apps_list}";
# save max application name length for output formating
for application in "${APPLICATIONS_ARRAY[@]}"; do
if [[ "${#application}" -gt "${APPLICATIONS_ARRAY_MAX_ITEM_LENGTH}" ]]; then
APPLICATIONS_ARRAY_MAX_ITEM_LENGTH="${#application}";
fi;
done;
read_agents_list;
if [[ "${AGENTS_ARRAY_COUNT}" == "0" ]]; then
echo -e "ERROR: no agents.";
exit 1;
fi;
mkdir -p build/production;
local index;
local build_exit_code;
index=1;
build_exit_code=0;
for application in "${APPLICATIONS_ARRAY[@]}"; do
runned=0;
while [[ "${runned}" == 0 && "${build_exit_code}" == 0 ]]; do
get_free_agent;
if [[ -z "${get_free_agent_result}" ]]; then
sleep 1;
i=0;
for pid in "${AGENTS_PIDS_ARRAY[@]}"; do
if [[ "${pid}" != 0 ]]; then
ps -p "${pid}" &>/dev/null;
if [[ "${?}" != 0 ]]; then
set_agent_free "${i}";
wait "${pid}";
exit_code="${?}";
if [[ "${exit_code}" != 0 ]]; then
build_exit_code="${exit_code}";
fi;
fi;
fi;
i=$((i+1));
done;
else
run_build_on_agent "${get_free_agent_result}" "${application}" "${index}" &
set_agent_busy "${get_free_agent_result}" "${!}";
runned=1;
fi;
done;
index=$((index+1));
done;
#wait by pids
wait;
duration_time=$(seconds_to_duration "$(($(date +%s)-START_TIME))");
echo -e "\nDuration time: ${duration_time}";
if [[ "${build_exit_code}" != 0 ]]; then
echo -e "BUILD FAILED (exit code: ${build_exit_code}).\n";
else
echo -e "Done.\n";
fi;
}
function f_usage {
echo -e "Usage:";
echo -e " ./dscmd.sh config";
echo -e " ./dscmd.sh applications-list";
echo -e " ./dscmd.sh add-agent";
echo -e " ./dscmd.sh remove-agent [--all]";
echo -e " ./dscmd.sh agents-list";
echo -e " ./dscmd.sh agents-test";
echo -e " ./dscmd.sh build [--all] <application1,application2,...>";
}
# --- main ---
if [[ "${1}" == "config" ]]; then
f_config;
elif [[ "${1}" == "applications-list" ]]; then
f_applications_list;
elif [[ "${1}" == "add-agent" ]]; then
f_add_agent;
elif [[ "${1}" == "remove-agent" ]]; then
f_remove_agent "${2}";
elif [[ "${1}" == "agents-list" ]]; then
f_agents_list;
elif [[ "${1}" == "agents-test" ]]; then
f_agents_test;
elif [[ "${1}" == "build" ]]; then
f_build "${2}";
else
f_usage;
fi;