aboutsummaryrefslogtreecommitdiff
path: root/lib/puppet/provider/host/parsed.rb
diff options
context:
space:
mode:
authorJosh Cooper <josh@puppet.com>2018-07-09 17:15:39 -0700
committerJosh Cooper <josh@puppet.com>2018-07-09 19:44:47 -0700
commit2ac89832b7e065df4490d81b3080b2b570a172ad (patch)
treef9f4f28f30a77c958e8e850acf954336b2301dc9 /lib/puppet/provider/host/parsed.rb
parent1c6ae8bfde5175c5d3512f5acb5352fda94a4801 (diff)
downloadpuppet-hosts_core-2ac89832b7e065df4490d81b3080b2b570a172ad.tar.gz
puppet-hosts_core-2ac89832b7e065df4490d81b3080b2b570a172ad.tar.bz2
Initial host import from puppet#ee7cf4d28077be7d1bdbbe934ea012d41d33deff
Diffstat (limited to 'lib/puppet/provider/host/parsed.rb')
-rw-r--r--lib/puppet/provider/host/parsed.rb46
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/puppet/provider/host/parsed.rb b/lib/puppet/provider/host/parsed.rb
new file mode 100644
index 0000000..d3d0039
--- /dev/null
+++ b/lib/puppet/provider/host/parsed.rb
@@ -0,0 +1,46 @@
+require 'puppet/provider/parsedfile'
+
+hosts = nil
+case Facter.value(:osfamily)
+when "Solaris"; hosts = "/etc/inet/hosts"
+when "windows"
+ require 'win32/resolv'
+ hosts = Win32::Resolv.get_hosts_path
+else
+ hosts = "/etc/hosts"
+end
+
+
+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*$/
+ hosts_pattern = '^([0-9a-f:]\S+)\s+([^#\s+]\S+)\s*(.*?)?(?:\s*#\s*(.*))?$'
+ record_line :parsed, :fields => %w{ip name host_aliases comment},
+ :optional => %w{host_aliases comment},
+ :match => /#{hosts_pattern}/,
+ :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].gsub!(/\s+/,' ') # Change delimiter
+ end
+ },
+ :to_line => proc { |hash|
+ [:ip, :name].each do |n|
+ raise ArgumentError, _("%{attr} is a required attribute for hosts") % { attr: n } unless hash[n] and hash[n] != :absent
+ end
+ str = "#{hash[:ip]}\t#{hash[:name]}"
+ if hash.include? :host_aliases and !hash[:host_aliases].nil? and hash[:host_aliases] != :absent
+ str += "\t#{hash[:host_aliases]}"
+ end
+ if hash.include? :comment and !hash[:comment].empty?
+ str += "\t# #{hash[:comment]}"
+ end
+ str
+ }
+
+ text_line :incomplete, :match => /(?! (#{hosts_pattern}))/
+end