summaryrefslogtreecommitdiffstats
path: root/lib/puppet/provider/host
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-09-03 21:06:41 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-09-03 21:06:41 +0000
commit270f4448e26a45d213f8aa35c9ecf53843382358 (patch)
treef2db6782fe77275f30d0f4f40a08266cd97ec187 /lib/puppet/provider/host
parentb9b338432ee0dbad7798685736fcc80ff0164924 (diff)
downloadpuppet-270f4448e26a45d213f8aa35c9ecf53843382358.tar.gz
puppet-270f4448e26a45d213f8aa35c9ecf53843382358.tar.xz
puppet-270f4448e26a45d213f8aa35c9ecf53843382358.zip
Beginning the process of moving parsedtypes to a provider. Each parsed
type will have a parsedfile provider, which will be responsible for all of the file parsing and generation. This should allow us to create a useful DSL for file handling, but it also *drastically* simplifies these types. These types have always been a bit fragile, and it was never quite clear who was responsible for what. Now, with the type/provider split, everything is much clearer. I still need to convert host, sshkey, and mount, and something needs to be done with cron. git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1547 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'lib/puppet/provider/host')
-rw-r--r--lib/puppet/provider/host/parsed.rb78
1 files changed, 78 insertions, 0 deletions
diff --git a/lib/puppet/provider/host/parsed.rb b/lib/puppet/provider/host/parsed.rb
new file mode 100644
index 000000000..c606562a2
--- /dev/null
+++ b/lib/puppet/provider/host/parsed.rb
@@ -0,0 +1,78 @@
+require 'puppet/provider/parsedfile'
+
+Puppet::Type.type(:host).provide :parsed, :parent => Puppet::Provider::ParsedFile do
+ @path = "/etc/hosts"
+ @filetype = Puppet::FileType.filetype(:flat)
+
+ confine :exists => @path
+
+ # Parse a host file
+ #
+ # This method also stores existing comments, and it stores all host
+ # jobs in order, mostly so that comments are retained in the order
+ # they were written and in proximity to the same jobs.
+ def self.parse(text)
+ count = 0
+ instances = []
+ text.chomp.split("\n").each { |line|
+ hash = {}
+ case line
+ when /^#/, /^\s*$/:
+ # add comments and blank lines to the list as they are
+ instances << line
+ else
+ if line.sub!(/^(\S+)\s+(\S+)\s*/, '')
+ hash[:ip] = $1
+ hash[:name] = $2
+
+ unless line == ""
+ line.sub!(/\s*/, '')
+ line.sub!(/^([^#]+)\s*/) do |value|
+ aliases = $1
+ unless aliases =~ /^\s*$/
+ hash[:alias] = aliases.split(/\s+/)
+ end
+
+ ""
+ end
+ end
+ else
+ raise Puppet::Error, "Could not match '%s'" % line
+ end
+
+ if hash[:alias] == ""
+ hash.delete(:alias)
+ end
+
+ instances << hash
+
+ count += 1
+ end
+ }
+
+ return instances
+ end
+
+ # Convert the current object into a host-style string.
+ def self.to_record(hash)
+ [:ip, :name].each do |n|
+ unless hash.has_key? n
+ raise ArgumentError, "%s is a required attribute for hosts" % n
+ end
+ end
+
+ str = "%s\t%s" % [hash[:ip], hash[:name]]
+
+ if hash.include? :alias
+ if hash[:alias].is_a? Array
+ str += "\t%s" % hash[:alias].join("\t")
+ else
+ raise ArgumentError, "Aliases must be specified as an array"
+ end
+ end
+
+ str
+ end
+end
+
+# $Id$