Sun, 21 Jan 2018 11:59:53 +0000
Reorder
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 xdg | |
10 | import string | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
11 | import logging |
2 | 12 | from functools import reduce |
0 | 13 | |
2 | 14 | # |
15 | # Defaults | |
16 | # | |
17 | ||
18 | defaults={ | |
21 | 19 | # borg |
20 | # Default: backup every 6 hours (21600 seconds) | |
21 | 'backup_interval': 21600, | |
22 | # Default: retry every 15 minutes if unable to connect / unfinished backup | |
23 | 'retry_interval': 900, | |
24 | # Extract passphrases at startup or on demand? | |
25 | '__extract_passphrases_at_startup': True, | |
26 | # Borg settings | |
27 | 'borg': { | |
28 | 'executable': 'borg', | |
29 | 'common_parameters': [], | |
30 | 'create_parameters': [], | |
31 | 'prune_parameters': [], | |
2 | 32 | } |
33 | } | |
34 | ||
35 | ||
36 | # | |
37 | # Type checking etc. | |
38 | # | |
39 | ||
40 | def error(x): | |
41 | raise AssertionError(x) | |
42 | ||
43 | def check_string(cfg, field, descr, loc, default=None): | |
44 | return check_field(cfg, field, descr, loc, default, | |
45 | lambda x: isinstance(x, str)) | |
46 | ||
47 | def check_dict(cfg, field, descr, loc, default=None): | |
48 | return check_field(cfg, field, descr, loc, default, | |
49 | lambda x: isinstance(x, dict)) | |
50 | ||
51 | def check_list(cfg, field, descr, loc, default=None): | |
52 | return check_field(cfg, field, descr, loc, default, | |
53 | lambda x: isinstance(x, list)) | |
54 | ||
55 | def is_list_of(x, chk): | |
56 | if x is None: | |
57 | return True | |
58 | elif isinstance(x, list): | |
59 | return reduce(lambda y, z: y and chk(z), x, True) | |
60 | else: | |
61 | return False | |
62 | ||
63 | def check_list_of_dicts(cfg, field, descr, loc, default=None): | |
64 | return check_field(cfg, field, descr, loc, default, | |
65 | lambda x: is_list_of(x, lambda z: isinstance(z, dict))) | |
66 | ||
67 | def check_list_of_strings(cfg, field, descr, loc, default=None): | |
68 | return check_field(cfg, field, descr, loc, default, | |
69 | lambda x: is_list_of(x, lambda z: isinstance(z, str))) | |
70 | ||
71 | def check_nonempty_list_of_strings(cfg, field, descr, loc): | |
72 | return check_list_of_strings(cfg, field, descr, loc) and cfg[field] | |
73 | ||
74 | ||
75 | def check_nonneg_int(cfg, field, descr, loc, default=None): | |
76 | return check_field(cfg, field, descr, loc, default, | |
77 | lambda x: isinstance(x, int) and x>=0) | |
78 | ||
79 | def check_field(cfg, field, descr, loc, default, check): | |
80 | if field in cfg: | |
81 | tmp=cfg[field] | |
82 | if not check(tmp): | |
83 | error("%s is of invalid type for %s" % (field, loc)) | |
84 | return tmp | |
85 | else: | |
86 | if default is not None: | |
87 | return default | |
88 | else: | |
89 | error("%s is not configured for %s" % (field, loc)) | |
90 | ||
91 | # | |
92 | # Conversion of config into command line | |
93 | # | |
94 | ||
95 | def arglistify(args): | |
96 | flatten=lambda l: [item for sublist in l for item in sublist] | |
97 | if args is None: | |
98 | return [] | |
99 | else: | |
100 | return flatten([['--' + key, str(d[key])] for d in args for key in d]) | |
101 | ||
102 | # | |
103 | # Load config on module load | |
104 | # | |
0 | 105 | |
106 | def expand_env(cfg, env): | |
107 | if isinstance(cfg, dict): | |
108 | out={key: expand_env(val, env) for key, val in cfg.items()} | |
109 | elif isinstance(cfg, list): | |
110 | out=[expand_env(val, env) for val in cfg] | |
111 | elif isinstance(cfg, str): | |
112 | out=string.Template(cfg).substitute(os.environ) | |
113 | else: | |
2 | 114 | out=cfg |
115 | ||
0 | 116 | return out |
117 | ||
118 | cfgfile=os.path.join(xdg.XDG_CONFIG_HOME, "borgend", "config.yaml") | |
119 | ||
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
120 | logging.info("Reading configuration file %s" % cfgfile) |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
121 | |
0 | 122 | if not (os.path.exists(cfgfile) and os.path.isfile(cfgfile)): |
22
c3e95212e3f0
py2app standalone app generation
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
123 | raise SystemExit('Configuration file required: {cfgfile}') |
0 | 124 | |
125 | with io.open(cfgfile, 'r') as file: | |
126 | settings=expand_env(yaml.load(file), os.environ); | |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
127 | |
2 | 128 | # |
129 | # Verify basic settings | |
130 | # | |
131 | ||
21 | 132 | def check_and_set(cfg, field, loc, defa, fn): |
133 | cfg[field]=fn(cfg, field, field, loc, defa[field]) | |
134 | ||
135 | def check_parameters(cmd): | |
136 | check_and_set(settings['borg'], cmd+'_parameters', | |
137 | 'borg', defaults['borg'], | |
138 | check_list_of_dicts) | |
2 | 139 | |
21 | 140 | check_and_set(settings, 'backup_interval', 'top-level', defaults, check_nonneg_int) |
141 | check_and_set(settings, 'retry_interval', 'top-level', defaults, check_nonneg_int) | |
142 | check_and_set(settings, '__extract_passphrases_at_startup', 'top-level', defaults, check_nonneg_int) | |
143 | check_and_set(settings, 'borg', 'top-level', defaults, check_dict) | |
144 | # Check parameters within 'borg' | |
145 | if True: | |
146 | check_and_set(settings['borg'], 'executable', 'borg', | |
147 | defaults['borg'], check_string) | |
2 | 148 | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
149 | check_parameters('common') |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
150 | check_parameters('create') |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
151 | check_parameters('prune') |
2 | 152 | |
153 |