[Pkg-puppet-devel] [SCM] Puppet packaging for Debian branch, experimental, updated. debian/2.6.8-1-844-g7ec39d5

Nick Lewis nick at puppetlabs.com
Tue May 10 08:01:51 UTC 2011


The following commit has been merged in the experimental branch:
commit ee7d2f92f9d3ec45b5c3a9cd3fe2f5832b17f23b
Author: Stefan Schulte <stefan.schulte at taunusstein.net>
Date:   Wed Nov 17 23:15:46 2010 +0100

    (#5274) New comment property for the hosttype
    
    When the parsefile provider for host parses /etc/hosts, it throws any
    inlinecomment away. As a result they are also purged in /etc/hosts after
    a puppetrun that detects a change. That could be dangerous because you
    will lose information even about unmanaged resources.
    
    So if you have something like
    
    192.168.0.1 hostentry_not_managed_by_puppet # Important comment
    
    in /etc/hosts the endresult will be
    
    192.168.0.1\thostentry_not_managed_by_puppet
    
    This patch introduces a new property "comment" for the host type. The
    provider is nearly a complete rewrite and a lot shorter and hopefully
    easier to understand.

diff --git a/lib/puppet/provider/host/parsed.rb b/lib/puppet/provider/host/parsed.rb
index 4f15eff..a303c4b 100644
--- a/lib/puppet/provider/host/parsed.rb
+++ b/lib/puppet/provider/host/parsed.rb
@@ -8,66 +8,36 @@ else
 end
 
 
-      Puppet::Type.type(:host).provide(
-        :parsed,
-  :parent => Puppet::Provider::ParsedFile,
-  :default_target => hosts,
-        
-  :filetype => :flat
-) do
+Puppet::Type.type(:host).provide(:parsed,:parent => Puppet::Provider::ParsedFile,
+  :default_target => hosts,:filetype => :flat) do
   confine :exists => hosts
 
   text_line :comment, :match => /^#/
   text_line :blank, :match => /^\s*$/
 
-  record_line :parsed, :fields => %w{ip name host_aliases},
-    :optional => %w{host_aliases},
-    :rts => true do |line|
-    hash = {}
-    if line.sub!(/^(\S+)\s+(\S+)\s*/, '')
-      hash[:ip] = $1
-      hash[:name] = $2
-
-      if line.empty?
-        hash[:host_aliases] = []
+  record_line :parsed, :fields => %w{ip name host_aliases comment},
+    :optional => %w{host_aliases comment},
+    :match    => /^(\S+)\s+(\S+)\s*(.*?)?(?:\s*#\s*(.*))?$/,
+    :post_parse => proc { |hash|
+      # An absent comment should match "comment => ''"
+      hash[:comment] = '' if hash[:comment].nil? or hash[:comment] == :absent
+      unless hash[:host_aliases].nil? or hash[:host_aliases] == :absent
+        hash[:host_aliases] = hash[:host_aliases].split(/\s+/)
       else
-        line.sub!(/\s*/, '')
-        line.sub!(/^([^#]+)\s*/) do |value|
-          aliases = $1
-          unless aliases =~ /^\s*$/
-            hash[:host_aliases] = aliases.split(/\s+/)
-          end
-
-          ""
-        end
+        hash[:host_aliases] = []
       end
-    else
-      raise Puppet::Error, "Could not match '#{line}'"
-    end
-
-    hash[:host_aliases] = [] if hash[:host_aliases] == ""
-
-    return hash
-  end
-
-  # Convert the current object into a host-style string.
-  def self.to_line(hash)
-    return super unless hash[:record_type] == :parsed
-    [:ip, :name].each do |n|
-      raise ArgumentError, "#{n} is a required attribute for hosts" unless hash[n] and hash[n] != :absent
-    end
-
-    str = "#{hash[:ip]}\t#{hash[:name]}"
-
-    if hash.include? :host_aliases and !hash[:host_aliases].empty?
-      if hash[:host_aliases].is_a? Array
+    },
+    :to_line  => proc { |hash|
+      [:ip, :name].each do |n|
+        raise ArgumentError, "#{n} is a required attribute for hosts" unless hash[n] and hash[n] != :absent
+      end
+      str = "#{hash[:ip]}\t#{hash[:name]}"
+      if hash.include? :host_aliases and !hash[:host_aliases].empty?
         str += "\t#{hash[:host_aliases].join("\t")}"
-      else
-        raise ArgumentError, "Host aliases must be specified as an array"
       end
-    end
-
-    str
-  end
+      if hash.include? :comment and !hash[:comment].empty?
+        str += "\t# #{hash[:comment]}"
+      end
+      str
+    }
 end
-
diff --git a/lib/puppet/type/host.rb b/lib/puppet/type/host.rb
index 8ab7504..1af74d8 100755
--- a/lib/puppet/type/host.rb
+++ b/lib/puppet/type/host.rb
@@ -5,11 +5,11 @@ module Puppet
     newproperty(:ip) do
       desc "The host's IP address, IPv4 or IPv6."
 
-    validate do |value|
-      unless value =~ /((([0-9a-fA-F]+:){7}[0-9a-fA-F]+)|(([0-9a-fA-F]+:)*[0-9a-fA-F]+)?::(([0-9a-fA-F]+:)*[0-9a-fA-F]+)?)|((25[0-5]|2[0-4][\d]|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})/
-      raise Puppet::Error, "Invalid IP address"
+      validate do |value|
+        unless value =~ /^((([0-9a-fA-F]+:){7}[0-9a-fA-F]+)|(([0-9a-fA-F]+:)*[0-9a-fA-F]+)?::(([0-9a-fA-F]+:)*[0-9a-fA-F]+)?)|((25[0-5]|2[0-4][\d]|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})$/
+          raise Puppet::Error, "Invalid IP address"
+        end
       end
-    end
 
     end
 
@@ -26,21 +26,6 @@ module Puppet
         currentvalue.join(" ")
       end
 
-      def retrieve
-        is = super
-        case is
-        when String
-          is = is.split(/\s*,\s*/)
-        when Symbol
-          is = [is]
-        when Array
-          # nothing
-        else
-          raise Puppet::DevError, "Invalid @is type #{is.class}"
-        end
-        is
-      end
-
       # We actually want to return the whole array here, not just the first
       # value.
       def should
@@ -61,9 +46,14 @@ module Puppet
 
       validate do |value|
         raise Puppet::Error, "Host aliases cannot include whitespace" if value =~ /\s/
+        raise Puppet::Error, "Host alias cannot be an empty string. Use an empty array to delete all host_aliases " if value =~ /^\s*$/
       end
     end
 
+    newproperty(:comment) do
+      desc "A comment that will be attached to the line with a # character"
+    end
+
     newproperty(:target) do
       desc "The file in which to store service information.  Only used by
         those providers that write to disk."

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list