Mon, 22 Jan 2018 18:16:51 +0000
Improved scheduler
0 | 1 | # |
2 | # Borgend borg launcher / processor | |
3 | # | |
4 | ||
5 | import json | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
6 | import logging |
20
fdfbe5d7b677
Keychain support and random fixes
Tuomo Valkonen <tuomov@iki.fi>
parents:
12
diff
changeset
|
7 | import os |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
8 | import borgend |
2 | 9 | from subprocess import Popen, PIPE |
10 | from config import settings, arglistify | |
0 | 11 | |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
12 | logger=borgend.logger.getChild(__name__) |
31 | 13 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
14 | necessary_opts=['--log-json', '--progress'] |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
15 | |
7 | 16 | necessary_opts_for={ |
17 | 'create': ['--json'], | |
18 | 'info': ['--json'], | |
19 | 'list': ['--json'], | |
20 | } | |
21 | ||
0 | 22 | class BorgInstance: |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
23 | def __init__(self, operation, archive_or_repository, args, argsl): |
0 | 24 | self.operation=operation; |
25 | self.args=args; | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
26 | self.archive_or_repository=archive_or_repository; |
2 | 27 | self.argsl=argsl; |
0 | 28 | |
29 | def construct_cmdline(self): | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
30 | cmd=([settings['borg']['executable']]+necessary_opts+ |
2 | 31 | arglistify(settings['borg']['common_parameters'])+ |
32 | [self.operation]) | |
33 | tmp1=self.operation+'_parameters' | |
34 | if tmp1 in settings['borg']: | |
35 | cmd=cmd+arglistify(settings['borg'][tmp1]) | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
36 | |
7 | 37 | if self.operation in necessary_opts_for: |
38 | cmd=cmd+necessary_opts_for[self.operation] | |
39 | ||
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
40 | return cmd+arglistify(self.args)+[self.archive_or_repository]+self.argsl |
0 | 41 | |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
42 | def launch(self, passphrase=None): |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
43 | cmd=self.construct_cmdline() |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
44 | |
31 | 45 | logger.info('Launching ' + str(cmd)) |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
46 | |
32 | 47 | # Set passphrase if not, or set to empty if not known, so borg |
48 | # won't hang waiting for it, which seems to happen even if we | |
49 | # close stdin. | |
29 | 50 | env=os.environ.copy() |
32 | 51 | env['BORG_PASSPHRASE']=passphrase or '' |
20
fdfbe5d7b677
Keychain support and random fixes
Tuomo Valkonen <tuomov@iki.fi>
parents:
12
diff
changeset
|
52 | |
24
94d58d514d69
Workaround to PYTHONPATH and PYTHONHOME being messed up by py2app.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
53 | # Workaround: if launched is a standalone app created with py2app, |
94d58d514d69
Workaround to PYTHONPATH and PYTHONHOME being messed up by py2app.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
54 | # borg will fail unless Python environment is reset. |
94d58d514d69
Workaround to PYTHONPATH and PYTHONHOME being messed up by py2app.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
55 | # TODO: Of course, this will fail if the system needs the variables |
94d58d514d69
Workaround to PYTHONPATH and PYTHONHOME being messed up by py2app.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
56 | # PYTHONPATH or PYTHONHOME set to certain values. |
29 | 57 | if '_PY2APP_LAUNCHED_' in env: |
58 | val=env['_PY2APP_LAUNCHED_'] | |
59 | if val=='1': | |
60 | del env['PYTHONPATH'] | |
61 | del env['PYTHONHOME'] | |
24
94d58d514d69
Workaround to PYTHONPATH and PYTHONHOME being messed up by py2app.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
62 | |
20
fdfbe5d7b677
Keychain support and random fixes
Tuomo Valkonen <tuomov@iki.fi>
parents:
12
diff
changeset
|
63 | self.proc=Popen(cmd, env=env, stdout=PIPE, stderr=PIPE, stdin=PIPE) |
12 | 64 | |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
65 | # We don't do passphrase input etc. |
12 | 66 | self.proc.stdin.close() |
0 | 67 | |
7 | 68 | def read_result(self): |
69 | stream=self.proc.stdout | |
70 | line=stream.read(-1) | |
71 | if line==b'': | |
31 | 72 | logger.debug('Borg stdout pipe EOF?') |
7 | 73 | return None |
74 | ||
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
75 | try: |
7 | 76 | return json.loads(line) |
32 | 77 | except Exception as err: |
31 | 78 | logger.warning('JSON parse failed on: "%s"' % line) |
7 | 79 | return None |
80 | ||
81 | def read_log(self): | |
82 | stream=self.proc.stderr | |
83 | try: | |
84 | line=stream.readline() | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
85 | except err: |
31 | 86 | logger.debug('Pipe read failed: %s' % str(err)) |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
87 | |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
88 | return {'type': 'log_message', |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
89 | 'levelname': 'CRITICAL', |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
90 | 'name': 'borgend.instance.BorgInstance', |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
91 | 'msgid': 'Borgend.Exception', |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
92 | 'message': err} |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
93 | |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
94 | if line==b'': |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
95 | |
31 | 96 | logger.debug('Borg stderr pipe EOF?') |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
97 | |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
98 | return None |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
99 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
100 | try: |
6 | 101 | res=json.loads(line) |
102 | if 'type' not in res: | |
103 | res['type']='UNKNOWN' | |
104 | return res | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
105 | except: |
31 | 106 | logger.debug('JSON parse failed on: "%s"' % str(line)) |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
107 | |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
108 | errmsg=line |
7 | 109 | for line in iter(stream.readline, b''): |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
110 | errmsg=errmsg+line |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
111 | |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
112 | return {'type': 'log_message', |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
113 | 'levelname': 'ERROR', |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
114 | 'name': 'borgend.instance.BorgInstance', |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
115 | 'msgid': 'Borgend.JSONFail', |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
116 | 'message': str(errmsg)} |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
117 | |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
118 | def terminate(self): |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
119 | self.proc.terminate() |
0 | 120 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
121 | def wait(self): |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
122 | return self.proc.wait() is not None |
0 | 123 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
124 | def has_terminated(self): |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
125 | return self.proc.poll() is not None |
0 | 126 |