Add build script for the new Arvados SSO server package.
[arvados.git] / jenkins / run-build-packages-sso.sh
1 #!/bin/bash
2
3
4 read -rd "\000" helpmessage <<EOF
5 $(basename $0): Build Arvados SSO package
6
7 Syntax:
8         WORKSPACE=/path/to/arvados-sso $(basename $0) [options]
9
10 Options:
11
12 --build-bundle-packages  (default: false)
13     Build package with vendor/bundle included
14 --debug
15     Output debug information (default: false)
16 --target
17     Distribution to build packages for (default: debian7)
18
19 WORKSPACE=path         Path to the Arvados SSO source tree to build packages from
20
21 EOF
22
23 EXITCODE=0
24 DEBUG=${ARVADOS_DEBUG:-0}
25 BUILD_BUNDLE_PACKAGES=0
26 TARGET=debian7
27
28 PARSEDOPTS=$(getopt --name "$0" --longoptions \
29     help,build-bundle-packages,debug,target: \
30     -- "" "$@")
31 if [ $? -ne 0 ]; then
32     exit 1
33 fi
34
35 eval set -- "$PARSEDOPTS"
36 while [ $# -gt 0 ]; do
37     case "$1" in
38         --help)
39             echo >&2 "$helpmessage"
40             echo >&2
41             exit 1
42             ;;
43         --target)
44             TARGET="$2"; shift
45             ;;
46         --debug)
47             DEBUG=1
48             ;;
49         --build-bundle-packages)
50             BUILD_BUNDLE_PACKAGES=1
51             ;;
52         --)
53             if [ $# -gt 1 ]; then
54                 echo >&2 "$0: unrecognized argument '$2'. Try: $0 --help"
55                 exit 1
56             fi
57             ;;
58     esac
59     shift
60 done
61
62 STDOUT_IF_DEBUG=/dev/null
63 STDERR_IF_DEBUG=/dev/null
64 DASHQ_UNLESS_DEBUG=-q
65 if [[ "$DEBUG" != 0 ]]; then
66     STDOUT_IF_DEBUG=/dev/stdout
67     STDERR_IF_DEBUG=/dev/stderr
68     DASHQ_UNLESS_DEBUG=
69 fi
70
71 debug_echo () {
72     echo "$@" >"$STDOUT_IF_DEBUG"
73 }
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
98 if ! [[ -n "$WORKSPACE" ]]; then
99   echo >&2 "$helpmessage"
100   echo >&2
101   echo >&2 "Error: WORKSPACE environment variable not set"
102   echo >&2
103   exit 1
104 fi
105
106 if ! [[ -d "$WORKSPACE" ]]; then
107   echo >&2 "$helpmessage"
108   echo >&2
109   echo >&2 "Error: $WORKSPACE is not a directory"
110   echo >&2
111   exit 1
112 fi
113
114 # Test for fpm
115 fpm --version >/dev/null 2>&1
116
117 if [[ "$?" != 0 ]]; then
118   echo >&2 "$helpmessage"
119   echo >&2
120   echo >&2 "Error: fpm not found"
121   echo >&2
122   exit 1
123 fi
124
125 RUN_BUILD_PACKAGES_PATH="`dirname \"$0\"`"
126 RUN_BUILD_PACKAGES_PATH="`( cd \"$RUN_BUILD_PACKAGES_PATH\" && pwd )`"  # absolutized and normalized
127 if [ -z "$RUN_BUILD_PACKAGES_PATH" ] ; then
128   # error; for some reason, the path is not accessible
129   # to the script (e.g. permissions re-evaled after suid)
130   exit 1  # fail
131 fi
132
133 debug_echo "$0 is running from $RUN_BUILD_PACKAGES_PATH"
134 debug_echo "Workspace is $WORKSPACE"
135
136 format_last_commit_here() {
137     local format=$1; shift
138     TZ=UTC git log -n1 --first-parent "--format=format:$format" .
139 }
140
141 version_from_git() {
142   # Generates a version number from the git log for the current working
143   # directory, and writes it to stdout.
144   local git_ts git_hash
145   declare $(format_last_commit_here "git_ts=%ct git_hash=%h")
146   echo "0.1.$(date -ud "@$git_ts" +%Y%m%d%H%M%S).$git_hash"
147 }
148
149 nohash_version_from_git() {
150     version_from_git | cut -d. -f1-3
151 }
152
153 timestamp_from_git() {
154     format_last_commit_here "%ct"
155 }
156
157 # verify build results
158 fpm_verify () {
159   FPM_EXIT_CODE=$1
160   shift
161   FPM_RESULTS=$@
162
163   FPM_PACKAGE_NAME=''
164   if [[ $FPM_RESULTS =~ ([A-Za-z0-9_\.-]*\.)(deb|rpm) ]]; then
165     FPM_PACKAGE_NAME=${BASH_REMATCH[1]}${BASH_REMATCH[2]}
166   fi
167
168   if [[ "$FPM_PACKAGE_NAME" == "" ]]; then
169     EXITCODE=1
170     echo "Error: $PACKAGE: Unable to figure out package name from fpm results:"
171     echo
172     echo $FPM_RESULTS
173     echo
174   elif [[ "$FPM_RESULTS" =~ "File already exists" ]]; then
175     echo "Package $FPM_PACKAGE_NAME exists, not rebuilding"
176   elif [[ 0 -ne "$FPM_EXIT_CODE" ]]; then
177     echo "Error building package for $1:\n $FPM_RESULTS"
178   fi
179 }
180
181 if [[ -f /etc/profile.d/rvm.sh ]]; then
182     source /etc/profile.d/rvm.sh
183     GEM="rvm-exec default gem"
184 else
185     GEM=gem
186 fi
187
188 # Make all files world-readable -- jenkins runs with umask 027, and has checked
189 # out our git tree here
190 chmod o+r "$WORKSPACE" -R
191
192 # More cleanup - make sure all executables that we'll package are 755
193 # No executables in the sso server package
194 #find -type d -name 'bin' |xargs -I {} find {} -type f |xargs -I {} chmod 755 {}
195
196 # Now fix our umask to something better suited to building and publishing
197 # gems and packages
198 umask 0022
199
200 debug_echo "umask is" `umask`
201
202 if [[ ! -d "$WORKSPACE/packages/$TARGET" ]]; then
203   mkdir -p $WORKSPACE/packages/$TARGET
204 fi
205
206 # Build the SSO server package
207
208 cd "$WORKSPACE"
209
210 SSO_VERSION=$(version_from_git)
211 PACKAGE_NAME=arvados-sso
212
213 if [[ ! -d "$WORKSPACE/tmp" ]]; then
214   mkdir $WORKSPACE/tmp
215 fi
216
217 if [[ "$BUILD_BUNDLE_PACKAGES" != 0 ]]; then
218   bundle install --path vendor/bundle >"$STDOUT_IF_DEBUG"
219 fi
220
221 /usr/bin/git rev-parse HEAD > git-commit.version
222
223 cd $WORKSPACE/packages/$TARGET
224
225 # Annoyingly, we require a database.yml file for rake assets:precompile to work. So for now,
226 # we do that in the upgrade script.
227 # TODO: add bogus database.yml file so we can precompile the assets and put them in the
228 # package. Then remove that database.yml file again. It has to be a valid file though.
229 #RAILS_ENV=production RAILS_GROUPS=assets bundle exec rake assets:precompile
230
231 # Append --depends X and other arguments specified by fpm-info.sh in
232 # the package source dir. These are added last so they can override
233 # the arguments added by this script.
234 declare -a fpm_args=()
235 declare -a fpm_depends=()
236 FPM_INFO="$WORKSPACE/fpm-info.sh"
237 if [[ -e "$FPM_INFO" ]]; then
238   debug_echo "Loading fpm overrides from $FPM_INFO"
239   source "$FPM_INFO"
240 fi
241
242 # This is the complete package with vendor/bundle included.
243 # It's big, so we do not build it by default.
244 if [[ "$BUILD_BUNDLE_PACKAGES" != 0 ]]; then
245   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" "-n" "${PACKAGE_NAME}-with-bundle" "-v" "$SSO_VERSION" "-x" "var/www/arvados-sso/current/.git" "-x" "var/www/arvados-sso/current/tmp" "-x" "var/www/arvados-sso/current/log" "-x" "var/www/arvados-sso/current/vendor/cache/*" "-x" "var/www/arvados-sso/current/packages" "--after-install=$RUN_BUILD_PACKAGES_PATH/arvados-sso-server-extras/postinst.sh")
246
247   for i in "${fpm_depends[@]}"; do
248     COMMAND_ARR+=('--depends' "$i")
249   done
250   COMMAND_ARR+=("${fpm_args[@]}")
251   COMMAND_ARR+=("$WORKSPACE/=/var/www/arvados-sso/current" "$RUN_BUILD_PACKAGES_PATH/arvados-sso-server-extras/arvados-sso-server-upgrade.sh=/usr/local/bin/arvados-sso-server-upgrade.sh")
252
253   debug_echo -e "\n${COMMAND_ARR[@]}\n"
254
255   FPM_RESULTS=$("${COMMAND_ARR[@]}")
256   FPM_EXIT_CODE=$?
257   fpm_verify $FPM_EXIT_CODE $FPM_RESULTS
258 fi
259
260 # Build the 'bare' package without vendor/bundle.
261 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" "-n" "${PACKAGE_NAME}" "-v" "$SSO_VERSION" "-x" "var/www/arvados-sso/current/.git" "-x" "var/www/arvados-sso/current/tmp" "-x" "var/www/arvados-sso/current/log" "-x" "var/www/arvados-sso/current/vendor/bundle" "-x" "var/www/arvados-sso/current/vendor/cache/*" "-x" "var/www/arvados-sso/current/packages" "--after-install=$RUN_BUILD_PACKAGES_PATH/arvados-sso-server-extras/postinst.sh")
262
263
264 for i in "${fpm_depends[@]}"; do
265   COMMAND_ARR+=('--depends' "$i")
266 done
267 COMMAND_ARR+=("${fpm_args[@]}")
268 COMMAND_ARR+=("$WORKSPACE/=/var/www/arvados-sso/current" "$RUN_BUILD_PACKAGES_PATH/arvados-sso-server-extras/arvados-sso-server-upgrade.sh=/usr/local/bin/arvados-sso-server-upgrade.sh")
269 debug_echo -e "\n${COMMAND_ARR[@]}\n"
270
271 FPM_RESULTS=$("${COMMAND_ARR[@]}")
272 FPM_EXIT_CODE=$?
273 fpm_verify $FPM_EXIT_CODE $FPM_RESULTS
274
275 # SSO server package build done
276
277 exit $EXITCODE