dcron recipe: do not use @foo syntax because it requires to give an ID
[slapos.git] / slapos / recipe / dcron.py
1 ##############################################################################
2 #
3 # Copyright (c) 2010 Vifib SARL and Contributors. All Rights Reserved.
4 #
5 # WARNING: This program as such is intended to be used by professional
6 # programmers who take the whole responsibility of assessing all potential
7 # consequences resulting from its eventual inadequacies and bugs
8 # End users who are looking for a ready-to-use solution with commercial
9 # guarantees and support are strongly adviced to contract a Free Software
10 # Service Company
11 #
12 # This program is Free Software; you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License
14 # as published by the Free Software Foundation; either version 3
15 # of the License, or (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #
26 ##############################################################################
27 import os
28
29 if __name__ == '__main__': # Hack to easily run test below.
30 GenericBaseRecipe = object
31 else:
32 from slapos.recipe.librecipe import GenericBaseRecipe
33 from zc.buildout import UserError
34
35 class Recipe(GenericBaseRecipe):
36
37 def install(self):
38 self.logger.info("Installing dcron...")
39
40 options = self.options
41 script = self.createWrapper(name=options['binary'],
42 command=options['dcrond-binary'].strip(),
43 parameters=[
44 '-s', options['cron-entries'],
45 '-c', options['crontabs'],
46 '-t', options['cronstamps'],
47 '-f', '-l', '5',
48 '-M', options['catcher']
49 ])
50
51 self.logger.debug('Main cron executable created at : %r', script)
52
53 self.logger.info("dcron successfully installed.")
54
55 return [script]
56
57
58 class Part(GenericBaseRecipe):
59
60 def install(self):
61 try:
62 periodicity = self.options['frequency']
63 except KeyError:
64 periodicity = self.options['time']
65 try:
66 periodicity = systemd_to_cron(periodicity)
67 except Exception:
68 raise UserError("Invalid systemd calendar spec %r" % periodicity)
69 cron_d = self.options['cron-entries']
70 name = self.options['name']
71 filename = os.path.join(cron_d, name)
72
73 with open(filename, 'w') as part:
74 part.write('%s %s\n' % (periodicity, self.options['command']))
75
76 return [filename]
77
78
79 day_of_week_dict = dict((name, dow) for dow, name in enumerate(
80 "sunday monday tuesday wednesday thursday friday saturday".split())
81 for name in (name, name[:3]))
82 symbolic_dict = dict(hourly = '0 * * * *',
83 daily = '0 0 * * *',
84 monthly = '0 0 1 * *',
85 weekly = '0 0 * * 0')
86
87 def systemd_to_cron(spec):
88 """Convert from systemd.time(7) calendar spec to crontab spec"""
89 try:
90 return symbolic_dict[spec]
91 except KeyError:
92 pass
93 if not spec.strip():
94 raise ValueError
95 spec = spec.split(' ')
96 try:
97 dow = ','.join(sorted('-'.join(str(day_of_week_dict[x.lower()])
98 for x in x.split('-', 1))
99 for x in spec[0].split(',')
100 if x))
101 del spec[0]
102 except KeyError:
103 dow = '*'
104 day = spec.pop(0) if spec else '*-*'
105 if spec:
106 time, = spec
107 elif ':' in day:
108 time = day
109 day = '*-*'
110 else:
111 time = '0:0'
112 day = day.split('-')
113 time = time.split(':')
114 if (# years not supported
115 len(day) > 2 and day.pop(0) != '*' or
116 # some crons ignore day of month if day of week is given, and dcron
117 # treats day of month in a way that is not compatible with systemd
118 dow != '*' != day[1] or
119 # seconds not supported
120 len(time) > 2 and int(time.pop())):
121 raise ValueError
122 month, day = day
123 hour, minute = time
124 spec = minute, hour, day, month, dow
125 for x, (y, z) in zip(spec, ((0, 60), (0, 24), (1, 31), (1, 12))):
126 if x != '*':
127 for x in x.split(','):
128 x = map(int, x.split('/', 1))
129 x[0] -= y
130 if x[0] < 0 or len(x) > 1 and x[0] >= x[1] or z <= sum(x):
131 raise ValueError
132 return ' '.join(spec)
133
134 def test(self):
135 def _(systemd, cron):
136 self.assertEqual(systemd_to_cron(systemd), cron)
137 _("Sat,Mon-Thu,Sun", "0 0 * * 0,1-4,6")
138 _("mon,sun *-* 2,1:23", "23 2,1 * * 0,1")
139 _("Wed, 17:48", "48 17 * * 3")
140 _("Wed-Sat,Tue 10-* 1:2", "2 1 * 10 2,3-6")
141 _("*-*-7 0:0:0", "0 0 7 * *")
142 _("10-15", "0 0 15 10 *")
143 _("monday *-12-* 17:00", "00 17 * 12 1")
144 _("12,14,13,12:20,10,30", "20,10,30 12,14,13,12 * * *") # TODO: sort
145 _("*-1/2-1,3 *:30", "30 * 1,3 1/2 *")
146 _("03-05 08:05", "05 08 05 03 *")
147 _("08:05:00", "05 08 * * *")
148 _("05:40", "40 05 * * *")
149 _("Sat,Sun 12-* 08:05", "05 08 * 12 0,6")
150 _("Sat,Sun 08:05", "05 08 * * 0,6")
151
152 def _(systemd):
153 self.assertRaises(Exception, systemd_to_cron, systemd)
154 _("test")
155 _("")
156 _("7")
157 _("121212:1:2")
158
159 _("Wed *-1")
160 _("08:05:40")
161 _("2003-03-05")
162
163 _("0-1"); _("13-1"); _("6/4-1"); _("5/8-1")
164 _("1-0"); _("1-32"); _("1-4/3"); _("1-14/18")
165 _("24:0");_("9/9:0"); _("8/16:0")
166 _("0:60"); _("0:22/22"); _("0:15/45")
167
168 if __name__ == '__main__':
169 import unittest
170 unittest.TextTestRunner().run(type('', (unittest.TestCase,), {
171 'runTest': test})())