1
2
3
4
5
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
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
76
77
78
79
80
81
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
module Puppet
newtype(:schedule) do
@doc = "Defined schedules for Puppet. The important thing to understand
about how schedules are currently implemented in Puppet is that they
can only be used to stop a resource from being applied, they never
guarantee that it is applied.
Every time Puppet applies its configuration, it will collect the
list of resources whose schedule does not eliminate them from
running right then, but there is currently no system in place to
guarantee that a given resource runs at a given time. If you
specify a very restrictive schedule and Puppet happens to run at a
time within that schedule, then the resources will get applied;
otherwise, that work may never get done.
Thus, it behooves you to use wider scheduling (e.g., over a couple of
hours) combined with periods and repetitions. For instance, if you
wanted to restrict certain resources to only running once, between
the hours of two and 4 AM, then you would use this schedule::
schedule { maint:
range => \"2 - 4\",
period => daily,
repeat => 1
}
With this schedule, the first time that Puppet runs between 2 and 4 AM,
all resources with this schedule will get applied, but they won't
get applied again between 2 and 4 because they will have already
run once that day, and they won't get applied outside that schedule
because they will be outside the scheduled range.
Puppet automatically creates a schedule for each valid period with the
same name as that period (e.g., hourly and daily). Additionally,
a schedule named *puppet* is created and used as the default,
with the following attributes::
schedule { puppet:
period => hourly,
repeat => 2
}
This will cause resources to be applied every 30 minutes by default.
"
newparam(:name) do
desc "The name of the schedule. This name is used to retrieve the
schedule when assigning it to an object::
schedule { daily:
period => daily,
range => [2, 4]
}
exec { \"/usr/bin/apt-get update\":
schedule => daily
}
"
isnamevar
end
newparam(:range) do
desc "The earliest and latest that a resource can be applied. This
is always a range within a 24 hour period, and hours must be
specified in numbers between 0 and 23, inclusive. Minutes and
seconds can be provided, using the normal colon as a separator.
For instance::
schedule { maintenance:
range => \"1:30 - 4:30\"
}
This is mostly useful for restricting certain resources to being
applied in maintenance windows or during off-peak hours."
# This is lame; properties all use arrays as values, but parameters don't.
# That's going to hurt eventually.
validate do |values|
values = [values] unless values.is_a?(Array)
values.each { |value|
unless value.is_a?(String) and
value =~ /\d+(:\d+){0,2}\s*-\s*\d+(:\d+){0,2}/
self.fail "Invalid range value '%s'" % value
end
}
end
munge do |values|
values = [values] unless values.is_a?(Array)
ret = []
values.each { |value|
range = []
# Split each range value into a hour, minute, second triad
value.split(/\s*-\s*/).each { |val|
# Add the values as an array.
range << val.split(":").collect { |n| n.to_i }
}
if range.length != 2
self.fail "Invalid range %s" % value
end
# Make sure the hours are valid
[range[0][0], range[1][0]].each do |n|
if n < 0 or n > 23
raise ArgumentError, "Invalid hour '%s'" % n
end
end
[range[0][1], range[1][1]].each do |n|
if n and (n < 0 or n > 59)
raise ArgumentError, "Invalid minute '%s'" % n
end
end
if range[0][0] > range[1][0]
self.fail(("Invalid range %s; " % value) +
"ranges cannot span days."
)
end
ret << range
}
# Now our array of arrays
ret
end
def match?(previous, now)
# The lowest-level array is of the hour, minute, second triad
# then it's an array of two of those, to present the limits
# then it's array of those ranges
unless @value[0][0].is_a?(Array)
@value = [@value]
end
@value.each do |value|
limits = value.collect do |range|
ary = [now.year, now.month, now.day, range[0]]
if range[1]
ary << range[1]
else
ary << now.min
end
if range[2]
ary << range[2]
else
ary << now.sec
end
time = Time.local(*ary)
unless time.hour == range[0]
self.devfail(
"Incorrectly converted time: %s: %s vs %s" % [time, time.hour, range[0]]
)
end
time
end
unless limits[0] < limits[1]
self.info(
"Assuming upper limit should be that time the next day"
)
ary = limits[1].to_a
ary[3] += 1
limits[1] = Time.local(*ary)
#self.devfail("Lower limit is above higher limit: %s" %
# limits.inspect
#)
end
#self.info limits.inspect
#self.notice now
return now.between?(*limits)
end
# Else, return false, since our current time isn't between
# any valid times
return false
end
end
newparam(:periodmatch) do
desc "Whether periods should be matched by number (e.g., the two times
are in the same hour) or by distance (e.g., the two times are
60 minutes apart)."
newvalues(:number, :distance)
defaultto :distance
end
newparam(:period) do
desc "The period of repetition for a resource. Choose from among
a fixed list of *hourly*, *daily*, *weekly*, and *monthly*.
The default is for a resource to get applied every time that
Puppet runs, whatever that period is.
Note that the period defines how often a given resource will get
applied but not when; if you would like to restrict the hours
that a given resource can be applied (e.g., only at night during
a maintenance window) then use the ``range`` attribute.
If the provided periods are not sufficient, you can provide a
value to the *repeat* attribute, which will cause Puppet to
schedule the affected resources evenly in the period the
specified number of times. Take this schedule::
schedule { veryoften:
period => hourly,
repeat => 6
}
This can cause Puppet to apply that resource up to every 10 minutes.
At the moment, Puppet cannot guarantee that level of
repetition; that is, it can run up to every 10 minutes, but
internal factors might prevent it from actually running that
often (e.g., long-running Puppet runs will squash conflictingly scheduled runs).
See the ``periodmatch`` attribute for tuning whether to match
times by their distance apart or by their specific value."
newvalues(:hourly, :daily, :weekly, :monthly, :never)
@@scale = {
:hourly => 3600,
:daily => 86400,
:weekly => 604800,
:monthly => 2592000
}
@@methods = {
:hourly => :hour,
:daily => :day,
:monthly => :month,
:weekly => proc do |prev, now|
# Run the resource if the previous day was after this weekday (e.g., prev is wed, current is tue)
# or if it's been more than a week since we ran
prev.wday > now.wday or (now - prev) > (24 * 3600 * 7)
end
}
def match?(previous, now)
return false if value == :never
value = self.value
case @resource[:periodmatch]
when :number
method = @@methods[value]
if method.is_a?(Proc)
return method.call(previous, now)
else
# We negate it, because if they're equal we don't run
return now.send(method) != previous.send(method)
end
when :distance
scale = @@scale[value]
# If the number of seconds between the two times is greater
# than the unit of time, we match. We divide the scale
# by the repeat, so that we'll repeat that often within
# the scale.
diff = (now.to_i - previous.to_i)
comparison = (scale / @resource[:repeat])
return (now.to_i - previous.to_i) >= (scale / @resource[:repeat])
end
end
end
newparam(:repeat) do
desc "How often the application gets repeated in a given period.
Defaults to 1. Must be an integer."
defaultto 1
validate do |value|
unless value.is_a?(Integer) or value =~ /^\d+$/
raise Puppet::Error,
"Repeat must be a number"
end
# This implicitly assumes that 'periodmatch' is distance -- that
# is, if there's no value, we assume it's a valid value.
return unless @resource[:periodmatch]
if value != 1 and @resource[:periodmatch] != :distance
raise Puppet::Error,
"Repeat must be 1 unless periodmatch is 'distance', not '%s'" % @resource[:periodmatch]
end
end
munge do |value|
unless value.is_a?(Integer)
value = Integer(value)
end
value
end
def match?(previous, now)
true
end
end
def self.instances
[]
end
def self.mkdefaultschedules
result = []
Puppet.debug "Creating default schedules"
result << self.new(
:name => "puppet",
:period => :hourly,
:repeat => "2"
)
# And then one for every period
@parameters.find { |p| p.name == :period }.value_collection.values.each { |value|
result << self.new(
:name => value.to_s,
:period => value
)
}
result
end
def match?(previous = nil, now = nil)
# If we've got a value, then convert it to a Time instance
if previous
previous = Time.at(previous)
end
now ||= Time.now
# Pull them in order
self.class.allattrs.each { |param|
if @parameters.include?(param) and
@parameters[param].respond_to?(:match?)
return false unless @parameters[param].match?(previous, now)
end
}
# If we haven't returned false, then return true; in other words,
# any provided schedules need to all match
return true
end
end
end
|