Mon, 29 Jan 2018 10:00:31 +0000
Show log directory in --help
23 | 1 | #!/usr/local/bin/python3 |
0 | 2 | # |
89
51cc2e25af38
Added author information headers and content information to source files
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
3 | # Borgend by Tuomo Valkonen, 2018 |
0 | 4 | # |
89
51cc2e25af38
Added author information headers and content information to source files
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
5 | # This file implements basic configuration processing |
51cc2e25af38
Added author information headers and content information to source files
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
6 | # |
51cc2e25af38
Added author information headers and content information to source files
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
7 | |
0 | 8 | |
9 | import yaml | |
10 | import io | |
11 | import os | |
12 | import string | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
13 | import logging |
43
8f3ac19f11b6
Use platform package to detect whether to:
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
14 | import platform |
2 | 15 | from functools import reduce |
80
a409242121d5
Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
79
diff
changeset
|
16 | |
a409242121d5
Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
79
diff
changeset
|
17 | from . import branding |
a409242121d5
Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
79
diff
changeset
|
18 | from . import locations |
0 | 19 | |
86
2fe66644c50d
Can use logging.getLogger directly now after proper packageisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
80
diff
changeset
|
20 | logger=logging.getLogger(__name__) |
31 | 21 | |
2 | 22 | # |
23 | # Defaults | |
24 | # | |
25 | ||
26 | defaults={ | |
21 | 27 | # borg |
28 | # Default: backup every 6 hours (21600 seconds) | |
29 | 'backup_interval': 21600, | |
30 | # Default: retry every 15 minutes if unable to connect / unfinished backup | |
31 | 'retry_interval': 900, | |
32 | # Extract passphrases at startup or on demand? | |
30
3dd525652dc8
Added no_quit_menu_entry option
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
33 | 'extract_passphrases_at_startup': True, |
3dd525652dc8
Added no_quit_menu_entry option
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
34 | # Do not insert a quit menu entry (useful for installing on computers of |
3dd525652dc8
Added no_quit_menu_entry option
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
35 | # inexperienced users) |
3dd525652dc8
Added no_quit_menu_entry option
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
36 | 'no_quit_menu_entry': False, |
21 | 37 | # Borg settings |
38 | 'borg': { | |
39 | 'executable': 'borg', | |
40 | 'common_parameters': [], | |
41 | 'create_parameters': [], | |
42 | 'prune_parameters': [], | |
2 | 43 | } |
44 | } | |
45 | ||
33
91421eeb4426
Use rumps.application_support instead of xdg paths. Also separated branding into config.py
Tuomo Valkonen <tuomov@iki.fi>
parents:
31
diff
changeset
|
46 | # |
2 | 47 | # Type checking etc. |
48 | # | |
49 | ||
50 | def error(x): | |
51 | raise AssertionError(x) | |
52 | ||
74
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
53 | def check_field(cfg, field, descr, loc, default, check): |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
54 | if field in cfg: |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
55 | tmp=cfg[field] |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
56 | if not check(tmp): |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
57 | error("%s is of invalid type for %s" % (field, loc)) |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
58 | return tmp |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
59 | else: |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
60 | if default is not None: |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
61 | return default |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
62 | else: |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
63 | error("%s is not configured for %s" % (field, loc)) |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
64 | |
30
3dd525652dc8
Added no_quit_menu_entry option
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
65 | def check_bool(cfg, field, descr, loc, default=None): |
3dd525652dc8
Added no_quit_menu_entry option
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
66 | return check_field(cfg, field, descr, loc, default, |
3dd525652dc8
Added no_quit_menu_entry option
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
67 | lambda x: isinstance(x, bool)) |
3dd525652dc8
Added no_quit_menu_entry option
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
68 | |
2 | 69 | def check_string(cfg, field, descr, loc, default=None): |
70 | return check_field(cfg, field, descr, loc, default, | |
71 | lambda x: isinstance(x, str)) | |
72 | ||
73 | def check_dict(cfg, field, descr, loc, default=None): | |
74 | return check_field(cfg, field, descr, loc, default, | |
75 | lambda x: isinstance(x, dict)) | |
76 | ||
77 | def check_list(cfg, field, descr, loc, default=None): | |
78 | return check_field(cfg, field, descr, loc, default, | |
79 | lambda x: isinstance(x, list)) | |
80 | ||
81 | def is_list_of(x, chk): | |
82 | if x is None: | |
83 | return True | |
84 | elif isinstance(x, list): | |
85 | return reduce(lambda y, z: y and chk(z), x, True) | |
86 | else: | |
87 | return False | |
88 | ||
89 | def check_list_of_dicts(cfg, field, descr, loc, default=None): | |
90 | return check_field(cfg, field, descr, loc, default, | |
91 | lambda x: is_list_of(x, lambda z: isinstance(z, dict))) | |
92 | ||
93 | def check_list_of_strings(cfg, field, descr, loc, default=None): | |
94 | return check_field(cfg, field, descr, loc, default, | |
95 | lambda x: is_list_of(x, lambda z: isinstance(z, str))) | |
96 | ||
97 | def check_nonempty_list_of_strings(cfg, field, descr, loc): | |
98 | return check_list_of_strings(cfg, field, descr, loc) and cfg[field] | |
99 | ||
100 | ||
101 | def check_nonneg_int(cfg, field, descr, loc, default=None): | |
102 | return check_field(cfg, field, descr, loc, default, | |
103 | lambda x: isinstance(x, int) and x>=0) | |
104 | ||
105 | ||
106 | # | |
74
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
107 | # Borg command line parameter configuration helper routines and classes |
2 | 108 | # |
109 | ||
74
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
110 | class BorgParameters: |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
111 | def __init__(self, common, create, prune): |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
112 | self.common=common or [] |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
113 | self.create=create or [] |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
114 | self.prune=prune or [] |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
115 | |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
116 | def from_config(cfg, loc): |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
117 | common=check_list_of_dicts(cfg, 'common_parameters', |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
118 | 'Borg parameters', loc, default=[]) |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
119 | |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
120 | create=check_list_of_dicts(cfg, 'create_parameters', |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
121 | 'Create parameters', loc, default=[]) |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
122 | |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
123 | prune=check_list_of_dicts(cfg, 'prune_parameters', |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
124 | 'Prune parameters', loc, default=[]) |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
125 | |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
126 | return BorgParameters(common, create, prune) |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
127 | |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
128 | def __add__(self, other): |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
129 | common=self.common+other.common |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
130 | create=self.create+other.create |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
131 | prune=self.prune+other.prune |
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
132 | return BorgParameters(common, create, prune) |
2 | 133 | |
134 | # | |
135 | # Load config on module load | |
136 | # | |
0 | 137 | |
138 | def expand_env(cfg, env): | |
139 | if isinstance(cfg, dict): | |
140 | out={key: expand_env(val, env) for key, val in cfg.items()} | |
141 | elif isinstance(cfg, list): | |
142 | out=[expand_env(val, env) for val in cfg] | |
143 | elif isinstance(cfg, str): | |
144 | out=string.Template(cfg).substitute(os.environ) | |
145 | else: | |
2 | 146 | out=cfg |
147 | ||
0 | 148 | return out |
149 | ||
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
|
150 | if not (os.path.exists(locations.cfgfile) and os.path.isfile(locations.cfgfile)): |
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
|
151 | raise SystemExit("Configuration file required: %s" % locations.cfgfile) |
0 | 152 | |
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
|
153 | logger.info("Reading configuration %s missing" % locations.cfgfile) |
0 | 154 | |
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
|
155 | with io.open(locations.cfgfile, 'r') as file: |
0 | 156 | settings=expand_env(yaml.load(file), os.environ); |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
157 | |
2 | 158 | # |
159 | # Verify basic settings | |
160 | # | |
161 | ||
21 | 162 | def check_and_set(cfg, field, loc, defa, fn): |
163 | cfg[field]=fn(cfg, field, field, loc, defa[field]) | |
164 | ||
165 | check_and_set(settings, 'backup_interval', 'top-level', defaults, check_nonneg_int) | |
166 | check_and_set(settings, 'retry_interval', 'top-level', defaults, check_nonneg_int) | |
30
3dd525652dc8
Added no_quit_menu_entry option
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
167 | check_and_set(settings, 'extract_passphrases_at_startup', 'top-level', defaults, check_nonneg_int) |
3dd525652dc8
Added no_quit_menu_entry option
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
168 | check_and_set(settings, 'no_quit_menu_entry', 'top-level', defaults, check_bool) |
21 | 169 | check_and_set(settings, 'borg', 'top-level', defaults, check_dict) |
170 | # Check parameters within 'borg' | |
171 | if True: | |
172 | check_and_set(settings['borg'], 'executable', 'borg', | |
173 | defaults['borg'], check_string) | |
2 | 174 | |
74
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
175 | borg_parameters=BorgParameters.from_config(settings['borg'], "top-level") |
2 | 176 |