summaryrefslogtreecommitdiffstats
path: root/lib/puppet
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-11-13 03:28:47 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-11-13 03:28:47 +0000
commit064ddbc218c56de91318c9dfedc481e67ed60750 (patch)
treee93341b790ae6279c22138d05bd3f641d3620edd /lib/puppet
parentbb80c1bb2c977fe462fa9d74031929547c2bbc40 (diff)
downloadpuppet-064ddbc218c56de91318c9dfedc481e67ed60750.tar.gz
puppet-064ddbc218c56de91318c9dfedc481e67ed60750.tar.xz
puppet-064ddbc218c56de91318c9dfedc481e67ed60750.zip
Hosts now work again, and it should be straightforward to create a netinfo provider, too.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1864 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'lib/puppet')
-rw-r--r--lib/puppet/provider/host/parsed.rb83
-rwxr-xr-xlib/puppet/provider/parsedfile.rb11
-rwxr-xr-xlib/puppet/provider/port/parsed.rb4
-rwxr-xr-xlib/puppet/type/host.rb16
-rw-r--r--lib/puppet/type/state.rb1
-rw-r--r--lib/puppet/util/fileparsing.rb41
6 files changed, 95 insertions, 61 deletions
diff --git a/lib/puppet/provider/host/parsed.rb b/lib/puppet/provider/host/parsed.rb
index c606562a2..e2e0786dc 100644
--- a/lib/puppet/provider/host/parsed.rb
+++ b/lib/puppet/provider/host/parsed.rb
@@ -1,62 +1,57 @@
require 'puppet/provider/parsedfile'
-Puppet::Type.type(:host).provide :parsed, :parent => Puppet::Provider::ParsedFile do
- @path = "/etc/hosts"
- @filetype = Puppet::FileType.filetype(:flat)
+hosts = nil
+case Facter.value(:operatingsystem)
+when "Solaris": hosts = "/etc/inet/hosts"
+else
+ hosts = "/etc/hosts"
+end
- confine :exists => @path
+Puppet::Type.type(:host).provide(:parsed,
+ :parent => Puppet::Provider::ParsedFile,
+ :default_target => hosts,
+ :filetype => :flat
+) do
+ confine :exists => hosts
- # 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
+ text_line :comment, :match => /^#/
+ text_line :blank, :match => /^\s*$/
- unless line == ""
- line.sub!(/\s*/, '')
- line.sub!(/^([^#]+)\s*/) do |value|
- aliases = $1
- unless aliases =~ /^\s*$/
- hash[:alias] = aliases.split(/\s+/)
- end
+ record_line :parsed, :fields => %w{ip name alias},
+ :optional => %w{alias},
+ :rts => true do |line|
+ hash = {}
+ if line.sub!(/^(\S+)\s+(\S+)\s*/, '')
+ hash[:ip] = $1
+ hash[:name] = $2
- ""
- end
+ unless line == ""
+ line.sub!(/\s*/, '')
+ line.sub!(/^([^#]+)\s*/) do |value|
+ aliases = $1
+ unless aliases =~ /^\s*$/
+ hash[:alias] = aliases.split(/\s+/)
end
- else
- raise Puppet::Error, "Could not match '%s'" % line
- end
- if hash[:alias] == ""
- hash.delete(:alias)
+ ""
end
-
- instances << hash
-
- count += 1
end
- }
+ else
+ raise Puppet::Error, "Could not match '%s'" % line
+ end
+
+ if hash[:alias] == ""
+ hash.delete(:alias)
+ end
- return instances
+ return hash
end
# Convert the current object into a host-style string.
- def self.to_record(hash)
+ def self.to_line(hash)
+ return super unless hash[:record_type] == :parsed
[:ip, :name].each do |n|
- unless hash.has_key? n
+ unless hash[n] and hash[n] != :absent
raise ArgumentError, "%s is a required attribute for hosts" % n
end
end
diff --git a/lib/puppet/provider/parsedfile.rb b/lib/puppet/provider/parsedfile.rb
index 40b977314..cdcc49b20 100755
--- a/lib/puppet/provider/parsedfile.rb
+++ b/lib/puppet/provider/parsedfile.rb
@@ -224,15 +224,6 @@ class Puppet::Provider::ParsedFile < Puppet::Provider
end
end
- # Write out the file.
- def self.store(records)
- if records.empty?
- Puppet.notice "No %s records for %s" % [self.name, @path]
- else
- target_object.write(self.to_file(instances))
- end
- end
-
# Initialize the object if necessary.
def self.target_object(target)
@target_objects[target] ||= @filetype.new(target)
@@ -273,11 +264,13 @@ class Puppet::Provider::ParsedFile < Puppet::Provider
end
end
self.class.modified(@state_hash[:target] || self.class.default_target)
+ return (@model.class.name.to_s + "_created").intern
end
def destroy
# We use the method here so it marks the target as modified.
self.ensure = :absent
+ return (@model.class.name.to_s + "_deleted").intern
end
def exists?
diff --git a/lib/puppet/provider/port/parsed.rb b/lib/puppet/provider/port/parsed.rb
index 510b461df..62905bcea 100755
--- a/lib/puppet/provider/port/parsed.rb
+++ b/lib/puppet/provider/port/parsed.rb
@@ -24,7 +24,9 @@ Puppet::Type.type(:port).provide(:parsed,
text_line :funky_darwin, :match => /^\s+\d+\//
# We have to manually parse the line, since it's so darn complicated.
- record_line :parsed, :fields => %w{name port protocols alias} do |line|
+ record_line :parsed, :fields => %w{name port protocols alias description},
+ :optional => %w{alias description}
+ do |line|
if line =~ /\/ddp/
raise "missed ddp in %s" % line
end
diff --git a/lib/puppet/type/host.rb b/lib/puppet/type/host.rb
index b2ed848fc..49a3144b8 100755
--- a/lib/puppet/type/host.rb
+++ b/lib/puppet/type/host.rb
@@ -1,7 +1,9 @@
require 'puppet/type/parsedtype'
module Puppet
- newtype(:host, Puppet::Type::ParsedType) do
+ newtype(:host) do
+ ensurable
+
newstate(:ip) do
desc "The host's IP address, IPv4 or IPv6."
end
@@ -75,6 +77,18 @@ module Puppet
end
end
+ newstate(:target) do
+ desc "The file in which to store service information. Only used by
+ those providers that write to disk (i.e., not NetInfo)."
+
+ defaultto { if @parent.class.defaultprovider.ancestors.include?(Puppet::Provider::ParsedFile)
+ @parent.class.defaultprovider.default_target
+ else
+ nil
+ end
+ }
+ end
+
newparam(:name) do
desc "The host name."
diff --git a/lib/puppet/type/state.rb b/lib/puppet/type/state.rb
index 9faa6d8ec..9b96b909f 100644
--- a/lib/puppet/type/state.rb
+++ b/lib/puppet/type/state.rb
@@ -190,7 +190,6 @@ class State < Puppet::Parameter
end
# Figure out which event to return.
- # not specified.
def event(name, event = nil)
if value_event = self.class.value_option(name, :event)
return value_event
diff --git a/lib/puppet/util/fileparsing.rb b/lib/puppet/util/fileparsing.rb
index 4056f2fff..390648404 100644
--- a/lib/puppet/util/fileparsing.rb
+++ b/lib/puppet/util/fileparsing.rb
@@ -145,7 +145,18 @@ module Puppet::Util::FileParsing
return nil
end
- # Define a new type of record. These lines get split into hashes.
+ # Define a new type of record. These lines get split into hashes. Valid
+ # options are:
+ # * <tt>:absent</tt>: What to use when a field is absent. Defaults to "".
+ # * <tt>:fields</tt>: The list of fields, as an array. By default, all
+ # fields are considered required.
+ # * <tt>:joiner</tt>: How to join fields together. Defaults to '\t'.
+ # * <tt>:optional</tt>: Which fields are optional. If these are missing,
+ # you'll just get the 'absent' value instead of an ArgumentError.
+ # * <tt>:rts</tt>: Whether to remove trailing whitespace. Defaults to false.
+ # If true, whitespace will be removed; if a regex, then whatever matches
+ # the regex will be removed.
+ # * <tt>:separator</tt>: The record separator. Defaults to /\s+/.
def record_line(name, options, &block)
unless options.include?(:fields)
raise ArgumentError, "Must include a list of fields"
@@ -162,6 +173,12 @@ module Puppet::Util::FileParsing
options[:absent] ||= ""
+ if options[:optional]
+ options[:optional] = options[:optional].collect { |f| symbolize(f) }
+ else
+ options[:optional] = []
+ end
+
options[:separator] ||= /\s+/
# Unless they specified a string-based joiner, just use a single
@@ -218,14 +235,28 @@ module Puppet::Util::FileParsing
else
joinchar = type[:joiner] || type[:separator]
- return type[:fields].collect { |field|
+ line = type[:fields].collect { |field|
# If the field is marked absent, use the appropriate replacement
- if details[field] == :absent
- type[:absent]
+ if details[field] == :absent or details[field].nil?
+ if type[:optional].include?(field)
+ type[:absent]
+ else
+ raise ArgumentError, "Field %s is required" % field
+ end
else
details[field].to_s
end
- }.join(joinchar)
+ }.reject { |c| c.nil?}.join(joinchar)
+
+ if regex = type[:rts]
+ # If they say true, then use whitespace; else, use their regex.
+ if regex == true
+ regex = /\s+$/
+ end
+ return line.sub(regex,'')
+ else
+ return line
+ end
end
end