# Script to boot strap new EC2 instances and get them connected to our Puppet Enterprise master
rpm --quiet -q git rubygems || yum -y install git rubygems
if rpm --quiet -q pe-puppet; then
# Puppet Enterprise is already installed, let's reconfigure it - this instance was most likely booted up in the past, or is using an AMI that already has Puppet baked in
service pe-puppet stop
cat > /etc/puppetlabs/puppet/puppet.conf <<EOF
vardir = /var/opt/lib/pe-puppet
logdir = /var/log/pe-puppet
rundir = /var/run/pe-puppet
modulepath = /etc/puppetlabs/puppet/modules:/opt/puppet/share/puppet/modules
user = pe-puppet
group = pe-puppet
archive_files = true
archive_file_server = puppet.COMPANYNAME.com
certname = $(curl -s http://169.254.169.254/latest/meta-data/instance-id).$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone).COMPANYNAME.local
server = puppet.COMPANYNAME.com
report = true
classfile = \$vardir/classes.txt
localconfig = \$vardir/localconfig
graph = true
pluginsync = true
service pe-puppet start
chkconfig pe-puppet on
# Puppet Enterprise was never installed, let's install it and configure it
cat > /root/puppet-enterprise-installer.answers <<EOF
q_puppetagent_certname=$(curl -s http://169.254.169.254/latest/meta-data/instance-id).$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone).COMPANYNAME.local
curl -o - 'https://pm.puppetlabs.com/puppet-enterprise/2.6.1/puppet-enterprise-2.6.1-el-6-x86_64.tar.gz' | tar -C /root/ -zxvf -
/root/puppet-enterprise-*/puppet-enterprise-installer -a /root/puppet-enterprise-installer.answers -l /root/puppet-install.log
rm -rf /root/puppet*
chkconfig pe-puppet on
# This is a last-resort timebomb that checks for the existence of a file that is created by our default Puppet manifest.
# If the file is not created within 10 minutes of startup, send out an email alert and shut the instance down.
# This prevents zombie instances from running (and being billed) without ever contacting Puppet; the autoscaling configuration should start a replacement instance once this shuts down.
if [ ! -f /etc/ec2-instance-id ]
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
PUBLIC_HOSTNAME=$(curl -s http://169.254.169.254/latest/meta-data/public-hostname)
AVAILABILITY_ZONE=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)
SECURITY_GROUPS=$(curl -s http://169.254.169.254/latest/meta-data/security-groups)
echo "From: root@$PUBLIC_HOSTNAME"
echo "To: admins@COMPANYNAME.com"
echo "Subject: $INSTANCE_ID Failed to register with Puppet"
echo "$INSTANCE_ID does not appear to have picked up a Puppet configuration in the last 10 minutes and will be automatically shut down in 5 minutes."
echo "Instance details:"
echo "Instance ID: $INSTANCE_ID"
echo "Hostname: $PUBLIC_HOSTNAME"
echo "Availability zone: $AVAILABILITY_ZONE"
echo "Security groups: $SECURITY_GROUPS"
echo "Uptime output: $(uptime)"
echo "ps auwx and /var/log/messages output follows:"
tail -n 100 /var/log/messages
echo "This message was generated by the EC2 cloud-spinup script"
) | sendmail -t
shutdown -h +1 "Puppet did not appear to start properly. Shutting down instance."