diff options
| author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-06-27 17:23:04 +0000 |
|---|---|---|
| committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-06-27 17:23:04 +0000 |
| commit | 772ea917e5b93f105118fcf0152cd0622f6a8b3c (patch) | |
| tree | 8749bdcd4a746d18bfe499f941c9631a3ea12ea1 | |
| parent | 08f113c6ccfb0bc3db4c717df575b1219a46603a (diff) | |
Adding support for special freebsd @schedule crap. Also making sure that cron listing works as expected.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1317 980ebf18-57e1-0310-9a29-db15c13687c0
| -rwxr-xr-x | lib/puppet/type/cron.rb | 145 | ||||
| -rw-r--r-- | test/data/types/cron/freebsd | 2 | ||||
| -rwxr-xr-x | test/types/cron.rb | 50 |
3 files changed, 146 insertions, 51 deletions
diff --git a/lib/puppet/type/cron.rb b/lib/puppet/type/cron.rb index 9b82d5e0d..c87618693 100755 --- a/lib/puppet/type/cron.rb +++ b/lib/puppet/type/cron.rb @@ -193,6 +193,21 @@ module Puppet end end + newstate(:special, Puppet::State::ParsedParam) do + desc "Special schedules only supported on FreeBSD." + + def specials + %w{reboot yearly annually monthly weekly daily midnight hourly} + end + + validate do |value| + unless specials().include?(value) + raise ArgumentError, "Invalid special schedule %s" % + value.inspect + end + end + end + newstate(:minute, CronParam) do self.boundaries = [0, 59] desc "The minute at which to run the cron job. @@ -456,20 +471,49 @@ module Puppet # See if we can match the hash against an existing cron job. def self.match(hash) - tmp = nil - return nil unless tmp = self.find { |obj| + self.find_all { |obj| obj[:user] == hash[:user] and obj.value(:command) == hash[:command][0] - } + }.each do |obj| + # we now have a cron job whose command exactly matches + # let's see if the other fields match - # we now have a cron job whose command exactly matches - # let's see if the other fields match + # First check the @special stuff + if hash[:special] + next unless obj.value(:special) == hash[:special] + end - fields().each do |field| - next if field == :command + # Then the normal fields. + matched = true + fields().each do |field| + next if field == :command + if hash[field] and ! obj.value(field) + #Puppet.info "Cron is missing %s: %s and %s" % + # [field, hash[field].inspect, obj.value(field).inspect] + matched = false + break + end - return false unless tmp[:user] == hash[:user] + if ! hash[field] and obj.value(field) + #Puppet.info "Hash is missing %s: %s and %s" % + # [field, obj.value(field).inspect, hash[field].inspect] + matched = false + break + end + + # FIXME It'd be great if I could somehow reuse how the + # fields are turned into text, but.... + next if (hash[field] == [:absent] and obj.value(field) == "*") + next if (hash[field].join(",") == obj.value(field)) + #Puppet.info "Did not match %s: %s vs %s" % + # [field, obj.value(field).inspect, hash[field].inspect] + matched = false + break + end + next unless matched + return obj end - return tmp + + return false end # Parse a user's cron job into individual cron objects. @@ -487,10 +531,13 @@ module Puppet envs = [] text.chomp.split("\n").each { |line| case line - when /^# Puppet Name: (.+)$/: hash[:name] = $1 + when /^# Puppet Name: (.+)$/ + hash[:name] = $1 + next when /^#/: # add other comments to the list as they are @instances[user] << line + next when /^\s*(\w+)\s*=\s*(.+)\s*$/: # Match env settings. if hash[:name] @@ -498,6 +545,14 @@ module Puppet else @instances[user] << line end + next + when /^@(\w+)\s+(.+)/ # FreeBSD special cron crap + fields().each do |field| + next if field == :command + hash[field] = :absent + end + hash[:special] = $1 + hash[:command] = $2 else if match = /^(\S+) (\S+) (\S+) (\S+) (\S+) (.+)$/.match(line) fields().zip(match.captures).each { |param, value| @@ -514,22 +569,25 @@ module Puppet end } else - raise Puppet::Error, "Could not match '%s'" % line + # Don't fail on unmatched lines, just warn on them + # and skip them. + Puppet.warning "Could not match '%s'" % line + next end + end - unless envs.empty? - hash[:environment] = envs - end + unless envs.empty? + hash[:environment] = envs + end - hash[:user] = user + hash[:user] = user - # Now convert our hash to an object. - hash2obj(hash) + # Now convert our hash to an object. + hash2obj(hash) - hash = {} - envs.clear - count += 1 - end + hash = {} + envs.clear + count += 1 } end @@ -581,6 +639,7 @@ module Puppet def self.store(user) unless @instances.include?(user) Puppet.notice "No cron instances for %s" % user + p @instances.keys return end @@ -686,28 +745,31 @@ module Puppet str = "" - # Don't store autogenerated names - #unless self.name =~ /^autocron/ - str = "# Puppet Name: %s\n" % self.name - #end + str = "# Puppet Name: %s\n" % self.name - if @states.include?(:environment) and @states[:environment].should != :absent - envs = @states[:environment].should - unless envs.is_a? Array - envs = [envs] - end + if @states.include?(:environment) and + @states[:environment].should != :absent + envs = @states[:environment].should + unless envs.is_a? Array + envs = [envs] + end - envs.each do |line| str += (line + "\n") end + envs.each do |line| str += (line + "\n") end end - - line = str + self.class.fields.collect { |f| - if hash[f] and hash[f] != :absent - hash[f] - else - "*" - end - }.join(" ") + line = nil + if special = self.value(:special) + line = str + "@%s %s" % + [special, self.value(:command)] + else + line = str + self.class.fields.collect { |f| + if hash[f] and hash[f] != :absent + hash[f] + else + "*" + end + }.join(" ") + end return line end @@ -728,8 +790,11 @@ module Puppet end unless ret - if name == :command + case name + when :command devfail "No command, somehow" + when :special + # nothing else #ret = (self.class.validstate?(name).default || "*").to_s ret = "*" diff --git a/test/data/types/cron/freebsd b/test/data/types/cron/freebsd new file mode 100644 index 000000000..fba1e310b --- /dev/null +++ b/test/data/types/cron/freebsd @@ -0,0 +1,2 @@ +@daily /path/to/some/job +@reboot /path/to/some/job diff --git a/test/types/cron.rb b/test/types/cron.rb index 3f2b4f86b..3b6384d59 100755 --- a/test/types/cron.rb +++ b/test/types/cron.rb @@ -11,14 +11,6 @@ require 'puppet' require 'test/unit' require 'facter' - -# Here we just want to unit-test our cron type, to verify that -#class TestCronType < Test::Unit::TestCase -# include TestPuppet -# -# -#end - class TestCron < Test::Unit::TestCase include TestPuppet def setup @@ -539,6 +531,7 @@ class TestCron < Test::Unit::TestCase fakedata("data/types/cron").each do |file| names = [] + text = File.read(file) obj.write(File.read(file)) @crontype.retrieve(@me) @@ -547,7 +540,8 @@ class TestCron < Test::Unit::TestCase names << cron.name end - cron = mkcron("filetest") + name = File.basename(file) + cron = mkcron("filetest-#{name}") assert_apply(cron) @@ -556,8 +550,12 @@ class TestCron < Test::Unit::TestCase names.each do |name| assert(@crontype[name], "Could not retrieve %s" % name) end - @crontype.each do |name, cron| - names << name + + tablines = @crontype.tab(@me).split("\n") + + text.split("\n").each do |line| + assert(tablines.include?(line), + "Did not get %s back out" % line.inspect) end end end @@ -649,6 +647,36 @@ class TestCron < Test::Unit::TestCase cron.is = [param, :absent] assert_equal("4", cron.value(param)) end + + # Make sure we can successfully list all cron jobs on all users + def test_cron_listing + crons = [] + %w{fake1 fake2 fake3 fake4 fake5}.each do |user| + crons << @crontype.create( + :name => "#{user}-1", + :command => "/usr/bin/#{user}", + :minute => "0", + :user => user, + :hour => user.sub("fake",'') + ) + + crons << @crontype.create( + :name => "#{user}-2", + :command => "/usr/sbin/#{user}", + :minute => "0", + :user => user, + :weekday => user.sub("fake",'') + ) + + assert_apply(*crons) + end + + list = @crontype.list.collect { |c| c.name } + + crons.each do |cron| + assert(list.include?(cron.name), "Did not match cron %s" % name) + end + end end # $Id$ |
