forked from gem/oq-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packager.sh
executable file
·1311 lines (1150 loc) · 44.5 KB
/
packager.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#
# packager.sh Copyright (C) 2014-2016 GEM Foundation
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake 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 Affero General Public License
# along with OpenQuake. If not, see <http://www.gnu.org/licenses/>.
#
# DESCRIPTION
#
# packager.sh automates procedures to:
# - test sources
# - build Ubuntu package (official or development version)
# - test Ubuntu package
#
# tests are performed inside linux containers (lxc) to achieve
# a good compromise between speed and isolation
#
# all lxc instances are ephemeral
#
# ephemeral containers are "clones" of a base container and have a
# temporary file system that reflects the contents of the base container
# but any modifications are stored in another overlayed
# file system (in-memory or disk)
#
if [ -n "$GEM_SET_DEBUG" -a "$GEM_SET_DEBUG" != "false" ]; then
export PS4='+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}: '
set -x
fi
set -e
GEM_GIT_REPO="git://github.com/gem"
GEM_GIT_PACKAGE="oq-engine"
GEM_GIT_DEPS="oq-hazardlib"
GEM_DEB_PACKAGE="python-${GEM_GIT_PACKAGE}"
GEM_DEB_SERIE="master"
if [ -z "$GEM_DEB_REPO" ]; then
GEM_DEB_REPO="$HOME/gem_ubuntu_repo"
fi
if [ -z "$GEM_DEB_MONOTONE" ]; then
GEM_DEB_MONOTONE="$HOME/monotone"
fi
GEM_BUILD_ROOT="build-deb"
GEM_BUILD_SRC="${GEM_BUILD_ROOT}/${GEM_DEB_PACKAGE}"
GEM_MAXLOOP=20
GEM_ALWAYS_YES=false
if [ "$GEM_EPHEM_CMD" = "" ]; then
GEM_EPHEM_CMD="lxc-start-ephemeral"
fi
if [ "$GEM_EPHEM_NAME" = "" ]; then
GEM_EPHEM_NAME="ubuntu-lxc-eph"
fi
LXC_VER=$(lxc-ls --version | cut -d '.' -f 1)
if [ $LXC_VER -lt 1 ]; then
echo "lxc >= 1.0.0 is required." >&2
exit 1
fi
LXC_TERM="lxc-stop -t 10"
LXC_KILL="lxc-stop -k"
if command -v lxc-copy &> /dev/null; then
# New lxc (>= 2.0.0) with lxc-copy
GEM_EPHEM_EXE="${GEM_EPHEM_CMD} -n ${GEM_EPHEM_NAME} -e"
else
# Old lxc (< 2.0.0) with lxc-start-ephimeral
GEM_EPHEM_EXE="${GEM_EPHEM_CMD} -o ${GEM_EPHEM_NAME} -d"
fi
NL="
"
TB=" "
#
# functions
#
# sig_hand - manages cleanup if the build is aborted
#
sig_hand () {
trap ERR
echo "signal trapped"
if [ "$lxc_name" != "" ]; then
set +e
scp "${lxc_ip}:/tmp/celeryd.log" "out_${BUILD_UBUVER}/celeryd.log"
scp "${lxc_ip}:ssh.log" "out_${BUILD_UBUVER}/ssh.history"
echo "Destroying [$lxc_name] lxc"
upper="$(mount | grep "${lxc_name}.*upperdir" | sed 's@.*upperdir=@@g;s@,.*@@g')"
if [ -f "${upper}.dsk" ]; then
loop_dev="$(sudo losetup -a | grep "(${upper}.dsk)$" | cut -d ':' -f1)"
fi
sudo $LXC_KILL -n $lxc_name
sudo umount /var/lib/lxc/$lxc_name/rootfs
sudo umount /var/lib/lxc/$lxc_name/ephemeralbind
echo "$upper" | grep -q '^/tmp/'
if [ $? -eq 0 ]; then
sudo umount "$upper"
sudo rm -r "$upper"
if [ "$loop_dev" != "" ]; then
sudo losetup -d "$loop_dev"
if [ -f "${upper}.dsk" ]; then
sudo rm -f "${upper}.dsk"
fi
fi
fi
sudo lxc-destroy -n $lxc_name
fi
if [ -f /tmp/packager.eph.$$.log ]; then
rm /tmp/packager.eph.$$.log
fi
exit 1
}
#
# dep2var <dep> - converts in a proper way the name of a dependency to a variable name
# <dep> the name of the dependency
#
dep2var () {
echo "$1" | sed 's/[-.]/_/g;s/\(.*\)/\U\1/g'
}
#
# repo_id_get - retry git repo from local git remote command
repo_id_get () {
local repo_name repo_line
if ! repo_name="$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null)"; then
repo_line="$(git remote -vv | grep "^origin[ ${TB}]" | grep '(fetch)$')"
if [ -z "$repo_line" ]; then
echo "no remote repository associated with the current branch, exit 1"
exit 1
fi
else
repo_name="$(echo "$repo_name" | sed 's@/.*@@g')"
repo_line="$(git remote -vv | grep "^${repo_name}[ ${TB}].*(fetch)\$")"
fi
if echo "$repo_line" | grep -q '[0-9a-z_-\.]\+@[a-z0-9_-\.]\+:'; then
repo_id="$(echo "$repo_line" | sed "s/^[^ ${TB}]\+[ ${TB}]\+[^ ${TB}@]\+@//g;s/.git[ ${TB}]\+(fetch)$/.git/g;s@/${GEM_GIT_PACKAGE}.git@@g;s@:@/@g")"
else
repo_id="$(echo "$repo_line" | sed "s/^[^ ${TB}]\+[ ${TB}]\+git:\/\///g;s/.git[ ${TB}]\+(fetch)$/.git/g;s@/${GEM_GIT_PACKAGE}.git@@g")"
fi
echo "$repo_id"
}
#
# mksafedir <dname> - try to create a directory and rise an alert if it already exists
# <dname> name of the directory to create
#
mksafedir () {
local dname
dname="$1"
if [ "$GEM_ALWAYS_YES" != "true" -a -d "$dname" ]; then
echo "$dname already exists"
echo "press Enter to continue or CTRL+C to abort"
read a
fi
rm -rf $dname
mkdir -p $dname
}
#
# usage <exitcode> - show usage of the script
# <exitcode> value of exitcode
#
usage () {
local ret
ret=$1
echo
echo "USAGE:"
echo " $0 [<-s|--serie> <precise|trusty|xenial>] [-D|--development] [-S--sources_copy] [-B|--binaries] [-U|--unsigned] [-R|--repository] build debian source package."
echo " if -s is present try to produce sources for a specific ubuntu version (precise, trusty or xenial),"
echo " (default precise)"
echo " if -S is present try to copy sources to <GEM_DEB_MONOTONE>/<BUILD_UBUVER>/source directory"
echo " if -B is present binary package is build too."
echo " if -R is present update the local repository to the new current package"
echo " if -D is present a package with self-computed version is produced."
echo " if -U is present no sign are perfomed using gpg key related to the mantainer."
echo
echo " $0 pkgtest <branch-name>"
echo " install oq-engine package and related dependencies into"
echo " an ubuntu lxc environment and run package tests and demos"
echo " $0 devtest <branch-name>"
echo " put oq-engine and oq-* dependencies sources in a lxc,"
echo " setup environment and run development tests."
echo
exit $ret
}
#
# _wait_ssh <lxc_ip> - wait until the new lxc ssh daemon is ready
# <lxc_ip> the IP address of lxc instance
#
_wait_ssh () {
local lxc_ip="$1"
for i in $(seq 1 20); do
if ssh $lxc_ip "echo begin"; then
break
fi
sleep 2
done
if [ $i -eq 20 ]; then
return 1
fi
}
_pkgbuild_innervm_run () {
local lxc_ip="$1"
local DPBP_FLAG="$2"
trap 'local LASTERR="$?" ; trap ERR ; (exit $LASTERR) ; return' ERR
ssh $lxc_ip mkdir build-deb
scp -r * $lxc_ip:build-deb
gpg -a --export | ssh $lxc_ip "sudo apt-key add -"
ssh $lxc_ip sudo apt-get update
ssh $lxc_ip sudo apt-get -y upgrade
ssh $lxc_ip sudo apt-get -y install build-essential dpatch fakeroot devscripts equivs lintian quilt
ssh $lxc_ip "sudo mk-build-deps --install --tool 'apt-get -y' build-deb/debian/control"
ssh $lxc_ip "cd build-deb && dpkg-buildpackage $DPBP_FLAG"
scp $lxc_ip:*.{tar.gz,changes,dsc} ../
if echo "$DPBP_FLAG" | grep -q -v -- '-S'; then
scp $lxc_ip:*.deb ../
fi
return
}
#
# _devtest_innervm_run <lxc_ip> <branch> - part of source test performed on lxc
# the following activities are performed:
# - extracts dependencies from oq-{engine,hazardlib, ..} debian/control
# files and install them
# - builds oq-hazardlib speedups
# - installs oq-engine sources on lxc
# - set up db
# - runs tests
# - runs coverage
# - collects all tests output files from lxc
#
# <lxc_ip> the IP address of lxc instance
# <branch> name of the tested branch
#
_devtest_innervm_run () {
local i old_ifs pkgs_list dep lxc_ip="$1" branch="$2"
trap 'local LASTERR="$?" ; trap ERR ; (exit $LASTERR) ; return' ERR
ssh $lxc_ip "rm -f ssh.log"
ssh $lxc_ip "sudo apt-get update"
ssh $lxc_ip "sudo apt-get -y upgrade"
gpg -a --export | ssh $lxc_ip "sudo apt-key add -"
# install package to manage repository properly
ssh $lxc_ip "sudo apt-get install -y python-software-properties"
# add custom packages
ssh $lxc_ip mkdir -p "repo"
scp -r ${GEM_DEB_REPO}/custom_pkgs $lxc_ip:repo/custom_pkgs
ssh $lxc_ip "sudo apt-add-repository \"deb file:/home/ubuntu/repo/custom_pkgs ${BUILD_UBUVER} main\""
ssh $lxc_ip "sudo apt-get update"
ssh $lxc_ip "sudo apt-get upgrade -y"
old_ifs="$IFS"
IFS=" "
for dep in $GEM_GIT_DEPS; do
# extract dependencies for source dependencies
pkgs_list="$(deps_list "deprec" _jenkins_deps/$dep/debian)"
ssh $lxc_ip "sudo apt-get install -y ${pkgs_list}"
# install source dependencies
cd _jenkins_deps/$dep
git archive --prefix ${dep}/ HEAD | ssh $lxc_ip "tar xv"
cd -
done
IFS="$old_ifs"
# extract dependencies for this package
pkgs_list="$(deps_list "all" debian)"
ssh $lxc_ip "sudo apt-get install -y ${pkgs_list}"
# build oq-hazardlib speedups and put in the right place
ssh $lxc_ip "export GEM_SET_DEBUG=$GEM_SET_DEBUG
set -e
if [ -n \"\$GEM_SET_DEBUG\" -a \"\$GEM_SET_DEBUG\" != \"false\" ]; then
export PS4='+\${BASH_SOURCE}:\${LINENO}:\${FUNCNAME[0]}: '
set -x
fi
cd oq-hazardlib
python ./setup.py build
for i in \$(find build/ -name *.so); do
o=\"\$(echo \"\$i\" | sed 's@^[^/]\+/[^/]\+/@@g')\"
cp \$i \$o
done"
# install sources of this package
git archive --prefix ${GEM_GIT_PACKAGE}/ HEAD | ssh $lxc_ip "tar xv"
# configure the machine to run tests
if [ -z "$GEM_DEVTEST_SKIP_TESTS" ]; then
if [ -n "$GEM_DEVTEST_SKIP_SLOW_TESTS" ]; then
# skip slow tests
skip_tests="!slow,"
fi
# run tests (in this case we omit 'set -e' to be able to read all tests outputs)
ssh $lxc_ip "export GEM_SET_DEBUG=$GEM_SET_DEBUG
set -e
if [ -n \"\$GEM_SET_DEBUG\" -a \"\$GEM_SET_DEBUG\" != \"false\" ]; then
export PS4='+\${BASH_SOURCE}:\${LINENO}:\${FUNCNAME[0]}: '
set -x
fi
export PYTHONPATH=\"\$PWD/oq-hazardlib:\$PWD/oq-engine\" ;
cd oq-engine; bin/oq dbserver start &
nosetests -v -a '${skip_tests}' --with-xunit --xunit-file=xunit-engine.xml --with-coverage --cover-package=openquake.engine --with-doctest openquake/engine/tests/
nosetests -v -a '${skip_tests}' --with-xunit --xunit-file=xunit-server.xml --with-coverage --cover-package=openquake.server --with-doctest openquake/server/tests/
# OQ Engine QA tests (splitted into multiple execution to track the performance)
nosetests -a '${skip_tests}qa,hazard' -v --with-xunit --xunit-file=xunit-qa-hazard.xml
nosetests -a '${skip_tests}qa,risk' -v --with-xunit --xunit-file=xunit-qa-risk.xml
nosetests -v --with-doctest --with-coverage --cover-package=openquake.risklib openquake/risklib
nosetests -v --with-doctest --with-coverage --cover-package=openquake.commonlib openquake/commonlib
nosetests -v --with-doctest --with-coverage --cover-package=openquake.commands openquake/commands
python-coverage xml --include=\"openquake/*\"
bin/oq dbserver stop"
scp "${lxc_ip}:oq-engine/xunit-*.xml" "out_${BUILD_UBUVER}/" || true
scp "${lxc_ip}:oq-engine/coverage.xml" "out_${BUILD_UBUVER}/" || true
else
if [ -d $HOME/fake-data/$GEM_GIT_PACKAGE ]; then
cp $HOME/fake-data/$GEM_GIT_PACKAGE/* "out_${BUILD_UBUVER}/"
fi
fi
# TODO: version check
# echo "NOW PRESS ENTER TO CONTINUE"
# read aaa
trap ERR
return
}
_builddoc_innervm_run () {
local i old_ifs pkgs_list dep lxc_ip="$1" branch="$2"
trap 'local LASTERR="$?" ; trap ERR ; (exit $LASTERR) ; return' ERR
ssh $lxc_ip "rm -f ssh.log"
ssh $lxc_ip "sudo apt-get update"
ssh $lxc_ip "sudo apt-get -y upgrade"
gpg -a --export | ssh $lxc_ip "sudo apt-key add -"
# install package to manage repository properly
# ssh $lxc_ip "sudo apt-get install -y python-software-properties"
old_ifs="$IFS"
IFS=" "
for dep in $GEM_GIT_DEPS; do
# extract dependencies for source dependencies
pkgs_list="$(deps_list "build" _jenkins_deps/$dep/debian)"
ssh $lxc_ip "sudo apt-get install -y ${pkgs_list}"
# install source dependencies
cd _jenkins_deps/$dep
git archive --prefix ${dep}/ HEAD | ssh $lxc_ip "tar xv"
cd -
done
IFS="$old_ifs"
# extract dependencies for this package
pkgs_list="$(deps_list "build" debian)"
ssh $lxc_ip "sudo apt-get install -y ${pkgs_list}"
# install sources of this package
git archive --prefix ${GEM_GIT_PACKAGE}/ HEAD | ssh $lxc_ip "tar xv"
ssh $lxc_ip "set -e ; export PYTHONPATH=\"\$PWD/oq-hazardlib:\$PWD/oq-engine\" ; cd oq-engine/doc/sphinx ; make html"
scp -r "${lxc_ip}:oq-engine/doc/sphinx/build/html" "out_${BUILD_UBUVER}/" || true
# TODO: version check
trap ERR
return
}
#
# _pkgtest_innervm_run <lxc_ip> <branch> - part of package test performed on lxc
# the following activities are performed:
# - adds local gpg key to apt keystore
# - copies 'oq-*' package repositories on lxc
# - adds repositories to apt sources on lxc
# - performs package tests (install, remove, reinstall ..)
# - set up db
# - runs celeryd
# - executes demos
#
# <lxc_ip> the IP address of lxc instance
# <branch> name of the tested branch
#
_pkgtest_innervm_run () {
local lxc_ip="$1" branch="$2" old_ifs from_dir
trap 'local LASTERR="$?" ; trap ERR ; (exit $LASTERR) ; return' ERR
ssh $lxc_ip "rm -f ssh.log"
ssh $lxc_ip "sudo apt-get update"
ssh $lxc_ip "sudo apt-get -y upgrade"
gpg -a --export | ssh $lxc_ip "sudo apt-key add -"
# install package to manage repository properly
ssh $lxc_ip "sudo apt-get install -y python-software-properties"
# create a remote "local repo" where place $GEM_DEB_PACKAGE package
ssh $lxc_ip mkdir -p "repo/${GEM_DEB_PACKAGE}"
scp ${GEM_BUILD_ROOT}/${GEM_DEB_PACKAGE}_*.deb ${GEM_BUILD_ROOT}/${GEM_DEB_PACKAGE}_*.changes \
${GEM_BUILD_ROOT}/${GEM_DEB_PACKAGE}_*.dsc ${GEM_BUILD_ROOT}/${GEM_DEB_PACKAGE}_*.tar.gz \
${GEM_BUILD_ROOT}/Packages* ${GEM_BUILD_ROOT}/Sources* ${GEM_BUILD_ROOT}/Release* $lxc_ip:repo/${GEM_DEB_PACKAGE}
ssh $lxc_ip "sudo apt-add-repository \"deb file:/home/ubuntu/repo/${GEM_DEB_PACKAGE} ./\""
if [ -f _jenkins_deps_info ]; then
source _jenkins_deps_info
fi
old_ifs="$IFS"
IFS=" $NL"
for dep in $GEM_GIT_DEPS; do
var_pfx="$(dep2var "$dep")"
var_repo="${var_pfx}_REPO"
var_branch="${var_pfx}_BRANCH"
var_commit="${var_pfx}_COMMIT"
if [ "${!var_repo}" != "" ]; then
dep_repo="${!var_repo}"
else
dep_repo="$GEM_GIT_REPO"
fi
if [ "${!var_branch}" != "" ]; then
dep_branch="${!var_branch}"
else
dep_branch="master"
fi
if [ "$dep_repo" = "$GEM_GIT_REPO" -a "$dep_branch" = "master" ]; then
GEM_DEB_SERIE="master"
else
GEM_DEB_SERIE="devel/$(echo "$dep_repo" | sed 's@^.*://@@g;s@/@__@g;s/\./-/g')__${dep_branch}"
fi
from_dir="${GEM_DEB_REPO}/${BUILD_UBUVER}/${GEM_DEB_SERIE}/python-${dep}.${!var_commit:0:7}"
time_start="$(date +%s)"
while true; do
if scp -r "$from_dir" $lxc_ip:repo/python-${dep}; then
break
fi
if [ "$dep_branch" = "$branch" ]; then
# NOTE: currently we retry for 1 hour to get the correct dep version
# if there is concordance between package and dependency branches
time_cur="$(date +%s)"
if [ $time_cur -gt $((time_start + 3600)) ]; then
return 1
fi
sleep 10
else
# NOTE: in the other case dep branch is 'master' and package branch isn't
# so we try to get the correct commit package and if it isn't yet built
# it fallback to the latest builded
from_dir="$(ls -drt ${GEM_DEB_REPO}/${BUILD_UBUVER}/${GEM_DEB_SERIE}/python-${dep}* | tail -n 1)"
scp -r "$from_dir" $lxc_ip:repo/python-${dep}
break
fi
done
ssh $lxc_ip "sudo apt-add-repository \"deb file:/home/ubuntu/repo/python-${dep} ./\""
done
IFS="$old_ifs"
# add custom packages
scp -r ${GEM_DEB_REPO}/custom_pkgs $lxc_ip:repo/custom_pkgs
ssh $lxc_ip "sudo apt-add-repository \"deb file:/home/ubuntu/repo/custom_pkgs ${BUILD_UBUVER} main\""
ssh $lxc_ip "sudo apt-get update"
ssh $lxc_ip "sudo apt-get upgrade -y"
# packaging related tests (install, remove, purge, install, reinstall)
ssh $lxc_ip "sudo apt-get install -y ${GEM_DEB_PACKAGE}"
ssh $lxc_ip "sudo apt-get remove -y ${GEM_DEB_PACKAGE}"
ssh $lxc_ip "sudo apt-get install -y ${GEM_DEB_PACKAGE}"
ssh $lxc_ip "sudo apt-get install --reinstall -y ${GEM_DEB_PACKAGE}"
# configure the machine to run tests
if [ -z "$GEM_PKGTEST_SKIP_DEMOS" ]; then
# use celery to run the demos
ssh $lxc_ip "celeryd --config openquake.engine.celeryconfig >/tmp/celeryd.log 2>&1 3>&1 &"
# wait for celeryd startup time
ssh $lxc_ip "
celeryd_wait() {
local cw_nloop=\"\$1\" cw_ret cw_i
if command -v celeryctl &> /dev/null; then
# celery 2.4
celery=celeryctl
elif command -v celery &> /dev/null; then
# celery 3
celery=celery
else
echo \"ERROR: no Celery available\"
return 1
fi
for cw_i in \$(seq 1 \$cw_nloop); do
cw_ret=\"\$(\$celery status)\"
if echo \"\$cw_ret\" | grep -iq '^error:'; then
if echo \"\$cw_ret\" | grep -ivq '^error: no nodes replied'; then
return 1
fi
else
return 0
fi
sleep 1
done
return 1
}
celeryd_wait $GEM_MAXLOOP"
fi
# run all of the hazard and risk demos
ssh $lxc_ip "export GEM_SET_DEBUG=$GEM_SET_DEBUG
set -e
if [ \$(cat /etc/passwd | grep ^openquake: | wc -l) -eq 0 ]; then
echo \"There's no 'openquake' user on this system. Installation may have failed.\"
exit 1
fi
# dbserver should be already started by supervisord. Let's have a check
# FIXME instead of using a 'sleep' we should use a better way to check that
# the dbserver is alive
sleep 10; sudo /usr/bin/supervisorctl status
if [ -n \"\$GEM_SET_DEBUG\" -a \"\$GEM_SET_DEBUG\" != \"false\" ]; then
export PS4='+\${BASH_SOURCE}:\${LINENO}:\${FUNCNAME[0]}: '
set -x
fi
cd /usr/share/openquake/engine/demos
for ini in \$(find . -name job.ini | sort); do
echo \"Running \$ini\"
for loop in \$(seq 1 $GEM_MAXLOOP); do
set +e
oq engine --run \$ini --exports xml,hdf5
oq_ret=\$?
set -e
if [ \$oq_ret -eq 0 ]; then
break
elif [ \$oq_ret -ne 2 ]; then
exit \$oq_ret
fi
sleep 1
done
if [ \$loop -eq $GEM_MAXLOOP ]; then
exit \$oq_ret
fi
done
# print the log of the last calculation
oq engine --show-log -1
# Try to export a set of results AFTER the calculation
# automatically creates a directory called out
echo \"Exporting output #1\"
oq engine --eo 1 /tmp/output
echo \"Exporting calculation #2\"
oq engine --eos 2 /tmp/out/eos_2
for demo_dir in \$(find . -type d | sort); do
if [ -f \$demo_dir/job_hazard.ini ]; then
cd \$demo_dir
echo \"Running \$demo_dir/job_hazard.ini\"
OQ_DISTRIBUTE=celery oq engine --run job_hazard.ini
echo \"Running \$demo_dir/job_risk.ini\"
oq engine --run job_risk.ini --exports csv,xml --hazard-calculation-id -1
cd -
fi
done
oq info --report risk
echo 'Listing hazard calculations'
oq engine --lhc
echo 'Listing risk calculations'
oq engine --lrc"
ssh $lxc_ip "oq engine --make-html-report today
oq engine --delete-calculation 1 --yes
oq engine --dc 1 --yes
oq purge -1; oq purge 0"
scp "${lxc_ip}:jobs-*.html" "out_${BUILD_UBUVER}/"
scp -r "${lxc_ip}:/usr/share/doc/${GEM_DEB_PACKAGE}/changelog*" "out_${BUILD_UBUVER}/"
trap ERR
return
}
#
# deps_list <listtype> <filename> - retrieve dependencies list from debian/control
# to be able to install them without the package
# listtype inform deps_list which control lines use to get dependencies
# dir the folder containing control and rules files
#
deps_list() {
local old_ifs out_list skip i d listtype="$1" control_file="$2"/control rules_file="$2"/rules
if grep -q "^${BUILD_UBUVER^^}_DEP" $rules_file; then
# Use custom dependencies in debian/rules
rules_dep=$(grep "^${BUILD_UBUVER^^}_DEP *= *" $rules_file | sed 's/([^)]*)//g' | sed 's/^.*= *//g')
rules_rec=$(grep "^${BUILD_UBUVER^^}_REC *= *" $rules_file | sed 's/([^)]*)//g' | sed 's/^.*= *//g')
fi
out_list=""
if [ "$listtype" = "all" ]; then
in_list="$((cat "$control_file" | egrep '^Depends:|^Recommends:|Build-Depends:' | sed 's/^\(Build-\)\?Depends://g;s/^Recommends://g' ; echo ", $rules_dep, $rules_rec") | tr '\n' ','| sed 's/,\+/,/g')"
elif [ "$listtype" = "deprec" ]; then
in_list="$((cat "$control_file" | egrep '^Depends:|^Recommends:' | sed 's/^Depends://g;s/^Recommends://g' ; echo ", $rules_dep, $rules_rec") | tr '\n' ','| sed 's/,\+/,/g')"
elif [ "$listtype" = "build" ]; then
in_list="$((cat "$control_file" | egrep '^Depends:|^Build-Depends:' | sed 's/^\(Build-\)\?Depends://g' ; echo ", $rules_dep") | tr '\n' ','| sed 's/,\+/,/g')"
else
in_list="$((cat "$control_file" | egrep "^Depends:" | sed 's/^Depends: //g'; echo ", $rules_dep") | tr '\n' ','| sed 's/,\+/,/g')"
fi
old_ifs="$IFS"
IFS=','
for i in $in_list ; do
item="$(echo "$i" | sed 's/^ \+//g;s/ \+$//g')"
pkg_name="$(echo "${item} " | cut -d ' ' -f 1)"
pkg_vers="$(echo "${item} " | cut -d ' ' -f 2)"
echo "[$pkg_name][$pkg_vers]" >&2
if echo "$pkg_name" | grep -q "^\${" ; then
continue
fi
skip=0
for d in $(echo "$GEM_GIT_DEPS" | sed 's/ /,/g'); do
if [ "$pkg_name" = "python-${d}" ]; then
skip=1
break
fi
done
if [ $skip -eq 1 ]; then
continue
fi
if [ "$out_list" = "" ]; then
out_list="$pkg_name"
else
out_list="$out_list $pkg_name"
fi
done
IFS="$old_ifs"
echo "$out_list"
return 0
}
#
# _lxc_name_and_ip_get <filename> - retrieve name and ip of the runned ephemeral lxc and
# put them into global vars "lxc_name" and "lxc_ip"
# <filename> file where lxc-start-ephemeral output is saved
#
_lxc_name_and_ip_get()
{
local filename="$1" i e
i=-1
e=-1
for i in $(seq 1 40); do
sleep 2
if grep -q "sudo lxc-console -n $GEM_EPHEM_NAME" $filename 2>&1 ; then
lxc_name="$(grep "sudo lxc-console -n $GEM_EPHEM_NAME" $filename | grep -v '+ echo' | sed "s/.*sudo lxc-console -n \($GEM_EPHEM_NAME\)/\1/g")"
for e in $(seq 1 40); do
sleep 2
if grep -q "$lxc_name" /var/lib/misc/dnsmasq*.leases ; then
lxc_ip="$(grep " $lxc_name " /var/lib/misc/dnsmasq*.leases | tail -n 1 | cut -d ' ' -f 3)"
break
fi
done
break
fi
done
if [ $i -eq 40 -o $e -eq 40 ]; then
return 1
fi
echo "SUCCESSFULLY RUNNED $lxc_name ($lxc_ip)"
return 0
}
deps_check_or_clone () {
local dep="$1" repo="$2" branch="$3"
local local_repo local_branch
if [ -d _jenkins_deps/$dep ]; then
cd _jenkins_deps/$dep
local_repo="$(git remote -v | head -n 1 | sed 's/origin[ ]\+//;s/ .*//g')"
if [ "$local_repo" != "$repo" ]; then
echo "Dependency $dep: cached repository version differs from required ('$local_repo' != '$repo')."
exit 1
fi
local_branch="$(git branch 2>/dev/null | sed -e '/^[^*]/d' | sed -e 's/* \(.*\)/\1/')"
if [ "$local_branch" != "$branch" ]; then
echo "Dependency $dep: cached branch version differs from required ('$local_branch' != '$branch')."
exit 1
fi
git clean -dfx
cd -
else
git clone --depth=1 -b $branch $repo _jenkins_deps/$dep
fi
}
#
# devtest_run <branch> - main function of source test
# <branch> name of the tested branch
#
devtest_run () {
local deps old_ifs branch="$1" branch_cur
if [ ! -d "out_${BUILD_UBUVER}" ]; then
mkdir "out_${BUILD_UBUVER}"
fi
if [ ! -d _jenkins_deps ]; then
mkdir _jenkins_deps
fi
#
# dependencies repos
#
# in test sources different repositories and branches can be tested
# consistently: for each openquake dependency it try to use
# the same repository and the same branch OR the gem repository
# and the same branch OR the gem repository and the "master" branch
#
repo_id="$(repo_id_get)"
if [ "$repo_id" != "$GEM_GIT_REPO" ]; then
repos="git://${repo_id} ${GEM_GIT_REPO}"
else
repos="${GEM_GIT_REPO}"
fi
old_ifs="$IFS"
IFS=" "
for dep in $GEM_GIT_DEPS; do
found=0
branch_cur="$branch"
for repo in $repos; do
# search of same branch in same repo or in GEM_GIT_REPO repo
if git ls-remote --heads $repo/${dep}.git | grep -q "refs/heads/$branch_cur" ; then
deps_check_or_clone "$dep" "$repo/${dep}.git" "$branch_cur"
found=1
break
fi
done
# if not found it fallback in master branch of GEM_GIT_REPO repo
if [ $found -eq 0 ]; then
branch_cur="master"
deps_check_or_clone "$dep" "$repo/${dep}.git" "$branch_cur"
fi
cd _jenkins_deps/$dep
commit="$(git log -1 | grep '^commit' | sed 's/^commit //g')"
cd -
echo "dependency: $dep"
echo "repo: $repo"
echo "branch: $branch_cur"
echo "commit: $commit"
echo
var_pfx="$(dep2var "$dep")"
if [ ! -f _jenkins_deps_info ]; then
touch _jenkins_deps_info
fi
if grep -q "^${var_pfx}_COMMIT=" _jenkins_deps_info; then
if ! grep -q "^${var_pfx}_COMMIT=$commit" _jenkins_deps_info; then
echo "ERROR: $repo -> $branch_cur changed during test:"
echo "before:"
grep "^${var_pfx}_COMMIT=" _jenkins_deps_info
echo "after:"
echo "${var_pfx}_COMMIT=$commit"
exit 1
fi
else
echo "${var_pfx}_COMMIT=$commit" >> _jenkins_deps_info
echo "${var_pfx}_REPO=$repo" >> _jenkins_deps_info
echo "${var_pfx}_BRANCH=$branch_cur" >> _jenkins_deps_info
fi
done
IFS="$old_ifs"
sudo echo
sudo ${GEM_EPHEM_EXE} 2>&1 | tee /tmp/packager.eph.$$.log &
_lxc_name_and_ip_get /tmp/packager.eph.$$.log
_wait_ssh $lxc_ip
set +e
_devtest_innervm_run "$lxc_ip" "$branch"
inner_ret=$?
scp "${lxc_ip}:ssh.log" "out_${BUILD_UBUVER}/devtest.history"
sudo $LXC_TERM -n $lxc_name
# NOTE: pylint returns errors too frequently to consider them a critical event
if pylint --rcfile pylintrc -f parseable openquake > pylint.txt ; then
echo "pylint exits without errors"
else
echo "WARNING: pylint exits with $? value"
fi
set -e
if [ -f /tmp/packager.eph.$$.log ]; then
rm /tmp/packager.eph.$$.log
fi
return $inner_ret
}
builddoc_run () {
local deps old_ifs branch="$1" branch_cur
if [ ! -d "out_${BUILD_UBUVER}" ]; then
mkdir "out_${BUILD_UBUVER}"
fi
if [ ! -d _jenkins_deps ]; then
mkdir _jenkins_deps
fi
#
# dependencies repos
#
# in test sources different repositories and branches can be tested
# consistently: for each openquake dependency it try to use
# the same repository and the same branch OR the gem repository
# and the same branch OR the gem repository and the "master" branch
#
repo_id="$(repo_id_get)"
if [ "$repo_id" != "$GEM_GIT_REPO" ]; then
repos="git://${repo_id} ${GEM_GIT_REPO}"
else
repos="${GEM_GIT_REPO}"
fi
old_ifs="$IFS"
IFS=" "
for dep in $GEM_GIT_DEPS; do
found=0
branch_cur="$branch"
for repo in $repos; do
# search of same branch in same repo or in GEM_GIT_REPO repo
if git ls-remote --heads $repo/${dep}.git | grep -q "refs/heads/$branch_cur" ; then
deps_check_or_clone "$dep" "$repo/${dep}.git" "$branch_cur"
found=1
break
fi
done
# if not found it fallback in master branch of GEM_GIT_REPO repo
if [ $found -eq 0 ]; then
branch_cur="master"
deps_check_or_clone "$dep" "$repo/${dep}.git" "$branch_cur"
fi
cd _jenkins_deps/$dep
commit="$(git log -1 | grep '^commit' | sed 's/^commit //g')"
cd -
echo "dependency: $dep"
echo "repo: $repo"
echo "branch: $branch_cur"
echo "commit: $commit"
echo
var_pfx="$(dep2var "$dep")"
if [ ! -f _jenkins_deps_info ]; then
touch _jenkins_deps_info
fi
if grep -q "^${var_pfx}_COMMIT=" _jenkins_deps_info; then
if ! grep -q "^${var_pfx}_COMMIT=$commit" _jenkins_deps_info; then
echo "ERROR: $repo -> $branch_cur changed during test:"
echo "before:"
grep "^${var_pfx}_COMMIT=" _jenkins_deps_info
echo "after:"
echo "${var_pfx}_COMMIT=$commit"
exit 1
fi
else
echo "${var_pfx}_COMMIT=$commit" >> _jenkins_deps_info
echo "${var_pfx}_REPO=$repo" >> _jenkins_deps_info
echo "${var_pfx}_BRANCH=$branch_cur" >> _jenkins_deps_info
fi
done
IFS="$old_ifs"
sudo echo
sudo ${GEM_EPHEM_EXE} 2>&1 | tee /tmp/packager.eph.$$.log &
_lxc_name_and_ip_get /tmp/packager.eph.$$.log
_wait_ssh $lxc_ip
set +e
_builddoc_innervm_run "$lxc_ip" "$branch"
inner_ret=$?
scp "${lxc_ip}:ssh.log" "out_${BUILD_UBUVER}/builddoc.history"
sudo $LXC_TERM -n $lxc_name
set -e
if [ -f /tmp/packager.eph.$$.log ]; then
rm /tmp/packager.eph.$$.log
fi
return $inner_ret
}
#
# pkgtest_run <branch> - main function of package test
# <branch> name of the tested branch
#
pkgtest_run () {
local i e branch="$1" commit
commit="$(git log --pretty='format:%h' -1)"
if [ ! -d "out_${BUILD_UBUVER}" ]; then
mkdir "out_${BUILD_UBUVER}"
fi
#
# run build of package
if [ -d ${GEM_BUILD_ROOT} ]; then
if [ ! -f ${GEM_BUILD_ROOT}/${GEM_DEB_PACKAGE}_*.deb ]; then
echo "'${GEM_BUILD_ROOT}' directory already exists but .deb file package was not found"
return 1
fi
else
$0 $BUILD_FLAGS
fi
#
# prepare repo and install $GEM_DEB_PACKAGE package
cd ${GEM_BUILD_ROOT}
dpkg-scanpackages . /dev/null >Packages
cat Packages | gzip -9c > Packages.gz
dpkg-scansources . > Sources
cat Sources | gzip > Sources.gz
cat > Release <<EOF
Origin: openquake-${BUILD_UBUVER}
Label: OpenQuake Local Ubuntu Repository
Codename: $BUILD_UBUVER
Date: $(date -R)
Architectures: amd64
Components: main
Description: OpenQuake Local Ubuntu Repository
SHA256:
EOF
printf ' '$(sha256sum Packages | cut --delimiter=' ' --fields=1)' %16d Packages\n' \
$(wc --bytes Packages | cut --delimiter=' ' --fields=1) >> Release
printf ' '$(sha256sum Packages.gz | cut --delimiter=' ' --fields=1)' %16d Packages.gz\n' \
$(wc --bytes Packages.gz | cut --delimiter=' ' --fields=1) >> Release
printf ' '$(sha256sum Sources | cut --delimiter=' ' --fields=1)' %16d Sources\n' \
$(wc --bytes Sources | cut --delimiter=' ' --fields=1) >> Release
printf ' '$(sha256sum Sources.gz | cut --delimiter=' ' --fields=1)' %16d Sources.gz\n' \
$(wc --bytes Sources.gz | cut --delimiter=' ' --fields=1) >> Release
gpg --armor --detach-sign --output Release.gpg Release
cd -
sudo echo
sudo ${GEM_EPHEM_EXE} 2>&1 | tee /tmp/packager.eph.$$.log &
_lxc_name_and_ip_get /tmp/packager.eph.$$.log
_wait_ssh $lxc_ip