Skip to content

Instantly share code, notes, and snippets.

@ticean
Created March 9, 2012 23:34
Show Gist options
  • Save ticean/2009297 to your computer and use it in GitHub Desktop.
Save ticean/2009297 to your computer and use it in GitHub Desktop.
Experimental Custom Boostrap
bash <<'EOF' || echo "Chef bootstrap failed!"
# This is a mixture of the Ironfan and @fnichol bootstraps.
# It keeps the chef ruby away from our other rubies.
#
# * installs a chef ruby using ruby_build.
# * upgrades rubygems rather than installing from source
# * pushes the node identity into the first-boot.json
# * installs the chef-client service and kicks off the first run of chef
#set -e
#<%= (@config[:verbosity].to_i > 1 ? 'set -v' : '') %>
CHEF_VERSION=<%= bootstrap_version_string.gsub(/.*[\s=]/,"") %>
RUBY_VERSION="1.9.3-p125"
RUBY_HOME="/opt/chef_ruby"
mkdir -p /tmp/knife-bootstrap
chmod 700 /tmp/knife-bootstrap
cd /tmp/knife-bootstrap
<%= "export http_proxy=\"#{knife_config[:bootstrap_proxy]}\"" if knife_config[:bootstrap_proxy] -%>
eval `cat /etc/lsb-release `
export DEBIAN_FRONTEND=noninteractive
date > /etc/box_build_time
echo -e "`date` \n\n**** \n**** apt update:\n****\n"
apt-get -y update
#apt-get -y upgrade
if [ ! -f /usr/bin/chef-client ]; then
### Ensure variables are declared
echo -e "*********\nCHEF_VERSION: $CHEF_VERSION\n**********"
[[ -z "$RUBY_VERSION" ]] && printf ">>> RUBY_VERSION must be set.\n" && exit 10
[[ -z "$RUBY_HOME" ]] && printf ">>> RUBY_HOME must be set.\n" && exit 11
### Install Packages
set -x
echo -e "`date` \n\n**** \n**** Installing base packages:\n****\n"
apt-get update
apt-get install -y sed grep tar gzip bzip2 bash curl git-core \
build-essential bison openssl libreadline6 libreadline6-dev \
zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 \
libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev autoconf \
libc6-dev ssl-cert
apt-get clean
if [[ ! -d "$RUBY_HOME" ]] ; then
### Install Self Contained Ruby with ruby-build
echo -e "**** \n**** Installing Ruby $RUBY_VERSION:\n****\n"
clone_dir=/tmp/ruby-build-$$
git clone https://github.com/sstephenson/ruby-build.git $clone_dir
$clone_dir/bin/ruby-build "$RUBY_VERSION" "$RUBY_HOME"
rm -rf $clone_dir
unset clone_dir
fi
### Installing Chef Gems
# core chef gems
${RUBY_HOME}/bin/gem install ohai --no-ri --no-rdoc
${RUBY_HOME}/bin/gem install chef --no-ri --no-rdoc <%= bootstrap_version_string %>
# used by user resource to set passwords
${RUBY_HOME}/bin/gem install ruby-shadow --no-ri --no-rdoc
# symlink chef binaries into /usr/bin
for b in chef-client chef-solo knife shef ohai ; do
ln -snf "${RUBY_HOME}/bin/$b" "/usr/bin/$b"
done
### Chef Cache Directory Setup
mkdir -p /var/chef/cache
chown root:root /var/chef/cache
else # no chef-client
echo -e "`date` \n\n**** \n**** Chef is present -- skipping apt/ruby/chef installation\n****\n"
fi # end ruby+chef install
apt-get install -y rsync
# fix a bug in chef that prevents debugging template errors
# will not work with --prerelease but that's OK hopefully opscode patches this crap soon
#bad_template_file="/usr/lib/ruby/gems/${RUBY_VERSION}/gems/chef-${CHEF_VERSION}/lib/chef/mixin/template.rb"
#if echo "0505c482b8b0b333ac71bbc8a1795d19 $bad_template_file" | md5sum -c - 2>/dev/null ; then
# curl https://github.com/mrflip/chef/commit/655a1967253a8759afb54f30b818bbcb7c309198.patch | sudo patch $bad_template_file
#fi
echo -e "`date` \n\n**** \n**** Knifing in the chef client config files:\n****\n"
mkdir -p /etc/chef
<%- if @config[:client_key] %>
(
cat <<'EOP'
<%= @config[:client_key] %>
EOP
) > /tmp/knife-bootstrap/client.pem
awk NF /tmp/knife-bootstrap/client.pem > /etc/chef/client.pem
<%- else %>
(
cat <<'EOP'
<%= validation_key %>
EOP
) > /tmp/knife-bootstrap/validation.pem
awk NF /tmp/knife-bootstrap/validation.pem > /etc/chef/validation.pem
<%- end %>
echo -e "`date` \n\n**** \n**** Nuking our temp files:\n****\n"
cd /tmp
rm -rf /tmp/knife-bootstrap
echo -e "`date` \n\n**** \n**** Creating chef client script:\n****\n"
(
cat <<'EOP'
<%= config_content %>
<%= @config[:node].chef_client_script_content %>
EOP
) > /etc/chef/client.rb
(
cat <<'EOP'
<%= { "run_list" => @run_list, "cluster_name" => @config[:node].cluster_name, "facet_name" => @config[:node].facet_name, "facet_index" => @config[:node].facet_index }.to_json %>
EOP
) > /etc/chef/first-boot.json
echo -e "`date` \n\n**** \n**** Adding chef client runit scripts:\n****\n"
( service chef-client stop >/dev/null 2>&1 ; sleep 1 ; killall chef-client 2>/dev/null ) || true
mkdir -p /var/log/chef /var/chef /etc/service /etc/sv/chef-client/{log/main,supervise}
cat > /etc/sv/chef-client/log/run <<EOP
#!/bin/bash
exec svlogd -tt ./main
EOP
cat > /etc/sv/chef-client/run <<EOP
#!/bin/bash
exec 2>&1
exec /usr/bin/env chef-client -i 43200 -s 20 -L /var/log/chef/client.log
EOP
chmod +x /etc/sv/chef-client/log/run /etc/sv/chef-client/run
ln -nfs /usr/bin/sv /etc/init.d/chef-client
service chef-client stop >/dev/null 2>&1 || true
<%- if (@config[:bootstrap_runs_chef_client].to_s == 'true') || (@chef_config.knife[:bootstrap_runs_chef_client].to_s == 'true') %>
echo -e "`date` \n\n**** \n**** First run of chef:\n****\n"
set -e
<%= start_chef %>
set +e
<%- end %>
echo -e "`date` \n\n**** \n**** Cleanup:\n****\n"
# make locate work good
updatedb
echo -e "`date` \n\n**** \n**** Enabling chef client service:\n****\n"
ln -nfs /etc/sv/chef-client /etc/service/chef-client
service chef-client start
echo -e "`date` \n\n**** \n**** Cluster Chef client bootstrap complete\n****\n"
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment