Skip to content

Instantly share code, notes, and snippets.

@fnichol
Created September 11, 2011 06:00
Show Gist options
  • Save fnichol/1209226 to your computer and use it in GitHub Desktop.
Save fnichol/1209226 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e
set -x
### How To Use
#
# sudo bash -c 'bash \
# <(curl -sLB https://raw.github.com/gist/1209226/bootstrap_chef_server.sh) \
# --hostname foo.example.com'
#
# Details pulled from:
# http://wiki.opscode.com/display/chef/Bootstrap+Chef+RubyGems+Installation
default_rubygems_version="1.8.10"
bootstrap_tar_url="http://fnichol.s3.amazonaws.com/chef-solo/bootstrap-latest-p1.tar.gz"
log() { printf "===> $*\n" ; return $? ; }
fail() { log "\nERROR: $*\n" ; exit 1 ; }
usage() {
printf "
Usage
bootstrap_chef_server.sh [options] [action]
Options
--rubygems-version|-r <x.y.z> - Installs a specific version of RubyGems
--hostname|-h <foo.example.com> - Sets the FQDN of host node
Actions
help - Display CLI help (this output)
"
}
set_hostname() {
if hostname | grep -q "$__hostname" >/dev/null ; then
log "Hostname is correct, so skipping..."
return
fi
local host_first="$(echo $__hostname | cut -d . -f 1)"
local hostnames="${__hostname} ${host_first}"
sed -i 's/^.*$/'$__hostname'/' /etc/hostname
if egrep -q '^127.0.1.1[[:space:]]' /etc/hosts >/dev/null ; then
sed -i 's@^\(127[.]0[.]1[.]1[[:space:]]\+\)@\1'"${hostnames}"' @' \
/etc/hosts
else
sed -i 's@^\(127[.]0[.]0[.]1[[:space:]]\+.*\)$@\1\n127.0.1.1 '"${hostnames}"' @' \
/etc/hosts
fi
service hostname start
}
install_ruby_packages() {
apt-get -y install ruby ruby-dev libopenssl-ruby rdoc ri irb \
build-essential zlib1g-dev libssl-dev libreadline5-dev wget ssl-cert
}
build_rubygems() {
if gem --version | grep -q "${__rubygems_version}" >/dev/null ; then
log "RubyGems ${__rubygems_version} is installed, so skipping..."
return
fi
# Download and extract the source
(cd /tmp && wget http://files.rubyforge.vm.bytemark.co.uk/rubygems/rubygems-${__rubygems_version}.tgz)
(cd /tmp && tar xfz rubygems-${__rubygems_version}.tgz)
# Setup and install
(cd /tmp/rubygems-$__rubygems_version && ruby setup.rb --no-format-executable)
# Clean up the source artifacts
rm -rf /tmp/rubygems-${__rubygems_version}*
}
install_chef() {
gem install ohai --no-ri --no-rdoc
gem install chef --no-ri --no-rdoc
}
build_chef_solo_config() {
mkdir -p /etc/chef
cat > /etc/chef/solo.rb <<SOLO_RB
file_cache_path "/tmp/chef-solo"
cookbook_path "/tmp/chef-solo/cookbooks"
SOLO_RB
cat > /etc/chef/bootstrap.json <<BOOTSTRAP_JSON
{
"chef_server" : {
"webui_enabled" : true,
"ssl_req" : "/C=CA/ST=Several/L=Locality/O=Example/OU=Operations/CN=${__hostname}/emailAddress=root@${__hostname}"
},
"run_list" : [
"recipe[chef-server::rubygems-install]",
"recipe[chef-server::apache-proxy]"
]
}
BOOTSTRAP_JSON
}
run_chef_solo() {
chef-solo -c /etc/chef/solo.rb -j /etc/chef/bootstrap.json \
-r $bootstrap_tar_url
}
# Parse CLI arguments
while [[ $# -gt 0 ]] ; do
token="$1"
shift
case "$token" in
--rubygems-version|-r)
case "$1" in
*.*.*)
__rubygems_version="$1"
shift
;;
*)
fail "--rubygems-version must be followed by a version number x.y.z"
;;
esac
;;
--hostname|-h)
case "$1" in
*.*)
__hostname="$1"
shift
;;
*)
fail "--hostname must be followed by a FQDN, i.e. foo.example.com"
;;
esac
;;
help|usage)
usage
exit 0
;;
*)
usage
exit 1
;;
esac
done
if [[ -z "$__hostname" ]] ; then
fail "--hostname is a required option"
fi
if [[ -z "$__rubygems_version" ]] ; then
__rubygems_version="$default_rubygems_version"
fi
# Perform the actual bootstrap
set_hostname
install_ruby_packages
build_rubygems
install_chef
build_chef_solo_config
run_chef_solo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment