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