Sat, 20 Jan 2018 21:27:05 +0000
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
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': |
14
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
12
diff
changeset
|
109 | if 'current' in status and 'total' in status: |
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
12
diff
changeset
|
110 | with self.lock: |
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
12
diff
changeset
|
111 | self.current_operation.progress_current=status['current'] |
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
12
diff
changeset
|
112 | self.current_operation.progress_total=status['total'] |
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
12
diff
changeset
|
113 | status, callback=self.__status_unlocked() |
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
12
diff
changeset
|
114 | if callback: |
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
12
diff
changeset
|
115 | callback(self, status) |
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 | def __result_listener(self): | |
10 | 145 | with self.lock: |
146 | status, callback=self.__status_unlocked() | |
147 | if callback: | |
148 | callback(self, status) | |
149 | ||
7 | 150 | logging.debug('Result listener thread waiting for result') |
151 | ||
152 | res=self.borg_instance.read_result() | |
153 | ||
154 | success=True | |
155 | ||
156 | logging.debug('Borg result: %s' % str(res)) | |
157 | ||
158 | if res==None: | |
159 | success=False | |
160 | ||
161 | logging.debug('Waiting for borg subprocess to terminate in result thread') | |
162 | ||
8 | 163 | success=success and self.borg_instance.wait() |
7 | 164 | |
8 | 165 | 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
|
166 | |
12 | 167 | self.thread_log.join() |
168 | ||
5 | 169 | with self.lock: |
10 | 170 | if self.current_operation['operation']=='create': |
171 | self.lastrun=self.current_operation['when_monotonic'] | |
5 | 172 | self.lastrun_success=success |
7 | 173 | self.thread_res=None |
12 | 174 | self.thread_log=None |
175 | self.borg_instance=None | |
176 | self.current_operation=None | |
177 | self.__schedule_unlocked() | |
10 | 178 | status, callback=self.__status_unlocked() |
179 | if callback: | |
180 | callback(self, status) | |
7 | 181 | |
10 | 182 | def __do_launch(self, queue, op, archive_or_repository, *args): |
183 | 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
|
184 | inst.launch() |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
185 | |
7 | 186 | t_log=Thread(target=self.__log_listener) |
187 | t_log.daemon=True | |
2 | 188 | |
7 | 189 | t_res=Thread(target=self.__result_listener) |
190 | t_res.daemon=True | |
191 | ||
192 | self.thread_log=t_log | |
193 | self.thread_res=t_res | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
194 | self.borg_instance=inst |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
195 | self.queue=queue |
10 | 196 | self.current_operation=op |
197 | 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
|
198 | |
7 | 199 | t_log.start() |
200 | t_res.start() | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
201 | |
10 | 202 | def __launch(self, op, queue): |
8 | 203 | if self.__is_running_unlocked(): |
204 | logging.info('Cannot start %s: already running %s' | |
205 | % (operation, self.current_operation)) | |
206 | return False | |
207 | else: | |
208 | if self.timer: | |
209 | logging.debug('Unscheduling timed operation due to launch of operation') | |
210 | self.timer=None | |
10 | 211 | self.scheduled_operation=None |
8 | 212 | |
10 | 213 | logging.debug("Launching '%s' on '%s'" % (op['operation'], self.name)) |
8 | 214 | |
10 | 215 | if op['operation']=='create': |
8 | 216 | archive="%s::%s%s" % (self.repository, |
217 | self.archive_prefix, | |
218 | self.archive_template) | |
2 | 219 | |
10 | 220 | self.__do_launch(queue, op, archive, |
8 | 221 | self.common_parameters+self.create_parameters, |
222 | self.paths) | |
10 | 223 | elif op['operation']=='prune': |
224 | self.__do_launch(queue, op, self.repository, | |
8 | 225 | ([{'prefix': self.archive_prefix}] + |
226 | self.common_parameters + | |
227 | self.prune_parameters)) | |
228 | else: | |
10 | 229 | logging.error("Invalid operaton '%s'" % op['operation']) |
8 | 230 | self.__schedule_unlocked() |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
231 | |
8 | 232 | return True |
233 | ||
234 | def create(self, queue): | |
10 | 235 | op={'operation': 'create', 'detail': 'manual'} |
8 | 236 | with self.lock: |
10 | 237 | res=self.__launch(op, queue) |
8 | 238 | return res |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
239 | |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
240 | def prune(self, queue): |
10 | 241 | op={'operation': 'prune', 'detail': 'manual'} |
8 | 242 | with self.lock: |
10 | 243 | res=self.__launch(op, queue) |
8 | 244 | return res |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
245 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
246 | # 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
|
247 | def abort(self): |
5 | 248 | with self.lock: |
249 | if self.borg_instance: | |
250 | self.borg_instance.terminate() | |
7 | 251 | thread_log=self.thread_log |
252 | thread_res=self.thread_res | |
253 | ||
254 | if thread_log: | |
255 | thread_log.terminate() | |
256 | ||
257 | if thread_res: | |
258 | thread_res.terminate() | |
259 | ||
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
260 | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
261 | def join(self): |
8 | 262 | logging.debug('Waiting for borg listener threads to terminate') |
7 | 263 | |
264 | with self.lock: | |
265 | thread_log=self.thread_log | |
266 | thread_res=self.thread_res | |
267 | ||
268 | if thread_log: | |
269 | thread_log.join() | |
270 | ||
271 | if thread_res: | |
272 | thread_res.join() | |
273 | ||
274 | 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
|
275 | |
8 | 276 | def __queue_timed_operation(self): |
277 | with self.lock: | |
10 | 278 | op=self.scheduled_operation |
279 | self.scheduled_operation=None | |
8 | 280 | self.timer=None |
281 | ||
282 | if self.__is_running_unlocked(): | |
283 | logging.info('Aborted queue operation, as an operation is already running') | |
284 | else: | |
285 | # TODO: Queue on 'repository' and online status for SSH, etc. | |
286 | ||
287 | # TODO: UI comms. queue? | |
10 | 288 | self.__launch(op, None) |
8 | 289 | |
290 | def __next_operation_unlocked(self): | |
291 | # TODO: pruning as well | |
5 | 292 | now=time.monotonic() |
293 | if not self.lastrun: | |
10 | 294 | initial_interval=self.retry_interval |
295 | if initial_interval==0: | |
296 | initial_interval=self.backup_interval | |
297 | if initial_interval==0: | |
298 | return None | |
299 | else: | |
300 | return {'operation': 'create', | |
301 | 'detail': 'initial', | |
302 | 'when_monotonic': now+initial_interval} | |
5 | 303 | elif not self.lastrun_success: |
10 | 304 | if self.retry_interval==0: |
305 | return None | |
306 | else: | |
307 | return {'operation': 'create', | |
308 | 'detail': 'retry', | |
309 | 'when_monotonic': self.lastrun+self.retry_interval} | |
5 | 310 | else: |
311 | if self.backup_interval==0: | |
10 | 312 | return None |
5 | 313 | else: |
10 | 314 | return {'operation': 'create', |
315 | 'detail': None, | |
316 | 'when_monotonic': self.lastrun+self.backup_interval} | |
317 | ||
318 | def __schedule_unlocked(self): | |
319 | if self.current_operation: | |
320 | return self.current_operation | |
321 | else: | |
322 | op=self.__next_operation_unlocked() | |
323 | ||
324 | if op: | |
325 | now=time.monotonic() | |
326 | delay=max(0, op['when_monotonic']-now) | |
327 | logging.info("Scheduling '%s' (detail: %s) of '%s' in %d seconds" % | |
328 | (op['operation'], op['detail'], self.name, delay)) | |
329 | tmr=Timer(delay, self.__queue_timed_operation) | |
330 | self.scheduled_operation=op | |
331 | self.timer=tmr | |
332 | tmr.start() | |
333 | ||
334 | return op | |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
335 | |
8 | 336 | def schedule(self): |
337 | with self.lock: | |
338 | return self.__schedule_unlocked() | |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
339 | |
10 | 340 | def set_status_update_callback(self, callback): |
341 | with self.lock: | |
342 | self.status_update_callback=callback | |
8 | 343 | |
10 | 344 | def status(self): |
345 | with self.lock: | |
346 | res=self.__status_unlocked() | |
347 | return res[0] | |
348 | ||
349 | def __status_unlocked(self): | |
350 | callback=self.status_update_callback | |
351 | if self.current_operation: | |
352 | status=self.current_operation | |
353 | status['type']='current' | |
354 | elif self.scheduled_operation: | |
355 | status=self.scheduled_operation | |
356 | status['type']='scheduled' | |
357 | else: | |
358 | status={'type': 'nothing'} | |
359 | ||
360 | status['name']=self.name | |
361 | ||
362 | if 'when_monotonic' in status: | |
363 | status['when']=(status['when_monotonic'] | |
364 | -time.monotonic()+time.time()) | |
365 | ||
366 | return status, callback | |
367 | ||
368 | ||
369 |