Class: IptablesWeb::Crontab

Inherits:
Object
  • Object
show all
Includes:
Mixin::Sudo
Defined in:
lib/iptables_web/crontab.rb

Instance Method Summary collapse

Instance Method Details

#execute(command) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/iptables_web/crontab.rb', line 27

def execute(command)
  if is_root?
    `sudo #{command}`
  else
    `#{command}`
  end
end

#is_root?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/iptables_web/crontab.rb', line 35

def is_root?
  Process.uid == 0
end

#jobsObject



6
7
8
# File 'lib/iptables_web/crontab.rb', line 6

def jobs
  execute('crontab -l 2> /dev/null').split("\n").reject { |l| l.empty? || l.include?('no crontab for') || l[0] == '#' }
end

#save(jobs) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/iptables_web/crontab.rb', line 10

def save(jobs)
  lines = ["##{Time.now}"]
  jobs.each do |job|
    lines << job
    lines << ''
  end
  file = Tempfile.new('crontab')
  file.write lines.join("\n")
  file.rewind
  execute "crontab #{file.path}"
ensure
  if file
    file.close
    file.unlink
  end
end