Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisroberts committed Jun 21, 2013
0 parents commit ff7a331
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# v0.0.1
* Initial commit
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# DNS

Automatically create DNS records for your nodes
8 changes: 8 additions & 0 deletions attributes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
default[:dns][:provider] = node[:cloud][:provider]
default[:dns][:domain] = node[:domain]
default[:dns][:credentials] = {}
default[:dns][:disable] = !node[:cloud]
default[:dns][:entry][:name] = node[:fqdn]
default[:dns][:entry][:type] = 'A'
default[:dns][:entry][:value] = node[:ipaddress]
default[:dns][:chef_client_config] = false
9 changes: 9 additions & 0 deletions metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name 'dns'
maintainer 'Chris Roberts'
maintainer_email '[email protected]'
license 'Apache 2.0'
description 'Create DNS entries for nodes'

version '0.0.1'

depends 'build-essential', '>= 1.1.0' # set minimum so we get compile time support
9 changes: 9 additions & 0 deletions recipes/chef-client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
include_recipe 'chef-client::config'

# Force chef-client config to use node name
begin
client_config = node.run_context.resource_collection.lookup("template[#{node["chef_client"]["conf_dir"]}/client.rb]")
client_config.variables.update(:chef_node_name => Chef::Config[:node_name])
rescue => e
Chef::Log.warn "Failed to locate chef client.rb template resource. If client.rb is managed, please check why it cannot be found: #{e}"
end
53 changes: 53 additions & 0 deletions recipes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require 'resolv'

include_recipe 'dns::fog'

ruby_block 'Apply DNS information' do
block do
require 'fog'

node.default[:dns][:entry][:name] = [node.name, node[:dns][:domain]].join('.')

con = Fog::DNS.new(
node[:dns][:credentials].merge(
:provider => node[:dns][:provider]
)
)
domain = con.list_domains.body['domains'].detect do |domain_hash|
domain_hash['name'] == node[:dns][:domain]
end
raise "Failed to locate registered domain in account for configured domain: #{node[:dns][:domain]}" unless domain
zone = con.zones.get(domain['id'])
raise "Failed to locate zone for configured domain: #{node[:dns][:domain]}" unless zone
record = zone.records.detect do |r|
r.name == node[:dns][:entry][:name]
end
if(record)
Chef::Log.info "DNS - Found existing record for: #{node[:dns][:entry][:name]}. Updating."
Chef::Log.info "DNS - Existing type: #{record.type} Existing value: #{record.value}"
record.value = node[:dns][:entry][:value]
record.type = node[:dns][:entry][:type].upcase
record.save
else
Chef::Log.info "DNS - No existing record found for #{node[:dns][:entry][:name]}. Creating."
zone.records.create(
:value => node[:dns][:entry][:value],
:name => node[:dns][:entry][:name],
:type => node[:dns][:entry][:type].upcase
)
end
Chef::Log.info "DNS - Record saved: name: #{node[:dns][:entry][:name]} type: #{node[:dns][:entry][:type]} value: #{node[:dns][:entry][:value]}"
end
not_if do
node[:dns][:disable] ||
begin
Resolv.getaddress(node[:dns][:entry][:name]) == node[:dns][:entry][:value]
rescue Resolv::ResolvError
false
end
end
end

if(node[:dns][:chef_client_config])
include_recipe 'dns::chef-client'
end
15 changes: 15 additions & 0 deletions recipes/fog.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
node.set[:build_essential][:compiletime] = true
include_recipe 'build-essential'

# Dependencies required by nokogiri (for fog)
%w(libxslt-dev libxml2-dev).each do |pkg|
c_pkg = package(pkg)
c_pkg.run_action(:install)
end


# TODO: Remove this once the gem_hell cookbook is ready to roll
chef_gem "fog" do
version '1.10.1'
action :install
end
33 changes: 33 additions & 0 deletions recipes/fqdn.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Ensure we have our entry name defaulted
node.default[:dns][:entry][:name] = [node.name, node[:dns][:domain]].join('.')

# Set desired hostname and fqdn based on entry name
node.set[:hosts_file][:hostname] = node[:dns][:entry][:name].split('.').first
node.set[:hosts_file][:fqdn] = node[:dns][:entry][:name]

include_recipe 'hosts_file'

template 'fqdn_set_hosts_file' do
source 'hosts.erb'
path node[:hosts_file][:path]
mode 0644
action :nothing
not_if do
node[:fqdn] == node[:dns][:entry][:name]
end
end.run_action(:create)

file '/etc/hostname' do
content "#{node[:hosts_file][:hostname]}\n"
end

execute "Set hostname to #{node[:hosts_file][:hostname]}" do
action :nothing
command "hostname #{node[:hosts_file][:hostname]}"
not_if "hostname | grep #{node[:hosts_file][:hostname]}"
end.run_action(:run)

ohai 'fqdn' do
action :nothing
end.run_action(:reload)

0 comments on commit ff7a331

Please sign in to comment.