Mon, 22 Jan 2018 02:37:46 +0000
README configuration file location update
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 |
20
fdfbe5d7b677
Keychain support and random fixes
Tuomo Valkonen <tuomov@iki.fi>
parents:
17
diff
changeset
|
8 | import keyring |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
9 | import borgend |
2 | 10 | from instance import BorgInstance |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
11 | from queue import Queue |
8 | 12 | from threading import Thread, Lock, Timer |
2 | 13 | |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
14 | logger=borgend.logger.getChild(__name__) |
31 | 15 | |
45
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
16 | # State |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
17 | INACTIVE=0 |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
18 | SCHEDULED=1 |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
19 | ACTIVE=2 |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
20 | BUSY=3 |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
21 | OFFLINE=4 |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
22 | ERRORS=5 |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
23 | |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
24 | def combine_state(state1, state2): |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
25 | return max(state1, state2) |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
26 | |
6 | 27 | loglevel_translation={ |
28 | 'CRITICAL': logging.CRITICAL, | |
29 | 'ERROR': logging.ERROR, | |
30 | 'WARNING': logging.WARNING, | |
31 | 'DEBUG': logging.DEBUG, | |
32 | 'INFO': logging.INFO | |
33 | } | |
34 | ||
35 | def translate_loglevel(x): | |
36 | if x in loglevel_translation: | |
37 | return loglevel_translation[x] | |
38 | else: | |
39 | return logging.ERROR | |
40 | ||
15 | 41 | def safe_get_int(t, x): |
42 | if x in t: | |
43 | tmp=t[x] | |
44 | if isinstance(tmp, int): | |
45 | return tmp | |
46 | return None | |
47 | ||
48 | ||
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
49 | class Backup: |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
50 | |
2 | 51 | def __decode_config(self, cfg): |
52 | loc0='backup target %d' % self.identifier | |
53 | ||
54 | self.name=config.check_string(cfg, 'name', 'Name', loc0) | |
55 | ||
56 | self.loc='backup target "%s"' % self.name | |
57 | ||
58 | self.repository=config.check_string(cfg, 'repository', | |
59 | 'Target repository', self.loc) | |
60 | ||
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
61 | 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
|
62 | 'Archive prefix', self.loc) |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
63 | |
2 | 64 | self.archive_template=config.check_string(cfg, 'archive_template', |
65 | 'Archive template', self.loc) | |
66 | ||
67 | self.backup_interval=config.check_nonneg_int(cfg, 'backup_interval', | |
68 | 'Backup interval', self.loc, | |
69 | config.defaults['backup_interval']) | |
70 | ||
71 | self.retry_interval=config.check_nonneg_int(cfg, 'retry_interval', | |
72 | 'Retry interval', self.loc, | |
73 | config.defaults['retry_interval']) | |
74 | ||
75 | self.paths=config.check_nonempty_list_of_strings(cfg, 'paths', 'Paths', self.loc) | |
76 | ||
6 | 77 | 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
|
78 | 'Borg parameters', self.loc, |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
79 | default=[]) |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
80 | |
6 | 81 | self.create_parameters=config.check_list_of_dicts(cfg, 'create_parameters', |
82 | 'Create parameters', self.loc, | |
83 | default=[]) | |
84 | ||
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
85 | self.prune_parameters=config.check_list_of_dicts(cfg, 'prune_parameters', |
6 | 86 | 'Prune parameters', self.loc, |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
87 | default=[]) |
2 | 88 | |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
89 | self.__keychain_account=config.check_string(cfg, 'keychain_account', |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
90 | 'Keychain account', self.loc, |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
91 | default='') |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
92 | |
32 | 93 | self.__passphrase=None |
94 | ||
30
3dd525652dc8
Added no_quit_menu_entry option
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
95 | if config.settings['extract_passphrases_at_startup']: |
32 | 96 | try: |
97 | self.extract_passphrase() | |
98 | except Exception: | |
99 | pass | |
20
fdfbe5d7b677
Keychain support and random fixes
Tuomo Valkonen <tuomov@iki.fi>
parents:
17
diff
changeset
|
100 | |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
101 | def extract_passphrase(self): |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
102 | acc=self.__keychain_account |
32 | 103 | if not self.__passphrase: |
104 | if acc and acc!='': | |
105 | logger.debug('Requesting passphrase') | |
106 | try: | |
107 | pw=keyring.get_password("borg-backup", acc) | |
108 | except Exception as err: | |
109 | logger.error('Failed to retrieve password: %s' % str(err)) | |
110 | raise err | |
111 | else: | |
112 | logger.debug('Received passphrase') | |
113 | self.__passphrase=pw | |
114 | else: | |
115 | self.__passphrase=None | |
116 | return self.__passphrase | |
2 | 117 | |
118 | def __init__(self, identifier, cfg): | |
119 | self.identifier=identifier | |
120 | ||
121 | self.__decode_config(cfg) | |
122 | ||
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
123 | self.config=config |
32 | 124 | self.lastrun_when=None |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
125 | self.borg_instance=None |
5 | 126 | self.current_operation=None |
7 | 127 | self.thread_log=None |
8 | 128 | self.thread_res=None |
129 | self.timer=None | |
10 | 130 | self.scheduled_operation=None |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
131 | self.lock=Lock() |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
132 | self.__status_update_callback=None |
45
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
133 | self.state=INACTIVE |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
134 | |
7 | 135 | def is_running(self): |
136 | with self.lock: | |
8 | 137 | running=self.__is_running_unlocked() |
138 | return running | |
139 | ||
140 | def __is_running_unlocked(self): | |
141 | running=self.current_operation | |
142 | if not running: | |
143 | # Consistency check | |
144 | assert((not self.borg_instance and not self.thread_log and | |
145 | not self.thread_res)) | |
7 | 146 | return running |
147 | ||
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
148 | def __block_when_running(self): |
7 | 149 | running=self.is_running() |
150 | assert(not running) | |
2 | 151 | |
7 | 152 | def __log_listener(self): |
31 | 153 | logger.debug('Log listener thread waiting for entries') |
7 | 154 | success=True |
155 | for status in iter(self.borg_instance.read_log, None): | |
31 | 156 | logger.debug(str(status)) |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
157 | t=status['type'] |
15 | 158 | |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
159 | errors_this_message=None |
15 | 160 | callback=None |
161 | ||
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
162 | if t=='progress_percent': |
15 | 163 | current=safe_get_int(status, 'current') |
164 | total=safe_get_int(status, 'total') | |
165 | if current is not None and total is not None: | |
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
|
166 | with self.lock: |
17 | 167 | self.current_operation['progress_current']=current |
168 | self.current_operation['progress_total']=total | |
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
|
169 | status, callback=self.__status_unlocked() |
15 | 170 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
171 | elif t=='archive_progress': |
15 | 172 | original_size=safe_get_int(status, 'original_size') |
173 | compressed_size=safe_get_int(status, 'compressed_size') | |
174 | deduplicated_size=safe_get_int(status, 'deduplicated_size') | |
175 | if original_size is not None and original_size is not None and deduplicated_size is not None: | |
176 | with self.lock: | |
17 | 177 | self.current_operation['original_size']=original_size |
178 | self.current_operation['compressed_size']=compressed_size | |
179 | self.current_operation['deduplicated_size']=deduplicated_size | |
15 | 180 | status, callback=self.__status_unlocked() |
181 | ||
182 | elif t=='progress_message': | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
183 | pass |
15 | 184 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
185 | elif t=='file_status': |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
186 | pass |
15 | 187 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
188 | elif t=='log_message': |
6 | 189 | if 'levelname' not in status: |
190 | status['levelname']='ERROR' | |
191 | if 'message' not in status: | |
192 | status['message']='UNKNOWN' | |
193 | if 'name' not in status: | |
194 | status['name']='borg' | |
15 | 195 | lvl=translate_loglevel(status['levelname']) |
31 | 196 | logger.log(lvl, status['name'] + ': ' + status['message']) |
15 | 197 | if lvl>=logging.WARNING: |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
198 | errors_this_message=status |
45
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
199 | state=ERRORS |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
200 | if ('msgid' in status and |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
201 | (status['msgid']=='LockTimeout' or # observed in reality |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
202 | status['msgid']=='LockErrorT' or # in docs |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
203 | status['msgid']=='LockErrorT')): # in docs |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
204 | state=BUSY |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
205 | with self.lock: |
45
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
206 | self.state=combine_state(self.state, state) |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
207 | status, callback=self.__status_unlocked() |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
208 | else: |
31 | 209 | logger.debug('Unrecognised log entry %s' % str(status)) |
15 | 210 | |
211 | if callback: | |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
212 | callback(self, status, errors=errors_this_message) |
6 | 213 | |
31 | 214 | logger.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
|
215 | |
6 | 216 | self.borg_instance.wait() |
217 | ||
31 | 218 | logger.debug('Borg subprocess terminated; terminating log listener thread') |
7 | 219 | |
220 | def __result_listener(self): | |
10 | 221 | with self.lock: |
222 | status, callback=self.__status_unlocked() | |
223 | if callback: | |
224 | callback(self, status) | |
225 | ||
31 | 226 | logger.debug('Result listener thread waiting for result') |
7 | 227 | |
228 | res=self.borg_instance.read_result() | |
229 | ||
45
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
230 | # Finish processing remaining errors |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
231 | self.thread_log.join() |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
232 | |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
233 | with self.lock: |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
234 | state=self.state |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
235 | |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
236 | # If there were no errors, reset back to INACTIVE state |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
237 | if state==ACTIVE: |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
238 | state=INACTIVE |
7 | 239 | |
31 | 240 | logger.debug('Borg result: %s' % str(res)) |
7 | 241 | |
45
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
242 | if res is None and state==INACTIVE: |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
243 | logger.error('No result from borg despite no error in log') |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
244 | state=ERRORS |
7 | 245 | |
31 | 246 | logger.debug('Waiting for borg subprocess to terminate in result thread') |
7 | 247 | |
45
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
248 | if not self.borg_instance.wait(): |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
249 | logger.critical('Borg subprocess did not terminate') |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
250 | state=combine_state(state, ERRORS) |
7 | 251 | |
45
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
252 | logger.debug('Borg subprocess terminated (end state %s); terminating result listener thread' % str(state)) |
12 | 253 | |
5 | 254 | with self.lock: |
10 | 255 | if self.current_operation['operation']=='create': |
32 | 256 | self.lastrun_when=self.current_operation['when_monotonic'] |
7 | 257 | self.thread_res=None |
12 | 258 | self.thread_log=None |
259 | self.borg_instance=None | |
260 | self.current_operation=None | |
261 | self.__schedule_unlocked() | |
45
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
262 | self.state=state |
10 | 263 | status, callback=self.__status_unlocked() |
264 | if callback: | |
265 | callback(self, status) | |
7 | 266 | |
10 | 267 | def __do_launch(self, queue, op, archive_or_repository, *args): |
32 | 268 | passphrase=self.extract_passphrase() |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
269 | |
10 | 270 | inst=BorgInstance(op['operation'], archive_or_repository, *args) |
32 | 271 | inst.launch(passphrase=passphrase) |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
272 | |
31 | 273 | logger.debug('Creating listener threads') |
274 | ||
7 | 275 | t_log=Thread(target=self.__log_listener) |
276 | t_log.daemon=True | |
2 | 277 | |
7 | 278 | t_res=Thread(target=self.__result_listener) |
279 | t_res.daemon=True | |
280 | ||
281 | self.thread_log=t_log | |
282 | self.thread_res=t_res | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
283 | self.borg_instance=inst |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
284 | self.queue=queue |
10 | 285 | self.current_operation=op |
286 | self.current_operation['when_monotonic']=time.monotonic() | |
45
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
287 | self.state=ACTIVE |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
288 | |
7 | 289 | t_log.start() |
290 | t_res.start() | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
291 | |
10 | 292 | def __launch(self, op, queue): |
8 | 293 | if self.__is_running_unlocked(): |
294 | logging.info('Cannot start %s: already running %s' | |
295 | % (operation, self.current_operation)) | |
296 | return False | |
297 | else: | |
298 | if self.timer: | |
31 | 299 | logger.debug('Unscheduling timed operation due to launch of operation') |
8 | 300 | self.timer=None |
10 | 301 | self.scheduled_operation=None |
8 | 302 | |
32 | 303 | try: |
304 | logger.debug("Launching '%s' on '%s'" % (op['operation'], self.name)) | |
8 | 305 | |
32 | 306 | if op['operation']=='create': |
307 | archive="%s::%s%s" % (self.repository, | |
308 | self.archive_prefix, | |
309 | self.archive_template) | |
2 | 310 | |
32 | 311 | self.__do_launch(queue, op, archive, |
312 | self.common_parameters+self.create_parameters, | |
313 | self.paths) | |
314 | elif op['operation']=='prune': | |
315 | self.__do_launch(queue, op, self.repository, | |
316 | ([{'prefix': self.archive_prefix}] + | |
317 | self.common_parameters + | |
318 | self.prune_parameters)) | |
319 | else: | |
320 | raise NotImplementedError("Invalid operation '%s'" % op['operation']) | |
321 | except Exception as err: | |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
322 | logger.debug('Rescheduling after failure') |
32 | 323 | self.lastrun_when=time.monotonic() |
45
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
324 | self.state=ERRORS |
8 | 325 | self.__schedule_unlocked() |
32 | 326 | raise err |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
327 | |
8 | 328 | return True |
329 | ||
330 | def create(self, queue): | |
10 | 331 | op={'operation': 'create', 'detail': 'manual'} |
8 | 332 | with self.lock: |
10 | 333 | res=self.__launch(op, queue) |
8 | 334 | return res |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
335 | |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
336 | def prune(self, queue): |
10 | 337 | op={'operation': 'prune', 'detail': 'manual'} |
8 | 338 | with self.lock: |
10 | 339 | res=self.__launch(op, queue) |
8 | 340 | return res |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
341 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
342 | # 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
|
343 | def abort(self): |
5 | 344 | with self.lock: |
345 | if self.borg_instance: | |
346 | self.borg_instance.terminate() | |
46
ecb41072a1b7
Unsuccesfull attempts at clean quit on exceptions that fall through
Tuomo Valkonen <tuomov@iki.fi>
parents:
45
diff
changeset
|
347 | #thread_log=self.thread_log |
ecb41072a1b7
Unsuccesfull attempts at clean quit on exceptions that fall through
Tuomo Valkonen <tuomov@iki.fi>
parents:
45
diff
changeset
|
348 | #thread_res=self.thread_res |
7 | 349 | |
46
ecb41072a1b7
Unsuccesfull attempts at clean quit on exceptions that fall through
Tuomo Valkonen <tuomov@iki.fi>
parents:
45
diff
changeset
|
350 | #if thread_log: |
ecb41072a1b7
Unsuccesfull attempts at clean quit on exceptions that fall through
Tuomo Valkonen <tuomov@iki.fi>
parents:
45
diff
changeset
|
351 | # thread_log.terminate() |
7 | 352 | |
46
ecb41072a1b7
Unsuccesfull attempts at clean quit on exceptions that fall through
Tuomo Valkonen <tuomov@iki.fi>
parents:
45
diff
changeset
|
353 | #if thread_res: |
ecb41072a1b7
Unsuccesfull attempts at clean quit on exceptions that fall through
Tuomo Valkonen <tuomov@iki.fi>
parents:
45
diff
changeset
|
354 | # thread_res.terminate() |
7 | 355 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
356 | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
357 | def join(self): |
31 | 358 | logger.debug('Waiting for borg listener threads to terminate') |
7 | 359 | |
360 | with self.lock: | |
361 | thread_log=self.thread_log | |
362 | thread_res=self.thread_res | |
363 | ||
364 | if thread_log: | |
365 | thread_log.join() | |
366 | ||
367 | if thread_res: | |
368 | thread_res.join() | |
369 | ||
370 | 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
|
371 | |
8 | 372 | def __queue_timed_operation(self): |
373 | with self.lock: | |
10 | 374 | op=self.scheduled_operation |
375 | self.scheduled_operation=None | |
8 | 376 | self.timer=None |
377 | ||
378 | if self.__is_running_unlocked(): | |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
379 | logger.info('Aborted queue operation, as an operation is already running') |
8 | 380 | else: |
381 | # TODO: Queue on 'repository' and online status for SSH, etc. | |
382 | ||
383 | # TODO: UI comms. queue? | |
10 | 384 | self.__launch(op, None) |
8 | 385 | |
386 | def __next_operation_unlocked(self): | |
387 | # TODO: pruning as well | |
5 | 388 | now=time.monotonic() |
32 | 389 | if not self.lastrun_when: |
10 | 390 | initial_interval=self.retry_interval |
391 | if initial_interval==0: | |
392 | initial_interval=self.backup_interval | |
393 | if initial_interval==0: | |
394 | return None | |
395 | else: | |
396 | return {'operation': 'create', | |
397 | 'detail': 'initial', | |
398 | 'when_monotonic': now+initial_interval} | |
45
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
399 | elif self.state>=BUSY: |
10 | 400 | if self.retry_interval==0: |
401 | return None | |
402 | else: | |
403 | return {'operation': 'create', | |
404 | 'detail': 'retry', | |
32 | 405 | 'when_monotonic': self.lastrun_when+self.retry_interval} |
5 | 406 | else: |
407 | if self.backup_interval==0: | |
10 | 408 | return None |
5 | 409 | else: |
10 | 410 | return {'operation': 'create', |
38 | 411 | 'detail': 'normal', |
32 | 412 | 'when_monotonic': self.lastrun_when+self.backup_interval} |
10 | 413 | |
414 | def __schedule_unlocked(self): | |
415 | if self.current_operation: | |
416 | return self.current_operation | |
417 | else: | |
418 | op=self.__next_operation_unlocked() | |
419 | ||
420 | if op: | |
421 | now=time.monotonic() | |
422 | delay=max(0, op['when_monotonic']-now) | |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
423 | logger.info("Scheduling '%s' (detail: %s) of '%s' in %d seconds" % |
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
424 | (op['operation'], op['detail'], self.name, delay)) |
10 | 425 | tmr=Timer(delay, self.__queue_timed_operation) |
426 | self.scheduled_operation=op | |
427 | self.timer=tmr | |
45
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
428 | self.state=combine_state(self.state, SCHEDULED) |
10 | 429 | tmr.start() |
430 | ||
431 | return op | |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
432 | |
8 | 433 | def schedule(self): |
434 | with self.lock: | |
435 | return self.__schedule_unlocked() | |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
436 | |
10 | 437 | def status(self): |
438 | with self.lock: | |
439 | res=self.__status_unlocked() | |
440 | return res[0] | |
441 | ||
442 | def __status_unlocked(self): | |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
443 | callback=self.__status_update_callback |
38 | 444 | |
10 | 445 | if self.current_operation: |
446 | status=self.current_operation | |
447 | status['type']='current' | |
38 | 448 | # Errors should be set by listeners |
10 | 449 | else: |
38 | 450 | if self.scheduled_operation: |
451 | status=self.scheduled_operation | |
452 | status['type']='scheduled' | |
453 | else: | |
454 | status={'type': 'nothing'} | |
10 | 455 | |
45
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
456 | status['name']=self.name |
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
457 | status['state']=self.state |
38 | 458 | |
459 | if 'detail' not in status: | |
460 | status['detail']='NONE' | |
10 | 461 | |
462 | if 'when_monotonic' in status: | |
463 | status['when']=(status['when_monotonic'] | |
464 | -time.monotonic()+time.time()) | |
465 | ||
466 | return status, callback | |
467 | ||
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
468 | def set_status_update_callback(self, callback): |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
469 | with self.lock: |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
470 | self.__status_update_callback=callback |
10 | 471 | |
472 |