-
Notifications
You must be signed in to change notification settings - Fork 10
/
entrypoint.sh
executable file
·76 lines (60 loc) · 1.89 KB
/
entrypoint.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
#!/bin/bash
set -e
setupSSH() {
local SSH_PATH="$HOME/.ssh"
mkdir -p "$SSH_PATH"
touch "$SSH_PATH/known_hosts"
echo "$INPUT_KEY" > "$SSH_PATH/deploy_key"
chmod 700 "$SSH_PATH"
chmod 600 "$SSH_PATH/known_hosts"
chmod 600 "$SSH_PATH/deploy_key"
eval $(ssh-agent)
ssh-add "$SSH_PATH/deploy_key"
ssh-keyscan -t rsa $INPUT_HOST >> "$SSH_PATH/known_hosts"
}
executeSSH() {
local LINES=$1
local COMMAND=""
# holds all commands separated by semi-colon
local COMMANDS=""
# this while read each commands in line and
# evaluate each line agains all environment variables
while IFS= read -r LINE; do
LINE=$(eval 'echo "$LINE"')
LINE=$(eval echo "$LINE")
COMMAND=$(echo $LINE)
if [ -z "$COMMANDS" ]; then
COMMANDS="$COMMAND"
else
COMMANDS="$COMMANDS;$COMMAND"
fi
done <<< $LINES
echo "$COMMANDS"
ssh -o StrictHostKeyChecking=no -p ${INPUT_PORT:-22} $INPUT_USER@$INPUT_HOST "$COMMANDS"
}
executeSCP() {
local LINES=$1
local COMMAND=
# this while read each commands in line and
# evaluate each line agains all environment variables
while IFS= read -r LINE; do
LINE=$(eval 'echo "$LINE"')
LINE=$(eval echo "$LINE")
COMMAND=$(echo $LINE)
# scp will fail if COMMAND is empty, this condition protects scp
if [[ $COMMAND = *[!\ ]* ]]; then
echo "scp -r -o StrictHostKeyChecking=no $COMMAND"
scp -r -o StrictHostKeyChecking=no $COMMAND
fi
done <<< $LINES
}
setupSSH
echo "+++++++++++++++++++RUNNING BEFORE SSH+++++++++++++++++++"
executeSSH "$INPUT_SSH_BEFORE"
echo "+++++++++++++++++++RUNNING BEFORE SSH+++++++++++++++++++"
echo "+++++++++++++++++++RUNNING SCP+++++++++++++++++++"
executeSCP "$INPUT_SCP"
echo "+++++++++++++++++++RUNNING SCP+++++++++++++++++++"
echo "+++++++++++++++++++RUNNING AFTER SSH+++++++++++++++++++"
executeSSH "$INPUT_SSH_AFTER"
echo "+++++++++++++++++++RUNNING AFTER SSH+++++++++++++++++++"