-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomation.sh
executable file
·88 lines (77 loc) · 1.7 KB
/
automation.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
#!/bin/bash
# A list of servers one per line
SERVER_LIST='/vagrant/servers'
#options for ssh
SSH_OPTIONS='-o ConnectTimeout=2 '
usage(){
#Show usage
echo "Usage: ${0} [-nsv] [-f FILE] COMMAND" >&2
echo 'Executes COMMAND as single command on every server' >&2
echo " -f FILE Use file for the list of servers. Default is ${SERVER_LIST} " >&2
echo ' -n Dry run mode. Display the COMMAND that will be executed' >&2
echo ' -s Execute the COMMAND using sudo on servers' >&2
echo ' -v Verbose mode. Shows also a server name' >&2
exit 1
}
#make sure non root
if [[ "${UID}" -eq 0 ]]
then
echo 'Its non root script...' >&2
usage
fi
#parse options
while getopts f:nsv OPTION
do
case ${OPTION} in
f) SERVER_LIST="${OPTARG}"
;;
n) DRY_RUN='true'
;;
s) SUDO='sudo'
;;
v) VERBOSE='true'
;;
?) usage
;;
esac
done
#Revome the options while leaving args
shift "$(( OPTIND - 1 ))"
#If user dont supply args
if [[ "${#}" -lt 1 ]]
then
usage
fi
# Anything else is the command line to be like single command
COMMAND="${@}"
#Check if file exists
if [[ ! -e "${SERVER_LIST}" ]]
then
echo "Cannot open server list file ${SERVER_LIST}" >&2
exit 1
fi
EXIT_STATUS='0'
#Loop SERVER_LIST
for SERVER in $(cat ${SERVER_LIST})
do
if [[ "${VERBOSE}" = 'true' ]]
then
echo "${SERVER}"
fi
SSH_COMMAND="ssh ${SSH_OPTIONS} ${SERVER} ${SUDO} ${COMMAND}"
#if dry run then dont run anyting
if [[ "${DRY_RUN}" = 'true' ]]
then
echo "Dry run: ${SSH_COMMAND}"
else
${SSH_COMMAND}
SSH_EXIT_STATUS="${?}"
#capture any non zeros
if [[ "${SSH_EXIT_STATUS}" -ne 0 ]]
then
EXIT_STATUS=${SSH_EXIT_STATUS}
echo "Execution fail on ${SERVER}" >&2
fi
fi
done
exit ${EXIT_STATUS}