Module: Promotion::Generator::Crontab
- Defined in:
- lib/promotion/generator/crontab.rb
Class Method Summary collapse
-
.contents(user, crontablist) ⇒ Object
Generates the contents for /var/cron/tabs/user, containing scheduled jobs.
-
.generate(specs) ⇒ Object
The crontab for each user must be deployed using the crontab tool.
Class Method Details
.contents(user, crontablist) ⇒ Object
Generates the contents for /var/cron/tabs/user, containing scheduled jobs
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/promotion/generator/crontab.rb', line 47 def self.contents(user, crontablist) contents = [] contents << "# /var/cron/tabs/#{user} - #{user}'s crontab \n#\n" contents << "#minute hour mday month wday command\n#\n\n" crontablist.each { |sched| if sched.attributes["Comment"] contents << "#______________________________\n" contents << "# #{sched.attributes["Comment"]} \n" end 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() contents << minute + "\t" + hour + "\t" + mday + "\t" + month + "\t" + wday + "\t" + cmd + "\n" } return(contents.join("")) end |
.generate(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 41 42 43 44 |
# File 'lib/promotion/generator/crontab.rb', line 6 def self.generate(specs) sym = "Crontab" begin 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| generatedContents = self.contents(user, crontablist) userCrontab = File.(user, Folders::Crontabs) begin tempFilename = user + ".tmp" tempFile = File.new(tempFilename, File::WRONLY | File::CREAT | File::TRUNC) tempFile.puts(generatedContents) # previous cron jobs are *NOT* preserved - the tab is competely generated! # otherwise we accumulate the cron-generated warning messages at the top of the file ensure tempFile.close unless tempFile.nil? || tempFile.closed? end $log.info("Checking temporary crontab written to #{tempFilename}.") crontabResults = `#{Files::Crontab} -u #{user} #{tempFilename}` if $?.exitstatus == 0 $log.info("crontab confirms that crontab syntax is correct for user #{user}.") else $log.error(crontabResults) raise end FileUtils.rm_f(tempFilename) $log.info("New crontab installed for user #{user}") } rescue => e $log.error("Error occurred while generating crontab\n#{e.}" + e.backtrace.join("\n")) exit 1 end end |