diff options
author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-03-14 17:49:19 +0000 |
---|---|---|
committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-03-14 17:49:19 +0000 |
commit | df4595e98f953432267756c84a37a5495e9720ef (patch) | |
tree | deda10a6947d4c6ce4777d3f9ea9922a0ddc7a70 /lib/puppet/provider | |
parent | b05ae2ae1262469df264e3a35b30f7a1d1805c18 (diff) | |
download | puppet-df4595e98f953432267756c84a37a5495e9720ef.tar.gz puppet-df4595e98f953432267756c84a37a5495e9720ef.tar.xz puppet-df4595e98f953432267756c84a37a5495e9720ef.zip |
Significantly reworking the internals of the fileparsing code. It now
passes around an instance of a FileRecord, rather than just a hash, which I
think makes it much easier to understand.
Moved the sshkey parsed provider test to its own directory and made it better.
This work is all being done so I can move cron jobs to using providers
instead of the current unmaintainable state of affairs.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2281 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'lib/puppet/provider')
-rwxr-xr-x | lib/puppet/provider/cron/crontab.rb | 90 | ||||
-rwxr-xr-x | lib/puppet/provider/parsedfile.rb | 20 | ||||
-rwxr-xr-x | lib/puppet/provider/sshkey/parsed.rb | 42 |
3 files changed, 122 insertions, 30 deletions
diff --git a/lib/puppet/provider/cron/crontab.rb b/lib/puppet/provider/cron/crontab.rb new file mode 100755 index 000000000..c50bc2680 --- /dev/null +++ b/lib/puppet/provider/cron/crontab.rb @@ -0,0 +1,90 @@ +require 'puppet/provider/parsedfile' + +tab = case Facter.value(:operatingsystem) + when "Solaris": :suntab + else + :crontab + end + +Puppet::Type.type(:cron).provide(:crontab, + :parent => Puppet::Provider::ParsedFile, + :filetype => tab +) do + commands :crontab => "crontab" + + attr_accessor :target + + text_line :comment, :match => %r{^#}, :post_parse => proc { |hash| + if hash[:line] =~ /Puppet Name: (.+)\s*$/ + hash[:name] = $1 + end + } + + text_line :blank, :match => /^\s+/ + + text_line :environment, :match => %r{/^\w+=/} + + record_line :crontab, :fields => %w{minute hour weekday month monthday command}, + :optional => %w{minute hour weekday month monthday}, :absent => "*" + + #record_line :freebsd_special, :fields => %w{special command}, + # :match => %r{^@(\w+)\s+(.+)} + # Apparently Freebsd will "helpfully" add a new TZ line to every + # single cron line, but not in all cases (e.g., it doesn't do it + # on my machine. This is my attempt to fix it so the TZ lines don't + # multiply. + #if tab =~ /^TZ=.+$/ + # return tab.sub(/\n/, "\n" + self.header) + #else + # return self.header() + tab + #end + + + # Return the header placed at the top of each generated file, warning + # users that modifying this file manually is probably a bad idea. + def self.header +%{# HEADER This file was autogenerated at #{Time.now} by puppet. While it +# HEADER can still be managed manually, it is definitely not recommended. +# HEADER Note particularly that the comments starting with 'Puppet Name' should +# HEADER not be deleted, as doing so could cause duplicate cron jobs.\n} + end + + # Collapse name and env records. + def self.prefetch_hook(records) + name = nil + envs = [] + records.each { |record| + case record[:record_type] + when :comment: + if record[:name] + name = record[:name] + record[:skip] = true + end + when :environment: + if name + envs << record[:line] + record[:skip] = true + end + else + if name + record[:name] = name + name = nil + end + unless envs.empty? + record[:environment] = envs + envs = [] + end + end + }.reject { |record| record[:skip] } + end + + def user=(user) + self.target = target + end + + def user + self.target + end +end + +# $Id$ diff --git a/lib/puppet/provider/parsedfile.rb b/lib/puppet/provider/parsedfile.rb index f1b77e56b..56474e273 100755 --- a/lib/puppet/provider/parsedfile.rb +++ b/lib/puppet/provider/parsedfile.rb @@ -117,8 +117,8 @@ class Puppet::Provider::ParsedFile < Puppet::Provider list.collect { |r| r[:name] } end - # Create attribute methods for each of the model's non-metaparam attributes. - def self.model=(model) + # Override the default method with a lot more functionality. + def self.mkmodelmethods [model.validproperties, model.parameters].flatten.each do |attr| attr = symbolize(attr) define_method(attr) do @@ -152,7 +152,12 @@ class Puppet::Provider::ParsedFile < Puppet::Provider @property_hash[attr] = val end end - @model = model + end + + # Always make the model methods. + def self.model=(model) + super + mkmodelmethods() end # Mark a target as modified so we know to flush it. This only gets @@ -179,12 +184,18 @@ class Puppet::Provider::ParsedFile < Puppet::Provider # Prefetch an individual target. def self.prefetch_target(target) - @records += retrieve(target).each do |r| + target_records = retrieve(target).each do |r| r[:on_disk] = true r[:target] = target r[:ensure] = :present end + if respond_to?(:prefetch_hook) + prefetch_hook(target_records) + end + + @records += target_records + # Set current property on any existing resource instances. target_records(target).find_all { |i| i.is_a?(Hash) }.each do |record| # Find any model instances whose names match our instances. @@ -222,7 +233,6 @@ class Puppet::Provider::ParsedFile < Puppet::Provider ensure @target = old end - end end diff --git a/lib/puppet/provider/sshkey/parsed.rb b/lib/puppet/provider/sshkey/parsed.rb index e247502ff..d6e175cf8 100755 --- a/lib/puppet/provider/sshkey/parsed.rb +++ b/lib/puppet/provider/sshkey/parsed.rb @@ -1,6 +1,5 @@ require 'puppet/provider/parsedfile' - known = nil case Facter.value(:operatingsystem) when "Darwin": known = "/etc/ssh_known_hosts" @@ -15,30 +14,23 @@ Puppet::Type.type(:sshkey).provide(:parsed, ) do text_line :comment, :match => /^#/ text_line :blank, :match => /^\s+/ - record_line :parsed, :fields => %w{name type key} - - # Override the line parsing a bit, so we can split the aliases out. - def self.parse_line(line) - hash = super - if hash[:name] =~ /,/ - names = hash[:name].split(",") - hash[:name] = names.shift - hash[:alias] = names - end - hash - end - - - def self.to_line(hash) - if hash[:alias] - hash = hash.dup - names = [hash[:name], hash[:alias]].flatten - - hash[:name] = [hash[:name], hash[:alias]].flatten.join(",") - hash.delete(:alias) - end - super(hash) - end + + record_line :parsed, :fields => %w{name type key}, + :post_parse => proc { |hash| + if hash[:name] =~ /,/ + names = hash[:name].split(",") + hash[:name] = names.shift + hash[:alias] = names + end + }, + :pre_gen => proc { |hash| + if hash[:alias] + names = [hash[:name], hash[:alias]].flatten + + hash[:name] = [hash[:name], hash[:alias]].flatten.join(",") + hash.delete(:alias) + end + } end # $Id$ |