build/install: Add USB boot option
[fuel.git] / 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/xenial/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/xenial/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/xenial/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/xenial/main/binary-amd64/Packages $REPO/dists/xenial/main/binary-amd64/Packages.backup
146     cp $REPO/dists/xenial/main/binary-amd64/Packages.gz $REPO/dists/xenial/main/binary-amd64/Packages.gz.backup
147     cp $REPO/dists/xenial/Release $REPO/dists/xenial/Release.backup
148     cp -Rvp $DEST/etc/puppet $DEST/etc/puppet.backup
149 }
150
151 post_make_live() {
152     if [ -d $TOP/release/puppet/modules ]; then
153         echo "Installing into Puppet:"
154         cd $TOP/release/puppet/modules
155         if [ `ls -1 | wc -l` -gt 0 ]; then
156             for dir in *
157             do
158                 echo "   $dir"
159                 cp -Rp $dir $DEST/etc/puppet/modules
160             done
161         fi
162     fi
163 }
164
165 make_live() {
166     prep_make_live
167     copy_packages
168     post_make_live
169 }
170
171
172 prep_make_iso() {
173     DEST=$TOP/newiso
174     REPO=$DEST/ubuntu
175     echo "Preparing ISO..."
176     echo "Unpack of old ISO..."
177     if [ -d newiso ]; then
178         chmod -R 755 newiso
179         rm -rf newiso
180     fi
181     mkdir newiso
182     fusermount -u $TMP_OLDISO 2>/dev/null || cat /dev/null
183     fuseiso -p $ORIGISO $TMP_OLDISO
184     sleep 1
185     cd $TMP_OLDISO
186     find . | cpio -pd $TOP/newiso
187     cd ..
188     fusermount -u $TMP_OLDISO
189     rm -Rf $TMP_OLDISO
190     chmod -R 755 $TOP/newiso
191 }
192
193 make_iso_image() {
194     echo "Making ISO..."
195     cd $DEST
196     find . -name TRANS.TBL -exec rm {} \;
197     rm -rf rr_moved
198
199     if [[ -z "$OPNFV_GIT_SHA" ]]; then
200         OPNFV_GIT_SHA=$(git rev-parse --verify HEAD)
201     fi
202
203     mkisofs --quiet -r -V "$VOLUMEID" -publisher "$PUBLISHER" \
204         -p "$OPNFV_GIT_SHA" -J -R -b isolinux/isolinux.bin \
205         -no-emul-boot \
206         -boot-load-size 4 -boot-info-table \
207         --hide-rr-moved \
208         --joliet-long \
209         -x "lost+found" -o $NEWISO .
210
211     isohybrid $NEWISO
212
213     isoinfo -d -i $NEWISO
214 }
215
216 # iso_copy_puppet: Create a new puppet-slave.tgz for the iso
217 iso_copy_puppet() {
218     echo "Installing into Puppet..."
219     mkdir -p $TMP_ISOPUPPET/release/puppet
220     cd $TMP_ISOPUPPET/release/puppet
221     tar xzf $DEST/puppet-slave.tgz
222     cd $TOP/release/puppet/modules
223
224     # Remove all .orig files before copying as they now have been verfied
225
226     if [ -d $TOP/release/puppet/modules ]; then
227         if [ `ls -1 | wc -l` -gt 0 ]; then
228             verify_orig_files $TMP_ISOPUPPET/release/puppet $TOP/release/puppet/modules
229             find $TOP/release/puppet/modules -type f -name '*.orig' -exec rm {} \;
230             for dir in $TOP/release/puppet/modules/*
231             do
232                 echo "   $dir"
233                 cp -Rp $dir $TMP_ISOPUPPET/release/puppet
234             done
235         fi
236     fi
237
238     cd $TMP_ISOPUPPET/release/puppet
239     tar czf $DEST/puppet-slave.tgz .
240     cd $TOP
241     rm -Rf $TMP_ISOPUPPET
242 }
243
244 # iso_modify_image: Add/patch files in the ISO root
245 iso_modify_image () {
246     # TODO: Add logic for ".orig" files (hey! make a function!), which would look
247     # something like:
248     #   1. Is there an .orig file?
249     #   2. If the file is not present in origiso -> Error exit
250     #   3. If the file is changed in origiso -> Error exit (need manual rebase)
251     #   4. Otherwise continue, but don't copy orig file (or maybe we should?)
252     # ... and corresponding reverse logic:
253     #   1. If there is no .orig file, but file is present in origiso -> Error exit
254     echo "Modify ISO files (wild copy)..."
255
256     verify_orig_files $DEST $TOP/release/isoroot
257     # Remove all .orig files before copying as they now have been verfied
258     find $TOP/release/isoroot -type f -name '*.orig' -exec rm {} \;
259
260     cd $TOP/release/isoroot
261     cp -Rvp . $DEST
262
263     # Add all Git info files
264     sort $TOP/gitinfo*.txt > $DEST/gitinfo.txt
265     cp $DEST/gitinfo.txt $REPORTFILE
266 }
267
268 make_iso() {
269     prep_make_iso
270     copy_packages
271     #iso_copy_puppet
272     iso_modify_image
273     make_iso_image
274 }
275
276 copy_packages() {
277     echo "Copying Debian packages..."
278     cd $TOP/release/packages/ubuntu/pool/debian-installer
279
280     for udeb in `ls -1 | grep '\.udeb$'`
281     do
282         echo "   $udeb"
283         cp $udeb $REPO/pool/debian-installer
284         echo "Did not expect a package here, not supported"
285         exit 1
286     done
287
288     cd $TOP/release/packages/ubuntu/pool/main
289     for deb in `ls -1 | grep '\.deb$'`
290     do
291         echo "   $deb"
292         cp $deb $REPO/pool/main
293         echo "Did not expect a package here, not supported"
294         exit 1
295     done
296
297     echo "Running Fuel package patch file"
298     pushd $REPO/pool/main > /dev/null
299
300     for line in `cat $TOP/apply_patches | grep -v "^#" | grep -v "^$"`; do
301         echo "Line is $line"
302         echo "Did not expect a line here, not supported"
303         exit 1
304         ref=`echo $line | cut -d '>' -f 1`
305         origpkg=`echo $line| cut -d '>' -f 2`
306         url=`echo $line | cut -d '>' -f 3`
307
308         if [ -z "$origpkg" ]; then
309             echo "Error: No origpkg for patching"
310             exit 1
311         fi
312
313         if [ -z "$url" ]; then
314             echo "Error: No url for patching"
315             exit 1
316         fi
317
318         if [ -z "$ref" ]; then
319             echo "Error: No reference text for patching"
320             exit 1
321         fi
322
323         echo "CM: Patching Fuel package for $ref" | tee -a $REPORTFILE
324         echo "CM: Replacing package $origpkg with $url" | tee -a $REPORTFILE
325         oldrev=`get_deb_rev $origpkg`
326         rm $origpkg
327         wget --quiet $url
328         topkg=`basename $url`
329         echo "CM: MD5 of new package:" | tee -a $REPORTFILE
330         md5sum $topkg | tee -a $REPORTFILE
331
332         patchname=`get_deb_name $topkg`
333         patchrev=`get_deb_rev $topkg`
334         echo "Correcting dependencies towards $patchname rev $patchrev - old rev $oldrev" | tee -a $REPORTFILE
335         $TOP/patch-packages/tools/correct_deps $patchname $oldrev $patchrev | tee -a $REPORTFILE
336         if [ $PIPESTATUS -ne 0 ]; then
337             exit 1
338         fi
339     done
340
341     printf "Done running Fuel patch file\n\n"
342     echo "Running add packages file"
343     for line in `cat $TOP/add_opnfv_packages | grep -v "^#" | grep -v "^$"`; do
344         echo "Line is $line"
345         echo "Did not expect a line here, not supported"
346         exit 1
347         ref=`echo $line | cut -d '>' -f 1`
348         origpkg=`echo $line| cut -d '>' -f 2`
349         url=`echo $line | cut -d '>' -f 3`
350
351         if [ -z "$origpkg" ]; then
352             echo "Error: No origpkg for patching"
353             exit 1
354         fi
355
356         if [ -z "$url" ]; then
357             echo "Error: No url for patching"
358             exit 1
359         fi
360
361         if [ -z "$ref" ]; then
362             echo "Error: No reference text for patching"
363             exit 1
364         fi
365
366         if [ "$origpkg" != "NONE" ]; then
367             echo "CM: Patching added package for $ref" | tee -a $REPORTFILE
368             echo "CM: Replacing package $origpkg with $url" | tee -a $REPORTFILE
369             oldrev=`get_deb_rev $origpkg`
370             rm $origpkg
371         else
372             echo "CM: Adding previoulsy uninstalled package for $ref" tee -a $REPORTFILE
373         fi
374         wget --quiet $url
375         topkg=`basename $url`
376         echo "CM: MD5 of new package:" | tee -a $REPORTFILE
377         md5sum $topkg | tee -a $REPORTFILE
378         if [ "$origpkg" != "NONE" ]; then
379             patchname=`get_deb_name $topkg`
380             patchrev=`get_deb_rev $topkg`
381             echo "Correcting dependencies towards $patchname rev $patchrev - old rev $oldrev" | tee -a $REPORTFILE
382             $TOP/patch-packages/tools/correct_deps $patchname $oldrev $patchrev | tee -a $REPORTFILE
383             if [ $PIPESTATUS -ne 0 ]; then
384                 exit 1
385             fi
386         fi
387     done
388     printf "Done running add packages file\n\n"
389
390     popd > /dev/null
391
392     if [ -f $TOP/patch-packages/release/patch-replacements ]; then
393         echo "Applying package patches" | tee -a $REPORTFILE
394         pushd $REPO/pool/main > /dev/null
395         echo "CM: I am now in $(pwd)"
396         printf "\n\n" | tee -a  $REPORTFILE
397         for line in `cat $TOP/patch-packages/release/patch-replacements`
398         do
399             echo "Did not expect a line here, not supported"
400             exit 1
401             echo "Processing $line ..."
402             frompkg=`echo $line | cut -d ">" -f 1`
403             topkg=`echo $line | cut -d ">" -f 2`
404             echo "CM: Applying patch to $frompkg" | tee -a $REPORTFILE
405             echo "CM: New package rev after patch: $topkg" | tee -a $REPORTFILE
406
407             if [ ! -f $frompkg ]; then
408                 echo "Error: Can't find $frompkg in repo"
409                 exit 1
410             else
411                 oldrev=`get_deb_rev $frompkg`
412                 echo "Removing $frompkg from repo"
413                 rm $frompkg
414             fi
415
416             if [ ! -f $TOP/patch-packages/release/packages/$topkg ]; then
417                 echo "Error: Can't find $topkg in patch release"
418                 exit 1
419             else
420                 echo "Adding $topkg to repo"
421                 pkg_dest=$(dirname $frompkg)
422                 cp $TOP/patch-packages/release/packages/$topkg $pkg_dest/
423             fi
424
425             pushd $pkg_dest > /dev/null
426             patchname=`get_deb_name $topkg`
427             patchrev=`get_deb_rev $topkg`
428             echo "Correcting dependencies towards $patchname rev $patchrev - old rev $oldrev" | tee -a $REPORTFILE
429             $TOP/patch-packages/tools/correct_deps $patchname $oldrev $patchrev | tee -a $REPORTFILE
430             if [ $PIPESTATUS -ne 0 ]; then
431                 exit 1
432             fi
433             popd > /dev/null
434         done
435         popd > /dev/null
436     fi
437
438     echo "Generating metadata..."
439     pushd $REPO > /dev/null
440
441     # The below methods are from 15B
442     APT_REL_CONF="$TOP/install/apt-ftparchive-release.conf"
443     APT_DEB_CONF="$TOP/install/apt-ftparchive-deb.conf"
444     APT_UDEB_CONF="$TOP/install/apt-ftparchive-udeb.conf"
445
446     echo Not running apt-ftparchive -c "${APT_REL_CONF}" generate "${APT_DEB_CONF}"
447     echo Not running apt-ftparchive generate "${APT_UDEB_CONF}"
448
449     # Fuel also needs this index file
450     # cat dists/xenial/main/binary-amd64/Packages | \
451     #    awk '/^Package:/{pkg=$2}
452     # /^Version:/{print pkg ": \"" $2 "\""}' > ubuntu-versions.yaml
453     # cp ubuntu-versions.yaml $DEST
454
455     # apt-ftparchive -c "${APT_REL_CONF}" release dists/mos10.0/ > dists/mos10.0/Release
456     # gzip -9cf dists/mos10.0/Release > dists/mos10.0/Release.gz
457
458     popd > /dev/null
459
460 }
461
462
463 #############################################################################
464
465 trap my_exit EXIT
466
467 CONF=`mktemp /tmp/XXXXXXX`
468 MODE=$1
469 TOP=`pwd`
470
471 if [ $MODE = "iso" ]; then
472     PUBLISHER="OPNFV"
473     TMP_OLDISO=`mktemp -d /tmp/XXXXXXX`
474     TMP_ISOPUPPET=`mktemp -d /tmp/XXXXXXX`
475     ORIGISO=$2
476     NEWISO=$3
477     VOLUMEID="$4_$5"
478     REPORTFILE="${NEWISO}.txt"
479     echo "Opening reportfile at $REPORTFILE"
480     touch $REPORTFILE
481     if [ ! -f $ORIGISO ]; then
482         echo "Can't find original iso at $ORIGISO"
483         rm $CONF
484         exit 1
485     fi
486
487     make_iso
488 else
489     echo "Unknown mode: $MODE"
490     exit 1
491 fi