-
Notifications
You must be signed in to change notification settings - Fork 43
/
upload.sh
executable file
·156 lines (133 loc) · 3.83 KB
/
upload.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
#!/bin/bash
# Disable patsub_replacement to avoid issues with '&' in pattern substitutions
shopt -u patsub_replacement
if [ -z "$PLUGIN_HOSTS" ]; then
echo "Specify at least one host!"
exit 1
fi
if [ -z "$PLUGIN_TARGET" ]; then
echo "Specify a target!"
exit 1
fi
DEFAULT_PORT=$PLUGIN_PORT
if [ -z "$PLUGIN_PORT" ]; then
echo "Port not specified, using default port 22!"
DEFAULT_PORT="22"
fi
SOURCE=$PLUGIN_SOURCE
if [ -z "$PLUGIN_SOURCE" ]; then
echo "No source folder specified, using default './'"
SOURCE="./"
fi
USER=$RSYNC_USER
if [ -z "$RSYNC_USER" ]; then
if [ -z "$PLUGIN_USER" ]; then
echo "No user specified, using root!"
USER="root"
else
USER=$PLUGIN_USER
fi
fi
SSH_KEY=$RSYNC_KEY
if [ -z "$RSYNC_KEY" ]; then
if [ -z "$PLUGIN_KEY" ]; then
echo "No private key specified!"
exit 1
fi
SSH_KEY=$PLUGIN_KEY
fi
if [ -z "$PLUGIN_ARGS" ]; then
ARGS=
else
ARGS=$PLUGIN_ARGS
fi
if [ -z "$PLUGIN_LOG_LEVEL" ]; then
LOG_LEVEL=quiet
else
LOG_LEVEL=$PLUGIN_LOG_LEVEL
fi
# Building rsync command
expr="rsync -az $ARGS"
if [[ -n "$PLUGIN_RECURSIVE" && "$PLUGIN_RECURSIVE" == "true" ]]; then
expr="$expr -r"
fi
if [[ -n "$PLUGIN_DELETE" && "$PLUGIN_DELETE" == "true" ]]; then
expr="$expr --del"
fi
expr="$expr -e 'ssh -p %s -o UserKnownHostsFile=/dev/null -o LogLevel=$LOG_LEVEL -o StrictHostKeyChecking=no'"
# Include
IFS=','; read -ra INCLUDE <<< "$PLUGIN_INCLUDE"
for include in "${INCLUDE[@]}"; do
expr="$expr --include=$include"
done
# Exclude
IFS=','; read -ra EXCLUDE <<< "$PLUGIN_EXCLUDE"
for exclude in "${EXCLUDE[@]}"; do
expr="$expr --exclude=$exclude"
done
# Filter
IFS=','; read -ra FILTER <<< "$PLUGIN_FILTER"
for filter in "${FILTER[@]}"; do
expr="$expr --filter=$filter"
done
expr="$expr $SOURCE"
# Prepare SSH
home="/root"
mkdir -p "$home/.ssh"
printf "StrictHostKeyChecking no\n" > "$home/.ssh/config"
chmod 0700 "$home/.ssh/config"
keyfile="$home/.ssh/id_rsa"
echo "$SSH_KEY" | grep -q "ssh-ed25519"
if [ $? -eq 0 ]; then
printf "Using ed25519 based key\n"
keyfile="$home/.ssh/id_ed25519"
fi
echo "$SSH_KEY" | grep -q "ecdsa-"
if [ $? -eq 0 ]; then
printf "Using ecdsa based key\n"
keyfile="$home/.ssh/id_ecdsa"
fi
echo "$SSH_KEY" > $keyfile
chmod 0600 $keyfile
function join_with { local d=$1; shift; echo -n "$1"; shift; printf "%s" "${@/#/$d}"; }
# Parse SSH precommands
IFS=','; read -ra COMMANDS <<< "$PLUGIN_PRESCRIPT"
prescript=$(join_with ' && ' "${COMMANDS[@]}")
# Parse SSH postcommands
IFS=','; read -ra COMMANDS <<< "$PLUGIN_SCRIPT"
postscript=$(join_with ' && ' "${COMMANDS[@]}")
# Run rsync
IFS=','; read -ra HOSTS <<< "$PLUGIN_HOSTS"
IFS=','; read -ra PORTS <<< "$PLUGIN_PORTS"
result=0
for ((i=0; i < ${#HOSTS[@]}; i++))
do
HOST=${HOSTS[$i]}
PORT=${PORTS[$i]}
if [ -z $PORT ]
then
# Default Port 22
PORT=$DEFAULT_PORT
fi
echo $(printf "%s" "$ $(printf "$expr" "$PORT") $USER@$HOST:$PLUGIN_TARGET ...")
if [ -n "$PLUGIN_PRESCRIPT" ]; then
echo $(printf "%s" "$ ssh -p $PORT $USER@$HOST ...")
echo $(printf "%s" " > $prescript ...")
eval "ssh -p $PORT $USER@$HOST '$prescript'"
result=$(($result+$?))
echo $(printf "%s" "$ ssh -p $PORT $USER@$HOST result: $?")
if [ "$result" -gt "0" ]; then exit $result; fi
fi
eval "$(printf "$expr" "$PORT") $USER@$HOST:$PLUGIN_TARGET"
result=$(($result+$?))
if [ "$result" -gt "0" ]; then exit $result; fi
if [ -n "$PLUGIN_SCRIPT" ]; then
echo $(printf "%s" "$ ssh -p $PORT $USER@$HOST ...")
echo $(printf "%s" " > $postscript ...")
eval "ssh -p $PORT $USER@$HOST '$postscript'"
result=$(($result+$?))
echo $(printf "%s" "$ ssh -p $PORT $USER@$HOST result: $?")
if [ "$result" -gt "0" ]; then exit $result; fi
fi
done
exit $result