Class: CronEdit::Crontab

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aUser = nil) ⇒ Crontab

Use crontab for user aUser



40
41
42
43
# File 'lib/cronedit.rb', line 40

def initialize aUser = nil
    @user = aUser
    rollback()
end

Class Method Details

.Add(anId, aDef) ⇒ Object

Add a new crontab entry definition. See instance add method



47
48
49
50
51
52
# File 'lib/cronedit.rb', line 47

def Add anId, aDef
    cm = Crontab.new
    entry = cm.add anId, aDef
    cm.commit
    entry
end

.ListObject

List current crontab.



62
63
64
# File 'lib/cronedit.rb', line 62

def List
    Crontab.new.list
end

.Remove(anId) ⇒ Object

Remove a crontab entry definition identified by anId from current crontab.



55
56
57
58
59
# File 'lib/cronedit.rb', line 55

def Remove anId
    cm = Crontab.new
    cm.remove anId
    cm.commit
end

Instance Method Details

#add(anId, aDef) ⇒ Object

Add a new crontab entry definition. Becomes effective only after commit().

  • aDef is can be a standart text definition or a Hash definition (see CronEntry::DEFAULTS)

  • anId is an identification of the entry (for later modification or deletion)

returns newly added CronEntry



71
72
73
# File 'lib/cronedit.rb', line 71

def add anId, aDef
    @adds[anId.to_s] = CronEntry.new( aDef )
end

#addAllFrom(anIO) ⇒ Object

Bulk addition of crontab entry definitions from stream (String, File, IO)



76
77
78
79
# File 'lib/cronedit.rb', line 76

def addAllFrom anIO
    #TODO: load file, parse it 
    raise 'Not implemented yet'
end

#commitObject

Merges the existing crontab with all modifications and installs the new crontab. returns the merged parsed crontab hash



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/cronedit.rb', line 89

def commit
    # merge crontab
    current = list()
    current.delete_if {|id, entry| @removals.include? id}
    merged = current.merge @adds

    # install it
    cmd = @user ? "crontab -u #{@user} -" : "crontab -"
    IO.popen(cmd,'w') {|io|
        merged.each {|id, entry|
            io.puts "# #{id}"
            io.puts entry
        }
        io.close
    }
    # No idea why but without this any wait crontab reads and writes appears not synchronizes
    sleep 0.01
    #clean changes :)
    rollback()
    merged
end

#listObject

Read the current crontab and parse it returns a Hash (entry id or index)=>CronEntry



125
126
127
128
129
130
# File 'lib/cronedit.rb', line 125

def list
    cmd = @user ? "crontab -u #{@user} -l" : "crontab -l"
    IO.popen(cmd) {|io|
        return parseCrontab(io)
    }
end

#listRawObject

Lists raw content from crontab returns array of text lines



134
135
136
137
138
139
140
# File 'lib/cronedit.rb', line 134

def listRaw
    cmd = @user ? "crontab -u #{@user} -l" : "crontab -l"
    IO.popen(cmd) {|io|
            entries = io.readlines
            return (entries.first =~ /^no/).nil? ? entries : []
    }
end

#parseCrontab(anIO) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/cronedit.rb', line 142

def parseCrontab anIO
        entries = {}
        idx = 0
        id = nil
        anIO.each_line { |l|
            l.strip!
            next if l.empty?
            return {} unless (l =~ /^no/).nil?
            if l=~/^#/ 
                id = l[1, l.size-1].strip
            else 
                key = id.nil? ? (idx+=1) : id
                entries[key.to_s]=l
                id = nil
            end
        }
        entries
end

#remove(anId) ⇒ Object

Remove a crontab entry definition identified by anId. Becomes effective only after commit().



82
83
84
85
# File 'lib/cronedit.rb', line 82

def remove anId
    @adds.delete anId.to_s
    @removals[anId.to_s]=anId.to_s
end

#reviewObject

Prints out the items to be added and removed



118
119
120
121
# File 'lib/cronedit.rb', line 118

def review
    puts "To be added: #{@adds.inspect}"
    puts "To be removed: #{@removals.keys.inspect}"
end

#rollbackObject

Discards all modifications (since last commit, or creation)



112
113
114
115
# File 'lib/cronedit.rb', line 112

def rollback
    @adds = {}
    @removals = {}
end