/
deploy-site
executable file
·337 lines (273 loc) · 8.31 KB
/
deploy-site
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
327
328
329
330
331
332
333
334
335
336
337
#!/bin/bash
########## Configuration / Usage Text / Etc below ##############
SCRIPT=$(basename ${0});
REPOSITORY=$(hostname | cut -d'-' -f 1)
HOSTNAME=$(hostname)
DEFAULTORG="development"
DEFAULTENVIRON="live"
DOMAIN=$(dnsdomainname)
USERNAME=$(whoami)
###### Destination Server Configuration
DEFAULTSERVER="github.com"
PORT="" ##### Custom ssh ports must include colon (ex. PORT=":7999")
USAGE=$(cat <<EOF_USAGE
USAGE: ${SCRIPT} <options>
The following options are supported:
--help : Displays this help message.
--branch [branch to deploy] : No Default, must specify.
--deployed : Lists information about the currently
: deployed tag for the current server.
--no-backup : Skips creating DB Backup (Not recommended)
--org [org in github] : Defaults to "${DEFAULTORG}".
--repo [name of repository] : Defaults to "${REPOSITORY}".
--server [servername] : Defaults to "${DEFAULTSERVER}".
Default user for all git operations will be the currently logged in
user (${USERNAME}).
EOF_USAGE
)
GITCONFIG=$(cat <<EOF_GITCONFIG
You need to configure your git identity and try again.
This information is required once for identifying who is
responsible for each tag this script creates during a deploy.
Example:
$ git config --global user.name "Classic Graphics"
$ git config --global user.email "youremail@knowclassic.com"
Templates to copy/paste:
git config --global user.name
git config --global user.email
EOF_GITCONFIG
)
# Default to creating a DB Backup
SKIPDB=false
######### Shouldn't need to change anything below #########
NUMARGS="$#"
# Print usage message and exit
print_usage () {
echo "${USAGE}" >&2
}
# return given variable if not in error
assign_value () {
case ${1} in
--*)
print_usage
exit 4
;;
*)
echo ${1}
;;
esac
}
# Function to display the deployed version of the site
# Along with other relevant information.
display_deployed () {
DEPLOYED=$(sed -n '/START DEPLOY INFO/,/END DEPLOY INFO/p' /var/www/.htaccess | tr -d [:space:])
if [ x${DEPLOYED} != "x" ]; then
IFS=$'#'
set ${DEPLOYED}
DEPINFO=$(cat <<EOF_DEPLOY
Current Deploy Information for this server:
Deploying user: ${4}
Drupal Version: ${7}.x
Environment: ${5}
Tag Deployed: ${5}-${6}
Branch Deployed: ${8}
Repository: ${9}
GitHub Organization: ${10}
EOF_DEPLOY
)
else
DEPINFO="Sorry, no information yet available."
fi
echo -e "${DEPINFO}\n\n"
exit
}
# parse the command line
# for each command line argument that requires
# a second parameter, assign "BAD" if it's empty
# or begins with -- (ie picking another argument)
while [ "${1}" != "" ]; do
case ${1} in
--no-backup)
SKIPDB=true;
shift 1
;;
--branch)
BRANCH=$(assign_value ${2})
shift 2
;;
--deployed)
display_deployed
;;
--org)
if [ x"${ORG}" == "x" ]; then
ORG=$(assign_value ${2})
fi
shift 2
;;
--repo)
REPO=$(assign_value ${2})
shift 2
;;
--server)
if [ x"${SERVER}" == "x" ]; then
SERVER="git@"$(assign_value ${2})
fi
shift 2
;;
--help|--*)
print_usage
exit 0
;;
esac
done
if [ "${ORG}" == "" ]; then
ORG="${DEFAULTORG}"
fi
if [ "${REPO}" == "" ]; then
if [[ "${HOSTNAME}" =~ "-" ]]; then
REPO=$(hostname | cut -d'-' -f 1)
else
REPO="${HOSTNAME}"
fi
fi
if [[ "${HOSTNAME}" =~ "-" ]]; then
ENVIRON=$(hostname | cut -d'-' -f 2)
else
ENVIRON="${DEFAULTENVIRON}"
fi
if [ "${SERVER}" == "" ]; then
SERVER="git@${DEFAULTSERVER}"
fi
if [ "${BRANCH}" == "" ]; then
if [ "${ENVIRON}" != "${DEFAULTENVIRON}" ]; then
print_usage
exit 3
else
BRANCH="master";
fi
fi
# We need arguments!
if [ ${NUMARGS} -eq 0 ] && [ "${ENVIRON}" != "${DEFAULTENVIRON}" ]; then
print_usage
exit 2
fi
if [ ! -e "${HOME}/.gitconfig" ]; then
echo "${GITCONFIG}" >&2;
exit 1;
fi
if [ -d /tmp/clone ]; then
echo "Removing existing clone directory..."
sudo rm -rf /tmp/clone
fi
GITURL="git+ssh://${SERVER}${PORT}/${ORG}/${REPO}.git"
echo "Cloning ${BRANCH}..."
mkdir /tmp/clone
git clone -b ${BRANCH} ${GITURL} /tmp/clone
cd /tmp/clone
TAG=$(date +%Y-%m-%d-%H-%M)
HASH=$(git log "${BRANCH}" -n 1 --pretty=format:%h)
echo "Tagging local clone..."
git tag -a -m "${BRANCH} ${HASH} to ${ENVIRON} by ${USERNAME}" "${ENVIRON}-${TAG}"
echo "Pushing tag to origin..."
git push origin "${ENVIRON}-${TAG}"
echo "Determining Drupal version..."
DVERS=$(drush status | sed -e "s/ *//g;" | grep 'Drupalversion' | cut --delimiter=: -f2 | cut --delimiter=. -f1)
if [ -r /opt/patches/D${DVERS}-ssl.patch ]; then
echo "Patching Drupal ${DVERS}.x core to support SSL database connection..."
# Apply Drupal ${DVERS} patch to source
patch -d /tmp/clone -p1 < /opt/patches/D${DVERS}-ssl.patch
elif [ "${DVERS}" == "7" ]; then
# Find the settings file for site
SETTINGSFILE=$(find /var/www -type f -name settings.php 2> /dev/null)
echo "Checking Drupal ${DVERS}.x ${SETTINGSFILE} for previously applied patch..."
# Look for the patch
PATCHED=$(grep -A 15 "^\$databases" ${SETTINGSFILE} | grep "'pdo'")
# was it found?
if [ "${PATCHED}" == "" ]; then
# Nope
echo "${SETTINGSFILE} not previously patched, patching..."
# Define the SSL options for PDO connection
PDOOPTIONS=$(cat << EOF
'pdo' => array ( PDO::MYSQL_ATTR_SSL_CA => '/etc/ssl/mysql/ca.crt', PDO::MYSQL_ATTR_SSL_CERT => '/etc/ssl/mysql/${HOSTNAME}.${DOMAIN}.crt', PDO::MYSQL_ATTR_SSL_KEY => '/etc/ssl/mysql/${HOSTNAME}.${DOMAIN}.key', ),
EOF
)
# Find the location in file to insert PDO SSL options
INSERTPOS=$(( $(grep -A0 -B0 -n1 "^\$databases" ${SETTINGSFILE} | cut -d: -f1) + 1 ))
# Patch the file
sed -i "${INSERTPOS}i${PDOOPTIONS}" ${SETTINGSFILE}
else
# Already patched
echo "${SETTINGSFILE} already patched, skipping..."
fi
else
# We have an unsupported install, Red Flag!!
echo "Error: No patch found for Drupal ${DVERS}.x!"
echo "Processing aborted!"
echo "EXAMINE SOURCE in /tmp/clone!!"
exit 2
fi
if [[ ! ${SKIPDB} ]]; then
echo "Removing old backup DB files..."
find /var/dbbackup/* -mtime +1 -delete
echo "Creating DB Backup..."
cd /var/www
drush sql-dump --result-file='/var/dbbackup/'${ENVIRON}'-'${TAG}'.sql'
echo "Compressing (bzip2) sql backup..."
bzip2 /var/dbbackup/${ENVIRON}-${TAG}.sql
else
echo "Skipped DB Backup . . ."
fi
echo "Configuring permissions..."
if [ "${ENVIRON}" == "live" ]; then
sudo chown -R www-data:www-data /tmp/clone
USER='-u www-data'
else
sudo chgrp -R www-data /tmp/clone
USER=''
fi
# Set permissions and sync
sudo chmod -R a-srwx /tmp/clone
sudo chmod -R ug+rX,g+w /tmp/clone
for D in `find /tmp/clone -type d`; do
sudo chmod g+s ${D}
done
echo "Syncing ${BRANCH} to /var/www..."
CMD='sudo '${USER}' rsync -rpl --exclude="private" --exclude=".git" --exclude=".gitignore" /tmp/clone/ /var/www/'
eval ${CMD}
# Just so we don't throw errors below when bash can't find the /tmp/clone directory
cd ${HOME}
echo "Removing clone directory..."
sudo rm -rf /tmp/clone
# Build a deploy Description block to place at the end of the .htaccess file
DEPLOY="## START DEPLOY INFO
# ${USERNAME}
# ${ENVIRON}
# ${TAG}
# ${DVERS}
# ${BRANCH}
# ${REPOSITORY}
# ${ORG}
## END DEPLOY INFO
"
# Remove the previous DEPLOY INFO
sudo sed -i '/START DEPLOY INFO/,/END DEPLOY INFO/d' /var/www/.htaccess
# add the info to the end of the .htaccess file
echo -e "${DEPLOY}" | sudo tee -a /var/www/.htaccess >/dev/null
# Find all .htaccess files and build include for apache
HTACCESS=$(find {/var/www,/var/private,/var/mailinglist,/var/private_files} -name .htaccess 2>/dev/null)
# Loop through the found files and create the include file for apache
HTINCLUDE=""
for F in ${HTACCESS}; do
DIR=$(dirname "${F}")
HTINCLUDE+=$(printf "<Directory %s>\\\n Include %s\\\n</Directory>\\\n" "${DIR}" "${F}")
done
# write the file
echo -e "${HTINCLUDE}" | sudo tee /etc/apache2/htaccess.d/htaccess.conf >/dev/null
sudo chmod 0644 /etc/apache2/htaccess.d/htaccess.conf
# reload apache (gracefully) to load newly created file
echo "Reloading apache..."
sudo apache2ctl graceful 2>/dev/null >/dev/null
echo "Launch complete!"
echo "Template for pasting to a related issue:"
echo "Deployed ${HASH} with tag ${ENVIRON}-${TAG}. - ${ORG^^}/${REPO^^}"
echo