Class: Backup::CronEdit

Inherits:
Object
  • Object
show all
Defined in:
lib/backup/cron_edit.rb

Overview

Adds or removes a command to the user’s crontab. To make sure that the Ruby application is invoked as a cron job it needs the environment variables that are available when run from command line. To meet these requirements the environment variables are read and added to the crontab. If variables already exist in the crontab they are overridden.

Constant Summary collapse

CRON_ENTRIES_FILE =

Temporary file that holds the entries to be written to the crontab

".cron_entries"
PRE_COMMENT =

Pre comment before the entered command

"#----command added by sycbackup---"
POST_COMMENT =

Post comment after the entered command

"#---------------------------------"

Instance Method Summary collapse

Instance Method Details

#add_command(command, environment = []) ⇒ Object

Adds a command to the user’s crontab. If the provided command is empty add_command will exit the application with exit status -1. The method uses the crontab -l and crontab file command. If the crontab call fails the error message and exit status of crontab will be returned and the application exits



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
67
68
69
70
71
72
73
74
75
# File 'lib/backup/cron_edit.rb', line 25

def add_command(command, environment=[])
  command = command.strip.squeeze(" ")

  if command.empty?
    STDERR.puts "Cannot add empty command to cron"
    exit -1
  end
  
  read_crontab_command = 'crontab -l'

  stdout, stderr, status = Open3.capture3(read_crontab_command)

  entries = [] + environment

  stdout.split(/\n/).each do |entry| 
    entry = entry.strip.squeeze(" ")
    variable = entry.match(/\A\w+(?=\=)/).to_s
    unless variable.empty?
      entries << entry unless environment.grep(/\A#{variable}/)
    else
      entries << entry
    end
  end

  unless entries.include? command
    entries << PRE_COMMENT
    entries << command
    entries << POST_COMMENT

    cron_entries_file = CRON_ENTRIES_FILE 
    File.open(cron_entries_file, 'w') do |f|
      entries.each {|entry| f.puts entry}
    end

    write_crontab_command = "crontab #{cron_entries_file}"

    stdout, stderr, status = Open3.capture3(write_crontab_command)

    cleanup

    unless status.exitstatus == 0
      STDERR.puts "There is a problem executing command"
      STDERR.puts write_crontab_command
      STDERR.puts stderr
      exit status.exitstatus
    end
  end

  command 

end

#cleanupObject

Removes the CRON_ENTRIES_FILE after the values have been written to crontab



132
133
134
# File 'lib/backup/cron_edit.rb', line 132

def cleanup
  File.delete CRON_ENTRIES_FILE if File.exists? CRON_ENTRIES_FILE
end

#remove_command(command) ⇒ Object

Removes a command from the user’s crontab. If the provided command is empty remove_command will exit the application with exit status -1. The method uses the crontab -l and crontab file command. If the crontab call fails the error message and exit status of crontab will be returned and the application exits



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/backup/cron_edit.rb', line 82

def remove_command(command)
  command = command.strip.squeeze(" ")

  if command.empty?
    STDERR.puts "Cannot delete empty command from crontab"
    exit -1
  end

  read_crontab_command = "crontab -l"

  stdout, stderr, status = Open3.capture3(read_crontab_command)

  unless status.exitstatus == 0
    STDERR.puts "There is a problem executing command"
    STDERR.puts read_crontab_command
    STDERR.puts stderr
    exit status.existatus
  end

  entries = stdout.split(/\n/).each {|entry| entry.strip.squeeze(" ")}

  entries.delete(PRE_COMMENT)
  entries.delete(command)
  entries.delete(POST_COMMENT)

  cron_entries_file = CRON_ENTRIES_FILE

  File.open(cron_entries_file, 'w') do |file|
    entries.each {|entry| file.puts entry}
  end

  write_crontab_command = "crontab #{cron_entries_file}"

  stdout, stderr, status = Open3.capture3(write_crontab_command)

  cleanup

  unless status.exitstatus == 0
    STDERR.puts "There is a problem executing command"
    STDERR.puts write_crontab_command
    STDERR.puts stderr
    exit status.exitstatus
  end

  command 

end