From b470ceb9b6b0f711b85e3b4710ec607eb6229f00 Mon Sep 17 00:00:00 2001 From: aes_ng Date: Fri, 2 Mar 2012 16:05:37 +0200 Subject: [PATCH] Support for dns aliases, dynamic creation of zone files (dependent on the environment fact) from Facter facts and a fix for environment support in puppet. --- custom/lib/facter/dnsaliases.rb | 8 +++++++ dns/files/create_dynamic_records.sh | 35 +++++++++++++++++++++++++++++ dns/manifests/init.pp | 9 +++++--- dnsaliases/manifests/init.pp | 21 +++++++++++++++++ puppet/manifests/init.pp | 2 +- puppet/templates/puppet.conf.erb | 4 ++++ 6 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 custom/lib/facter/dnsaliases.rb create mode 100755 dns/files/create_dynamic_records.sh create mode 100644 dnsaliases/manifests/init.pp diff --git a/custom/lib/facter/dnsaliases.rb b/custom/lib/facter/dnsaliases.rb new file mode 100644 index 0000000..55cf53c --- /dev/null +++ b/custom/lib/facter/dnsaliases.rb @@ -0,0 +1,8 @@ + +if File.exists?("/etc/dnsaliases") + Facter.add("dnsaliases") do + setcode do + Facter::Util::Resolution.exec('cat /etc/dnsaliases').chomp + end + end +end diff --git a/dns/files/create_dynamic_records.sh b/dns/files/create_dynamic_records.sh new file mode 100755 index 0000000..8e253e8 --- /dev/null +++ b/dns/files/create_dynamic_records.sh @@ -0,0 +1,35 @@ +#!/bin/sh + +if [ $# != 2 ]; then + echo "ERR: Must have two arguments: DNS server name and zone/environment" 1>&2 + return 2 +fi + +ZONE=$2 +SERVER=$1 +FACTS_DIR="/var/lib/puppet/yaml/facts" +ZONE_FILE="/srv/puppet/files/common/dns/db.$ZONE-dynamic.$SERVER" + +if [ ! -d "${FACTS_DIR}" ]; then + echo "ERR: Cannot find Facter facts directory ${FACTS_DIR}" 1>&2 + exit 3 +fi + +if [ -f "${ZONE_FILE}" ]; then + rm $ZONE_FILE +fi +cd $FACTS_DIR +for i in `grep -l "environment: $ZONE" *`; +do + echo $i + hn=`grep hostname $i | cut -f 2 -d ':' | tr -d ' '` + ip=`grep "ipaddress:" $i | cut -f 2 -d ':'` + a=`grep "aliases:" $i | cut -f 2 -d ':' | tr -d '"' | tr -d ' '| tr ',' "\n"`; + echo "$hn IN A $ip" >> $ZONE_FILE + if [ "$a" != "" ]; then + for j in $a; + do + echo "$j CNAME $hn" >> $ZONE_FILE + done + fi +done diff --git a/dns/manifests/init.pp b/dns/manifests/init.pp index e6cba45..2f09728 100644 --- a/dns/manifests/init.pp +++ b/dns/manifests/init.pp @@ -236,9 +236,12 @@ define dns::zone($role = "master", $master = [], $slaves = [], $source = "AUTO") } file { "${dns::server::chroot}${zonedir}/db.${zonefile}-dynamic": ensure => present, - source => [ "puppet:///files/dns/db.${zonefile}-dynamic.${homename}", - "puppet:///modules/dns/empty", ], - mode => 0640, + content => generate( + "/bin/sh", + "/etc/puppet/modules/dns/files/create_dynamic_records.sh", + $homename, + $zone), + mode => 0640, owner => "root", group => $dns::server::group, require => $operatingsystem ? { diff --git a/dnsaliases/manifests/init.pp b/dnsaliases/manifests/init.pp new file mode 100644 index 0000000..ddf107a --- /dev/null +++ b/dnsaliases/manifests/init.pp @@ -0,0 +1,21 @@ +# . +# +# === Global variables +# +# $aliases: +# Comma-separated list of aliases for this host. +# + +class dnsaliases( $aliases ) { + + $content = "$aliases\n" + + file { "/etc/dnsaliases": + ensure => present, + mode => 0644, + owner => root, + group => root, + content => $content, + } + +} diff --git a/puppet/manifests/init.pp b/puppet/manifests/init.pp index 00c775e..a436753 100644 --- a/puppet/manifests/init.pp +++ b/puppet/manifests/init.pp @@ -1,7 +1,7 @@ # Install and configure Puppet client. # -class puppet::client { +class puppet::client ($environment='') { tag("bootstrap") diff --git a/puppet/templates/puppet.conf.erb b/puppet/templates/puppet.conf.erb index d214146..bd6f2e6 100644 --- a/puppet/templates/puppet.conf.erb +++ b/puppet/templates/puppet.conf.erb @@ -39,6 +39,10 @@ # The bit length of keys. keylength = <%= puppet_keylength %> +<% if !(environment == '') %> + # Environment this entity is in. + environment = <%= environment %> +<% end -%> <% if puppetversion[/\d+/].to_i >= 2 -%> [agent] # Ignore site manifest when run as agent. Fixes warnings about