-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote.sh
executable file
·350 lines (304 loc) · 8.87 KB
/
remote.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
#!/usr/bin/env bash
#
# Author: mail _^_ AT _^_ wangbo _^_ im
#
#
# Enable strict bash mode
set -euo pipefail
IFS=$'\t\n'
REMOTE_USER="${REMOTE_USER:-}"
PARALLELISM="${PARALLELISM:-4}"
SSH=(
#"echo"
"ssh"
"-4"
"-q"
"-T"
)
SCP=(
#"echo"
"scp"
"-4"
"-q"
# Limit QPS in 1024 Kbit/s
# "-l" "1024"
)
RSYNC=(
#"echo"
"rsync"
"-4"
"-a"
"-q"
# "--bwlimit=1.5m"
)
LOG_LEVEL="${LOG_LEVEL:-WARN}"
export LOG_LEVEL
#
# Dirs
SCRIPT_FILE_PATH=`realpath $0`
SCRIPT_DIR_PATH=`dirname "${SCRIPT_FILE_PATH}"`
MAIN_SCRIPT_PATH="${SCRIPT_DIR_PATH}/main.script"
# source common config
for each_i in "${SCRIPT_DIR_PATH}"/include.*.conf; do
if [ -f "$each_i" ]; then . "$each_i"; fi
done
__usage() {
__warn ""
__warn "Usage: ${SCRIPT_FILE_PATH##*/} COMMAND PATH_TO_TARGET_PROJECT_DIR [REMOTE_IP ...]"
__warn ""
__warn "\twhere COMMAND is one command listing here:"
__warn "\t\tstart stop restart status sync run"
__warn "\t PATH_TO_TARGET_PROJECT_DIR is a symlink to or the directory for target project"
__warn "\t REMOTE_IP-s are ip for target hosts"
__warn ""
__warn "OR ${SCRIPT_FILE_PATH##*/} ssh exec [args ...]"
__warn "OR ${SCRIPT_FILE_PATH##*/} scp file_to_scp_from_remote_to_local [more_files ...]"
__warn ""
}
local_ips=("")
while read each_i; do
ip4=$(echo "$each_i" | sed -n 's/.*inet \([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/p')
if [ -n "$ip4" ]; then local_ips[${#local_ips[@]}]=$ip4; fi
done < <(ip -4 addr)
remote_user="$REMOTE_USER"
if [ -z "$remote_user" ]; then
remote_user="$(who am i | awk '{print $1}')"
fi
#
# Rsync directory from localhost to remote hosts.
#
# $1: directory to rsync
__sync() {
local d="${1%%/}"
local deref_d="$(realpath $d)"
__debug "Try to sync \"$d\" to remote hosts"
for each_i in "${REMOTE_IPS[@]}"; do
if [ -n "$each_i" ]; then
is_local=""
for each_j in "${local_ips[@]}"; do
if [ "$each_i" == "$each_j" ]; then
__info "Configured remote ip \"$each_i\" is of localhost, ignore it"
is_local="Y"
fi
done
if [ -z "$is_local" ]; then
local h="$remote_user@$each_i"
__debug "Try to sync \"$d\" to remote host \"$h\""
__debug "Syncing directory \"$deref_d\" to host \"$h\""
"${RSYNC[@]}" "$deref_d/" "$h:$deref_d"
__info "Syncing directory \"$deref_d\" to host \"$h\" --> DONE"
if [ "$d" != "$deref_d" ]; then
__debug "Updating symlink \"$d\" to \"$deref_d\" on host \"$h\""
"${SSH[@]}" $h rm -f "$d"
"${SSH[@]}" $h ln -sf "$deref_d" "$d"
__info "Updating symlink \"$d\" to \"$deref_d\" on host \"$h\" --> DONE"
fi
__debug "Try to sync \"$d\" to remote host \"$h\" --> DONE"
fi
fi
done
__debug "Try to sync \"$d\" to remote hosts --> DONE"
}
#
# Executing command for project on localhost and remote hosts parallely.
#
# $1: target directory
# $2: command (start/stop/status/check)
__async_run() {
local d="${1%%/}"
local c="$2"
local tmp_file=".tmp.$(date +%Y%m%dT%H%M%S.%N)"
echo -n "" > $tmp_file
for each_i in "${REMOTE_IPS[@]}"; do
if [ -n "$each_i" ]; then
is_local=""
for each_j in "${local_ips[@]}"; do
if [ "$each_i" == "$each_j" ]; then
is_local="Y"
fi
done
local h="$remote_user@$each_i"
if [ -z "$is_local" ]; then
# delay to run in xargs
echo "${SSH[@]}" $h bash "$MAIN_SCRIPT_PATH" "$d/" "$c" >> $tmp_file
else
# delay to run in xargs
echo bash "$MAIN_SCRIPT_PATH" "$d/" "$c" >> $tmp_file
fi
fi
done
echo "echo All Done !!!" >> $tmp_file
__debug "Try to run \"$c\" for \"$d\" on all configured hosts"
cat $tmp_file | xargs -I "{}" -n1 -P$PARALLELISM bash -c '{}'
__info "Try to run \"$c\" for \"$d\" on all configured hosts --> DONE"
rm -f $tmp_file
}
#
# Executing command for project on localhost and remote hosts one by one.
#
# $1: target directory
# $2: command (start/stop/status/check)
__sync_run() {
local d="${1%%/}"
local c="$2"
for each_i in "${REMOTE_IPS[@]}"; do
if [ -n "$each_i" ]; then
is_local=""
for each_j in "${local_ips[@]}"; do
if [ "$each_i" == "$each_j" ]; then
is_local="Y"
fi
done
local h="$remote_user@$each_i"
if [ -z "$is_local" ]; then
__debug "Try to run \"$c\" for \"$d\" on remote host \"$h\""
"${SSH[@]}" $h bash "$MAIN_SCRIPT_PATH" "$d/" "$c"
__info "Try to run \"$c\" for \"$d\" on remote host \"$h\" --> DONE"
else
__debug "Try to run \"$c\" for \"$d\" on local host \"$h\""
bash "$MAIN_SCRIPT_PATH" "$d/" "$c"
__info "Try to run \"$c\" for \"$d\" on local host \"$h\" --> DONE"
fi
fi
done
}
#
# Run SCP on localhost and remote hosts one by one.
#
# $@: files to be scp back
__scp() {
local msg=""
for each_f in "$@"; do
local lf="${each_f##*/}"
lf="${lf%%/}"
for each_i in "${REMOTE_IPS[@]}"; do
if [ -n "$each_i" ]; then
is_local=""
for each_j in "${local_ips[@]}"; do
if [ "$each_i" == "$each_j" ]; then
is_local="Y"
fi
done
local h="$remote_user@$each_i"
local lff="${lf}_${each_i}"
msg="Try to scp \"$each_f\" as \"$lff\""
if [ -z "$is_local" ]; then
msg="$msg from remote host \"$h\""
else
msg="$msg from localhost"
fi
__debug "$msg"
if [ -z "$is_local" ]; then
"${SCP[@]}" "$h:$each_f" "${lff}"
else
cp -f "$each_f" "${lff}"
fi
__info "$msg --> DONE"
fi
done
done
}
#
# Run SSH on localhost and remote hosts one by one.
#
# $@: exec and args to be ran
__ssh() {
local msg=""
for each_i in "${REMOTE_IPS[@]}"; do
if [ -n "$each_i" ]; then
is_local=""
for each_j in "${local_ips[@]}"; do
if [ "$each_i" == "$each_j" ]; then
is_local="Y"
fi
done
local h="$remote_user@$each_i"
msg="Try to exec"
for each_k in "$@"; do
msg="$msg \"$each_k\""
done
if [ -z "$is_local" ]; then
msg="$msg on remote host \"$h\""
else
msg="$msg on localhost"
fi
__debug "$msg"
if [ -z "$is_local" ]; then
"${SSH[@]}" $h "$@"
else
"$@"
fi
__info "$msg --> DONE"
fi
done
}
c="${1:-sync}"; shift || true
d="${1:-}"; shift || true
if [ -n "$d" ]; then d="$(realpath -s $d)"; fi
__assert_directory_exists_or_exit() {
if [ -z "$1" ]; then
__fatal "Project directory not specified"
__usage
exit 1
elif [ ! -d "$1" ]; then
__fatal "Project directory \"$1\" not exists"
__usage
exit 1
fi
}
REMOTE_IPS=(""); tmp=0
for each_i in "$@"; do
REMOTE_IPS[$((tmp++))]="$each_i"
done
__debug "Try to execute command \"$c\" for project \"$d\" onto \"${REMOTE_IPS[@]}\""
case "$c" in
"start"|"stop"|"status"| "check")
__assert_directory_exists_or_exit "$d"
__sync_run "$d" "$c"
;;
"restart")
__assert_directory_exists_or_exit "$d"
__sync_run "$d" stop
__sync_run "$d" start
;;
"sync")
__assert_directory_exists_or_exit "$d"
__sync "$d"
;;
"run")
__assert_directory_exists_or_exit "$d"
__async_run "$d" run
;;
"ssh "*)
c="${c#ssh }"
IFS=' '
S=("")
i=0
for each_i in $c; do
S[$((i++))]="$each_i"
done
IFS=$'\t\n'
__ssh "${S[@]}"
;;
"scp "*)
c="${c#scp }"
IFS=' '
S=("")
i=0
for each_i in $c; do
S[$((i++))]="$each_i"
done
IFS=$'\t\n'
__scp "${S[@]}"
;;
"help")
__usage
;;
*)
__fatal "Unknown command \"$c\""
__usage
exit 1
;;
esac
__info "Try to execute command \"$c\" for project \"$d\" onto \"${REMOTE_IPS[@]}\" --> DONE"
# vim:set nu rnu list et sr ts=4 sw=4 ft=sh: