-
Notifications
You must be signed in to change notification settings - Fork 1
/
execute-script-remotely.sh
executable file
·387 lines (333 loc) · 8.85 KB
/
execute-script-remotely.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
#!/bin/bash
cd `dirname $0`
. ./common.sh
case $- in *x*) USE_X="-x";; *) USE_X=;; esac
#set +x
#echo "#@@@@@@#@@@@@@#@@@@@# $@ #@@@@@@#@@@@@@#@@@@@#"
function exitfunction()
{
if [ -n "$remote_dir" ]; then
if [ -d $remote_dir ]; then
rm -r $remote_dir
fi
fi
if [ -n "$full_remote_dir" ]; then
if [ -d $full_remote_dir ]; then
sudo rm -r $full_remote_dir
fi
fi
if [ -n "$USE_X" ]; then
set -x
fi
exit $1
}
trap 'exitfunction' EXIT
usage="
This script sends script to the remote machine either by lxc send or ssh.
Usage:
$(basename $0) <script path> [--ssh-address <host_address>]
[--lxc-name <lxcname>] [--user <user>]
[--debug] [--step-debug] [--extra-executable] [--use-repo]
-- <arguments that will get send to the remote script>
where
<script path> - path to the script, to send. The script doesn't need to
have execution rights.
--ssh-address - ssh address in format [user@]host[:port] to the remote
system. Port defaults to 22, and user to the current user.
--lxc-name - name of the lxc container to send the command to.
The command will be transfered by and executed
by means of the lxc api.
--user - name of the user, on behalf of which the script
will be run. The username defaults to the connected
user in case of ssh, and root in case of lxc.
--step-debug - Will run the target script with bash -x,
the line debugging option.
--repo-path - Will use repo_path environment variable to use local file repository.
Will temporarily mount the folder, when used with lxc,
Do nothing when used locally
And mount using sshfs when used remotely via ssh.
--extra-executable - Extra file(s) that will be transfered to the remote host.
Each instantion of this parameter will add another file.
Example:
$(basename $0) test.sh --ssh-address testnode -- file tmp.flag
"
function errcho()
{
>&2 echo $@
}
dir_resolve()
{
cd "$1" 2>/dev/null || return $? # cd to desired directory; if fail, quell any error messages but return exit status
echo "`pwd -P`" # output full, link-resolved path
}
exec_mypath=${0%/*}
exec_mypath=`dir_resolve $exec_mypath`
cd $exec_mypath
exec_host=localhost
if [ "$1" == "--help" ]; then
echo "$usage"
exit 0
fi
exec_script=$1
if [ -z "$exec_script" ]; then
echo "$usage"
exit 0
fi
shift
if [ ! -f "$exec_script" ]; then
errcho "Cannot find script at location $exec_script"
exit 1
fi
#1 - localhost
#2 - by ssh on remote host.
#3 - lxc node
exec_mode=2
exec_fulldebug=0
debug=0
exec_lxcname=""
exec_opts=""
log=""
exec_user=""
use_repo=0
exec_debug=0
exec_port=22
collect_dependencies "$exec_script"
add_dependency "common.sh"
for plik in lib*.sh; do
add_dependency "$plik"
done
while [[ $# > 0 ]]
do
exec_key="$1"
shift
case $exec_key in
--debug|-x)
exec_debug=1
debug=1
;;
--extra-executable)
add_dependency "$1"
shift
;;
--ssh-address)
ssh_address=$1
shift
;;
--step-debug)
exec_fulldebug=1
;;
--user)
exec_user=$1
shift
;;
--lxc-name)
exec_lxcname=$1
shift
;;
--repo-path)
repo_path="$1"
shift
;;
--)
exec_opts="$*"
shift $#
;;
--log)
log=$1
shift
;;
*)
echo "Unkown parameter '$exec_key'. Aborting."
exit 1
;;
esac
done
if [ "$exec_fulldebug" -eq "1" ]; then
set -x
fi
#set -x
if [ -n "$debug" ]; then
if [ -z "$log" ]; then
log=/dev/stdout
fi
fi
if [ -z "${ssh_address}" ] && [ -z "${exec_lxcname}" ]; then
errcho "You must provide either --ssh-address or --lxc-name argument."
exit 1
fi
if [ -n "${ssh_address}" ] && [ -n "${exec_lxcname}" ]; then
errcho "You must provide only one, either --ssh-address or --lxc-name argument."
exit 1
fi
if [ -n "${ssh_address}" ]; then
pattern='^(([[:alnum:]]+)://)?(([[:alnum:]]+)@)?([^:^@]+)(:([[:digit:]]+))?$'
if [[ "$ssh_address" =~ $pattern ]]; then
proto=${BASH_REMATCH[2]}
sshuser=${BASH_REMATCH[4]}
sshhost=${BASH_REMATCH[5]}
exec_host="${sshhost}"
sshport=${BASH_REMATCH[7]}
else
errcho "You must put proper address of the ssh server in the first argument, e.g. [email protected]:2022"
exit 1
fi
if [ -z "$proto" ]; then
proto='ssh'
fi
if [ -z "$sshuser" ]; then
sshuser="$USER"
fi
if [ -z "$sshport" ]; then
sshport='22'
fi
if [ "$proto" != 'ssh' ]; then
errcho "You must connect using the ssh protocol, not $proto."
exit 1
fi
if [ "$sshport" != "22" ]; then
exec_portarg1="-p $sshport"
exec_portarg2="-P $sshport"
else
exec_portarg1=''
exec_portarg2=''
fi
exec_mode=2
else
sshuser='root'
fi
if [ -z "$exec_user" ]; then
if [ "$exec_mode" == "2" ]; then
exec_user=$sshuser
fi
if [ "$exec_mode" == "3" ]; then
exec_user=root
fi
fi
if [ "$exec_host" == "localhost" ]; then
exec_mode=1
fi
if [ "$exec_debug" -eq 1 ]; then
exec_opts="$exec_opts --debug"
fi
if [ -n "$log" ]; then
if [ "$exec_host" != "localhost" ]; then
prefix="$exec_user@$exec_host"
else
if [ "$exec_user" == "$(whoami)" ]; then
prefix=""
else
prefix="$exec_user@localhost"
fi
fi
logatpreviousmsg "$prefix $exec_script $exec_opts"
fi
if [ -n "$exec_lxcname" ]; then
if lxc info ${exec_lxcname}>/dev/null 2>/dev/null; then
exec_sshhome=`getent passwd $(whoami) | awk -F: '{ print $6 }'`
exec_lxcsudo=""
else
errcho "Cannot find lxc container with name $exec_lxcname"
exit 1
fi
exec_mode=3
fi
case $exec_mode in
1)
#localhost
remote_dir=$(mktemp -d)
rsync -avPR ${dependencies[@]} $tmpdir
if [ "$exec_user" != "$(whoami)" ]; then
exec_prefix="sudo -i -u ${exec_user} -- "
sudo chown -R ${exec_user} $remote_dir
else
exec_prefix=""
fi
;;
2)
#ssh
if [ "${sshuser}" == "${exec_user}" ]; then
exec_prefix="ssh ${exec_portarg1} $sshuser@$exec_host -- "
else
exec_prefix="ssh ${exec_portarg1} $sshuser@$exec_host -- sudo -Hu $exec_user -- "
fi
remote_dir=$(ssh ${exec_portarg1} ${sshuser}@${exec_host} -- mktemp -d)
rsync -avPR ${dependencies[@]} ${sshuser}@${exec_host}:${remote_dir}
if [ "${sshuser}" != "${exec_user}" ]; then
ssh ${exec_portarg1} ${sshuser}@${exec_host} -- chown -R ${exec_user} ${remote_dir}
fi
if [ "$use_repo" == "1" ]; then
errcho "--use-repo not supported when calling via ssh."
# if !ssh ${sshuser}@${exec_host} ${exec_portarg1} -- dpkg -s "$package">/dev/null 2> /dev/null; then
# logexec ssh ${sshuser}@${exec_host} ${exec_portarg1} -- sudo apt-get --yes --force-yes -q install sshfs
# fi
exit 1
fi
;;
3)
#lxc
remote_dir=$(lxc exec ${exec_lxcname} --mode=non-interactive -- mktemp -d)
exec_prefix="lxc exec ${exec_lxcname} --mode=non-interactive -- "
for plik in ${dependencies[@]}; do
lxc exec ${exec_lxcname} --mode=non-interactive -- mkdir -p "$(dirname ${exec_lxcname}/${plik})"
lxc file push ${plik} ${exec_lxcname}/${remote_dir}/${plik}
done
if [ "root" != "${exec_user}" ]; then
${exec_prefix} chown -R ${exec_user} ${remote_dir}
fi
if [ ! "$repo_path" == "" ]; then
if ! lxc config device list ${exec_lxcname} | grep tmprepo; then
lxc exec ${exec_lxcname} --mode=non-interactive -- mkdir -p ${repo_path}
lxc config device add ${exec_lxcname} tmprepo disk path="${repo_path}" source="${repo_path}"
fi
fi
;;
esac
function appendlog ()
{
#Funkcja zbierająca log na końcu pracy
if [ -n "$log" ] && [ "$log" != "/dev/stdout" ]; then
case $exec_mode in
1)
cat $remote_dir/log.log >>$log
;;
2)
#ssh
ssh $sshuser@$exec_host cat $remote_dir/log.log >>$log
;;
3)
#lxc
lxc exec ${exec_lxcname} --mode=non-interactive -- cat $remote_dir/log.log >>$log
if [ ! "$repo_path" == "" ]; then
lxc config device remove ${exec_lxcname} tmprepo disk
fi
;;
*)
echo "Error. Aborting."
exit 1
;;
esac
# echo "########################### RETURN #############################" >>$log
# echo >>$log
# echo >>$log
fi
}
#A teraz wykonujemy skrypt
if [ -n "$log" ] && [ "$log" != "/dev/stdout" ] ; then
trap 'appendlog' ERR
# logheading $exec_prefix $remote_dir $exec_opts
# echo "$exec_prefix $remote_dir $exec_opts"
fi
if [ "$exec_fulldebug" -eq "1" ]; then
# if [ "$exec_fulldebug" -eq "1" ]; then
# echo "EXECUTING $exec_prefix bash -x -- $remote_dir $exec_opts"
# fi
$exec_prefix bash -x -- ${remote_dir}/${exec_script} $exec_opts
else
$exec_prefix bash -- ${remote_dir}/${exec_script} $exec_opts
fi
exec_err=$?
trap 'errorhdl' ERR
appendlog
if [ $exec_err -ne 0 ]; then
exit $exec_err
fi