~bkerensa/ubuntu/raring/puppet/new-upstream-release

« back to all changes in this revision

Viewing changes to lib/puppet/provider/host/parsed.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2011-07-25 01:00:37 UTC
  • mfrom: (1.1.24 upstream) (3.1.25 sid)
  • Revision ID: james.westby@ubuntu.com-20110725010037-875vuxs10eboqgw3
Tags: 2.7.1-1ubuntu1
* Merge from debian unstable.  Remaining changes:
  - debian/puppetmaster-passenger.postinst: Use cacrl instead of hostcrl to
    set the location of the CRL in apache2 configuration. Fix apache2
    configuration on upgrade as well (LP: #641001)
  - move all puppet dependencies to puppet-common since all the code
    actually located in puppet-common.
  - move libagueas from a recommend to a dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
end
9
9
 
10
10
 
11
 
      Puppet::Type.type(:host).provide(
12
 
        :parsed,
13
 
  :parent => Puppet::Provider::ParsedFile,
14
 
  :default_target => hosts,
15
 
        
16
 
  :filetype => :flat
17
 
) do
 
11
Puppet::Type.type(:host).provide(:parsed,:parent => Puppet::Provider::ParsedFile,
 
12
  :default_target => hosts,:filetype => :flat) do
18
13
  confine :exists => hosts
19
14
 
20
15
  text_line :comment, :match => /^#/
21
16
  text_line :blank, :match => /^\s*$/
22
17
 
23
 
  record_line :parsed, :fields => %w{ip name host_aliases},
24
 
    :optional => %w{host_aliases},
25
 
    :rts => true do |line|
26
 
    hash = {}
27
 
    if line.sub!(/^(\S+)\s+(\S+)\s*/, '')
28
 
      hash[:ip] = $1
29
 
      hash[:name] = $2
30
 
 
31
 
      if line.empty?
32
 
        hash[:host_aliases] = []
33
 
      else
34
 
        line.sub!(/\s*/, '')
35
 
        line.sub!(/^([^#]+)\s*/) do |value|
36
 
          aliases = $1
37
 
          unless aliases =~ /^\s*$/
38
 
            hash[:host_aliases] = aliases.split(/\s+/)
39
 
          end
40
 
 
41
 
          ""
42
 
        end
43
 
      end
44
 
    else
45
 
      raise Puppet::Error, "Could not match '#{line}'"
46
 
    end
47
 
 
48
 
    hash[:host_aliases] = [] if hash[:host_aliases] == ""
49
 
 
50
 
    return hash
51
 
  end
52
 
 
53
 
  # Convert the current object into a host-style string.
54
 
  def self.to_line(hash)
55
 
    return super unless hash[:record_type] == :parsed
56
 
    [:ip, :name].each do |n|
57
 
      raise ArgumentError, "#{n} is a required attribute for hosts" unless hash[n] and hash[n] != :absent
58
 
    end
59
 
 
60
 
    str = "#{hash[:ip]}\t#{hash[:name]}"
61
 
 
62
 
    if hash.include? :host_aliases and !hash[:host_aliases].empty?
63
 
      if hash[:host_aliases].is_a? Array
64
 
        str += "\t#{hash[:host_aliases].join("\t")}"
65
 
      else
66
 
        raise ArgumentError, "Host aliases must be specified as an array"
67
 
      end
68
 
    end
69
 
 
70
 
    str
71
 
  end
 
18
  record_line :parsed, :fields => %w{ip name host_aliases comment},
 
19
    :optional => %w{host_aliases comment},
 
20
    :match    => /^(\S+)\s+(\S+)\s*(.*?)?(?:\s*#\s*(.*))?$/,
 
21
    :post_parse => proc { |hash|
 
22
      # An absent comment should match "comment => ''"
 
23
      hash[:comment] = '' if hash[:comment].nil? or hash[:comment] == :absent
 
24
      unless hash[:host_aliases].nil? or hash[:host_aliases] == :absent
 
25
        hash[:host_aliases].gsub!(/\s+/,' ') # Change delimiter
 
26
      end
 
27
    },
 
28
    :to_line  => proc { |hash|
 
29
      [:ip, :name].each do |n|
 
30
        raise ArgumentError, "#{n} is a required attribute for hosts" unless hash[n] and hash[n] != :absent
 
31
      end
 
32
      str = "#{hash[:ip]}\t#{hash[:name]}"
 
33
      if hash.include? :host_aliases and !hash[:host_aliases].nil? and hash[:host_aliases] != :absent
 
34
        str += "\t#{hash[:host_aliases]}"
 
35
      end
 
36
      if hash.include? :comment and !hash[:comment].empty?
 
37
        str += "\t# #{hash[:comment]}"
 
38
      end
 
39
      str
 
40
    }
72
41
end
73