-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnewbuild.sh
executable file
·264 lines (212 loc) · 7.34 KB
/
newbuild.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
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
#!/bin/bash
# Script to do a new CycleStreets import run, install and test it
usage()
{
cat << EOF
SYNOPSIS
$0 -h -P -q -r -m email [config]
OPTIONS
-h Show this message
-m Take an email address as an argument - for notifications when the build breaks or completes
-P POIs (places of interest) build - special option that stops after the import phase.
-q Suppress helpful messages, error messages are still produced
-r Removes the oldest routing edition
ARGUMENTS
[config]
Optional configuration file or symlink to one.
When a symlink is provided it's basename is used as an alias to create symlink to the output routing edtion.
DESCRIPTION
Builds a new routing edition, installs on the local server, and runs tests, optionally emailing results.
EOF
}
# Controls echoed output default to on
verbose=1
# By default do not remove oldest routing edtion
removeOldest=
# Default to no notification
notifyEmail=
# POIs build
poisBuild=
# Routing edition alias: a folder name which is used as a symlink e.g. centralLondon or custom641
editionAlias=
# http://wiki.bash-hackers.org/howto/getopts_tutorial
# An opening colon in the option-string switches to silent error reporting mode.
# Colons after letters indicate that those options take an argument e.g. m takes an email address.
while getopts "hm:Pqr" option ; do
case ${option} in
h) usage; exit ;;
m)
# Set the notification email address
notifyEmail=$OPTARG
;;
# POIs build
P) poisBuild=1
;;
# Set quiet mode and proceed
q)
# Turn off verbose messages by setting this variable to the empty string
verbose=
;;
# Remove oldest routing edition
r) removeOldest=1
;;
# Missing expected argument
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
\?) echo "Invalid option: -$OPTARG" >&2 ; exit ;;
esac
done
# After getopts is done, shift all processed options away with
shift $((OPTIND-1))
# Helper function
# Echo output only if the verbose option has been set
vecho()
{
if [ "${verbose}" ]; then
echo -e $1
fi
}
### CREDENTIALS ###
# Get the script directory see: http://stackoverflow.com/a/246128/180733
# The second single line solution from that page is probably good enough as it is unlikely that this script itself will be symlinked.
ScriptHome="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Change to the script's folder
cd ${ScriptHome}
# Name of the credentials file
configFile=${ScriptHome}/.config.sh
# Generate your own credentials file by copying from .config.sh.template
if [ ! -x ${configFile} ]; then
echo -e "#\tThe config file, ${configFile}, does not exist or is not executable. Copy your own based on the ${configFile}.template file, or create a symlink to the configuration."
exit 1
fi
# Load the credentials
. ${configFile}
### Main body of script ###
# Bomb out if something goes wrong
set -e
## Start
vecho "#\tStarting $0"
# Check optional argument
if [ -n "$1" ]; then
importConfig=$1
else
# Set default
importConfig=${importContentFolder}/.config.php
fi
# Dereference any symlink
if [ -L "${importConfig}" ]; then
# Use the symlink as the alias for the edition
editionAlias=`basename ${importConfig}`
importConfig=`readlink ${importConfig}`
fi
# Import type
# The type of the import can be determined either from the alias, the basename of the config file
# or failing that the full path of the config file
importTitle=
# Use alias if present
if [ -n "${editionAlias}" ]; then
importTitle=${editionAlias}
else
# Use the basename
importTitle=`basename ${importConfig}`
# If the basename matches the default then use the full path
if [ "${importTitle}" = ".config.php" ]; then
importTitle=${importConfig}
fi
fi
## Optionally remove oldest routing edtion
if [ "${removeOldest}" ]; then
live-deployment/remove-routing-edition.sh oldest
fi
## Import (the -f overrides the current edition if it is for the same date)
if import-deployment/import.sh -f $importConfig;
then
vecho "#\t$(date)\tImport completed just fine."
else
# Gather a report and send it
if [ -n "${notifyEmail}" ]; then
# Generate a build error report
reportFile=${ScriptHome}/buildError.txt
echo -e "#\tBuild stopped during import script as follows." > ${reportFile}
echo -e "#\n#\n#\tYours,\n#\t\t\t${0##*/}\n\n" >> ${reportFile}
# Append last lines of import log
tail -n50 ${importContentFolder}/log.txt >> ${reportFile}
# Send report
cat ${reportFile} | mail -s "${csHostname} import stopped" "${notifyEmail}"
else
vecho "#\tBuild stopped during import script"
fi
exit 1
fi
# Useful binding
# The defaults-extra-file is a positional argument which must come first.
superMysql="mysql --defaults-extra-file=${mySuperCredFile} -hlocalhost"
# Determine latest edition (the -s suppresses the tabular output)
newEdition=$(${superMysql} -s cyclestreets<<<"SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME LIKE 'routing%' order by SCHEMA_NAME desc limit 1;")
vecho "#\tNew edition: ${newEdition}"
# Optionally create an alias for the routing edtion
if [ -n "${editionAlias}" ]; then
# The new routing edition will be written to this location
importMachineEditions=${importContentFolder}/output
# Remove any existing link
rm -f ${importMachineEditions}/${editionAlias}
# Create a symlink to the new edition - this allows remote machines to install the edition using the alias
ln -s ${importMachineEditions}/${newEdition} ${importMachineEditions}/${editionAlias}
vecho "#\tAlias: ${editionAlias}"
fi
# Break here on POIs build
if [ -n "$poisBuild" ]; then
vecho "#\t$(date)\tPOIs builds stop here"
exit
fi
## Install
if live-deployment/installLocalLatestEdition.sh ${newEdition} ;
then
vecho "#\t$(date)\tLocal install completed just fine."
else
if [ -n "${notifyEmail}" ]; then
echo "During install local lastest edition" | mail -s "${csHostname} import stopped" "${notifyEmail}"
else
vecho "#\tImport stopped during install local lastest edition"
fi
exit 2
fi
## Switch
if live-deployment/switch-routing-edition.sh ;
then
vecho "#\t$(date)\tSwitch routing edition completed just fine."
else
if [ -n "${notifyEmail}" ]; then
echo "During switch routing edition" | mail -s "${csHostname} import stopped" "${notifyEmail}"
else
vecho "#\t$(date)\tImport stopped during switch routing edition"
fi
exit 3
fi
## Test the built routing edition
cd "${websitesContentFolder}"
# Generate Build Summary message
summaryFile=import/buildSummary.txt
echo -e "#\tBuild summary" > ${summaryFile}
echo -e "#\tConfig file: ${importConfig}" >> ${summaryFile}
# Append last few lines of import log
tail -n3 import/log.txt >> ${summaryFile}
# Run tests relevant to the new build, appending to summary
php runtests.php "call=nearestpoint" >> ${summaryFile}
php runtests.php "call=journey&apiVersion=1" >> ${summaryFile}
php runtests.php "call=journey&apiVersion=2" >> ${summaryFile}
# Compare new coverage with when the elevation.values auto tests were created
php runtests.php "call=elevation.values&name=Elevation auto generated test:" >> ${summaryFile}
# Mail summary
if [ -n "${notifyEmail}" ]; then
# Send last lines of log and test results
cat ${summaryFile} | mail -s "${csHostname} import ${importTitle} finished" "${notifyEmail}"
fi
# Report
cat ${summaryFile}
## Finish
vecho "#\tFinished $0"
# Indicates safe exit
: