1 | import json |
---|
2 | from invirt.common import * |
---|
3 | from os.path import getmtime |
---|
4 | |
---|
5 | default_src_path = '/etc/invirt/master.yaml' |
---|
6 | default_cache_path = '/var/lib/invirt/cache.json' |
---|
7 | lock_file = '/var/lib/invirt/cache.lock' |
---|
8 | |
---|
9 | def load(src_path = default_src_path, |
---|
10 | cache_path = default_cache_path, |
---|
11 | force_refresh = False): |
---|
12 | """ |
---|
13 | Try loading the configuration from the faster-to-load JSON cache at |
---|
14 | cache_path. If it doesn't exist or is outdated, load the configuration |
---|
15 | instead from the original YAML file at src_path and regenerate the cache. |
---|
16 | I assume I have the permissions to write to the cache directory. |
---|
17 | """ |
---|
18 | # Namespace container for various state variables, so that they can be |
---|
19 | # updated by closures. |
---|
20 | ns = struct() |
---|
21 | |
---|
22 | if force_refresh: |
---|
23 | ns.do_refresh = True |
---|
24 | else: |
---|
25 | src_mtime = getmtime(src_path) |
---|
26 | try: cache_mtime = getmtime(cache_path) |
---|
27 | except OSError: ns.do_refresh = True |
---|
28 | else: ns.do_refresh = src_mtime > cache_mtime |
---|
29 | |
---|
30 | if not ns.do_refresh: |
---|
31 | # Try reading from the cache first. This must be transactionally |
---|
32 | # isolated from concurrent writes to prevent reading an incomplete |
---|
33 | # (changing) version of the data (but the transaction can share the |
---|
34 | # lock with other concurrent reads). |
---|
35 | @with_lock_file(lock_file, False) |
---|
36 | def read_cache(): |
---|
37 | try: ns.cfg = with_closing(file(cache_path))(lambda f: json.read(f.read())) |
---|
38 | except: ns.do_refresh = True |
---|
39 | |
---|
40 | if ns.do_refresh: |
---|
41 | # Atomically reload the source and regenerate the cache. The read and |
---|
42 | # write must be a single transaction, or a stale version may be |
---|
43 | # written. |
---|
44 | @with_lock_file(lock_file) |
---|
45 | def refresh_cache(): |
---|
46 | import yaml |
---|
47 | try: default_loader = yaml.CSafeLoader |
---|
48 | except: default_loader = yaml.SafeLoader |
---|
49 | ns.cfg = with_closing(file(src_path))(lambda f: yaml.load(f, default_loader)) |
---|
50 | try: with_closing(file(cache_path, 'w'))(lambda f: f.write(json.write(ns.cfg))) |
---|
51 | except: pass # silent failure |
---|
52 | return ns.cfg |
---|
53 | |
---|
54 | dicts = load() |
---|
55 | structs = dicts2struct(dicts) |
---|
56 | |
---|
57 | # vim:et:sw=4:ts=4 |
---|