Last active
February 10, 2018 17:50
-
-
Save ltitus210/9b0e7b833d7d8a25a809d9b20b3b2670 to your computer and use it in GitHub Desktop.
Packer Bootstrap Script
This file contains 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 | |
### Formatting ### | |
TIMESTAMP=`date +%Y%m%d` | |
NC='\033[0m' # No Color | |
RED='\033[0;31m' | |
YELLOW='\033[0;33m' | |
GREEN='\033[0;32m' | |
### OS detection ### | |
if [ -f /etc/redhat-release ] | |
then | |
echo -e "${GREEN}The detected OS is: `cat /etc/redhat-release`" | |
else | |
echo -e "${RED}This script is only supported on RHEL and CentOS." ; exit 1 | |
fi | |
### House keeping ### | |
echo "First some OS updates and house keeping..." | |
yum clean all && yum update -y | |
[ $? != 0 ] && echo -e "${RED}A problem has occured running OS updates." && exit 1 | |
### Pre-existing installation detection ### | |
if [ -f /etc/puppet/puppet.conf ] || [ -f /etc/puppetlabs/puppet/puppet.conf ] | |
then | |
echo -e "${YELLOW}Puppet already installed." ; exit 1 | |
else | |
echo "Installing Puppet..." | |
fi | |
### Install the Puppet 4 yum repo ### | |
rpm -ivh https://yum.puppetlabs.com/puppetlabs-release-pc1-el-`cat /etc/redhat-release | grep -oE '[0-9]+\.[0-9]+' | cut -c1`.noarch.rpm | |
[ $? != 0 ] && echo -e "${RED} A problem has occured adding the Puppet repo." && exit 1 | |
### Install Puppet ### | |
yum -y install puppet-agent | |
[ $? != 0 ] && echo -e "${RED}A problem has occured installing the Puppet agent." && exit 1 | |
### Configure Puppet Agent if server and environment are not already set ### | |
PUPPET_SERVER=`curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/puppet_master" -H "Metadata-Flavor: Google"` | |
PUPPET_ENVIRONMENT=`curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/puppet_environment" -H "Metadata-Flavor: Google"` | |
grep -q '\[main\]' /etc/puppetlabs/puppet/puppet.conf || echo '[main]' >> /etc/puppetlabs/puppet/puppet.conf | |
grep -q '\[agent\]' /etc/puppetlabs/puppet/puppet.conf || echo '[agent]' >> /etc/puppetlabs/puppet/puppet.conf | |
grep -q 'server=' /etc/puppetlabs/puppet/puppet.conf || sed -i_"${TIMESTAMP}"-1.bak "/\[agent\]/i server=$PUPPET_SERVER" /etc/puppetlabs/puppet/puppet.conf | |
grep -q 'environment=' /etc/puppetlabs/puppet/puppet.conf || sed -i_"${TIMESTAMP}"-2.bak "/\[agent\]/i environment=$PUPPET_ENVIRONMENT" /etc/puppetlabs/puppet/puppet.conf | |
### Initial Puppet checkin ### | |
/opt/puppetlabs/bin/puppet agent -t --waitforcert=10 | |
[ $? != 2 ] && echo -e "${RED}A problem has occured during Puppet checkin." && exit 1 | |
### Disable Puppet service ### | |
/opt/puppetlabs/bin/puppet resource service puppet enable=false --environment production | |
[ $? != 0 ] && echo -e "${YELLOW}A problem has occured modifying the Puppet agent boot service." && exit 1 | |
### Clean up Puppet certs ### | |
rm -rf /etc/puppetlabs/puppet/ssl/* | |
echo -e "${GREEN}Puppet certs deleted." | |
### Done ### | |
echo -e "${GREEN}Puppet install finished." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment