Class: Makesure::Cron

Inherits:
Object
  • Object
show all
Defined in:
lib/makesure/cron.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Cron

specify a file (or directory containing files) to load system specifications from



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/makesure/cron.rb', line 6

def initialize(opts = {})
  opts = {
    :cron_path => "/etc/cron.d/makesure.cron",
    :rvm       => false
  }.merge(opts)
  
  Makesure.log "updating '#{opts[:cron_path]}'"
  
  cron_file = File.expand_path(opts[:cron_path])
  crontab = build_crontab_output(opts)
  Makesure.debug "#{cron_file}:\n#{crontab}"
  
  File.open(cron_file, "w") do |f|
    f.write(crontab)
  end
end

Instance Method Details

#build_crontab_output(opts) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/makesure/cron.rb', line 23

def build_crontab_output(opts)
  chdir = Makesure.chdir || File.dirname(Makesure.makesurefile)
  crontab = ""
  crontab << "# Autogenerated by makesure: you probably don't want to edit this file\n\n"
  crontab << "# Cron environment\n"
  crontab << (Makesure.cron_env || "#   customize this by setting Makesure.cron_env in your Makesurefile")
  crontab << "\n"
  Makesure.systems.each do |s|
    uid = s.uid || Makesure.uid
    crontab << "\n"
    crontab << "## Tasks for '#{s.name}'\n"
    crontab << "# Commands\n"
    s.cmds.each do |c|
      crontab << "#{c.cron}\t"
      crontab << "#{uid} " if uid
      crontab << "cd #{chdir} && "

      cmd = "makesure-runner run #{c.command.inspect}"
      crontab << if (opts[:rvm])
        "/bin/bash -l -c '#{cmd}'"
      else
        cmd
      end
      crontab << "\n"
    end

    crontab << "# Verification\n"
    s.verifies.each do |v|
      crontab << "#{v.cron}\t"
      crontab << "#{uid} " if uid
      crontab << "cd #{chdir} && "

      cmd = "makesure-runner verify #{s.name} #{v.name}"
      crontab << if (opts[:rvm])
        "/bin/bash -l -c '#{cmd}'"
      else
        cmd
      end
      crontab << "\n"
    end
    # TODO summarizes
  end
  crontab
end