Created
April 4, 2014 09:08
-
-
Save bobtfish/9970919 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script generates tools/bootstrap, which unpacks a chunk of puppet code from itself | |
# and runs puppet against it. | |
set -eu | |
SCRIPT=$( readlink -f "$0" ) | |
HERE=$( dirname "$SCRIPT" ) | |
STAGE=`tempfile --suffix build-puppet-bootstrap` | |
TAR=`tempfile --suffix build-puppet-bootstrap` | |
if [ "$BUILD_NUMBER" == "" ];then | |
BUILD_NUMBER="handrolled" | |
fi | |
echo Building bootstrap into $STAGE | |
echo "#!/bin/bash" > $STAGE | |
echo >> $STAGE | |
echo '# DO NOT make changes to this file, it is autogenerated!' >> $STAGE | |
echo '# To make changes, please edit syssvn/puppet/tools/buildbootstrap' >> $STAGE | |
echo >> $STAGE | |
echo "echo 'Puppet bootstrap build ID: $BUILD_NUMBER'" >> $STAGE | |
# We go on to invoke puppet as root, so insist on being root to run | |
echo 'if [ $(id -un) != "root" ];then' >> $STAGE | |
echo ' echo "You must run this bootstrap script with sudo, or be root"' >> $STAGE | |
echo ' exit 1' >> $STAGE | |
echo 'fi' >> $STAGE | |
# Bail if puppet not installed | |
echo 'PUPPET=`which puppet`' >> $STAGE | |
echo 'if [ $? != 0 ];then' >> $STAGE | |
echo ' echo "Puppet is not installed on this machine, please install for bootstrapping"' >> $STAGE | |
echo ' exit 1' >> $STAGE | |
echo 'fi' >> $STAGE | |
# Make a temp directory and cd into it | |
echo 'UNPACKIN=`mktemp -d`' >> $STAGE | |
echo 'cd $UNPACKIN || exit 1' >> $STAGE | |
# Stuff a base 64'd tar file of relevant puppet modules (the ones included from bootstrap.pp) into the script, | |
# in a way that executing it will cause the blob of puppet code to get unpacked into the temporary directory | |
echo 'cat << 'EOF'|base64 -id -|tar xzf -'>>$STAGE | |
FILES_TO_INCLUDE=$(cat manifests/bootstrap.pp | egrep '^ *include '| cut -f 1 -d : | sed 's/include /modules\//g' | sed 's/ *//') | |
FILES_TO_INCLUDE="manifests/bootstrap.pp vendor/modules/stdlib vendor/modules/concat vendor/modules/apt modules/cron $FILES_TO_INCLUDE" | |
echo Including these files in the tar: "$FILES_TO_INCLUDE" | |
echo tar czf - manifests/bootstrap.pp $FILES_TO_INCLUDE | |
tar czf - $FILES_TO_INCLUDE |base64 ->>$STAGE | |
# Append base64 blob of puppet code into heredoc | |
rm $TAR | |
echo EOF >> $STAGE | |
echo >> $STAGE | |
# Invoke puppet now we're unpacked | |
echo 'echo "Unpacked puppet bootstrap in $UNPACKIN"' >> $STAGE | |
echo 'mkdir -p /var/lib/puppet/lib' >> $STAGE | |
echo 'cp -r modules/*/lib/* vendor/modules/*/lib/* /var/lib/puppet/lib/' >> $STAGE | |
echo 'puppet apply --modulepath=modules/:vendor/modules/ --templatedir files/ manifests/bootstrap.pp $@' >> $STAGE | |
echo 'if [ $? != 0 ];then' >> $STAGE | |
echo ' echo "Puppet run failed, leaving bootstrap files in $UNPACKIN"' >> $STAGE | |
echo ' exit 1' >> $STAGE | |
echo 'fi' >> $STAGE | |
echo 'rm -rf $UNPACKIN' >> $STAGE | |
cp $STAGE tools/bootstrap | |
chmod 775 tools/bootstrap | |
echo Bootstrap is now ready at tools/bootstrap |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment