Last active
July 4, 2017 11:00
-
-
Save jordigg/be23c98e8adbf37f2d8c to your computer and use it in GitHub Desktop.
Bootstrap script for Mac via Puppet
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
#!/usr/bin/env bash | |
# | |
# This bootstraps Puppet on Mac OS X 10.12 | |
# | |
# Optional environmental variables: | |
# - PUPPET_PACKAGE_URL: The URL to the Puppet package to install. | |
# | |
# Optional parameters | |
# - ROLE: Defines the role of the agent on a custom fact | |
# | |
# How to: | |
# sudo chmod +x mac_bootstrap.sh | |
# sudo ROLE=role TYPE=type ./mac_bootstrap.sh | |
# | |
set -e | |
#-------------------------------------------------------------------- | |
# Modifiable variables, please set them via environmental variables. | |
#-------------------------------------------------------------------- | |
PUPPET_PACKAGE_URL=${PUPPET_PACKAGE_URL:-"https://downloads.puppetlabs.com/mac/10.12/PC1/x86_64/puppet-agent-1.10.4-1.osx10.12.dmg"} | |
#-------------------------------------------------------------------- | |
# 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 "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 | |
# Add custom facts with role | |
mkdir -p /opt/puppetlabs/facter/facts.d/ | |
# Add custom facts | |
if [ "${ROLE}" != "" ]; then | |
echo "Adding Role ${ROLE}" | |
echo "role=${ROLE}" > /opt/puppetlabs/facter/facts.d/role.txt | |
fi | |
if [ "${TYPE}" != "" ]; then | |
echo "Adding Node type ${TYPE}" | |
echo "type=${TYPE}" > /opt/puppetlabs/facter/facts.d/type.txt | |
else | |
echo "Adding Node type desktop" | |
echo "type=desktop" > /opt/puppetlabs/facter/facts.d/type.txt | |
fi | |
export PATH=$PATH:/opt/puppetlabs/bin | |
echo "Puppet installed successfully and running for the first time" | |
puppet agent -t |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment