Sat, 20 Jan 2018 19:57:05 +0000
Semi-working menu items.
NOTES:
Python closures suck dog's balls... first and the last program I
write in Python until somebody fixes the brain-damaged scoping
that requires word _b=b hacks etc. to get normal behaviour.
See also http://math.andrej.com/2009/04/09/pythons-lambda-is-broken/
Rumps also sucks, apparently no way to update single menu items, one
always has to rebuild the entire menu. Spent hours trying to get it to
work until giving in.
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
1 | # |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
2 | # Borgend Backup instance |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
3 | # |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
4 | |
2 | 5 | import config |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
6 | import logging |
5 | 7 | import time |
2 | 8 | from instance import BorgInstance |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
9 | from queue import Queue |
8 | 10 | from threading import Thread, Lock, Timer |
2 | 11 | |
6 | 12 | loglevel_translation={ |
13 | 'CRITICAL': logging.CRITICAL, | |
14 | 'ERROR': logging.ERROR, | |
15 | 'WARNING': logging.WARNING, | |
16 | 'DEBUG': logging.DEBUG, | |
17 | 'INFO': logging.INFO | |
18 | } | |
19 | ||
20 | def translate_loglevel(x): | |
21 | if x in loglevel_translation: | |
22 | return loglevel_translation[x] | |
23 | else: | |
24 | return logging.ERROR | |
25 | ||
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
26 | class Backup: |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
27 | |
2 | 28 | def __decode_config(self, cfg): |
29 | loc0='backup target %d' % self.identifier | |
30 | ||
31 | self.name=config.check_string(cfg, 'name', 'Name', loc0) | |
32 | ||
33 | self.loc='backup target "%s"' % self.name | |
34 | ||
35 | self.repository=config.check_string(cfg, 'repository', | |
36 | 'Target repository', self.loc) | |
37 | ||
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
38 | self.archive_prefix=config.check_string(cfg, 'archive_prefix', |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
39 | 'Archive prefix', self.loc) |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
40 | |
2 | 41 | self.archive_template=config.check_string(cfg, 'archive_template', |
42 | 'Archive template', self.loc) | |
43 | ||
44 | self.backup_interval=config.check_nonneg_int(cfg, 'backup_interval', | |
45 | 'Backup interval', self.loc, | |
46 | config.defaults['backup_interval']) | |
47 | ||
48 | self.retry_interval=config.check_nonneg_int(cfg, 'retry_interval', | |
49 | 'Retry interval', self.loc, | |
50 | config.defaults['retry_interval']) | |
51 | ||
52 | self.paths=config.check_nonempty_list_of_strings(cfg, 'paths', 'Paths', self.loc) | |
53 | ||
6 | 54 | self.common_parameters=config.check_list_of_dicts(cfg, 'common_parameters', |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
55 | 'Borg parameters', self.loc, |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
56 | default=[]) |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
57 | |
6 | 58 | self.create_parameters=config.check_list_of_dicts(cfg, 'create_parameters', |
59 | 'Create parameters', self.loc, | |
60 | default=[]) | |
61 | ||
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
62 | self.prune_parameters=config.check_list_of_dicts(cfg, 'prune_parameters', |
6 | 63 | 'Prune parameters', self.loc, |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
64 | default=[]) |
2 | 65 | |
66 | ||
67 | def __init__(self, identifier, cfg): | |
68 | self.identifier=identifier | |
69 | ||
70 | self.__decode_config(cfg) | |
71 | ||
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
72 | self.config=config |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
73 | self.lastrun=None |
5 | 74 | self.lastrun_success=None |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
75 | self.borg_instance=None |
5 | 76 | self.current_operation=None |
7 | 77 | self.thread_log=None |
8 | 78 | self.thread_res=None |
79 | self.timer=None | |
10 | 80 | self.scheduled_operation=None |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
81 | self.lock=Lock() |
10 | 82 | self.status_update_callback=None |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
83 | |
7 | 84 | def is_running(self): |
85 | with self.lock: | |
8 | 86 | running=self.__is_running_unlocked() |
87 | return running | |
88 | ||
89 | def __is_running_unlocked(self): | |
90 | running=self.current_operation | |
91 | if not running: | |
92 | # Consistency check | |
93 | assert((not self.borg_instance and not self.thread_log and | |
94 | not self.thread_res)) | |
7 | 95 | return running |
96 | ||
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
97 | def __block_when_running(self): |
7 | 98 | running=self.is_running() |
99 | assert(not running) | |
2 | 100 | |
7 | 101 | def __log_listener(self): |
102 | logging.debug('Log listener thread waiting for entries') | |
103 | success=True | |
104 | for status in iter(self.borg_instance.read_log, None): | |
105 | logging.debug(str(status)) | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
106 | t=status['type'] |
7 | 107 | #may_indicate_finished=False |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
108 | if t=='progress_percent': |
7 | 109 | #may_indicate_finished=True |
110 | # Temporary output | |
111 | if 'current' not in status: | |
112 | status['current']=0 | |
113 | if 'total' not in status: | |
114 | status['total']=0 | |
115 | print('%d / %d' % (status['current'], status['total'])) | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
116 | elif t=='archive_progress': |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
117 | pass |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
118 | elif t=='progress_message': |
7 | 119 | #may_indicate_finished=True |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
120 | pass |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
121 | elif t=='file_status': |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
122 | pass |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
123 | elif t=='log_message': |
6 | 124 | if 'levelname' not in status: |
125 | status['levelname']='ERROR' | |
126 | if 'message' not in status: | |
127 | status['message']='UNKNOWN' | |
128 | if 'name' not in status: | |
129 | status['name']='borg' | |
130 | logging.log(translate_loglevel(status['levelname']), | |
131 | status['name'] + ': ' + status['message']) | |
7 | 132 | # set success=False? |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
133 | elif t=='exception': |
7 | 134 | success=False |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
135 | elif t=='unparsed_error': |
7 | 136 | success=False |
6 | 137 | |
7 | 138 | logging.debug('Waiting for borg subprocess to terminate in log thread') |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
139 | |
6 | 140 | self.borg_instance.wait() |
141 | ||
7 | 142 | logging.debug('Borg subprocess terminated; terminating log listener thread') |
143 | ||
144 | with self.lock: | |
145 | self.thread_log=None | |
8 | 146 | self.__finish_and_reschedule_if_both_listeners_terminated() |
7 | 147 | |
148 | ||
149 | def __result_listener(self): | |
10 | 150 | with self.lock: |
151 | status, callback=self.__status_unlocked() | |
152 | if callback: | |
153 | callback(self, status) | |
154 | ||
7 | 155 | logging.debug('Result listener thread waiting for result') |
156 | ||
157 | res=self.borg_instance.read_result() | |
158 | ||
159 | success=True | |
160 | ||
161 | logging.debug('Borg result: %s' % str(res)) | |
162 | ||
163 | if res==None: | |
164 | success=False | |
165 | ||
166 | logging.debug('Waiting for borg subprocess to terminate in result thread') | |
167 | ||
8 | 168 | success=success and self.borg_instance.wait() |
7 | 169 | |
8 | 170 | logging.debug('Borg subprocess terminated (success: %s); terminating result listener thread' % str(success)) |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
171 | |
5 | 172 | with self.lock: |
10 | 173 | if self.current_operation['operation']=='create': |
174 | self.lastrun=self.current_operation['when_monotonic'] | |
5 | 175 | self.lastrun_success=success |
7 | 176 | self.thread_res=None |
8 | 177 | self.__finish_and_reschedule_if_both_listeners_terminated() |
10 | 178 | status, callback=self.__status_unlocked() |
179 | if callback: | |
180 | callback(self, status) | |
7 | 181 | |
8 | 182 | def __finish_and_reschedule_if_both_listeners_terminated(self): |
7 | 183 | if self.thread_res==None and self.thread_log==None: |
184 | logging.debug('Both threads terminated') | |
5 | 185 | self.borg_instance=None |
186 | self.current_operation=None | |
8 | 187 | self.__schedule_unlocked() |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
188 | |
10 | 189 | def __do_launch(self, queue, op, archive_or_repository, *args): |
190 | inst=BorgInstance(op['operation'], archive_or_repository, *args) | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
191 | inst.launch() |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
192 | |
7 | 193 | t_log=Thread(target=self.__log_listener) |
194 | t_log.daemon=True | |
2 | 195 | |
7 | 196 | t_res=Thread(target=self.__result_listener) |
197 | t_res.daemon=True | |
198 | ||
199 | self.thread_log=t_log | |
200 | self.thread_res=t_res | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
201 | self.borg_instance=inst |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
202 | self.queue=queue |
10 | 203 | self.current_operation=op |
204 | self.current_operation['when_monotonic']=time.monotonic() | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
205 | |
7 | 206 | t_log.start() |
207 | t_res.start() | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
208 | |
10 | 209 | def __launch(self, op, queue): |
8 | 210 | if self.__is_running_unlocked(): |
211 | logging.info('Cannot start %s: already running %s' | |
212 | % (operation, self.current_operation)) | |
213 | return False | |
214 | else: | |
215 | if self.timer: | |
216 | logging.debug('Unscheduling timed operation due to launch of operation') | |
217 | self.timer=None | |
10 | 218 | self.scheduled_operation=None |
8 | 219 | |
10 | 220 | logging.debug("Launching '%s' on '%s'" % (op['operation'], self.name)) |
8 | 221 | |
10 | 222 | if op['operation']=='create': |
8 | 223 | archive="%s::%s%s" % (self.repository, |
224 | self.archive_prefix, | |
225 | self.archive_template) | |
2 | 226 | |
10 | 227 | self.__do_launch(queue, op, archive, |
8 | 228 | self.common_parameters+self.create_parameters, |
229 | self.paths) | |
10 | 230 | elif op['operation']=='prune': |
231 | self.__do_launch(queue, op, self.repository, | |
8 | 232 | ([{'prefix': self.archive_prefix}] + |
233 | self.common_parameters + | |
234 | self.prune_parameters)) | |
235 | else: | |
10 | 236 | logging.error("Invalid operaton '%s'" % op['operation']) |
8 | 237 | self.__schedule_unlocked() |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
238 | |
8 | 239 | return True |
240 | ||
241 | def create(self, queue): | |
10 | 242 | op={'operation': 'create', 'detail': 'manual'} |
8 | 243 | with self.lock: |
10 | 244 | res=self.__launch(op, queue) |
8 | 245 | return res |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
246 | |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
247 | def prune(self, queue): |
10 | 248 | op={'operation': 'prune', 'detail': 'manual'} |
8 | 249 | with self.lock: |
10 | 250 | res=self.__launch(op, queue) |
8 | 251 | return res |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
252 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
253 | # TODO: Decide exact (manual) abort mechanism. Perhaps two stages |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
254 | def abort(self): |
5 | 255 | with self.lock: |
256 | if self.borg_instance: | |
257 | self.borg_instance.terminate() | |
7 | 258 | thread_log=self.thread_log |
259 | thread_res=self.thread_res | |
260 | ||
261 | if thread_log: | |
262 | thread_log.terminate() | |
263 | ||
264 | if thread_res: | |
265 | thread_res.terminate() | |
266 | ||
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
267 | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
268 | def join(self): |
8 | 269 | logging.debug('Waiting for borg listener threads to terminate') |
7 | 270 | |
271 | with self.lock: | |
272 | thread_log=self.thread_log | |
273 | thread_res=self.thread_res | |
274 | ||
275 | if thread_log: | |
276 | thread_log.join() | |
277 | ||
278 | if thread_res: | |
279 | thread_res.join() | |
280 | ||
281 | assert(self.thread_log==None and self.thread_res==None) | |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
282 | |
8 | 283 | def __queue_timed_operation(self): |
284 | with self.lock: | |
10 | 285 | op=self.scheduled_operation |
286 | self.scheduled_operation=None | |
8 | 287 | self.timer=None |
288 | ||
289 | if self.__is_running_unlocked(): | |
290 | logging.info('Aborted queue operation, as an operation is already running') | |
291 | else: | |
292 | # TODO: Queue on 'repository' and online status for SSH, etc. | |
293 | ||
294 | # TODO: UI comms. queue? | |
10 | 295 | self.__launch(op, None) |
8 | 296 | |
297 | def __next_operation_unlocked(self): | |
298 | # TODO: pruning as well | |
5 | 299 | now=time.monotonic() |
300 | if not self.lastrun: | |
10 | 301 | initial_interval=self.retry_interval |
302 | if initial_interval==0: | |
303 | initial_interval=self.backup_interval | |
304 | if initial_interval==0: | |
305 | return None | |
306 | else: | |
307 | return {'operation': 'create', | |
308 | 'detail': 'initial', | |
309 | 'when_monotonic': now+initial_interval} | |
5 | 310 | elif not self.lastrun_success: |
10 | 311 | if self.retry_interval==0: |
312 | return None | |
313 | else: | |
314 | return {'operation': 'create', | |
315 | 'detail': 'retry', | |
316 | 'when_monotonic': self.lastrun+self.retry_interval} | |
5 | 317 | else: |
318 | if self.backup_interval==0: | |
10 | 319 | return None |
5 | 320 | else: |
10 | 321 | return {'operation': 'create', |
322 | 'detail': None, | |
323 | 'when_monotonic': self.lastrun+self.backup_interval} | |
324 | ||
325 | def __schedule_unlocked(self): | |
326 | if self.current_operation: | |
327 | return self.current_operation | |
328 | else: | |
329 | op=self.__next_operation_unlocked() | |
330 | ||
331 | if op: | |
332 | now=time.monotonic() | |
333 | delay=max(0, op['when_monotonic']-now) | |
334 | logging.info("Scheduling '%s' (detail: %s) of '%s' in %d seconds" % | |
335 | (op['operation'], op['detail'], self.name, delay)) | |
336 | tmr=Timer(delay, self.__queue_timed_operation) | |
337 | self.scheduled_operation=op | |
338 | self.timer=tmr | |
339 | tmr.start() | |
340 | ||
341 | return op | |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
342 | |
8 | 343 | def schedule(self): |
344 | with self.lock: | |
345 | return self.__schedule_unlocked() | |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
346 | |
10 | 347 | def set_status_update_callback(self, callback): |
348 | with self.lock: | |
349 | self.status_update_callback=callback | |
8 | 350 | |
10 | 351 | def status(self): |
352 | with self.lock: | |
353 | res=self.__status_unlocked() | |
354 | return res[0] | |
355 | ||
356 | def __status_unlocked(self): | |
357 | callback=self.status_update_callback | |
358 | if self.current_operation: | |
359 | status=self.current_operation | |
360 | status['type']='current' | |
361 | elif self.scheduled_operation: | |
362 | status=self.scheduled_operation | |
363 | status['type']='scheduled' | |
364 | else: | |
365 | status={'type': 'nothing'} | |
366 | ||
367 | status['name']=self.name | |
368 | ||
369 | if 'when_monotonic' in status: | |
370 | status['when']=(status['when_monotonic'] | |
371 | -time.monotonic()+time.time()) | |
372 | ||
373 | return status, callback | |
374 | ||
375 | ||
376 |