Sun, 28 Jan 2018 11:54:46 +0000
Better package-like organisation
0 | 1 | # |
2 | # Borgend borg launcher / processor | |
3 | # | |
4 | ||
80
a409242121d5
Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
79
diff
changeset
|
5 | import os |
0 | 6 | import json |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
7 | import logging |
2 | 8 | from subprocess import Popen, PIPE |
0 | 9 | |
80
a409242121d5
Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
79
diff
changeset
|
10 | from . import loggers |
a409242121d5
Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
79
diff
changeset
|
11 | from .config import settings |
a409242121d5
Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
79
diff
changeset
|
12 | |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
13 | logger=loggers.get(__name__) |
31 | 14 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
15 | necessary_opts=['--log-json', '--progress'] |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
16 | |
7 | 17 | necessary_opts_for={ |
18 | 'create': ['--json'], | |
19 | 'info': ['--json'], | |
20 | 'list': ['--json'], | |
21 | } | |
22 | ||
74
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
23 | # Conversion of config into command line |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
24 | def arglistify(args): |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
25 | flatten=lambda l: [item for sublist in l for item in sublist] |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
26 | if args is None: |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
27 | return [] |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
28 | else: |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
29 | return flatten([['--' + key, str(d[key])] for d in args for key in d]) |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
30 | |
0 | 31 | class BorgInstance: |
74
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
32 | def __init__(self, operation, archive_or_repository, |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
33 | common_params, op_params, paths): |
0 | 34 | self.operation=operation; |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
35 | self.archive_or_repository=archive_or_repository; |
74
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
36 | self.common_params=common_params |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
37 | self.op_params=op_params |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
38 | self.paths=paths |
0 | 39 | |
40 | def construct_cmdline(self): | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
41 | cmd=([settings['borg']['executable']]+necessary_opts+ |
74
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
42 | arglistify(self.common_params)+ |
2 | 43 | [self.operation]) |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
44 | |
7 | 45 | if self.operation in necessary_opts_for: |
46 | cmd=cmd+necessary_opts_for[self.operation] | |
47 | ||
74
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
48 | return (cmd+arglistify(self.op_params) |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
49 | +[self.archive_or_repository]+self.paths) |
0 | 50 | |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
51 | def launch(self, passphrase=None): |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
52 | cmd=self.construct_cmdline() |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
53 | |
31 | 54 | logger.info('Launching ' + str(cmd)) |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
55 | |
32 | 56 | # Set passphrase if not, or set to empty if not known, so borg |
57 | # won't hang waiting for it, which seems to happen even if we | |
58 | # close stdin. | |
29 | 59 | env=os.environ.copy() |
32 | 60 | env['BORG_PASSPHRASE']=passphrase or '' |
20
fdfbe5d7b677
Keychain support and random fixes
Tuomo Valkonen <tuomov@iki.fi>
parents:
12
diff
changeset
|
61 | |
24
94d58d514d69
Workaround to PYTHONPATH and PYTHONHOME being messed up by py2app.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
62 | # 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
|
63 | # 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
|
64 | # 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
|
65 | # PYTHONPATH or PYTHONHOME set to certain values. |
29 | 66 | if '_PY2APP_LAUNCHED_' in env: |
67 | val=env['_PY2APP_LAUNCHED_'] | |
68 | if val=='1': | |
69 | del env['PYTHONPATH'] | |
70 | del env['PYTHONHOME'] | |
24
94d58d514d69
Workaround to PYTHONPATH and PYTHONHOME being messed up by py2app.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
71 | |
20
fdfbe5d7b677
Keychain support and random fixes
Tuomo Valkonen <tuomov@iki.fi>
parents:
12
diff
changeset
|
72 | self.proc=Popen(cmd, env=env, stdout=PIPE, stderr=PIPE, stdin=PIPE) |
12 | 73 | |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
74 | # We don't do passphrase input etc. |
12 | 75 | self.proc.stdin.close() |
0 | 76 | |
7 | 77 | def read_result(self): |
78 | stream=self.proc.stdout | |
79 | line=stream.read(-1) | |
80 | if line==b'': | |
31 | 81 | logger.debug('Borg stdout pipe EOF?') |
7 | 82 | return None |
83 | ||
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
84 | try: |
7 | 85 | return json.loads(line) |
32 | 86 | except Exception as err: |
31 | 87 | logger.warning('JSON parse failed on: "%s"' % line) |
7 | 88 | return None |
89 | ||
90 | def read_log(self): | |
91 | stream=self.proc.stderr | |
92 | try: | |
93 | line=stream.readline() | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
94 | except err: |
31 | 95 | 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
|
96 | |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
97 | return {'type': 'log_message', |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
98 | 'levelname': 'CRITICAL', |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
99 | 'name': 'borgend.instance.BorgInstance', |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
100 | 'msgid': 'Borgend.Exception', |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
101 | 'message': err} |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
102 | |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
103 | if line==b'': |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
104 | |
31 | 105 | logger.debug('Borg stderr pipe EOF?') |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
106 | |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
107 | return None |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
108 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
109 | try: |
6 | 110 | res=json.loads(line) |
111 | if 'type' not in res: | |
112 | res['type']='UNKNOWN' | |
113 | return res | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
114 | except: |
31 | 115 | 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
|
116 | |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
117 | errmsg=line |
7 | 118 | 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
|
119 | errmsg=errmsg+line |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
120 | |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
121 | return {'type': 'log_message', |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
122 | 'levelname': 'ERROR', |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
123 | 'name': 'borgend.instance.BorgInstance', |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
124 | 'msgid': 'Borgend.JSONFail', |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
125 | 'message': str(errmsg)} |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
126 | |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
127 | def terminate(self): |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
128 | self.proc.terminate() |
0 | 129 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
130 | def wait(self): |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
131 | return self.proc.wait() is not None |
0 | 132 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
133 | def has_terminated(self): |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
134 | return self.proc.poll() is not None |
0 | 135 |