5824: Merge branch 'master' into 5824-keep-web-workbench
[arvados-dev.git] / jenkins / run-test-packages-sso.sh
1 #!/bin/bash
2
3 . `dirname "$(readlink -f "$0")"`/run-library.sh
4
5 read -rd "\000" helpmessage <<EOF
6 $(basename $0): Test Arvados SSO package
7
8 Syntax:
9         WORKSPACE=/path/to/arvados-sso $(basename $0) [options]
10
11 Options:
12
13 --debug
14     Output debug information (default: false)
15 --target
16     Distribution to build packages for (default: debian7)
17
18 WORKSPACE=path         Path to the Arvados SSO source tree
19
20 EOF
21
22 EXITCODE=0
23 DEBUG=${ARVADOS_DEBUG:-0}
24 BUILD_BUNDLE_PACKAGES=0
25 TARGET=debian7
26 SUDO=/usr/bin/sudo
27
28 if [[ ! -e "$SUDO" ]]; then
29   SUDO=
30 fi
31
32 PARSEDOPTS=$(getopt --name "$0" --longoptions \
33     help,build-bundle-packages,debug,target: \
34     -- "" "$@")
35 if [ $? -ne 0 ]; then
36     exit 1
37 fi
38
39 eval set -- "$PARSEDOPTS"
40 while [ $# -gt 0 ]; do
41     case "$1" in
42         --help)
43             echo >&2 "$helpmessage"
44             echo >&2
45             exit 1
46             ;;
47         --target)
48             TARGET="$2"; shift
49             ;;
50         --debug)
51             DEBUG=1
52             ;;
53         --build-bundle-packages)
54             BUILD_BUNDLE_PACKAGES=1
55             ;;
56         --)
57             if [ $# -gt 1 ]; then
58                 echo >&2 "$0: unrecognized argument '$2'. Try: $0 --help"
59                 exit 1
60             fi
61             ;;
62     esac
63     shift
64 done
65
66 STDOUT_IF_DEBUG=/dev/null
67 STDERR_IF_DEBUG=/dev/null
68 DASHQ_UNLESS_DEBUG=-q
69 if [[ "$DEBUG" != 0 ]]; then
70     STDOUT_IF_DEBUG=/dev/stdout
71     STDERR_IF_DEBUG=/dev/stderr
72     DASHQ_UNLESS_DEBUG=
73 fi
74
75 case "$TARGET" in
76     debian7)
77         FORMAT=deb
78         ;;
79     debian8)
80         FORMAT=deb
81         ;;
82     ubuntu1204)
83         FORMAT=deb
84         ;;
85     ubuntu1404)
86         FORMAT=deb
87         ;;
88     centos6)
89         FORMAT=rpm
90         ;;
91     *)
92         echo -e "$0: Unknown target '$TARGET'.\n" >&2
93         exit 1
94         ;;
95 esac
96
97 if ! [[ -n "$WORKSPACE" ]]; then
98   echo >&2 "$helpmessage"
99   echo >&2
100   echo >&2 "Error: WORKSPACE environment variable not set"
101   echo >&2
102   exit 1
103 fi
104
105 if ! [[ -d "$WORKSPACE" ]]; then
106   echo >&2 "$helpmessage"
107   echo >&2
108   echo >&2 "Error: $WORKSPACE is not a directory"
109   echo >&2
110   exit 1
111 fi
112
113 title () {
114     txt="********** $1 **********"
115     printf "\n%*s%s\n\n" $((($COLUMNS-${#txt})/2)) "" "$txt"
116 }
117
118 checkexit() {
119     if [[ "$1" != "0" ]]; then
120         title "!!!!!! $2 FAILED !!!!!!"
121     fi
122 }
123
124
125 # Find the SSO server package
126
127 cd "$WORKSPACE"
128
129 SSO_VERSION=$(version_from_git)
130 PACKAGE_NAME=arvados-sso-server
131
132 if [[ "$FORMAT" == "deb" ]]; then
133   PACKAGE_PATH=$WORKSPACE/packages/$TARGET/${PACKAGE_NAME}_${SSO_VERSION}_amd64.deb
134 elif [[ "$FORMAT" == "rpm" ]]; then
135   PACKAGE_PATH=$WORKSPACE/packages/$TARGET/${PACKAGE_NAME}-${SSO_VERSION}-1.x86_64.rpm
136 fi
137
138 # Test 1a: the package to test must exist
139 if [[ ! -f $PACKAGE_PATH ]]; then
140   echo "Latest package not found at $PACKAGE_PATH. Please build the package first."
141   exit 1
142 fi
143
144 if [[ "$FORMAT" == "deb" ]]; then
145   # Test 1b: the system/container where we're running the tests must be clean
146   set +e
147   dpkg -l |grep $PACKAGE_NAME -q
148   if [[ "$?" != "1" ]]; then
149     echo "Please make sure the $PACKAGE_NAME package is not installed before running this script"
150     exit 1
151   fi
152   set -e
153 fi
154
155 if [[ -e "/var/www/arvados-sso" ]]; then
156   echo "Please make sure /var/www/arvados-sso does not exist before running this script"
157   exit 1
158 fi
159
160 # Prepare the machine
161 if [[ "$FORMAT" == "deb" ]]; then
162   $SUDO apt-get update
163 elif [[ "$FORMAT" == "rpm" ]]; then
164   $SUDO yum check-update
165 fi
166 $SUDO mkdir -p /etc/arvados/sso
167
168 if [[ ! -e "/etc/arvados/sso/application.yml" ]]; then
169   RANDOM_PASSWORD=`date | md5sum |cut -f1 -d' '`
170   cp config/application.yml.example /etc/arvados/sso/application.yml
171   sed -i -e 's/uuid_prefix: ~/uuid_prefix: zzzzz/' /etc/arvados/sso/application.yml
172   sed -i -e "s/secret_token: ~/secret_token: $RANDOM_PASSWORD/" /etc/arvados/sso/application.yml
173 fi
174
175 if [[ ! -e "/etc/arvados/sso/database.yml" ]]; then
176   # We haven't installed our dependencies yet, but we need to set up our
177   # database configuration now. Install postgresql if need be.
178   if [[ "$FORMAT" == "deb" ]]; then
179     install_package postgresql
180   elif [[ "$FORMAT" == "rpm" ]]; then
181     install_package postgresql-server
182     # postgres packaging on CentOS6 is kind of primitive, needs an initdb
183     $SUDO service postgresql initdb
184     if [ "$TARGET" = "centos6" ]; then
185       sed -i -e "s/127.0.0.1\/32          ident/127.0.0.1\/32          md5/" /var/lib/pgsql/data/pg_hba.conf
186       sed -i -e "s/::1\/128               ident/::1\/128               md5/" /var/lib/pgsql/data/pg_hba.conf
187     fi
188   fi
189   $SUDO service postgresql start
190
191   RANDOM_PASSWORD=`date | md5sum |cut -f1 -d' '`
192   cat >/etc/arvados/sso/database.yml <<EOF
193 production:
194   adapter: postgresql
195   encoding: utf8
196   database: sso_provider_production
197   username: sso_provider_user
198   password: $RANDOM_PASSWORD
199   host: localhost
200 EOF
201   if [[ "$SUDO" != '' ]]; then
202     $SUDO -u postgres psql -c "CREATE USER sso_provider_user WITH PASSWORD '$RANDOM_PASSWORD'"
203     $SUDO -u postgres createdb sso_provider_production -O sso_provider_user
204   else
205     install_package sudo
206     if [ "$TARGET" = "centos6" ]; then
207       # Work around silly CentOS6 default, cf. https://bugzilla.redhat.com/show_bug.cgi?id=1020147
208       sed -i -e 's/Defaults    requiretty/#Defaults    requiretty/' /etc/sudoers
209     fi
210     /usr/bin/sudo -u postgres psql -c "CREATE USER sso_provider_user WITH PASSWORD '$RANDOM_PASSWORD'"
211     /usr/bin/sudo -u postgres createdb sso_provider_production -O sso_provider_user
212   fi
213 fi
214
215 if [[ "$FORMAT" == "deb" ]]; then
216   # Test 2: the package should install cleanly
217   # In order to get the package dependencies, we need to first do an install
218   # with dpkg that will fail, then run apt-get to install the dependencies,
219   # and the subsequent dpkg installation should succeed.
220   set +e
221   $SUDO dpkg -i $PACKAGE_PATH > /dev/null 2>&1
222   $SUDO apt-get -f install --yes
223   set -e
224   $SUDO dpkg -i $PACKAGE_PATH || EXITCODE=2
225
226   checkexit $EXITCODE "dpkg -i $PACKAGE_PATH"
227
228   # Test 3: the package should remove cleanly
229   $SUDO apt-get remove $PACKAGE_NAME --yes || EXITCODE=3
230
231   checkexit $EXITCODE "apt-get remove $PACKAGE_PATH --yes"
232
233   # Test 4: the package configuration should remove cleanly
234   $SUDO dpkg --purge $PACKAGE_NAME || EXITCODE=4
235
236   checkexit $EXITCODE "dpkg --purge $PACKAGE_PATH"
237
238   if [[ -e "/var/www/arvados-sso" ]]; then
239     EXITCODE=4
240   fi
241
242   checkexit $EXITCODE "leftover items under /var/www/arvados-sso"
243
244   # Test 5: the package should remove cleanly with --purge
245   $SUDO dpkg -i $PACKAGE_PATH || EXITCODE=5
246
247   checkexit $EXITCODE "dpkg -i $PACKAGE_PATH"
248
249   $SUDO apt-get remove $PACKAGE_NAME --purge --yes || EXITCODE=5
250
251   checkexit $EXITCODE "apt-get remove $PACKAGE_PATH --purge --yes"
252
253   if [[ -e "/var/www/arvados-sso" ]]; then
254     EXITCODE=5
255   fi
256
257   checkexit $EXITCODE "leftover items under /var/www/arvados-sso"
258
259 elif [[ "$FORMAT" == "rpm" ]]; then
260
261   # Set up Nginx first
262   # (courtesy of https://www.phusionpassenger.com/library/walkthroughs/deploy/ruby/ownserver/nginx/oss/el6/install_passenger.html)
263   $SUDO yum install -q -y epel-release pygpgme curl
264   $SUDO curl --fail -sSLo /etc/yum.repos.d/passenger.repo https://oss-binaries.phusionpassenger.com/yum/definitions/el-passenger.repo
265   $SUDO yum install -q -y nginx passenger
266   $SUDO sed -i -e 's/^# passenger/passenger/' /etc/nginx/conf.d/passenger.conf
267   # Done setting up Nginx
268
269   # Test 2: the package should install cleanly
270   $SUDO yum -q -y --nogpgcheck localinstall $PACKAGE_PATH || EXITCODE=3
271
272   checkexit $EXITCODE "yum -q -y --nogpgcheck localinstall $PACKAGE_PATH"
273
274   # Test 3: the package should remove cleanly
275   $SUDO yum -q -y remove $PACKAGE_NAME || EXITCODE=3
276
277   checkexit $EXITCODE "yum -q -y remove $PACKAGE_PATH"
278
279   if [[ -e "/var/www/arvados-sso" ]]; then
280     EXITCODE=3
281   fi
282
283   checkexit $EXITCODE "leftover items under /var/www/arvados-sso"
284
285 fi
286
287 if [[ "$EXITCODE" == "0" ]]; then
288   echo "Testing complete, no errors!"
289 else
290   echo "Errors while testing!"
291 fi
292
293 exit $EXITCODE