]> git.evergreen-ils.org Git - Evergreen.git/blob - build/tools/make_release
LP2061136 - Stamping 1405 DB upgrade script
[Evergreen.git] / build / tools / make_release
1 #!/bin/bash
2
3 GIT_ABS=`git rev-parse --show-toplevel`
4 RELEASE_DIR=$GIT_ABS/../release/
5 GIT_BRANCH=`git rev-parse --abbrev-ref HEAD | sed 's|.*/||'`
6 HEADURLBASE="http://git.evergreen-ils.org/?p=Evergreen.git;a=shortlog;h=refs/heads/tags/"
7 HEADURL="$HEADURLBASE$GIT_BRANCH"
8
9 # Drop to the root of the checkout
10 cd $GIT_ABS
11
12 VERSION=AUTO # -v (version)
13 PREV_BRANCH=AUTO # -f (from)
14 PREV_VERSION=AUTO # -F (from version)
15 NO_UPGRADE=AUTO # -n
16 IS_PREVIEW=AUTO # -p
17 TAG_ONLY=NO # -t
18 BUILD_ONLY=NO # -b
19 UPGRADE_PREVIEW=NO # -r
20 SKIP_I18N=NO # -i
21 SKIP_BUILD_BROWSER_CLIENT=NO # -C
22 PG_HOST=NONE # -H
23 PG_USER=NONE # -U
24 PG_PASS=NONE # -P
25 PG_PORT=NONE # -O
26
27 # path to OpenSRF libraries
28 [ "$(which osrf_config)" ] && OSRF_JS_PATH="$(osrf_config --libdir)/javascript";
29
30
31 while getopts ":hv:f:F:nptbrij:CH:D:U:P:O:" opt; do
32     case $opt in
33         v)
34             VERSION=$OPTARG
35         ;;
36         f)
37             PREV_BRANCH=$OPTARG
38         ;;
39         F)
40             PREV_VERSION=$OPTARG
41         ;;
42         n)
43             NO_UPGRADE=YES
44         ;;
45         r)
46             UPGRADE_PREVIEW=YES
47         ;;
48         i)
49             SKIP_I18N=YES
50         ;;
51         p)
52             IS_PREVIEW=YES
53         ;;
54         t)
55             TAG_ONLY=YES
56         ;;
57         b)
58             BUILD_ONLY=YES
59         ;;
60
61         j)
62             OSRF_JS_PATH="$OPTARG"
63         ;;
64         C)
65             SKIP_BUILD_BROWSER_CLIENT=YES
66         ;;
67         H)
68             PG_HOST="$OPTARG"
69         ;;
70         U)
71             PG_USER="$OPTARG"
72         ;;
73         P)
74             PG_PASS="$OPTARG"
75         ;;
76         O)
77             PG_PORT="$OPTARG"
78         ;;
79         \?)
80             echo "Invalid Option: -$OPTARG"
81             exit 1
82         ;;
83         :)
84             echo "-$OPTARG requires an argument."
85             exit 1
86         ;;
87         h)
88             echo "$0 [-v VERSION] [-f PREV_BRANCH | -t | -b] [-F PREV_VERSION] [-n] [-p]"
89             echo "   VERSION is auto-detected by default and is based on the currently checked out branch."
90             echo "   PREV_BRANCH is auto-detected by default but that isn't reliable. Include remote name!"
91             echo "   PREV_VERSION Is auto-detected by default and is based on the PREV_BRANCH's name."
92             echo "   -n specifies that you don't want an upgrade script to be auto-generated."
93             echo "   -p specifies that this is a preview build."
94             echo "   -t turns on tag only mode."
95             echo "   -b turns on build only mode."
96             echo "   -r prompt to preview upgrade SQL in editor before committing."
97             echo "   -i skip i18n; primarily useful for (quickly) testing this script."
98             echo "   -j opensrf javascript library path.  If osrf_config is found, the value derived from osrf_config --libdir."
99             echo "   -C Skip building the browser client. Useful only if all you want to do is generate the DB update script."
100             echo "   -H Postgres [H]ostname (optional)"
101             echo "   -D Postgres [D]atabase name (optional)"
102             echo "   -U Postgres [U]sername (optional)"
103             echo "   -P Postgres [P]assword (optional)"
104             echo "   -O Postgres p[O]rt"
105             echo ""
106             echo "   Postgres credentials are used to invoke the EXPERIMENTAL"
107             echo "   feature to update the Enhanced Concerto dataset."
108             echo ""
109             echo "   The provided Pg credentials needs to have permissions to create"
110             echo "   databases; using a Pg superuser will be most convenient."
111             echo ""
112             echo "   The commit created by the process to update the dataset"
113             echo "   should be thoroughly reviewed before producing the release."
114             echo ""
115             echo "   Generally, the update of the Enhanced Concerto dataset should"
116             echo "   be done and tested well before the release tarball is built; the main"
117             echo "   dependency is when the database schema becomes stable for the"
118             echo "   release, particularly changes that add or remove tables or"
119             echo "   columns."
120             echo ""
121             echo "   NOTE: -t and -b override PREV_BRANCH/PREV_VERSION, but -b overrides -t."
122             exit -1
123         ;;
124     esac
125 done
126
127 if [ -z "$OSRF_JS_PATH" ]; then
128     echo "Unable to find OpenSRF JavaScript library path.  Specify with -j";
129     exit 1;
130 fi;
131
132 if [ "$TAG_ONLY" == "YES" ]; then
133     PREV_BRANCH="TAG"
134 fi
135
136 if [ "$BUILD_ONLY" == "YES" ]; then
137     PREV_BRANCH="PACKAGE"
138 fi
139
140 if [ "$VERSION" == "AUTO" ]; then
141     # Auto-pick version based on branch name
142     echo AUTO VERSION
143     VERSION=`echo $GIT_BRANCH | sed 's/^rel_\([0-9]\+\)_\([0-9]\+\)_\([0-9]\+\)_\(.\+\)$/\1.\2.\3-\4/'`
144     VERSION=`echo $VERSION | sed 's/^rel_\([0-9]\+\)_\([0-9]\+\)_\([0-9]\+\)$/\1.\2.\3/'`
145     VERSION=`echo $VERSION | sed 's/^rel_\([0-9]\+\)_\([0-9]\+\)_\(.\+\)$/\1.\2-\3/'`
146     VERSION=`echo $VERSION | sed 's/^rel_\([0-9]\+\)_\([0-9]\+\)$/\1.\2/'`
147     if [ "$VERSION" = "$GIT_BRANCH" ]; then
148         echo "AUTO VERSION FAILED."
149         exit 1
150     fi
151 fi
152 SHORT_VERSION=`echo $VERSION | grep -o '^[0-9]\+\.[0-9]\+'`
153 echo "Version: $VERSION Short: $SHORT_VERSION"
154
155 # prep a couple alternate represenations
156 DASH_VERSION=`echo $VERSION | sed 's/\./-/g'`
157 SHORT_DASH_VERSION=`echo $SHORT_VERSION | sed 's/\./-/g'`
158 UNDER_VERSION=`echo $VERSION | sed -e 's/\./_/g;s/-/_/g'`
159
160 PREVIEW_TEXT=""
161
162 if [ "$IS_PREVIEW" == "YES" ]; then
163     PREVIEW_TEXT="previews/"
164 fi
165
166 # Release Preamble
167 # For adding into README for release builds
168 # The head is used to check if we have it already (no need to add it twice)
169 RELEASE_PREAMBLE_HEAD="Preamble: Getting an Evergreen official release tarball"
170 RELEASE_PREAMBLE=$( cat <<PREAMBLE
171 $RELEASE_PREAMBLE_HEAD
172 -------------------------------------------------------
173
174 To download and extract the source for the current release of Evergreen, issue
175 the following commands as the *user* Linux account:
176
177 [source, bash]
178 ------------------------------------------------------------------------------
179 wget -c http://evergreen-ils.org/downloads/${PREVIEW_TEXT}Evergreen-ILS-$VERSION.tar.gz
180 tar xzf Evergreen-ILS-$VERSION.tar.gz
181 ------------------------------------------------------------------------------
182
183 PREAMBLE
184 )
185
186 # This defines what the preamble comes before
187 RELEASE_PREAMBLE_BEFORE="Preamble: Developer instructions"
188
189 if [ "$PREV_BRANCH" != "TAG" -a "$PREV_BRANCH" != "PACKAGE" ]; then
190     if [ "$PREV_BRANCH" == "AUTO" ]; then
191         echo "AUTO PREVIOUS BRANCH"
192         PREV_BRANCH=`echo ${UNDER_VERSION%_*}`
193         PREV_BRANCH=`git branch -r | grep "rel_${PREV_BRANCH}_[^_]\+$" | sort -rV | head -n1`
194         PREV_BRANCH=`echo $PREV_BRANCH`
195         read -p "Does branch $PREV_BRANCH look like the previous version (y/n)?"
196         if [ "$REPLY" != "y" -a "$REPLY" != 'Y' ]; then
197             echo "Please specify the previous branch as second parameter. To continue auto-version, use AUTO as first parameter."
198             exit 1
199         fi
200     fi
201     git show $PREV_BRANCH &>/dev/null
202     if [ $? -ne 0 -o -z "$PREV_BRANCH" ]; then
203         echo "PREVIOUS VERSION COMMIT NOT FOUND";
204         exit 1
205     fi
206     if [ "$PREV_VERSION" == "AUTO" ]; then
207         echo "AUTO PREVIOUS VERSION"
208         PREV_BRANCH_END=`echo $PREV_BRANCH | sed 's|.*/||'`
209         PREV_VERSION=`echo $PREV_BRANCH_END | sed 's/^rel_\([0-9]\+\)_\([0-9]\+\)_\([0-9]\+\)_\(.\+\)$/\1.\2.\3-\4/'`
210         PREV_VERSION=`echo $PREV_VERSION | sed 's/^rel_\([0-9]\+\)_\([0-9]\+\)_\([0-9]\+\)$/\1.\2.\3/'`
211         PREV_VERSION=`echo $PREV_VERSION | sed 's/^rel_\([0-9]\+\)_\([0-9]\+\)_\(.\+\)$/\1.\2-\3/'`
212         PREV_VERSION=`echo $PREV_VERSION | sed 's/^rel_\([0-9]\+\)_\([0-9]\+\)$/\1.\2/'`
213         if [ "$PREV_VERSION" == "$PREV_BRANCH_END" ]; then
214             echo "AUTO PREVIOUS VERSION FAILED."
215             exit 1
216         fi
217     fi
218     echo "Previous Version: $PREV_VERSION"
219 else
220     echo "Tagging or packaging, no need for previous version"
221 fi
222
223 if [ "$PREV_BRANCH" != "PACKAGE" ]; then
224
225     echo "Applying to Application.pm - HEAD -> $DASH_VERSION"
226     echo "Alt: $SHORT_DASH_VERSION -> $DASH_VERSION"
227     sed -i -e "s/\"$SHORT_DASH_VERSION[^\"]*\"/\"$DASH_VERSION\"/" -e "s/\"HEAD\"/\"$DASH_VERSION\"/" $GIT_ABS/Open-ILS/src/perlmods/lib/OpenILS/Application.pm
228
229     echo "Applying to README:"
230
231     if [ "$PREV_BRANCH" != "TAG" ]; then
232         if [ "$(grep "$RELEASE_PREAMBLE_HEAD" $GIT_ABS/README )" ]; then
233             echo "Updating old download links"
234             sed -i -e "s|\(previews/\)\?Evergreen-ILS-.*\.tar\.gz|${PREVIEW_TEXT}Evergreen-ILS-$VERSION.tar.gz|" $GIT_ABS/README
235             sed -i -e "s| Evergreen-ILS-.*\.tar\.gz| Evergreen-ILS-$VERSION.tar.gz|" $GIT_ABS/README
236         else
237             echo "Adding Download Preamble"
238             perl -pi -e "s|^|$RELEASE_PREAMBLE\n\n| if /$RELEASE_PREAMBLE_BEFORE/" $GIT_ABS/README
239         fi
240     fi
241
242     echo "Applying to configure.ac:"
243     echo "AC_INIT and AM_INIT_AUTOMAKE entries"
244     sed -i -e "s/^\(AC_INIT(Open-ILS, \)[^,]*,/\1$VERSION,/" -e "s/^\(AM_INIT_AUTOMAKE(\[[^]]*], \[\)[^]]*]/\1$VERSION]/" configure.ac
245
246     echo "Finding/updating old \$HeadURL\$ entries"
247     HEADURLBASE=`echo ${HEADURLBASE} | sed 's/\?/\\?/'`
248     for file in `grep -Rl --exclude=make_release "$HEADURLBASE" $GIT_ABS`
249     do
250         echo $file
251         sed -i "s|${HEADURLBASE}[A-Za-z0-9_]*|$HEADURL|" $file
252     done
253
254     echo "Applying \$HeadURL\$ - $HEADURL"
255     for file in `grep -Rl --exclude=make_release '\\$HeadURL\\$' $GIT_ABS`
256     do
257         echo $file
258         sed -i "s|\\\$HeadURL\\\$|$HEADURL|" $file
259     done
260
261     if [ "$PREV_BRANCH" = "TAG" ]; then
262         echo "Committing (but not pushing) the lot of it"
263         git commit -asm "Tagging version"
264         exit 0;
265     fi
266
267     echo "Applying version to 002.schema.config.sql:"
268     echo "config.upgrade_log entry for $VERSION"
269     sed -i -e "s/\(INSERT INTO config.upgrade_log (version[^)]*) VALUES ('\)[0-9]*\('.*;\).*/&\n\1$VERSION\2/" $GIT_ABS/Open-ILS/src/sql/Pg/002.schema.config.sql;
270
271     if [ "$NO_UPGRADE" == "AUTO" ]; then
272         echo "Checking for DB upgrade potential...."
273         git ls-tree --name-only $PREV_BRANCH -- Open-ILS/src/sql/Pg/upgrade/ | cut -d/ -f6 | cut -d. -f1 | sort > old_upgrades.txt
274         git ls-tree --name-only HEAD -- Open-ILS/src/sql/Pg/upgrade/ | cut -d/ -f6 | cut -d. -f1 | sort > new_upgrades.txt
275         UPGRADE_CHECK=`diff old_upgrades.txt new_upgrades.txt | grep '^>' | cut -d' ' -f2`
276         UPGRADE_FILE=Open-ILS/src/sql/Pg/version-upgrade/$PREV_VERSION-$VERSION-upgrade-db.sql
277         if [ -f "$UPGRADE_FILE" ]; then
278             echo "Upgrade script for $PREV_VERSION-$VERSION already exists. Skipping."
279             UPGRADE_FILE=""
280         else
281             echo "Building Upgrade Script."
282             echo "--Upgrade Script for $PREV_VERSION to $VERSION" > $UPGRADE_FILE
283             echo "\set eg_version '''$VERSION'''" >> $UPGRADE_FILE
284             echo "BEGIN;" >> $UPGRADE_FILE
285             grep "config.upgrade_log.*$VERSION" Open-ILS/src/sql/Pg/002.schema.config.sql >> $UPGRADE_FILE
286             for NUM in $UPGRADE_CHECK; do
287                 cat Open-ILS/src/sql/Pg/upgrade/$NUM.* 2>/dev/null | grep -v '^\s*\(BEGIN\|COMMIT\);\s*$' >> $UPGRADE_FILE
288             done;
289             echo "COMMIT;" >> $UPGRADE_FILE
290             #Add call to auditor.update_auditors() to keep auditor tables up to date
291             echo "" >> $UPGRADE_FILE
292             echo "-- Update auditor tables to catch changes to source tables." >> $UPGRADE_FILE
293             echo "--   Can be removed/skipped if there were no schema changes." >> $UPGRADE_FILE
294             echo "SELECT auditor.update_auditors();" >> $UPGRADE_FILE
295             MAYBE_DUPES=`grep -oP 'CREATE (OR REPLACE )?FUNCTION +\K[^ ]*(?= *\()' $UPGRADE_FILE | sort | grep -P '^(.*)\n\1$' | sort -u`
296             if [ -n "$MAYBE_DUPES" ]; then
297                 echo ""
298                 echo "The following functions may be needlessly duplicated in the upgrade script:"
299                 echo "$MAYBE_DUPES"
300                 echo ""
301                 echo "For reference, I am writing the list to maybe_dupes.txt"
302                 echo "$MAYBE_DUPES" > maybe_dupes.txt
303             fi
304             echo ""
305             if [ "$UPGRADE_PREVIEW" = "YES" ]; then
306                 read -p "Please manually check the upgrade file."
307                 ${EDITOR:-vi} $UPGRADE_FILE
308             fi;
309             git add $UPGRADE_FILE
310         fi
311     fi
312
313     grep -i -m 1 Signed-off-by ChangeLog &> /dev/null
314     if [ $? -ne 0 ]; then
315         echo "Building ChangeLog"
316         git log --cherry-pick --right-only --no-merges --pretty --summary --numstat $PREV_BRANCH...HEAD > $GIT_ABS/ChangeLog
317     else
318         echo "Not overwriting existing ChangeLog!"
319     fi
320
321     echo "Committing (but not pushing) the lot of it"
322     COMMIT_MESSAGE="Bumping version numbers and adding Changelog"
323     if [ -n "$UPGRADE_FILE" ]; then
324         COMMIT_MESSAGE="Bumping version numbers, adding Upgrade Script and Changelog"
325     fi
326     git commit -asm "$COMMIT_MESSAGE"
327     if [ "$PG_HOST" != "NONE" ]; then
328         echo "Attempting auto-upgrade for Enhanced Concerto Dataset"
329         $GIT_ABS/Open-ILS/src/support-scripts/make_concerto_from_evergreen_db.pl \
330         --db-host "$PG_HOST" --db-user "$PG_USER" --db-pass "$PG_PASS" --db-port "$PG_PORT" \
331         --output-folder "$GIT_ABS/Open-ILS/tests/datasets/sql/concerto_enhanced" \
332         --evergreen-repo "$GIT_ABS" \
333         --perform-upgrade \
334         --non-interactive
335
336         if [ $? -ne 0 ]; then
337             echo "Failed to auto-upgrade the Enhanced Concerto Dataset"
338             echo "Proceeding without it"
339             read -p "Press [Enter] key to continue"
340         else
341             echo "Committing Enhanced Concerto Dataset to git"
342             COMMIT_MESSAGE="Upgrade Enhanced Concerto Dataset"
343             git commit -asm "$COMMIT_MESSAGE"
344         fi
345     fi
346 fi
347
348 echo "Building release dump"
349 cd $GIT_ABS
350 mkdir -p ../release
351 git archive --prefix=Evergreen-ILS-$VERSION/ HEAD | (cd ../release && tar xf -)
352 cd ../release/Evergreen-ILS-$VERSION
353 if [ -f ./autogen.sh ]; then
354     echo "Running autogen"
355     ./autogen.sh
356 else
357     echo "Running autoreconf"
358     autoreconf -i
359 fi
360
361 if [ "$SKIP_I18N" == "NO" ]; then
362     cd build/i18n
363     echo "Building i18n"
364     make install_all_locales
365     echo "i18n Cleanup"
366     cd ..
367     rm -rf i18n
368     cd ..
369 fi;
370
371 echo "Installing Dojo"
372 if [ ! -f "../dojo.tgz" ]; then
373     wget http://evergreen-ils.org/downloads/dojo.tgz -O ../dojo.tgz
374 fi
375 tar xzf ../dojo.tgz -C Open-ILS/web/js/dojo/
376
377 echo "Prepping server download files"
378
379 if [ "$SKIP_BUILD_BROWSER_CLIENT" == "NO" ]; then
380     # AngularJS staff client
381     cd $RELEASE_DIR/Evergreen-ILS-$VERSION
382     echo "Building AngularJS browser staff client"
383     cd Open-ILS/web/js/ui/default/staff/
384     npm install   # fetch build dependencies
385     npm run build-prod # copy to build dir and minify JS files
386     # npm cache is big and unnecessary in the final build. remove it.
387     rm -r node_modules 
388     cd ../../../../../ # Open-ILS dir
389
390     # OPAC dependencies
391     cd web/opac/deps
392     npm install # fetch dependencies
393     # and leave them in node_modules; there's no build step
394     cd ../../.. # Open-ILS dir
395
396     # Angular staff client
397     echo "Building Angular browser staff client"
398     cd src/eg2
399     npm install   # fetch build dependencies
400     ng build --configuration production --localize true
401     # npm cache is big and unnecessary in the final build. remove it.
402     rm -rf node_modules 
403     # similarly, the .angular directory, which includes the build
404     # cache, need not be shipped in the release tarball
405     rm -rf .angular
406 else
407     echo "Skipping browser client build"
408 fi
409
410 cd $RELEASE_DIR
411 tar czf Evergreen-ILS-$VERSION.tar.gz Evergreen-ILS-$VERSION/
412 md5sum Evergreen-ILS-$VERSION.tar.gz > Evergreen-ILS-$VERSION.tar.gz.md5
413 if [ $PREV_BRANCH != "PACKAGE" ]; then # We need to have tagged to do this ;)
414     cp Evergreen-ILS-$VERSION/ChangeLog ChangeLog-$PREV_VERSION-$VERSION
415 fi
416
417 echo "Removing build directory"
418 rm -rf Evergreen-ILS-$VERSION/
419
420 echo ""
421 echo "FOR YOU TODO:"
422 echo "* TEST the release"
423 if [ "$PREV_BRANCH" != "PACKAGE" ]; then
424     echo "* Push release branch"
425 fi
426 echo "* Upload files"
427 echo "* Send emails"