-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathentrypoint.sh
More file actions
executable file
·326 lines (300 loc) · 6.81 KB
/
entrypoint.sh
File metadata and controls
executable file
·326 lines (300 loc) · 6.81 KB
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
#!/bin/sh
set -e
ACTION_ID=action-rsync
K_PREFIX=""
# Drone CI
if [ -n "$DRONE_BRANCH" ]; then
K_PREFIX="PLUGIN_"
fi
if [ -n "$PLUGIN_VERBOSE" ]; then
VERBOSE="$PLUGIN_VERBOSE"
fi
if [ -n "$PLUGIN_MODE" ]; then
MODE="$PLUGIN_MODE"
fi
if [ -n "$PLUGIN_HOST" ]; then
HOST="$PLUGIN_HOST"
fi
if [ -n "$PLUGIN_REMOTE_HOSTS" ]; then
REMOTE_HOSTS="$PLUGIN_REMOTE_HOSTS"
fi
if [ -n "$PLUGIN_TARGET" ]; then
TARGET="$PLUGIN_TARGET"
fi
if [ -n "$PLUGIN_KEY" ]; then
KEY="$PLUGIN_KEY"
fi
if [ -n "$PLUGIN_PASSWORD" ]; then
PASSWORD="$PLUGIN_PASSWORD"
fi
if [ -n "$PLUGIN_USER" ]; then
USER="$PLUGIN_USER"
fi
if [ -n "$PLUGIN_PORT" ]; then
PORT="$PLUGIN_PORT"
fi
if [ -n "$PLUGIN_SOURCE" ]; then
SOURCE="$PLUGIN_SOURCE"
fi
if [ -n "$PLUGIN_ARGS" ]; then
ARGS="$PLUGIN_ARGS"
fi
if [ -n "$PLUGIN_ARGS_MORE" ]; then
ARGS_MORE="$PLUGIN_ARGS_MORE"
fi
if [ -n "$PLUGIN_SSH_ARGS" ]; then
SSH_ARGS="$PLUGIN_SSH_ARGS"
fi
if [ -n "$PLUGIN_RUN_SCRIPT_ON" ]; then
RUN_SCRIPT_ON="$PLUGIN_RUN_SCRIPT_ON"
fi
if [ -n "$PLUGIN_PRE_SCRIPT" ]; then
PRE_SCRIPT="$PLUGIN_PRE_SCRIPT"
fi
if [ -n "$PLUGIN_POST_SCRIPT" ]; then
POST_SCRIPT="$PLUGIN_POST_SCRIPT"
fi
# Github action
if [ -n "$GITHUB_WORKSPACE" ]; then
cd "$GITHUB_WORKSPACE"
fi
if [ -z "$VERBOSE" ]; then
VERBOSE=false
fi
__err=0
log() {
if [ "$VERBOSE" = "true" ]; then
printf "[$ACTION_ID] %s\n" "$*"
fi
}
warn() {
printf "[$ACTION_ID] %s\n" "$*" 1>&2
}
err() {
__err=$((__err + 1))
printf "[$ACTION_ID] %s\n" "$*" 1>&2
}
die() {
err "$*"
exit 1
}
if [ -z "$MODE" ]; then
MODE=push
else
MODE=$(echo "$MODE" | tr '[:upper:]' '[:lower:]')
case "$MODE" in
push | pull | local) ;;
*)
die "Invalid \$${K_PREFIX}MODE. Must be one of [push, pull, local]"
;;
esac
fi
if [ -z "$REMOTE_HOSTS" ]; then
if [ -z "$HOST" ]; then
case "$MODE" in
push | pull)
die "Must specify \$${K_PREFIX}HOST or \$${K_PREFIX}REMOTE_HOSTS! (Remote host)"
;;
esac
fi
REMOTE_HOSTS="$HOST"
else
REMOTE_HOSTS=$(printf "%s" "$REMOTE_HOSTS" | tr ',\r\n' ' ')
fi
if [ -z "$TARGET" ]; then
die "Must specify \$${K_PREFIX}TARGET! (Target folder or file. If you set it as a file, must set \$${K_PREFIX}SOURCE as file too.)"
fi
if [ -z "$KEY" ]; then
if [ -z "$PASSWORD" ]; then
case "$MODE" in
push | pull)
die "Must provide either \$${K_PREFIX}KEY or \$${K_PREFIX}PASSWORD! (ssh private key or ssh password)"
;;
esac
else
warn "Using \$${K_PREFIX}PASSWORD is less secure, please consider using \$${K_PREFIX}KEY instead."
fi
fi
if [ -z "$USER" ]; then
USER="root"
case "$MODE" in
push | pull)
log "\$${K_PREFIX}USER not specified, using default: '$USER'."
;;
esac
fi
if [ -z "$PORT" ]; then
PORT="22"
case "$MODE" in
push | pull)
log "\$${K_PREFIX}PORT not specified, using default: $PORT."
;;
esac
fi
if [ -z "$SOURCE" ]; then
SOURCE="./"
log "\$${K_PREFIX}SOURCE not specified, using default folder: '$SOURCE'."
fi
if [ -z "$ARGS" ]; then
ARGS="-azv --delete --exclude=/.git/ --exclude=/.github/"
log "\$${K_PREFIX}ARGS not specified, using default rsync arguments: '$ARGS'."
fi
if [ -n "$ARGS_MORE" ]; then
log "\$${K_PREFIX}ARGS_MORE specified, will append to \$${K_PREFIX}ARGS."
fi
if [ -z "$SSH_ARGS" ]; then
SSH_ARGS="-p $PORT -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=quiet"
case "$MODE" in
push | pull)
log "\$${K_PREFIX}SSH_ARGS not specified, using default: '$SSH_ARGS'."
;;
esac
else
log "You specified \$${K_PREFIX}SSH_ARGS, so \$${K_PREFIX}PORT will be ignored."
fi
if [ -z "$RUN_SCRIPT_ON" ]; then
RUN_SCRIPT_ON=target
log "\$${K_PREFIX}RUN_SCRIPT_ON not specified, using default: '$RUN_SCRIPT_ON'"
else
RUN_SCRIPT_ON=$(echo "$RUN_SCRIPT_ON" | tr '[:upper:]' '[:lower:]')
fi
case "$RUN_SCRIPT_ON" in
local)
REAL_RUN_SCRIPT_ON="$RUN_SCRIPT_ON"
;;
remote)
REAL_RUN_SCRIPT_ON="$RUN_SCRIPT_ON"
if [ "$MODE" = "local" ]; then
die "Invalid setup: cannot run scripts on remote when \$${K_PREFIX}MODE is 'local'."
fi
;;
source)
if [ "$MODE" = "local" ]; then
REAL_RUN_SCRIPT_ON=local
elif [ "$MODE" = "push" ]; then
REAL_RUN_SCRIPT_ON=local
else
REAL_RUN_SCRIPT_ON=remote
fi
;;
target)
if [ "$MODE" = "local" ]; then
REAL_RUN_SCRIPT_ON=local
elif [ "$MODE" = "push" ]; then
REAL_RUN_SCRIPT_ON=remote
else
REAL_RUN_SCRIPT_ON=local
fi
;;
*)
die "Invalid \$${K_PREFIX}RUN_SCRIPT_ON, must be one of [local, remote, source, target]"
;;
esac
# Prepare
if [ -n "$KEY" ]; then
case "$MODE" in
push | pull)
mkdir -p "$HOME/.ssh"
echo "$KEY" | tr -d '\r' >"$HOME/.ssh/key"
chmod 600 "$HOME/.ssh/key"
;;
esac
cmd_ssh=$(printf "ssh -i %s %s" "$HOME/.ssh/key" "$SSH_ARGS")
elif [ -n "$PASSWORD" ]; then
export SSHPASS="$PASSWORD"
cmd_ssh=$(printf "sshpass -e ssh %s" "$SSH_ARGS")
fi
case "$MODE" in
push | pull)
if [ -n "$KEY" ]; then
cmd_rsync=$(printf "rsync %s %s -e '%s'" "$ARGS" "$ARGS_MORE" "$cmd_ssh")
elif [ -n "$PASSWORD" ]; then
cmd_rsync=$(printf "sshpass -e rsync %s %s -e 'ssh %s'" "$ARGS" "$ARGS_MORE" "$SSH_ARGS")
fi
;;
local)
cmd_rsync=$(printf "rsync %s %s" "$ARGS" "$ARGS_MORE")
;;
esac
case "$REAL_RUN_SCRIPT_ON" in
local)
cmd_rsync_script=$(printf "rsync -av")
;;
remote)
if [ -n "$KEY" ]; then
cmd_rsync_script=$(printf "rsync -avz -e '%s'" "$cmd_ssh")
elif [ -n "$PASSWORD" ]; then
cmd_rsync_script=$(printf "sshpass -e rsync -avz -e 'ssh %s'" "$SSH_ARGS")
fi
;;
esac
run_script() {
name="$1"
src="$2"
log "========== $name starting =========="
if [ "$REAL_RUN_SCRIPT_ON" = "remote" ]; then
dest=$(eval "$cmd_ssh" "$USER@$HOST" 'mktemp')
else
dest=$(mktemp)
fi
if [ "$REAL_RUN_SCRIPT_ON" = "remote" ]; then
eval "$cmd_rsync_script" "$src" "$USER@$HOST:$dest"
else
eval "$cmd_rsync_script" "$src" "$dest"
fi
log "========== $name sent =========="
if [ "$REAL_RUN_SCRIPT_ON" = "remote" ]; then
eval "$cmd_ssh" "$USER@$HOST" "sh $dest"
else
sh "$dest"
fi
log "========== $name executed =========="
if [ "$REAL_RUN_SCRIPT_ON" = "remote" ]; then
eval "$cmd_ssh" "$USER@$HOST" "rm $dest"
else
rm "$dest"
fi
log "========== $name removed =========="
}
__run_count=0
run_once() {
if [ -n "$PRE_SCRIPT" ]; then
pre_src=$(mktemp)
printf "%s\n" "$PRE_SCRIPT" >"$pre_src"
run_script "Pre script" "$pre_src"
fi
case "$MODE" in
push)
eval "$cmd_rsync" "$SOURCE" "$USER@$HOST:$TARGET"
;;
pull)
eval "$cmd_rsync" "$USER@$HOST:$SOURCE" "$TARGET"
;;
local)
eval "$cmd_rsync" "$SOURCE" "$TARGET"
;;
esac
if [ -n "$POST_SCRIPT" ]; then
post_src=$(mktemp)
printf "%s\n" "$POST_SCRIPT" >"$post_src"
run_script "Post script" "$post_src"
fi
__run_count=$((__run_count + 1))
}
# Execute
if [ "$MODE" = "local" ]; then
run_once
else
log "Starting execution with mode '$MODE' on host(s): $REMOTE_HOSTS"
for h in $REMOTE_HOSTS; do
HOST="$h"
run_once
done
fi
# final handler
if [ "$__run_count" -eq 0 ]; then
err "No successful execution was detected"
fi
if [ "$__err" -ne 0 ]; then
exit 1
fi