-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·145 lines (128 loc) · 3.32 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·145 lines (128 loc) · 3.32 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
#!/bin/sh
set -e
usage() {
cat <<EOF
Usage: $0 ssh-server [options]
Options:
-b, --skip-build Skip building. Use last local build
-u, --skip-upload Skip building and uploading. Use unfinished server-side deployment
-n, --no-batchmode Disable batchmode for ssh
-h, --help Show this help message
EOF
}
error() {
echo "Error: $1" >&2
[ -n "$3" ] && echo && usage
exit "${2:-1}"
}
openssh_opts='-t '
while [ "${1#-}" != "$1" ]; do
for opt in $([ "${1#--}" = "$1" ] && echo "${1#-}" | grep -o . || echo "$1"); do
case "$opt" in
'--')
shift
break 2
;;
'b' | '--skip-build' | 's')
skip_build=1
;;
'u' | '--skip-upload')
skip_build=1
skip_upload=1
;;
'n' | '--no-batchmode')
no_batchmode=1
;;
'h' | '--help')
usage
exit 0
;;
'-')
error "Invalid options" 1 usage
;;
--*)
error "Unknown option: $opt" 1 usage
;;
*)
error "Unknown option: -$opt" 1 usage
;;
esac
done
shift
unset opt
done
[ -z "$no_batchmode" ] && openssh_opts='-o BatchMode=true '
case "$1" in
*@*) ;;
*)
error "Please specify a valid ssh connection! (user@host)" 1 usage
;;
esac
cd "$(dirname "$0")"
if [ "$skip_build" != "1" ]; then
npm run build
printf '\n'
fi
if [ "$skip_upload" != "1" ]; then
ssh "$openssh_opts" "$1" /bin/sh <<'EOF'
set -e
rm -rf "$HOME/emexlabs"
EOF
scp -rC "$openssh_opts" ./build "$1:~/emexlabs"
ssh "$openssh_opts" "$1" /bin/sh <<'EOF'
set -e
find "$HOME/emexlabs" -type d -exec chmod 755 {} +
find "$HOME/emexlabs" -type f -exec chmod 644 {} +
EOF
fi
# Bootstrap comparison
set +e
ssh "$openssh_opts" "$1" /bin/sh <<'EOF'
diff -qr "$HOME/emexlabs/bootstrap" "/var/www/emexlabs/bootstrap" >/dev/null 2>&1
exit $?
EOF
bootstrap_diff=$?
set -e
if [ "$bootstrap_diff" -ne 0 ]; then
if [ "$bootstrap_diff" -eq 1 ]; then
printf 'Bootstrap is different! Replace it? [y/N] '
else
printf 'Bootstrap check failed! Continue anyway? [y/N] '
fi
if [ ! -t 0 ]; then
error "No stdin. Exiting." 2
fi
read -r response
case "$response" in
[yY][eE][sS]|[yY]) ;;
*) printf "Cancelling deployment.\n"; exit 1 ;;
esac
fi
ssh "$openssh_opts" "$1" /bin/sh <<'EOF'
set -e
# Deployment
trap '
status=$?
printf "\n\033[31;1mDeployment failed!\nMaybe your servers mv command does not support --exchange?\033[0m\n" >&2
exit $status
' 0
mv --exchange -T "$HOME/emexlabs" "/var/www/emexlabs"
trap '
status=$?
printf "\n\033[31;1mBackup failed!\033[0m\n" >&2
exit $status
' 0
printf '\n\033[32;1mDeployed successfully!\033[0m\n\nBacking up old site...\n'
# Backup creation
mkdir -p "$HOME/backups"
backup_timestamp=$(date +%Y%m%d_%H%M%S)
backup_path="backups/website-${backup_timestamp}"
i=0
while [ -e "$HOME/$backup_path" ]; do
i=$((i + 1))
backup_path="backups/website-${backup_timestamp}-$i"
done
mv "$HOME/emexlabs" "$HOME/$backup_path"
trap - 0
printf '\n\033[33;1mBackup successful!\033[0m\n'
EOF