Module: Promotion::Generator::Crontab

Defined in:
lib/promotion/generator/crontab.rb

Class Method Summary collapse

Class Method Details

.check(specs) ⇒ Object

The crontab for each user must be deployed using the crontab tool



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/promotion/generator/crontab.rb', line 6

def self.check(specs)
  schedules = {}  # keyed on user, value is array of schedule elements
  specs.each { |spec|
    spec.elements.each("/Specification/Crontab/Schedule") { |sched|
      user = sched.attributes["User"]
      schedules[user] = [] unless schedules.has_key?(user)
      schedules[user] << sched
    }
  }
  schedules.each { |user, crontablist|
    userCrontab = File.expand_path(user, "/var/cron/tabs/")
    next unless File.exists?(userCrontab)
    contents = IO.readlines(userCrontab).collect!{ |s| s.gsub(/\s/,"") }
    proposals = []
    crontablist.each { |sched|
      minute = sched.attributes["Minute"] || "*"
      hour   = sched.attributes["Hour"]   || "*"
      mday   = sched.attributes["DayOfMonth"] || "*"
      month  = sched.attributes["Month"]  || "*"
      wday   = sched.attributes["DayOfWeek"] || "*"
      cmd    = (sched.elements["Command"].cdatas[0]).value().strip()
      needed = minute + "\t" + hour + "\t" + mday + "\t" + month + "\t" + wday + "\t" + cmd
      unless contents.include?(needed.gsub(/\s/,""))
        if sched.attributes["Comment"]
          proposals << "# #{sched.attributes["Comment"]}"
        end
        proposals << needed
      end
    }
    if proposals.size > 0
      header = "# min\thour\tmday\tmonth\twday\tcommand\n"
      puts("\nSuggested changes to /var/cron/tabs/#{user}:", header, proposals.join("\n"), "\n")
    end
  }
end