Skip to content

Instantly share code, notes, and snippets.

@cphrmky
Forked from thommay/ec2.rb
Created January 14, 2013 16:23
Show Gist options
  • Save cphrmky/4531246 to your computer and use it in GitHub Desktop.
Save cphrmky/4531246 to your computer and use it in GitHub Desktop.
require_recipe 'fog'
route53_rr node[:ec2][:instance_id] do
record_type "CNAME"
fqdn "#{node[:ec2][:instance_id]}.#{node["route53"]["domain"]}"
rdata(["#{node[:ec2][:public_hostname]}."])
accesskey node["route53"]["accesskey"]
secretkey node["route53"]["secretkey"]
zoneid node["route53"]["zoneid"]
action :update
end
new_hostname = "#{node["route53"]["ec2"]["type"]}-#{node["route53"]["ec2"]["env"]}-#{node[:ec2][:instance_id]}"
new_fqdn = "#{new_hostname}.#{node["route53"]["domain"]}"
route53_rr new_hostname do
record_type "CNAME"
fqdn new_fqdn
rdata(["#{node[:ec2][:public_hostname]}."])
accesskey node["route53"]["accesskey"]
secretkey node["route53"]["secretkey"]
zoneid node["route53"]["zoneid"]
action :update
end
ruby_block "edit resolv.conf" do
block do
rc = Chef::Util::FileEdit.new("/etc/resolv.conf")
rc.search_file_replace_line(/^search/, "search #{node["route53"]["domain"]} compute-1.internal")
rc.search_file_replace_line(/^domain/, "domain #{node["route53"]["domain"]}")
rc.write_file
end
end
ruby_block "edit etc hosts" do
block do
rc = Chef::Util::FileEdit.new("/etc/hosts")
rc.search_file_replace_line(/^127\.0\.0\.1.*localhost$/, "127.0.0.1 #{new_fqdn} #{new_hostname} localhost")
rc.write_file
end
end
execute "hostname --file /etc/hostname" do
action :nothing
end
file "/etc/hostname" do
content "#{new_hostname}"
notifies :run, "execute[hostname --file /etc/hostname]", :immediately
end
node.automatic_attrs["hostname"] = new_hostname
node.automatic_attrs["fqdn"] = new_fqdn
node.automatic_attrs["domain"] = node.route53.domain
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment