Puppet Setup

by firebelly
51 deployments · 18 still active · last rev. 4 years ago

Basic setup of Linode to work with Puppet.

Compatible with: No distros currently supported
						#!/bin/bash
#
# Modified from http://www.linode.com/stackscripts/view/?StackScriptID=2266
#
# Debian 6 with Puppet Agent
#
# Setup a Linode as a Puppet Agent with a remote Puppet Master.
#
# Requires StackScript Bash Library (StackScriptID: 1)
#
# 1* [Optional] Starts logging
#	* Logging is done with synchronous output to the STDOUT
#	* You can see both stackscript.log & stackscript.err in the root folder
# 2* Installs "puppet rdoc" and all dependent packages
# 3* [Optional] Sets Puppet Agent environment
# 4* [Optional] Adds an IP for puppet in the hosts file
# 5* [Optional] Run `puppetd --waitforcert 60 --test` for initial handshake.
#	* An IP address or domain for the Puppet Master must be specified
#	* Command waits for a signed cert on the CA
#	* On Puppet Master run `puppetca --list` to view list of unsigned certs
#	* On Puppet Master run `puppetca --sign fqdn` (where fqdn is your nodes domain) to sign cert
#
# <UDF name="puppetagent_env"  Label="Puppet Agent Environment"                example="Ex: development, production, etc." default="" />
# <UDF name="puppetmaster_location"  Label="Puppet Master Location"                        example="Ex: 12.34.56.78 or puppet.mydomain.com (* Required to initialize Puppet Secure Communications)" default="" />
# <UDF name="puppet_seccom"    Label="Initialize Puppet Secure Communications" example="(* Requires Puppet Master IP) Runs `puppetd --waitforcert 60 --test` to create a new SSL key and a request for cert on the Puppet Master.  On the Puppet Master run `puppetca --list` to see pending requests and `puppetca --sign fqdn` where fqdn is the hostname for this agent." oneOf="Yes,No" default="No"/>
# <UDF name="stackscriptslog"  Label="Log StackScripts Activity"               example="Log output & any errors to roots home. Output is still sent to STDOUT and may be watched from LISH" oneOf="No,Yes" default="Yes" />
#


# Error Logging
if [ "$STACKSCRIPTSLOG" == "Yes" ]; then
    exec > >(tee -a /root/stackscript.log)
    exec 2> >(tee -a /root/stackscript.err)
fi

source <ssinclude StackScriptID="1">


######################
## SYSTEM FUNCTIONS ##
######################

function apt_install {
    # Install package with apt-get

    # $1 = Package(s)

    if [ ! -n "$1" ]; then
        echo "apt_install requires one or more packages as its first argument"
        return 1;
    fi

    apt-get -y install $1
}
######################
## PUPPET FUNCTIONS ##
######################

function puppet_install {
    # Bring system up to date
    system_update                               # Defined in StackScriptID 1

    # Install Puppet Agent
    apt_install "puppet rdoc"
    
    echo "[agent]" >> /etc/puppet/puppet.conf
}

function puppet_config_agent_env {
    # Configure Puppet Agent environment

    # $1 = Agent Environment

    if [ ! -n "$1" ]; then
        echo "puppet_config_agent_env requires the Puppet evironment as its first argument"
        return 1;
    fi
    
    echo "environment = $1" >> /etc/puppet/puppet.conf
}

function puppet_config_agent_master_dns {
    # Configure Puppet Agent for remote connection

    # $1 = Puppet Master IP or domain

    if [ ! -n "$1" ]; then
        echo "puppet_config_agent_master_dns requires the Puppet Master IP or domain as its first argument"
        return 1;
    fi
    
    IP=$1
    TEST=`echo "${IP}." | grep -E "([0-9]{1,3}\.){4}"`

    if [ "$TEST" ]
    then
       add_hosts "$1" "puppet"
    else
       echo "server=$1" >> /etc/puppet/puppet.conf
    fi
}

function puppet_config_agent_seccom {
    # Initialize Secure Communications with Puppet Master

    puppetd --waitforcert 60 --test
}

######################
## STACKSCRIPT FLOW ##
######################

# Add Backports PPA & Install software
puppet_install

# Set Agents environment
if [ -n "$PUPPETAGENT_ENV" ]; then
    puppet_config_agent_env "$PUPPETAGENT_ENV"
fi


# Set IP for Puppetmaster
if [ -n "$PUPPETMASTER_LOCATION" ]; then
    puppet_config_agent_master_dns "$PUPPETMASTER_LOCATION"
fi

# Configure CSR
if [ -n "$PUPPETMASTER_LOCATION" -a "$PUPPET_SECCOM" == "Yes" ]; then
    puppet_config_agent_seccom
fi