Skip to content

Instantly share code, notes, and snippets.

@zircote
Created November 11, 2012 06:37
Show Gist options
  • Save zircote/4053971 to your computer and use it in GitHub Desktop.
Save zircote/4053971 to your computer and use it in GitHub Desktop.
AWS Route53 Hostname manipulation Chef Cookbook (needs more time and tests but is working as desired with VPC)
# cluster identifier
default[:route53][:zone] = "my.default-domain.com"
interface "eth0" {
supersede host-name "<%= node['hostname'] %>";
supersede domain-name "<%= node['route53']['zone'] %> ec2.internal";
}
#!/bin/bash
#
# /etc/sudoers
#
# Generated by Chef for <%= node[:fqdn] %>
#
. /etc/profile.d/aws-apitools-common.sh
export PATH=/opt/aws/bin:$PATH
ec2-describe-tags -O $AWS_ACCESS_KEY_ID -W $AWS_SECRET_ACCESS_KEY \
--filter "resource-id=${1}" \
--filter "resource-type=instance" --filter "key=Name" | cut -f5
#
# Cookbook Name:: aws_base
# Recipe:: default
#
# Copyright 2012, zircote
#
# All rights reserved - Do Not Redistribute
#
unless node['ec2']['instance_id'].to_s.strip.length == 0
aws = data_bag_item("aws", "route53")
if node['ec2']['public_hostname'].to_s.strip.length == 0
cname =node['ec2']['local_ipv4']
else
cname = node['ec2']['public_hostname']
end
execute "install_pip" do
command <<-EOF
if ! which cli53; then
sudo easy_install -U pip
sudo pip install -U cli53
fi
EOF
end
template "/usr/bin/ec2-name" do
source "ec2-name.erb"
owner "root"
group "root"
mode 0755
variables(
{:aws => aws}
)
end
service "network" do
supports :status => true, :restart => true, :reload => true
action :nothing
end
template "/etc/dhcp/dhclient.conf" do
source "dhclient.conf.erb"
owner "root"
group "root"
mode 0644
variables({
:hostname => node[:hostname]
})
notifies :restart, resources(:service => "network"), :immediately
action :nothing
end
ruby_block "set and fetch hostnames" do
block do
command = <<-EOF
export AWS_ACCESS_KEY_ID=#{aws['aws_access_key_id']}
export AWS_SECRET_ACCESS_KEY=#{aws['aws_secret_access_key']}
/usr/bin/ec2-name #{node[:ec2][:instance_id]}
EOF
hostname = `#{command}`.sub('.' + node['route53']['zone'], '').to_s.strip()
if hostname.length == 0
hostname = node[:ec2][:instance_id1]
end
node.automatic_attrs[:hostname] = hostname
node.automatic_attrs[:fqdn] = "#{node['hostname']}.#{node['route53']['zone']}"
record_type = 'A'
unless /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.match(cname)
record_type = 'CNAME'
end
shell=<<-EOF
export AWS_ACCESS_KEY_ID=#{aws['aws_access_key_id']}
export AWS_SECRET_ACCESS_KEY=#{aws['aws_secret_access_key']}
echo "127.0.0.1 #{node['fqdn']} #{node['hostname']} localhost" > /etc/hosts
echo "#{node['hostname']}" > /etc/hostname
hostname --file /etc/hostname
cli53 rrcreate -x 600 -r #{node['route53']['zone']} #{node['hostname']} #{record_type} #{cname}
EOF
unless /Success/.match(`#{shell}`)
Chef::Log.info("Failed to set Hostname")
end
end
notifies :create, resources(:template => "/etc/dhcp/dhclient.conf"), :immediately
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment