-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbootstrap.sh
397 lines (380 loc) · 10.8 KB
/
bootstrap.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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# Copyright_License {
#
# Copyright (C) 2016 VirtualFlightRadar-Backend
# A detailed list of copyright holders can be found in the file "AUTHORS".
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# }
# set SUDO if not root
if [ $(id -u) -ne 0 ]; then
SUDO='sudo'
fi
export SUDO="${SUDO:-}"
# set logging PROMPT
export PROMPT="$(basename $0)"
# logging function
# param 1 may indicate log level
# all other params are printed
function log() {
local TIME=$(date +"%T")
case $1 in
-i)
echo -en "\033[0;32m[INFO ]\033[0m"
shift
;;
-w)
echo -en "\033[0;33m[WARN ]\033[0m"
shift
;;
-e)
echo -en "\033[0;31m[ERROR]\033[0m"
shift
;;
esac
local ROUTINE=''
if [ -n "${FUNCNAME[1]}" ]; then
ROUTINE="->${FUNCNAME[1]}"
fi
echo " ${TIME} ${PROMPT}${ROUTINE}:: $*"
}
# require an env var to be set and not empty
# params are the var names
function require() {
local error=0
for v in $*; do
if [ -z "${!v}" ]; then
log -e Env. $v is not set!
error=1
fi
done
return $error
}
# exit the script with 1 and execute/log given params
# all params preceeded by -e are executed
# all other params are logged
function fail() {
local MSG=''
while [ $# -gt 0 ]; do
case $1 in
-e)
$2
shift
;;
*)
MSG="${MSG} $1"
;;
esac
shift
done
log -e ${MSG}
exit 1
}
# wait for user input to confirm or deny
# all params are logged
# if AUTO_CONFIRM is set skip
function confirm() {
log -i $*
if [ -z "${AUTO_CONFIRM}" ]; then
read -r -p "Confirm? [yes|no]: " READ
case ${READ} in
[yY] | yes | YES)
return 0
;;
*)
return 1
;;
esac
fi
log -w Automatically confirmed.
return 0
}
# set PKG_MANAGER to the distributions package manager
function resolve_pkg_manager() {
local error=0
if [ "$(which 2>&1)" == "" ]; then
APT="$(which apt-get || true)"
YUM="$(which yum || true)"
DNF="$(which dnf || true)"
APK="$(which apk || true)"
else
RELEASE="$(cat /etc/*-release)"
if [ "$(echo $RELEASE | grep -oiE '(ubuntu|debian)')" != "" ]; then
APT="apt-get"
elif [ "$(echo $RELEASE | grep -oiE '(centos)')" != "" ]; then
YUM="yum"
elif [ "$(echo $RELEASE | grep -oiE '(fedora)')" != "" ]; then
DNF="dnf"
elif [ "$(echo $RELEASE | grep -oiE 'alpine')" != "" ]; then
APK="apk"
fi
fi
if [ -n "$APT" ]; then
export PKG_MANAGER="$APT"
log -i Using apt as package manager.
elif [ -n "$YUM" ]; then
export PKG_MANAGER="$YUM"
log -i Using yum as package manager.
elif [ -n "$DNF" ]; then
export PKG_MANAGER="$DNF"
log -i Using dnf as package manager.
elif [ -n "$APK" ]; then
export PKG_MANAGER="$APK"
log -i Using apk as package manager.
else
log -e Could not determine package manager!
error=1
fi
return $error
}
# install all build dependencies
function install_deps() {
set -eE
log -i INSTALL DEPENDENCIES
trap "fail Failed to install dependencies!" ERR
resolve_pkg_manager
require PKG_MANAGER
case $PKG_MANAGER in
*apt-get)
local UPDATE='apt-get update'
local SETUP=''
local INSTALL='install -y'
local BOOST='libboost-dev libboost-system-dev libboost-regex-dev libboost-program-options-dev'
local GCC="g++ make cmake"
;;
*yum)
local UPDATE=''
local SETUP='yum -y install epel-release'
local INSTALL='install -y'
local BOOST='boost boost-devel'
local GCC="gcc-c++ make cmake"
;;
*dnf)
local UPDATE=''
local SETUP=''
local INSTALL='install -y'
local BOOST='boost boost-devel'
local GCC="gcc-c++ make cmake"
;;
*apk)
local UPDATE='apk update'
local SETUP=''
local INSTALL='add'
local BOOST='boost-dev boost-system boost-regex boost-program_options'
local GCC='g++ make cmake'
;;
esac
require GCC BOOST INSTALL
ALL="$GCC"
if [ -z "$CUSTOM_BOOST" ]; then
ALL="$ALL $BOOST"
fi
if [ -n "$UPDATE" ]; then $SUDO $UPDATE; fi
if [ -n "$SETUP" ]; then $SUDO $SETUP; fi
log -i "$SUDO" "$PKG_MANAGER" "$INSTALL" "$ALL"
$SUDO $PKG_MANAGER $INSTALL $ALL
trap - ERR
}
# build for production
function build() {
set -eE
log -i BUILD VFRB
require VFRB_ROOT
local LS=""
if [ -n "${VFRB_LINK_STATIC}" ]; then
LS="-DBOOST_STATIC=1 --no-warn-unused-cli"
fi
if [ -n "${VFRB_BIN_TAG}" ]; then
LS="$LS -DVFRB_BIN_TAG=$VFRB_BIN_TAG"
fi
trap "fail -e popd Build has failed!" ERR
pushd $VFRB_ROOT/build/
cmake .. $LS
make release -j$(nproc)
popd
trap - ERR
}
# install systemd service
function install() {
set -eE
log -i INSTALL VFRB SERVICE
require VFRB_ROOT VFRB_INI
trap "fail -e popd Service installation has failed!" ERR
pushd $VFRB_ROOT/build/
$SUDO make install
$SUDO systemctl enable vfrb.service
log -i VFRB service created.
popd
trap - ERR
}
# install test dependencies
function install_test_deps() {
set -eE
log -i INSTALL TEST DEPENDENCIES
trap "fail Failed to install test dependencies!" ERR
resolve_pkg_manager
require PKG_MANAGER
case $PKG_MANAGER in
*apt-get)
local TOOLS='cppcheck clang-format-6.0 wget netcat procps perl lcov lua5.3 lua-argparse lua-socket lua-posix'
;;
*)
log -e Tests currently only run under ubuntu/debian systems.
return 1
;;
esac
require TOOLS
ALL="$TOOLS"
log -i "$SUDO" "$PKG_MANAGER" -y install "$ALL"
$SUDO $PKG_MANAGER -y install $ALL
trap - ERR
}
# build for testing
function build_test() {
set -eE
log -i BUILD VFRB TESTS
require VFRB_ROOT
trap "fail -e popd Build has failed!" ERR
pushd "$VFRB_ROOT/build/"
cmake ..
make unittest regression -j$(nproc)
popd
trap - ERR
}
# perform static code analysis
function static_analysis() {
set -eE
log -i RUN STATIC CODE ANALYSIS
require VFRB_ROOT
trap "fail -e popd Static code analysis failed!" ERR
pushd $VFRB_ROOT
cppcheck --enable=warning,style,performance,unusedFunction,missingInclude -q -I include/ src/
local FORMAT="clang-format-6.0"
! $FORMAT --version >/dev/null 2>&1
if [ $? -eq 0 ]; then
FORMAT="clang-format"
fi
for f in $(find src/ include/ -type f); do
diff -u <(cat $f) <($FORMAT -style=file $f) || true
done &>/tmp/format.diff
if [ "$(wc -l /tmp/format.diff | cut -d' ' -f1)" -gt 0 ]; then
log -e Code format does not comply to the specification.
cat /tmp/format.diff
rm /tmp/format.diff
return 1
fi
rm /tmp/format.diff
popd
trap - ERR
}
# execute testing binary
function run_unit_test() {
set -eE
log -i RUN UNIT TESTS
require VFRB_ROOT
local error=1
local VFRB_UUT="$(find $VFRB_ROOT/build/ -name '*vfrb_test-*' -executable | head -n1)"
trap "fail -e popd Unit tests failed!" ERR
pushd $VFRB_ROOT
lcov -i -d build/CMakeFiles/unittest.dir -c -o reports/test_base.info
! $VFRB_UUT &>reports/unittests.xml
if [ $? -eq 1 ]; then
error=0
fi
cat reports/unittests.xml
$(exit $error)
popd
trap - ERR
}
# execute regression tests
function run_regression() {
set -eE
log -i RUN REGRESSION TESTS
require VFRB_ROOT
local VFRB_UUT="$(find $VFRB_ROOT/build/ -name '*vfrb_regression-*' -executable | head -n1)"
lcov -i -d $VFRB_ROOT/build/CMakeFiles/regression.dir -c -o $VFRB_ROOT/reports/vfrb_base.info
if ! $VFRB_UUT; then $(exit 0); fi
if ! $VFRB_UUT -v -g -c bla.txt; then $(exit 0); fi
trap "fail -e popd -e '$SUDO pkill -2 -f $VFRB_UUT' Regression tests have failed!" ERR
pushd $VFRB_ROOT/test/resources
log -i Start mocking servers
./regression.sh serve
sleep 2
log -i Start vfrb
$VFRB_UUT -c test.ini &
sleep 2
log -i Connect to vfrb
./regression.sh receive
./regression.sh receive
sleep 5
log -i Stop vfrb and run check
$SUDO pkill -2 -f $VFRB_UUT || true
sleep 4
./regression.sh check
log -i "Test for reconnects"
$VFRB_UUT -c test.ini >vfrb.log 2>&1 &
sleep 5
./regression.sh serve
sleep 5
$SUDO pkill -2 -f $VFRB_UUT || true
# just to cleanup servers
./regression.sh check >/dev/null 2>&1 || true
if [ $(cat vfrb.log | grep -o 'connected to' | wc -l) -lt 4 ]; then
log -e "reconnect test failed"
$(exit 1)
fi
log -i Test windclient timeout
lua server.lua 44403 nil >serv.log 2>&1 &
local S_PID=$!
$VFRB_UUT -c test.ini >/dev/null 2>&1 &
sleep 10
$SUDO pkill -2 -f $VFRB_UUT || true
kill -9 $S_PID
if [ $(cat serv.log | grep -o 'Connection from' | wc -l) -lt 2 ]; then
log -e "timeout test failed"
$(exit 1)
fi
popd
trap - ERR
}
# generate coverage report for unit and regression tests
function gen_coverage() {
set -eE
log -i GENERATE COVERAGE REPORT
require VFRB_ROOT
trap "fail -e popd Coverage report generation failed!" ERR
pushd $VFRB_ROOT
lcov -d build/CMakeFiles/unittest.dir -c -o reports/test.info
lcov -d build/CMakeFiles/regression.dir -c -o reports/vfrb.info
lcov -a reports/test_base.info -a reports/test.info -a reports/vfrb_base.info -a reports/vfrb.info \
-o reports/all.info
lcov --remove reports/all.info '*test/*' '/usr/*' -o reports/lcov.info
lcov --list reports/lcov.info
popd
trap - ERR
}
# build docker image for production
function docker_image() {
set -eE
log -i BUILD DOCKER IMAGE
require VFRB_ROOT VFRB_VERSION
if [ ! -x /usr/bin/docker ]; then
log -e docker must be installed to create a docker image
return 1
fi
trap "fail -e popd Docker image creation failed!" ERR
pushd $VFRB_ROOT
$SUDO docker build . -t user/vfrb:latest -t user/vfrb:${VFRB_VERSION}
popd
trap - ERR
}