Skip to content

Instantly share code, notes, and snippets.

@dickolsson
Last active October 10, 2015 19:58
Show Gist options
  • Save dickolsson/3742569 to your computer and use it in GitHub Desktop.
Save dickolsson/3742569 to your computer and use it in GitHub Desktop.
Puppet bootstrap script for Ubuntu servers.
#!/bin/bash
# Puppet bootstrapping. Currently only supports Ubuntu 12.04.
#
# TODO:
# - Make idempotent
# - Support Ubuntu 10.04 and Debian 6
version_os=`expr match "$(lsb_release -c)" '.*\<\(.*\)$'`
version_stdlib=03ec16e291a70ac5ac412be36ae3b86a771b98af
puppet_confdir=/etc/puppet
ruby_libdir=/usr/lib/ruby/1.8
facts_dir=/etc/facter/facts.d
known_host_github="github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ=="
while getopts "a:b:f:g:hinp:r:s:w:" OPT; do
case $OPT in
a) ipaddress=$OPTARG;;
b) branch=$OPTARG;;
f) facts=("$OPTARG");;
g) repository=$OPTARG;;
i) interactive=true;;
k) known_host="$OPTARG";;
n) autosign=true;;
p) proxy=$OPTARG;;
r) role=$OPTARG;;
w) wait=$OPTARG;;
h) echo "$0 [-hin] [-a ipaddress] [-w seconds] [-r role] [-g git_repository] [-b branch] [-k known_host_key] [-p http_proxy] [-f \"fact1=value1 fact2=value2\"]" && exit 0;;
\?) exit 1;;
esac
done
time_start=$(date +%s)
if [[ $proxy ]]; then
export http_proxy=$proxy
fi
wget -P /tmp http://apt.puppetlabs.com/puppetlabs-release-${version_os}.deb && dpkg -i /tmp/puppetlabs-release-${version_os}.deb && apt-get update && apt-get -y install puppet libstomp-ruby mcollective mcollective-plugins-facts-facter mcollective-plugins-provisioner
status=$?
mkdir -p ${ruby_libdir}/facter && wget -P ${ruby_libdir}/facter https://raw.github.com/puppetlabs/puppetlabs-stdlib/${version_stdlib}/lib/facter/facter_dot_d.rb
if [[ $role ]]; then
facts+=" role=${role}"
fi
if [[ $facts ]]; then
mkdir -p ${facts_dir} && echo > ${facts_dir}/bootstrap.txt
for fact in $facts; do
echo >> ${facts_dir}/bootstrap.txt "${fact}"
done
fi
if [[ $role == "posh" ]]; then
ipaddress=127.0.0.1
fi
if [[ $ipaddress ]]; then
echo >> /etc/hosts "${ipaddress} puppet"
fi
if [[ $role == "posh" ]] || [[ $role == "master" ]]; then
mkdir -p /var/puppet && cat >> ${puppet_confdir}/puppet.conf <<EOF
modulepath = ${puppet_confdir}/modules:/usr/share/puppet/modules:/var/puppet/modules
manifestdir = /var/puppet/manifests
EOF
fi
cat >> ${puppet_confdir}/puppet.conf <<EOF
[agent]
pluginsync = true
EOF
if [[ ! -f ~/.ssh/id_rsa ]]; then
mkdir ~/.ssh && ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
fi
if [[ $interactive ]]; then
cat ~/.ssh/id_rsa.pub && echo && echo "You probably want this generated SSH key. Press any key to continue." && read
fi
if [[ $role == "posh" ]] || [[ $role == "master" ]]; then
if [[ $autosign ]]; then
echo > ${puppet_confdir}/autosign.conf "*"
fi
if [[ $repository ]]; then
if [[ $repository =~ "github.com" ]] && [[ ! $known_host ]]; then
known_host=$known_host_github
fi
if [[ $known_host ]]; then
echo >> /etc/ssh/ssh_known_hosts "${known_host}"
fi
apt-get -y install git-core && git clone --recursive ${repository} /var/puppet
if [[ $branch ]]; then
cd /var/puppet && git checkout ${branch}
fi
fi
puppet master --mkusers
fi
if [[ $role == "posh" ]] || [[ $role == "broker" ]]; then
if [[ $wait ]]; then
puppet agent --test --waitforcert $wait
else
puppet agent --test
fi
status=$?
fi
time_stop=$(date +%s)
echo "Finished bootstrap run in $(( $time_stop - $time_start )) seconds" && exit $status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment