Fri, 19 Jan 2018 16:53:13 +0000
subprocess improvements
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 |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
10 | from threading import Thread, Lock |
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 |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
77 | self.thread=None |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
78 | self.lock=Lock() |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
79 | |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
80 | def __block_when_running(self): |
5 | 81 | with self.lock: |
82 | not_running=self.borg_instance is None and self.thread is None | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
83 | assert(not_running) |
2 | 84 | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
85 | def __listener(self): |
6 | 86 | success=False |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
87 | for status in iter(self.borg_instance.read, None): |
6 | 88 | logging.info(str(status)) |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
89 | t=status['type'] |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
90 | if t=='progress_percent': |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
91 | pass |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
92 | elif t=='archive_progress': |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
93 | pass |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
94 | elif t=='progress_message': |
6 | 95 | if 'finished' in status: |
96 | logging.info('Borg subprocess finished succesfully') | |
97 | success=status['finished'] | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
98 | elif t=='progress_percent': |
6 | 99 | # Temporary output |
100 | 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
|
101 | pass |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
102 | elif t=='file_status': |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
103 | pass |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
104 | elif t=='log_message': |
6 | 105 | if 'levelname' not in status: |
106 | status['levelname']='ERROR' | |
107 | if 'message' not in status: | |
108 | status['message']='UNKNOWN' | |
109 | if 'name' not in status: | |
110 | status['name']='borg' | |
111 | logging.log(translate_loglevel(status['levelname']), | |
112 | status['name'] + ': ' + status['message']) | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
113 | elif t=='exception': |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
114 | pass |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
115 | elif t=='unparsed_error': |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
116 | pass |
6 | 117 | |
118 | logging.info('Waiting for borg subprocess to terminate') | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
119 | |
6 | 120 | self.borg_instance.wait() |
121 | ||
122 | logging.info('Borg subprocess terminated; terminating listener thread') | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
123 | |
5 | 124 | with self.lock: |
125 | if self.current_operation=='create': | |
126 | self.lastrun=self.time_started | |
127 | self.lastrun_success=success | |
128 | self.borg_instance=None | |
129 | self.thread=None | |
130 | self.current_operation=None | |
131 | self.time_started=None | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
132 | |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
133 | def __launch(self, queue, operation, archive_or_repository, *args): |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
134 | |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
135 | inst=BorgInstance(operation, archive_or_repository, *args) |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
136 | inst.launch() |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
137 | |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
138 | t=Thread(target=self.__listener) |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
139 | t.daemon=True |
2 | 140 | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
141 | self.thread=t |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
142 | self.borg_instance=inst |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
143 | self.queue=queue |
5 | 144 | self.current_operation=operation |
145 | self.time_started=time.monotonic() | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
146 | |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
147 | t.start() |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
148 | |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
149 | def create(self, queue): |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
150 | self.__block_when_running() |
2 | 151 | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
152 | archive="%s::%s%s" % (self.repository, |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
153 | self.archive_prefix, |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
154 | self.archive_template) |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
155 | |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
156 | self.__launch(queue, 'create', archive, |
6 | 157 | self.common_parameters+self.create_parameters, |
158 | self.paths) | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
159 | |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
160 | def prune(self, queue): |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
161 | self.__block_when_running() |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
162 | self.__launch(queue, 'prune', self.repository, |
6 | 163 | ([{'prefix': self.archive_prefix}] + |
164 | self.common_parameters + | |
165 | self.prune_parameters)) | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
166 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
167 | # 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
|
168 | def abort(self): |
5 | 169 | with self.lock: |
170 | if self.borg_instance: | |
171 | self.borg_instance.terminate() | |
172 | if self.thread: | |
173 | self.thread.terminate() | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
174 | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
175 | def join(self): |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
176 | if self.thread: |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
177 | self.thread.join() |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
178 | |
5 | 179 | def next_action(): |
180 | # TODO pruning as well | |
181 | now=time.monotonic() | |
182 | if not self.lastrun: | |
183 | return 'create', now+self.retry_interval | |
184 | elif not self.lastrun_success: | |
185 | return 'create', self.lastrun+self.retry_interval | |
186 | else: | |
187 | if self.backup_interval==0: | |
188 | return 'none', 0 | |
189 | else: | |
190 | return 'create', self.lastrun+self.backup_interval | |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
191 | |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
192 |