Skip to content

Instantly share code, notes, and snippets.

@jbz
Created August 2, 2011 20:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jbz/1121173 to your computer and use it in GitHub Desktop.
Save jbz/1121173 to your computer and use it in GitHub Desktop.
EC2 userdata.sh script to install Chef and register with Chef server
#!/bin/bash
# User data to configure a vanilla Ubuntu EC2 instance.
# Installs chef-client (with minimal dependencies),
# configures chef, and adds roles
test $UID == 0 || (echo "Error: must run as root"; exit 1)
######### STEP 1: OPERATING SYSTEM CONFIGURATION
BOOTLOG="/var/log/bootstrap.log"
# In a virtual private cloud...
# Use public apt sources instead of internal EC2 mirrors
<% if use_vpc? %>
perl -pi -e 's/[\w-]+\.ec2/us/' /etc/apt/sources.list
<% end %>
apt-get update -y
echo "Performing apt-get upgrade..." >>$BOOTLOG
apt-get upgrade -y
######### STEP 2: RUBY, GEMS, RUNIT, AND CHEF
CHEF_VERSION='0.10.0'
# Miminal apt packages to install rubygems, chef gem
echo "Installing Ruby and dependencies..." >>$BOOTLOG
apt-get install ruby ruby-dev libopenssl-ruby curl runit -y
# Install rubygems if it's not already installed
which gem || (
echo "Installing RubyGems..." >>$BOOTLOG
tmp=`mktemp -d`
cd $tmp
curl -s 'http://s3.amazonaws.com/Packages_Ubuntu_Hardy_64/rubygems-1.3.7.tgz' | tar -zx
cd rubygems-1.3.7
ruby setup.rb --no-format-executable --no-ri --no-rdoc
cd -
rm -rf $tmp)
######### STEP 3: INSTALL CHEF GEM
echo "Installing Chef version ${CHEF_VERSION}..." >>$BOOTLOG
which chef-client || gem install chef -v${CHEF_VERSION} --no-ri --no-rdoc
######### STEP 3: CONFIGURE CHEF CLIENT
mkdir -p /etc/chef
# Write the validation certificate
cat > /etc/chef/validation.pem <<EOF
<%= chef_validator_key %>
EOF
# Write chef-client config
echo "Writing client configuration..." >>$BOOTLOG
NODE_NAME=<%= [project_name, stage_name, role]*'.' %>.`curl -s http://169.254.169.254/1.0/meta-data/instance-id/`
ATTRIBUTES_FILE=/etc/chef/attributes.json
cat > /etc/chef/client.rb <<EOF
node_name '$NODE_NAME'
log_level :info
log_location '/var/log/chef-client.log'
chef_server_url '<%= chef_api_url %>'
validation_client_name '<%= chef_validator_name %>'
json_attribs '$ATTRIBUTES_FILE'
environment '<%=chef_environment_name %>'
EOF
# Set roles for first chef run
echo "Writing node runlist..." >>$BOOTLOG
cat > $ATTRIBUTES_FILE <<EOF
{
"run_list": [
"recipe[chef-client::config]",
"recipe[chef-client::service]",
"recipe[chef-client::delete_validation]",
"recipe[runit]",
"role[riak-node]"
],
"chef_client": {
"server_url": "<%= chef_api_url %>",
"validation_client_name": "<%= chef_validator_name %>",
"environment": "<%=chef_environment_name %>"
},
"chef_packages": { "chef": {"version": "${CHEF_VERSION}" }}
}
EOF
######### STEP 3: CONFIGURE CHEF RUNIT SERVICE
mkdir -p /etc/sv/chef-client/supervise
mkdir -p /etc/sv/chef-client/log/main
mkdir -p /etc/sv/chef-client/log/supervise
chmod 0700 /etc/sv/chef-client/supervise
chmod 0700 /etc/sv/chef-client/log/supervise
cat >/etc/sv/chef-client/run <<EOF
#!/bin/bash
# (changed to run under bash, to support RVM)
PATH=/usr/local/bin:/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin
# Load RVM
RVM_LOADER="/usr/local/rvm/scripts/rvm"
[ -f \$RVM_LOADER ] && . \$RVM_LOADER
exec 2>&1
exec /usr/bin/env chef-client -i 30
EOF
cat >/etc/sv/chef-client/log/run <<EOF
#!/bin/sh
exec svlogd -tt ./main
EOF
chmod 0755 /etc/sv/chef-client/run
chmod 0755 /etc/sv/chef-client/log/run
ln -s /etc/sv/chef-client /etc/service/chef-client
ln -s /usr/bin/sv /etc/init.d/chef-client
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment