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