Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
# chef install
sudo apt-get -q -y install ruby ruby1.8-dev libopenssl-ruby1.8 rdoc ri irb build-essential wget >> /tmp/chef_bootstrap.log
cd /tmp
wget http://rubyforge.org/frs/download.php/55066/rubygems-1.3.2.tgz
tar zxvf rubygems-1.3.2.tgz >> /tmp/chef_bootstrap.log
cd rubygems-1.3.2
sudo ruby setup.rb --no-ri --no-rdoc >> /tmp/chef_bootstrap.log
sudo ln -sfv /usr/bin/gem1.8 /usr/bin/gem >> /tmp/chef_bootstrap.log
sudo gem sources -a http://gems.opscode.com >> /tmp/chef_bootstrap.log
sudo gem install ohai chef --no-ri --no-rdoc >> /tmp/chef_bootstrap.log
# chef configure
mkdir /etc/chef
mkdir /var/chef
mkdir /var/log/chef
echo -ne '# Chef Solo config
log_level :info
log_location "/var/log/chef/chef.log"
file_cache_path "/var/chef"
file_store_path "/var/chef"
cookbook_path "/var/chef/cookbooks"
ssl_verify_mode :verify_none
Chef::Log::Formatter.show_time = false
' > /etc/chef/solo.rb
# chef runscript
echo -ne '#!/usr/bin/ruby
require "rubygems"
require "open-uri"
require "json"
require "tempfile"
API_VERSION = "2008-02-01"
user_data_url = "http://169.254.169.254/#{API_VERSION}/user-data"
begin
user_data = open(user_data_url).read
rescue OpenURI::HTTPError
puts "User data not found"
exit
end
puts "User data:\n#{user_data}"
begin
data = JSON.parse(user_data)
puts "User data is a valid json string"
unless data["cookbooks"]
puts "User data does not contain cookbooks path"
exit
end
unless data["dna"]
puts "User data does not contain dna path"
exit
end
user_data_file = Tempfile.new("user_data")
user_data_file.chmod(0700)
user_data_file.write(data["dna"].to_json)
user_data_file.close
system "chef-solo -c /etc/chef/solo.rb -r #{data["cookbooks"]} -j #{user_data_file.path}"
user_data_file.unlink
rescue JSON::ParserError
puts "User data not a valid json string"
puts "json exception: #{$!}"
end
' > /usr/sbin/chef-run-dna
chmod 755 /usr/sbin/chef-run-dna
# run chef on first boot
sed -i '/^exit/ i \/usr\/sbin\/chef-run-dna > /tmp/chef-run-dna.log' /root/firstboot.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.