7710: add crunchrunner to gostuff
[arvados-dev.git] / jenkins / run-build-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): Build Arvados SSO server package
7
8 Syntax:
9         WORKSPACE=/path/to/arvados-sso $(basename $0) [options]
10
11 Options:
12
13 --build-bundle-packages  (default: false)
14     Build package with vendor/bundle included
15 --debug
16     Output debug information (default: false)
17 --target
18     Distribution to build packages for (default: debian7)
19
20 WORKSPACE=path         Path to the Arvados SSO source tree to build packages from
21
22 EOF
23
24 EXITCODE=0
25 DEBUG=${ARVADOS_DEBUG:-0}
26 BUILD_BUNDLE_PACKAGES=0
27 TARGET=debian7
28
29 PARSEDOPTS=$(getopt --name "$0" --longoptions \
30     help,build-bundle-packages,debug,target: \
31     -- "" "$@")
32 if [ $? -ne 0 ]; then
33     exit 1
34 fi
35
36 eval set -- "$PARSEDOPTS"
37 while [ $# -gt 0 ]; do
38     case "$1" in
39         --help)
40             echo >&2 "$helpmessage"
41             echo >&2
42             exit 1
43             ;;
44         --target)
45             TARGET="$2"; shift
46             ;;
47         --debug)
48             DEBUG=1
49             ;;
50         --build-bundle-packages)
51             BUILD_BUNDLE_PACKAGES=1
52             ;;
53         --)
54             if [ $# -gt 1 ]; then
55                 echo >&2 "$0: unrecognized argument '$2'. Try: $0 --help"
56                 exit 1
57             fi
58             ;;
59     esac
60     shift
61 done
62
63 STDOUT_IF_DEBUG=/dev/null
64 STDERR_IF_DEBUG=/dev/null
65 DASHQ_UNLESS_DEBUG=-q
66 if [[ "$DEBUG" != 0 ]]; then
67     STDOUT_IF_DEBUG=/dev/stdout
68     STDERR_IF_DEBUG=/dev/stderr
69     DASHQ_UNLESS_DEBUG=
70 fi
71
72 case "$TARGET" in
73     debian7)
74         FORMAT=deb
75         ;;
76     debian8)
77         FORMAT=deb
78         ;;
79     ubuntu1204)
80         FORMAT=deb
81         ;;
82     ubuntu1404)
83         FORMAT=deb
84         ;;
85     centos6)
86         FORMAT=rpm
87         ;;
88     *)
89         echo -e "$0: Unknown target '$TARGET'.\n" >&2
90         exit 1
91         ;;
92 esac
93
94 if ! [[ -n "$WORKSPACE" ]]; then
95   echo >&2 "$helpmessage"
96   echo >&2
97   echo >&2 "Error: WORKSPACE environment variable not set"
98   echo >&2
99   exit 1
100 fi
101
102 if ! [[ -d "$WORKSPACE" ]]; then
103   echo >&2 "$helpmessage"
104   echo >&2
105   echo >&2 "Error: $WORKSPACE is not a directory"
106   echo >&2
107   exit 1
108 fi
109
110 # Test for fpm
111 fpm --version >/dev/null 2>&1
112
113 if [[ "$?" != 0 ]]; then
114   echo >&2 "$helpmessage"
115   echo >&2
116   echo >&2 "Error: fpm not found"
117   echo >&2
118   exit 1
119 fi
120
121 RUN_BUILD_PACKAGES_PATH="`dirname \"$0\"`"
122 RUN_BUILD_PACKAGES_PATH="`( cd \"$RUN_BUILD_PACKAGES_PATH\" && pwd )`"  # absolutized and normalized
123 if [ -z "$RUN_BUILD_PACKAGES_PATH" ] ; then
124   # error; for some reason, the path is not accessible
125   # to the script (e.g. permissions re-evaled after suid)
126   exit 1  # fail
127 fi
128
129 debug_echo "$0 is running from $RUN_BUILD_PACKAGES_PATH"
130 debug_echo "Workspace is $WORKSPACE"
131
132 if [[ -f /etc/profile.d/rvm.sh ]]; then
133     source /etc/profile.d/rvm.sh
134     GEM="rvm-exec default gem"
135 else
136     GEM=gem
137 fi
138
139 if [[ "$TARGET" == "centos6" ]]; then
140   # CentOS6 comes with git 1.7.1, but we want at least 1.7.6
141   # because we use git status --ignore in fpm-info.sh
142   cd /usr/src
143   install_package libcurl-devel zlib-devel wget gettext
144   wget https://www.kernel.org/pub/software/scm/git/git-1.8.5.6.tar.gz
145   tar xzf git-1.8.5.6.tar.gz
146   cd git-1.8.5.6
147   make configure
148   ./configure --prefix=/usr --without-tcltk
149   make all
150   make install
151 fi
152
153 # Make all files world-readable -- jenkins runs with umask 027, and has checked
154 # out our git tree here
155 chmod o+r "$WORKSPACE" -R
156
157 # More cleanup - make sure all executables that we'll package are 755
158 # No executables in the sso server package
159 #find -type d -name 'bin' |xargs -I {} find {} -type f |xargs -I {} chmod 755 {}
160
161 # Now fix our umask to something better suited to building and publishing
162 # gems and packages
163 umask 0022
164
165 debug_echo "umask is" `umask`
166
167 if [[ ! -d "$WORKSPACE/packages/$TARGET" ]]; then
168   mkdir -p $WORKSPACE/packages/$TARGET
169 fi
170
171 # Build the SSO server package
172
173 cd "$WORKSPACE"
174
175 SSO_VERSION=$(version_from_git)
176 PACKAGE_NAME=arvados-sso-server
177
178 if [[ ! -d "$WORKSPACE/tmp" ]]; then
179   mkdir $WORKSPACE/tmp
180 fi
181
182 if [[ "$BUILD_BUNDLE_PACKAGES" != 0 ]]; then
183   bundle install --path vendor/bundle >"$STDOUT_IF_DEBUG"
184 fi
185
186 /usr/bin/git rev-parse HEAD > git-commit.version
187
188 cd $WORKSPACE/packages/$TARGET
189
190 # Annoyingly, we require a database.yml file for rake assets:precompile to work.
191
192 # TODO: add bogus database.yml file so we can precompile the assets and put them in the
193 # package. Then remove that database.yml file again. It has to be a valid file though.
194 #RAILS_ENV=production RAILS_GROUPS=assets bundle exec rake assets:precompile
195
196 # There are just 2 excludes left here, all the others are pulled in via fpm-info.sh, which
197 # takes .gitignore into account via a call to git status:
198 #
199 # 1. The .git directory is excluded by git implicitly, so we can't pick it up from .gitignore.
200 # 2. The packages directory needs to be explictly excluded here because it will only be listed
201 # if it exists at the time fpm-info.sh runs. If it does not exist at that time, this script
202 # will create it and when fpm runs, it will include the directory. So we add it to the exclude
203 # list explicitly here, just in case.
204 declare -a COMMAND_ARR=("fpm" "--maintainer=Ward Vandewege <ward@curoverse.com>" "--vendor='Curoverse, Inc.'" "--url='https://arvados.org'" "--description='Arvados SSO server - Arvados is a free and open source platform for big data science.'" "--license='Expat License'" "-s" "dir" "-t" "$FORMAT" "-v" "$SSO_VERSION" "-x" "var/www/arvados-sso/current/.git" "-x" "var/www/arvados-sso/current/packages" "--after-install=$RUN_BUILD_PACKAGES_PATH/arvados-sso-server-extras/arvados-sso-server.postinst" "--before-remove=$RUN_BUILD_PACKAGES_PATH/arvados-sso-server-extras/arvados-sso-server.prerm" "--after-remove=$RUN_BUILD_PACKAGES_PATH/arvados-sso-server-extras/arvados-sso-server.postrm" )
205
206 if [[ "$BUILD_BUNDLE_PACKAGES" != 0 ]]; then
207   # This is the complete package with vendor/bundle included.
208   # It's big, so we do not build it by default.
209   COMMAND_ARR+=("-n" "${PACKAGE_NAME}-with-bundle")
210 else
211   # The default package excludes vendor/bundle
212   COMMAND_ARR+=("-n" "${PACKAGE_NAME}" "-x" "var/www/arvados-sso/current/vendor/bundle")
213 fi
214
215 # Append --depends X and other arguments specified by fpm-info.sh in
216 # the package source dir. These are added last so they can override
217 # the arguments added by this script.
218 declare -a fpm_args=()
219 declare -a fpm_depends=()
220 FPM_INFO="$WORKSPACE/fpm-info.sh"
221 if [[ -e "$FPM_INFO" ]]; then
222   debug_echo "Loading fpm overrides from $FPM_INFO"
223   source "$FPM_INFO"
224 fi
225
226 for i in "${fpm_depends[@]}"; do
227   COMMAND_ARR+=('--depends' "$i")
228 done
229 COMMAND_ARR+=("${fpm_args[@]}")
230 COMMAND_ARR+=("$WORKSPACE/=/var/www/arvados-sso/current")
231 debug_echo -e "\n${COMMAND_ARR[@]}\n"
232
233 FPM_RESULTS=$("${COMMAND_ARR[@]}")
234 FPM_EXIT_CODE=$?
235
236 fpm_verify $FPM_EXIT_CODE $FPM_RESULTS
237
238 # SSO server package build done
239
240 exit $EXITCODE