dbb26d6d5e3c91197a5373bb1e9e21ec8156e8f3
[genesis.git] / fuel / build / install / install.sh
1 #!/bin/bash -e
2 ##############################################################################
3 # Copyright (c) 2015 Ericsson AB and others.
4 # stefan.k.berg@ericsson.com
5 # jonas.bjurel@ericsson.com
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12
13 my_exit() {
14     rc=$?
15
16     cd /tmp
17
18     if [ -n "$TMP_HOSTMOUNT" ]; then
19         if [ -d "$TMP_HOSTMOUNT" ]; then
20             fusermount -u $TMP_HOSTMOUNT
21             rmdir $TMP_HOSTMOUNT
22         fi
23     fi
24
25     if [ -d "$TMP_OLDISO" ]; then
26         fusermount -u $TMP_OLDISO
27         rmdir $TMP_OLDISO
28     fi
29
30     if [ -f "$CONF" ]; then
31         rm $CONF
32     fi
33
34     if [ -d "$TMP_ISOPUPPET" ]; then
35         rm -Rf $TMP_ISOPUPPET
36     fi
37 }
38
39 get_deb_name() {
40     ar p $1 control.tar.gz | tar xzO ./control | grep "^Package:.* " | sed 's/.* //'
41     if [ $PIPESTATUS -ne 0 ]; then
42         echo "Error in get_deb_name($1)"
43         exit 1
44     fi
45 }
46
47 get_deb_rev() {
48     ar p $1 control.tar.gz | tar xzO ./control | grep "^Version:.* " | sed 's/.* //'
49     if [ $PIPESTATUS -ne 0 ]; then
50         echo "Error in get_deb_rev($1)"
51         exit 1
52     fi
53 }
54
55
56 # Added logic for ".orig" files:
57 #   1. Is there an .orig file for the new file?
58 #   2. If the file is not present in base ISO -> Error!
59 #   3. If the file is changed i base ISO -> Error!  (need manual rebase)
60 #   4. If there is no .orig file, but file is present in base ISO: Error!
61 verify_orig_files() {
62     OLDDIR=$1
63     NEWDIR=$2
64
65     pushd $NEWDIR >/dev/null
66     for f in $(find * -type f -name '*.orig')
67     do
68         origfile=$NEWDIR/$f
69         oldfile=$OLDDIR/$(echo $f | sed 's/.orig$//')
70         newfile=$NEWDIR/$(echo $f | sed 's/.orig$//')
71
72         origfile=${newfile}.orig
73         # If no corresponding base file, error
74         if [ ! -f $oldfile ]; then
75             printf "\n\n\n\n"
76             echo "Error: found ${newfile} but no"
77             echo "Error: ${oldfile}"
78             echo "Error: Manual rebase is needed!"
79             printf "\n\n\n\n"
80             exit 1
81         fi
82
83         # If orig file differs from base file, error
84         if ! diff -q $origfile $oldfile > /dev/null; then
85             printf "\n\n\n\n"
86             echo "Error: $origfile differs from"
87             echo "Error: $oldfile!"
88             echo "Error: Manual rebase is needed!"
89             printf "\n\n\n\n"
90             exit 1
91         fi
92
93     done
94
95
96     # Check that there we're not overwriting oldfiles without having a ".orig" copy
97     for f in $(find * -type f ! -name '*.orig')
98     do
99         newfile=$NEWDIR/$(echo $f | sed 's/.orig$//')
100         origfile=${newfile}.orig
101         oldfile=$OLDDIR/$f
102         if [ -f $oldfile ]; then
103             if [ ! -f $origfile ]; then
104                 printf "\n\n\n\n"
105                 echo "Error: Will overwrite $oldfile, but there is no"
106                 echo "Error: $origfile!"
107                 echo "Error: You need to create the `basename $origfile`!"
108                 printf "\n\n\n\n"
109                 exit 1
110             fi
111         fi
112     done
113
114
115     popd >/dev/null
116 }
117
118 prep_make_live() {
119     DEST=$TMP_HOSTMOUNT
120     REPO=$DEST/var/www/nailgun/ubuntu/fuelweb/x86_64
121     echo "Live install"
122     ssh-copy-id root@$FUELHOST
123     sshfs root@1${FUELHOST}:/ $TMP_HOSTMOUNT
124
125     if [ -f  $REPO/dists/precise/main/binary-amd64/Packages.backup ]; then
126         echo "Error - found backup file for Packages!"
127         exit 1
128     fi
129
130     if [ -f  $REPO/dists/precise/main/binary-amd64/Packages.gz.backup ]; then
131         echo "Error - found backup file for Packages.gz!"
132         exit 1
133     fi
134
135     if [ -f  $REPO/dists/precise/Release.backup ]; then
136         echo "Error - found backup file for Release!"
137         exit 1
138     fi
139
140     if [ -d  $DEST/etc/puppet.backup ]; then
141         echo "Error - found backup file for Puppet!"
142         exit 1
143     fi
144
145     cp $REPO/dists/precise/main/binary-amd64/Packages $REPO/dists/precise/main/binary-amd64/Packages.backup
146     cp $REPO/dists/precise/main/binary-amd64/Packages.gz $REPO/dists/precise/main/binary-amd64/Packages.gz.backup
147     cp $REPO/dists/precise/Release $REPO/dists/precise/Release.backup
148     cp -Rvp $DEST/etc/puppet $DEST/etc/puppet.backup
149 }
150
151 post_make_live() {
152     echo "Installing into Puppet:"
153     cd $TOP/release/puppet/modules
154     for dir in *
155     do
156         echo "   $dir"
157         cp -Rp $dir $DEST/etc/puppet/modules
158     done
159 }
160
161 make_live() {
162     prep_make_live
163     copy_packages
164     post_make_live
165 }
166
167
168 prep_make_iso() {
169     DEST=$TOP/newiso
170     REPO=$DEST/ubuntu
171     echo "Making ISO..."
172     echo "Unpack of old ISO..."
173     if [ -d newiso ]; then
174         chmod -R 755 newiso
175         rm -rf newiso
176     fi
177     mkdir newiso
178     fusermount -u $TMP_OLDISO 2>/dev/null || cat /dev/null
179     fuseiso -p $ORIGISO $TMP_OLDISO
180     sleep 1
181     cd $TMP_OLDISO
182     find . | cpio -pd $TOP/newiso
183     cd ..
184     fusermount -u $TMP_OLDISO
185     rm -Rf $TMP_OLDISO
186     chmod -R 755 $TOP/newiso
187 }
188
189 make_iso_image() {
190     echo "Making ISO..."
191     cd $DEST
192     find . -name TRANS.TBL -exec rm {} \;
193     rm -rf rr_moved
194
195     mkisofs --quiet -r -V "$VOLUMEID" -publisher "$PUBLISHER" \
196         -p `git rev-parse --verify HEAD` -J -R -b isolinux/isolinux.bin \
197         -no-emul-boot \
198         -boot-load-size 4 -boot-info-table \
199         --hide-rr-moved \
200         -x "lost+found" -o $NEWISO .
201
202     isoinfo -d -i $NEWISO
203 }
204
205 # iso_copy_puppet: Create a new puppet-slave.tgz for the iso
206 iso_copy_puppet() {
207     echo "Installing into Puppet..."
208     mkdir -p $TMP_ISOPUPPET/release/puppet
209     cd $TMP_ISOPUPPET/release/puppet
210     tar xzf $DEST/puppet-slave.tgz
211     cd $TOP/release/puppet/modules
212
213     verify_orig_files $TMP_ISOPUPPET/release/puppet $TOP/release/puppet/modules
214     # Remove all .orig files before copying as they now have been verfied
215     find $TOP/release/puppet/modules -type f -name '*.orig' -exec rm {} \;
216
217     for dir in $TOP/release/puppet/modules/*
218     do
219         echo "   $dir"
220         cp -Rp $dir $TMP_ISOPUPPET/release/puppet
221     done
222     cd $TMP_ISOPUPPET/release/puppet
223
224
225     tar czf $DEST/puppet-slave.tgz .
226     cd $TOP
227     rm -Rf $TMP_ISOPUPPET
228 }
229
230 # iso_modify_image: Add/patch files in the ISO root
231 iso_modify_image () {
232     # TODO: Add logic for ".orig" files (hey! make a function!), which would look
233     # something like:
234     #   1. Is there an .orig file?
235     #   2. If the file is not present in origiso -> Error exit
236     #   3. If the file is changed in origiso -> Error exit (need manual rebase)
237     #   4. Otherwise continue, but don't copy orig file (or maybe we should?)
238     # ... and corresponding reverse logic:
239     #   1. If there is no .orig file, but file is present in origiso -> Error exit
240     echo "Modify ISO files (wild copy)..."
241
242     verify_orig_files $DEST $TOP/release/isoroot
243     # Remove all .orig files before copying as they now have been verfied
244     find $TOP/release/isoroot -type f -name '*.orig' -exec rm {} \;
245
246     cd $TOP/release/isoroot
247     cp -Rvp . $DEST
248 }
249
250 make_iso() {
251     prep_make_iso
252     copy_packages
253     iso_copy_puppet
254     iso_modify_image
255     make_iso_image
256 }
257
258 copy_packages() {
259     echo "Copying Debian packages..."
260     cd $TOP/release/packages/ubuntu/pool/debian-installer
261
262     for udeb in `ls -1 | grep '\.udeb$'`
263     do
264         echo "   $udeb"
265         cp $udeb $REPO/pool/debian-installer
266     done
267
268     cd $TOP/release/packages/ubuntu/pool/main
269     for deb in `ls -1 | grep '\.deb$'`
270     do
271         echo "   $deb"
272         cp $deb $REPO/pool/main
273     done
274
275     echo "Running Fuel package patch file"
276     pushd $REPO/pool/main > /dev/null
277
278     for line in `cat $TOP/apply_patches | grep -v "^#" | grep -v "^$"`; do
279         echo "Line is $line"
280         ref=`echo $line | cut -d '>' -f 1`
281         origpkg=`echo $line| cut -d '>' -f 2`
282         url=`echo $line | cut -d '>' -f 3`
283
284         if [ -z "$origpkg" ]; then
285             echo "Error: No origpkg for patching"
286             exit 1
287         fi
288
289         if [ -z "$url" ]; then
290             echo "Error: No url for patching"
291             exit 1
292         fi
293
294         if [ -z "$ref" ]; then
295             echo "Error: No reference text for patching"
296             exit 1
297         fi
298
299         echo "CM: Patching Fuel package for $ref" | tee -a $REPORTFILE
300         echo "CM: Replacing package $origpkg with $url" | tee -a $REPORTFILE
301         oldrev=`get_deb_rev $origpkg`
302         rm $origpkg
303         wget --quiet $url
304         topkg=`basename $url`
305         echo "CM: MD5 of new package:" | tee -a $REPORTFILE
306         md5sum $topkg | tee -a $REPORTFILE
307
308         patchname=`get_deb_name $topkg`
309         patchrev=`get_deb_rev $topkg`
310         echo "Correcting dependencies towards $patchname rev $patchrev - old rev $oldrev" | tee -a $REPORTFILE
311         $TOP/patch-packages/tools/correct_deps $patchname $oldrev $patchrev | tee -a $REPORTFILE
312         if [ $PIPESTATUS -ne 0 ]; then
313             exit 1
314         fi
315     done
316
317     printf "Done running Fuel patch file\n\n"
318
319     echo "Running add packages file"
320     for line in `cat $TOP/add_opnfv_packages | grep -v "^#" | grep -v "^$"`; do
321         echo "Line is $line"
322         ref=`echo $line | cut -d '>' -f 1`
323         origpkg=`echo $line| cut -d '>' -f 2`
324         url=`echo $line | cut -d '>' -f 3`
325
326         if [ -z "$origpkg" ]; then
327             echo "Error: No origpkg for patching"
328             exit 1
329         fi
330
331         if [ -z "$url" ]; then
332             echo "Error: No url for patching"
333             exit 1
334         fi
335
336         if [ -z "$ref" ]; then
337             echo "Error: No reference text for patching"
338             exit 1
339         fi
340
341         if [ "$origpkg" != "NONE" ]; then
342             echo "CM: Patching added package for $ref" | tee -a $REPORTFILE
343             echo "CM: Replacing package $origpkg with $url" | tee -a $REPORTFILE
344             oldrev=`get_deb_rev $origpkg`
345             rm $origpkg
346         else
347             echo "CM: Adding previoulsy uninstalled package for $ref" tee -a $REPORTFILE
348         fi
349         wget --quiet $url
350         topkg=`basename $url`
351         echo "CM: MD5 of new package:" | tee -a $REPORTFILE
352         md5sum $topkg | tee -a $REPORTFILE
353         if [ "$origpkg" != "NONE" ]; then
354             patchname=`get_deb_name $topkg`
355             patchrev=`get_deb_rev $topkg`
356             echo "Correcting dependencies towards $patchname rev $patchrev - old rev $oldrev" | tee -a $REPORTFILE
357             $TOP/patch-packages/tools/correct_deps $patchname $oldrev $patchrev | tee -a $REPORTFILE
358             if [ $PIPESTATUS -ne 0 ]; then
359                 exit 1
360             fi
361         fi
362     done
363     printf "Done running add packages file\n\n"
364
365     popd > /dev/null
366
367     if [ -f $TOP/patch-packages/release/patch-replacements ]; then
368         echo "Applying package patches" | tee -a $REPORTFILE
369         pushd $REPO/pool/main > /dev/null
370         printf "\n\n" | tee -a  $REPORTFILE
371         for line in `cat $TOP/patch-packages/release/patch-replacements`
372         do
373             frompkg=`echo $line | cut -d ">" -f 1`
374             topkg=`echo $line | cut -d ">" -f 2`
375             echo "CM: Applying patch to $frompkg" | tee -a $REPORTFILE
376             echo "CM: New package rev after patch: $topkg" | tee -a $REPORTFILE
377
378             if [ ! -f $frompkg ]; then
379                 echo "Error: Can't find $frompkg in repo"
380                 exit 1
381             else
382                 oldrev=`get_deb_rev $frompkg`
383                 echo "Removing $frompkg from repo"
384                 rm $frompkg
385             fi
386
387             if [ ! -f $TOP/patch-packages/release/packages/$topkg ]; then
388                 echo "Error: Can't find $topkg in patch release"
389                 exit 1
390             else
391                 echo "Adding $topkg to repo"
392                 cp $TOP/patch-packages/release/packages/$topkg .
393             fi
394
395             patchname=`get_deb_name $topkg`
396             patchrev=`get_deb_rev $topkg`
397             echo "Correcting dependencies towards $patchname rev $patchrev - old rev $oldrev" | tee -a $REPORTFILE
398             $TOP/patch-packages/tools/correct_deps $patchname $oldrev $patchrev | tee -a $REPORTFILE
399             if [ $PIPESTATUS -ne 0 ]; then
400                 exit 1
401             fi
402         done
403         popd > /dev/null
404     fi
405
406     echo "Generating metadata..."
407     pushd $REPO > /dev/null
408
409     # The below methods are from 15B
410     APT_REL_CONF="$TOP/install/apt-ftparchive-release.conf"
411     APT_DEB_CONF="$TOP/install/apt-ftparchive-deb.conf"
412     APT_UDEB_CONF="$TOP/install/apt-ftparchive-udeb.conf"
413
414     apt-ftparchive -c "${APT_REL_CONF}" generate "${APT_DEB_CONF}"
415     apt-ftparchive generate "${APT_UDEB_CONF}"
416
417     # Fuel also needs this index file
418     cat dists/precise/main/binary-amd64/Packages | \
419         awk '/^Package:/{pkg=$2}
420     /^Version:/{print pkg ": \"" $2 "\""}' > ubuntu-versions.yaml
421     cp ubuntu-versions.yaml $DEST
422
423     apt-ftparchive -c "${APT_REL_CONF}" release dists/precise/ > dists/precise/Release
424     gzip -9cf dists/precise/Release > dists/precise/Release.gz
425
426     popd > /dev/null
427
428 }
429
430
431 #############################################################################
432
433 trap my_exit EXIT
434
435 CONF=`mktemp /tmp/XXXXXXX`
436 MODE=$1
437 TOP=`pwd`
438
439 if [ $MODE = "iso" ]; then
440     PUBLISHER="OPNFV"
441     TMP_OLDISO=`mktemp -d /tmp/XXXXXXX`
442     TMP_ISOPUPPET=`mktemp -d /tmp/XXXXXXX`
443     ORIGISO=$2
444     NEWISO=$3
445     VOLUMEID="$4 $5"
446     REPORTFILE="${NEWISO}.txt"
447     if [ ! -f $ORIGISO ]; then
448         echo "Can't find original MOS 5.1 iso at $ORIGISO"
449         rm $CONF
450         exit 1
451     fi
452
453     make_iso
454 else
455     echo "Unknown mode: $MODE"
456     exit 1
457 fi