Skip to content

Instantly share code, notes, and snippets.

@TheCatPlusPlus
Created September 25, 2014 13:17
Show Gist options
  • Save TheCatPlusPlus/16e70cb47e46b1978daa to your computer and use it in GitHub Desktop.
Save TheCatPlusPlus/16e70cb47e46b1978daa to your computer and use it in GitHub Desktop.
#!/bin/bash
function go_back() {
cd /vagrant
}
trap go_back EXIT
echo "==> Preparing Puppet run"
echo "==> Provisioning logs will be put into /vagrant/tmp (<project directory>/tmp on the host)"
[ -d /tmp/puppet ] || mkdir /tmp/puppet
[ -d /vagrant/tmp ] || mkdir /vagrant/tmp
rm /vagrant/tmp/{puppet,librarian-puppet}.log > /dev/null
touch /vagrant/tmp/{puppet,librarian-puppet}.log
cd /tmp/puppet
cp -r /vagrant/config/puppet/* ./
echo "==> Installing modules with Librarian"
librarian-puppet install --verbose > /vagrant/tmp/librarian-puppet.log 2>&1 || {
echo "!!! Librarian-Puppet run failed, dependencies weren't installed properly."
echo "!!! This might be our bug (remember to attach librarian-puppet.log when reporting) or a failure of Puppet Forge."
echo "!!! Recheck Puppetfile.lock and see if Forge is accessible."
exit 1
}
echo "==> Applying Puppet configuration"
if [ -f /tmp/provision.debug ]; then
echo "==> ... in debug mode, because /tmp/provision.debug exists"
DEBUG_FLAG='--debug'
else
DEBUG_FLAG=''
fi
puppet apply --modulepath=modules --detailed-exitcodes $DEBUG_FLAG site.pp > /vagrant/tmp/puppet.log 2>&1
if [[ $? == 2 || $? == 0 ]]; then
result=1
else
result=0
fi
# strip colour codes from the log and then do manual search for Error:
# because Puppet sometimes returns success exit code even when there's been
# an error in applying the catalog
sed -i -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" /vagrant/tmp/puppet.log
if grep '^Error:' /vagrant/tmp/puppet.log > /dev/null; then
grep_result=1
else
grep_result=0
fi
if [[ $result == 1 && $grep_result == 0 ]]; then
echo "==> Provisioning completed sucessfully"
exit 0
elif [[ $result == 1 && $grep_result == 1 ]]; then
echo "!!! Puppet reported a successful run, but the log contains lines beginning with Error."
echo "!!! This is probably a bug!"
echo "!!! Report this, remember to attach the logs (librarian-puppet.log and puppet.log)."
exit 1
else
echo "!!! Puppet run failed, this shouldn't have happened!"
echo "!!! Report this, remember to attach the logs (librarian-puppet.log and puppet.log)."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment