Skip to content

Instantly share code, notes, and snippets.

@makuk66
Created July 19, 2011 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makuk66/1093016 to your computer and use it in GitHub Desktop.
Save makuk66/1093016 to your computer and use it in GitHub Desktop.
Chef knife ec2 server create template for Natty, with node name prefix
# This is a knife ec2 server create template for Ubuntu 11.4.
# It is based on the 10.4 version in the 0.10.2 Chef distribution available here:
# https://github.com/opscode/chef/blob/master/chef/lib/chef/knife/bootstrap/ubuntu10.04-apt.erb
# with modifications to:
# - use the natty APT repository
# - install Chef 0.10.2
# - avoid starting the /etc/init.d/chef-client service until the client.rb has been written
# - allow a CHEF_NODE_NAME_PREFIX environment variable to prefix to the node name
bash -c '
# MAK: use lsb-release to pick up release name, and add -0.10 to get chef 0.10
if [ ! -f /usr/bin/chef-client ]; then
echo "chef chef/chef_server_url string <%= Chef::Config[:chef_server_url] %>" | debconf-set-selections
[ -f /etc/apt/sources.list.d/opscode.list ] || echo "deb http://apt.opscode.com "`lsb_release -cs`"-0.10 main" > /etc/apt/sources.list.d/opscode.list
wget -O- http://apt.opscode.com/packages@opscode.com.gpg.key | apt-key add -
fi
apt-get update
# MAK: use policy-rc.d to prevent chef-client starting and registering before we write client.rb
(cat <<'EOP'
#!/bin/sh
exit 101
EOP
) > /usr/sbin/policy-rc.d
chmod 755 /usr/sbin/policy-rc.d
apt-get install -y chef
# MAK: remove policy.rc
rm -f /usr/sbin/policy-rc.d
<% unless Chef::Config[:validation_client_name] == "chef-validator" -%>
[ `grep -qx "validation_client_name \"<%= Chef::Config[:validation_client_name] %>\"" /etc/chef/client.rb` ] || echo "validation_client_name \"<%= Chef::Config[:validation_client_name] %>\"" >> /etc/chef/client.rb
<% end -%>
(
cat <<'EOP'
<%= IO.read(Chef::Config[:validation_key]) %>
EOP
) > /tmp/validation.pem
awk NF /tmp/validation.pem > /etc/chef/validation.pem
rm /tmp/validation.pem
<% if @config[:chef_node_name] %>
[ `grep -qx "node_name \"<%= @config[:chef_node_name] %>\"" /etc/chef/client.rb` ] || echo "node_name \"<%= @config[:chef_node_name] %>\"" >> /etc/chef/client.rb
<% end -%>
# MAK: use an environment variable to pass in a hostname prefix, so your node gets called e.g. web-server-i-123abc
<% if (! ENV['CHEF_NODE_NAME_PREFIX'].nil?) and ::File.exists?('/usr/bin/ec2metadata') %>
(
cat <<'EOP'
node_name "<%= ENV['CHEF_NODE_NAME_PREFIX'] %>`ec2metadata --instance-id`"
EOP
) >> /etc/chef/client.rb
<% end -%>
<% unless (Chef::Config[:environment] == "" or Chef::Config[:environment] == "_default") -%>
[ `grep -qx "environment \"<%= Chef::Config[:environment] %>\"" /etc/chef/client.rb` ] || echo "environment \"<%= Chef::Config[:environment] %>\"" >> /etc/chef/client.rb
<% end -%>
(
cat <<'EOP'
<%= { "run_list" => @run_list }.to_json %>
EOP
) > /etc/chef/first-boot.json
/usr/bin/chef-client -j /etc/chef/first-boot.json
# MAK: start chef-client because we prevented that previously
/etc/init.d/chef-client start
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment