[784] | 1 | import json |
---|
[778] | 2 | from invirt.common import * |
---|
[781] | 3 | from os.path import getmtime |
---|
[726] | 4 | |
---|
[771] | 5 | default_src_path = '/etc/invirt/master.yaml' |
---|
[781] | 6 | default_cache_path = '/var/lib/invirt/cache.json' |
---|
[793] | 7 | lock_file = '/var/lib/invirt/cache.lock' |
---|
[726] | 8 | |
---|
[771] | 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 | """ |
---|
[806] | 18 | |
---|
[793] | 19 | # Namespace container for various state variables, so that they can be |
---|
| 20 | # updated by closures. |
---|
| 21 | ns = struct() |
---|
| 22 | |
---|
[771] | 23 | if force_refresh: |
---|
[806] | 24 | do_refresh = True |
---|
[771] | 25 | else: |
---|
| 26 | src_mtime = getmtime(src_path) |
---|
[793] | 27 | try: cache_mtime = getmtime(cache_path) |
---|
[806] | 28 | except OSError: do_refresh = True |
---|
| 29 | else: do_refresh = src_mtime > cache_mtime |
---|
[771] | 30 | |
---|
[806] | 31 | if not do_refresh: |
---|
[793] | 32 | # Try reading from the cache first. This must be transactionally |
---|
| 33 | # isolated from concurrent writes to prevent reading an incomplete |
---|
| 34 | # (changing) version of the data (but the transaction can share the |
---|
[806] | 35 | # lock with other concurrent reads). This isolation is accomplished |
---|
| 36 | # using an atomic filesystem rename in the refreshing stage. |
---|
| 37 | try: ns.cfg = with_closing(file(cache_path)) ( |
---|
[796] | 38 | lambda f: json.read(f.read())) |
---|
[806] | 39 | except: do_refresh = True |
---|
[778] | 40 | |
---|
[806] | 41 | if do_refresh: |
---|
[781] | 42 | # Atomically reload the source and regenerate the cache. The read and |
---|
| 43 | # write must be a single transaction, or a stale version may be |
---|
[806] | 44 | # written (if another read/write of a more recent configuration |
---|
| 45 | # is interleaved). The final atomic rename is to keep this |
---|
| 46 | # transactionally isolated from the above cache read. If we fail to |
---|
| 47 | # acquire the lock, just try to load the master configuration. |
---|
| 48 | import yaml |
---|
| 49 | try: loader = yaml.CSafeLoader |
---|
| 50 | except: loader = yaml.SafeLoader |
---|
| 51 | try: |
---|
| 52 | @with_lock_file(lock_file) |
---|
| 53 | def refresh_cache(): |
---|
| 54 | ns.cfg = with_closing(file(src_path)) ( |
---|
| 55 | lambda f: yaml.load(f, loader)) |
---|
| 56 | try: with_closing(file(cache_path + '.tmp', 'w')) ( |
---|
| 57 | lambda f: f.write(json.write(ns.cfg))) |
---|
| 58 | except: pass # silent failure |
---|
| 59 | else: os.rename(cache_path + '.tmp', cache_path) |
---|
| 60 | except IOError: |
---|
[796] | 61 | ns.cfg = with_closing(file(src_path)) ( |
---|
[806] | 62 | lambda f: yaml.load(f, loader)) |
---|
[793] | 63 | return ns.cfg |
---|
[771] | 64 | |
---|
[778] | 65 | dicts = load() |
---|
| 66 | structs = dicts2struct(dicts) |
---|
| 67 | |
---|
[726] | 68 | # vim:et:sw=4:ts=4 |
---|