Skip to content

Instantly share code, notes, and snippets.

@scottames
Last active August 29, 2015 14:22
Show Gist options
  • Save scottames/3204638cc46a331be01b to your computer and use it in GitHub Desktop.
Save scottames/3204638cc46a331be01b to your computer and use it in GitHub Desktop.
This bootstraps Puppet on Mac OS X 10.7+
#!/usr/bin/env bash
#
# This bootstraps Puppet on Mac OS X 10.7+
# - adopted from: https://github.com/hashicorp/puppet-bootstrap/blob/master/mac_os_x.sh
#
# Optional environmental variables:
# - FACTER_PACKAGE_URL: The URL to the Facter package to install.
# - PUPPET_PACKAGE_URL: The URL to the Puppet package to install.
# - HIERA_PACKAGE_URL: The URL to the Hiera package to install.
#
set -e
#--------------------------------------------------------------------
# Modifiable variables, please set them via environmental variables.
#--------------------------------------------------------------------
FACTER_PACKAGE_URL=${FACTER_PACKAGE_URL:-"http://downloads.puppetlabs.com/mac/facter-latest.dmg"}
HIERA_PACKAGE_URL=${HIERA_PACKAGE_URL:-"http://downloads.puppetlabs.com/mac/hiera-latest.dmg"}
PUPPET_PACKAGE_URL=${PUPPET_PACKAGE_URL:-"http://downloads.puppetlabs.com/mac/puppet-latest.dmg"}
# change to true to enable puppet as a service
PUPPET_AGENT_SERVICE=false
#--------------------------------------------------------------------
# NO TUNABLES BELOW THIS POINT.
#--------------------------------------------------------------------
if [ "$EUID" -ne "0" ]; then
echo "This script must be run as root." >&2
exit 1
fi
# This function will download a DMG from a URL, mount it, find
# the `pkg` in it, install that pkg, and unmount the package.
function install_dmg() {
local name="$1"
local url="$2"
local dmg_path=$(mktemp -t "${name}-dmg")
echo "Installing: ${name}"
# Download the package into the temporary directory
echo "-- Downloading DMG..."
curl -L -o "${dmg_path}" "${url}" 2>/dev/null
# Mount it
echo "-- Mounting DMG..."
local plist_path=$(mktemp -t puppet-bootstrap)
hdiutil attach -plist "${dmg_path}" > "${plist_path}"
mount_point=$(grep -E -o '/Volumes/[-.a-zA-Z0-9]+' "${plist_path}")
# Install. It will be the only pkg in there, so just find any pkg
echo "-- Installing pkg..."
pkg_path=$(find "${mount_point}" -name '*.pkg' -mindepth 1 -maxdepth 1)
installer -pkg "${pkg_path}" -target / >/dev/null
# Unmount
echo "-- Unmounting and ejecting DMG..."
hdiutil eject "${mount_point}" >/dev/null
}
# Install Puppet and Facter
install_dmg "Facter" "${FACTER_PACKAGE_URL}"
install_dmg "Hiera" "${HIERA_PACKAGE_URL}"
install_dmg "Puppet" "${PUPPET_PACKAGE_URL}"
# Hide all users from the loginwindow with uid below 500, which will include the puppet user
defaults write /Library/Preferences/com.apple.loginwindow Hide500Users -bool YES
# Create puppet user + group
puppet resource group puppet ensure=present
sudo puppet resource user puppet ensure=present gid=puppet shell='/sbin/nologin'
if [ "${PUPPET_AGENT_SERVICE}" = true ]; then
# Create a service for the puppet agent
cat << EOF > /Library/LaunchDaemons/com.puppetlabs.puppet.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.puppetlabs.puppet</string>
<key>OnDemand</key>
<false/>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/puppet</string>
<string>agent</string>
<string>--no-daemonize</string>
<string>--logdest</string>
<string>syslog</string>
<string>--color</string>
<string>false</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>ServiceDescription</key>
<string>Puppet agent service</string>
<key>ServiceIPC</key>
<false/>
</dict>
</plist>
EOF
# Set permissions on the service plist file
chown root:wheel /Library/LaunchDaemons/com.puppetlabs.puppet.plist
chmod 644 /Library/LaunchDaemons/com.puppetlabs.puppet.plist
# enable and start the puppet agent
puppet resource service puppet ensure=running enable=true
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment