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