-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_files.sh
executable file
·186 lines (159 loc) · 3.94 KB
/
update_files.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
#!/bin/bash
# Rsync files on remote hosts.
# Copyright (C) 2017 Bryzgalov Peter @ Stair Lab CHITECH
# TODO
# [V] Read PROJ_FOLDER and REMOTE_PATH from CSV file.
. init.sh
. $config_file
usage=$(cat <<USAGEBLOCK
Update files on remote hosts with rsync.
Use CL arguments and $CSV_file file. CL arguments has priority.
$0 [-a <[user@]host1,[user@]host2...>] [-i identity file] -d <dirname> -r <path>
Options:
-a Remote hosts addresses, comma-separated list.
-d Local folder with task (project) files.
-r Remote path for storing task and framework files relative to home dir..
USAGEBLOCK
)
key_opt=""
REMOTE=""
ssh_com=""
echo "Reading from $CSV_file"
while read -r line; do
IFS="," read -ra arr <<< "$line"
case "${arr[0]}" in
hosts)
remote_hosts=("${arr[@]/hosts}")
if [[ -n "$debug" ]]; then
echo "hosts: ${remote_hosts[@]}"
fi
;;
remote_path)
REMOTE_PATH="${arr[1]}"
echo "Using remote path $REMOTE_PATH"
;;
folder)
PROJ_FOLDER="${arr[1]}"
echo "Using proj folder $PROJ_FOLDER"
;;
key)
KEY="${arr[1]}";key_opt="-i ${arr[1]}";shift;
;;
esac
done < $CSV_file
# Reading CL arguments
while test $# -gt 0; do
case "$1" in
-h | --help)
echo $usage
exit 0
;;
-a)
REMOTE="$2";shift;
;;
-d)
PROJ_FOLDER="$2";shift;
;;
-r)
REMOTE_PATH="$2";shift;
;;
-i)
KEY="$2";key_opt="-i $2";shift;
;;
--debug)
debug=YES
;;
-*)
echo "Invalid option: $1"
echo "$usage"
exit 1;;
esac
shift
done
if [[ -z "$REMOTE_PATH" ]] || [[ -z "$PROJ_FOLDER" ]]; then
if [[ -z "$REMOTE_PATH" ]]; then
echo "Remote path is empty"
fi
if [[ -z "$PROJ_FOLDER" ]]; then
echo "proj folder is empty"
fi
echo "$usage"
exit 1
fi
set -e
if [[ -n "$REMOTE" ]]; then
rhost=$REMOTE
echo $rhost
if [[ -n "$KEY" ]]; then
SSH_KEY="-e \"ssh $key_opt\""
else
SSH_KEY=""
fi
echo $ssh_com
if [[ -n "$debug" ]]; then
if [[ "$debug" == "2" ]]; then
OPT="-avviic --progress"
else
OPT="-avic --progress"
fi
else
OPT="-ac --progress"
fi
if [[ -n "$debug" ]]; then
echo "Compare ./$PROJ_FOLDER/ with $rhost:$REMOTE_PATH/$PROJ_FOLDER/"
fi
# Copy task files to remote
eval rsync $OPT $SSH_KEY --exclude-from "rsyncexclude_task.txt" ./$PROJ_FOLDER/ $rhost:$REMOTE_PATH/$PROJ_FOLDER/
# Copy framework files to remote
eval rsync $OPT $SSH_KEY --include-from "rsyncinclude_framework.txt" --exclude='*' ./ $rhost:$REMOTE_PATH/
exit 0
fi
echo "${remote_hosts[@]}"
# Copy files to remote locations
for rhost in "${remote_hosts[@]}"; do
if [[ "$rhost" == "hosts" ]] || [[ "$rhost" == "localhost" ]] || [[ -z "$rhost" ]]; then
continue
fi
echo $rhost
if [[ -n "$key_opt" ]]; then
SSH_KEY="-e \"ssh $key_opt\""
else
SSH_KEY=""
fi
if [[ -n "$debug" ]]; then
echo "rsync ssh option: $SSH_KEY"
echo "Testing SSH connection to $rhost with ssh $key_opt $rhost"
fi
printf "hostname" > $cmd_filename
HOSTNAME=$(RemoteExec $cmd_filename $rhost "$key_opt")
if [[ -z "$HOSTNAME" ]]; then
echo "Cannot connect with ssh $key_opt $rhost."
exit 1
fi
if [[ -n "$debug" ]]; then
echo $HOSTNAME
fi
if [[ -n "$debug" ]]; then
if [[ "$debug" == "2" ]]; then
OPT="-aviic --progress"
else
OPT="-avic --progress"
fi
else
OPT="-avc --progress"
fi
if [[ -n "$debug" ]]; then
echo "Compare ./$PROJ_FOLDER/ with $rhost:$REMOTE_PATH/$PROJ_FOLDER/"
fi
# Copy task files to remote
cmd="rsync $OPT $SSH_KEY --exclude-from \"rsyncexclude_task.txt\" ./$PROJ_FOLDER/ $rhost:$REMOTE_PATH/$PROJ_FOLDER/"
if [[ -n "$debug" ]]; then
echo $cmd
fi
eval $cmd 2>/dev/null
cmd="rsync $OPT $SSH_KEY --include-from \"rsyncinclude_framework.txt\" --exclude='*' ./ $rhost:$REMOTE_PATH/"
if [[ -n "$debug" ]]; then
echo $cmd
fi
eval $cmd 2>/dev/null
done