Accept an argument that designates a node group name. Based on this name
[bootmanager.git] / build.sh
1 #!/bin/bash
2 #
3 # Builds bootmanager.sh[.sgn], which is the PlanetLab Boot Manager script.
4 #
5 # The bootmanager.sh script contains in it a uuencoded tarball of the
6 # Boot Manager, customized for this PLC installation.
7 #
8 # Aaron Klingaman <alk@absarokasoft.com>
9 # Mark Huang <mlhuang@cs.princeton.edu>
10 # Marc E. Fiuczynski <mef@cs.princeton.edu>
11 # Copyright (C) 2004-2007 The Trustees of Princeton University
12 #
13 # $Id: build.sh,v 1.5 2006/04/03 19:40:55 mlhuang Exp $
14 #
15
16 # Source PLC configuration
17 if [ -f /etc/planetlab/plc_config ] ; then
18     . /etc/planetlab/plc_config
19 else
20     PLC_BOOT_HOST=boot.planet-lab.org
21     PLC_API_HOST=www.planet-lab.org
22     PLC_API_PATH=PLCAPI
23 fi
24
25 # Do not tolerate errors
26 set -e
27
28 NODEGROUP=$1
29
30 BOOTSTRAPDIR="/boot"
31 if [ -n "$NODEGROUP" ] ; then
32         BOOTSTRAPDIR="/boot/$NODEGROUP"
33 fi
34
35
36 # Change to our source directory
37 srcdir=$(cd $(dirname $0) && pwd -P)
38
39 # Translate configuration file
40 sed -i -e "s|SUPPORT_FILE_DIR=.*|SUPPORT_FILE_DIR=$BOOTSTRAPDIR|" $srcdir/source/configuration
41
42 # Source bootmanager configuration
43 . $srcdir/source/configuration
44
45 # Write boot script. plc_www/boot/index.php writes this script out
46 # after a nonce check.
47
48 DEST_SCRIPT=bootmanager.sh
49 if [ -n "$NODEGROUP" ] ; then
50         DEST_SCRIPT="${NODEGROUP}_bootmanager.sh"
51         # Remove the old version or any sym links prior to re-writing
52         rm -f ${DEST_SCRIPT}
53 fi
54
55 cat > $DEST_SCRIPT <<EOF
56 #!/bin/bash
57 #
58 # PlanetLab Boot Manager $VERSION
59 #
60 # DO NOT EDIT. Generated by $USER@$HOSTNAME at
61 # $(date) 
62 #
63
64 # Do not tolerate errors
65 set -e
66
67 UUDECODE=/usr/bin/uudecode
68
69 # once we get the beta cds out of use, this can be removed
70 if [ ! -x \$UUDECODE ]; then
71   UUDECODE=/tmp/uudecode
72   curl -s http://$PLC_BOOT_HOST/boot/uudecode.gz | gzip -d -c > \$UUDECODE
73   chmod +x \$UUDECODE
74 fi
75
76 EOF
77
78 echo '($UUDECODE | /bin/tar -C /tmp -xj) << _EOF_' >> $DEST_SCRIPT
79
80 # XXX Currently, the value of PLC_API_PORT is set to 80 by default, so
81 # that the portions of the web site that still use oldapi can continue
82 # to work. However, the Boot Manager supports HTTPS access, which we
83 # want to remain the default, so hard code 443 here.
84 sed -i -e "s@^BOOT_API_SERVER.*@BOOT_API_SERVER=https://$PLC_API_HOST:443/$PLC_API_PATH/@" \
85     $srcdir/source/configuration
86
87 # Replace the default debug SSH key
88 if [ -f "$PLC_DEBUG_SSH_KEY_PUB" ] ; then
89     install -D -m 644 "$PLC_DEBUG_SSH_KEY_PUB" $srcdir/source/debug_files/debug_root_ssh_key
90 fi
91
92 # Add pypcilib
93 pypcilib=`mktemp -d "/tmp/.bootmanager.XXXXXX"`
94 mkdir $pypcilib/source
95 cp $(rpm -ql pypcilib | grep -v '\.py[co]$') $pypcilib/source
96
97 # Embed the uuencoded tarball in the script
98 tar -cj -C $srcdir source/ -C $pypcilib source/ | uuencode -m - >> $DEST_SCRIPT
99
100 # Remove temp directory
101 rm -fr $pypcilib
102
103 echo '_EOF_' >> $DEST_SCRIPT
104 echo 'cd /tmp/source' >> $DEST_SCRIPT
105 echo 'chmod +x BootManager.py && ./BootManager.py' >> $DEST_SCRIPT
106
107 # Sign the whole script, if the keyring is on this machine.
108 if [ -f "$PLC_ROOT_GPG_KEY" -a -f "$PLC_ROOT_GPG_KEY_PUB" ] ; then
109     gpg --homedir=/root \
110         --no-default-keyring \
111         --keyring "$PLC_ROOT_GPG_KEY_PUB" \
112         --secret-keyring "$PLC_ROOT_GPG_KEY" \
113         --yes --sign --output $DEST_SCRIPT.sgn \
114         $DEST_SCRIPT
115 else
116     echo "Warning: Remember to sign $PWD/$DEST_SCRIPT!" >&2
117 fi