[1197] | 1 | from __future__ import with_statement |
---|
| 2 | |
---|
[784] | 3 | import json |
---|
[778] | 4 | from invirt.common import * |
---|
[816] | 5 | from os import rename |
---|
[781] | 6 | from os.path import getmtime |
---|
[1197] | 7 | from contextlib import closing |
---|
[726] | 8 | |
---|
[771] | 9 | default_src_path = '/etc/invirt/master.yaml' |
---|
[781] | 10 | default_cache_path = '/var/lib/invirt/cache.json' |
---|
[1197] | 11 | lock_path = '/var/lib/invirt/cache.lock' |
---|
[726] | 12 | |
---|
[771] | 13 | def load(src_path = default_src_path, |
---|
| 14 | cache_path = default_cache_path, |
---|
| 15 | force_refresh = False): |
---|
| 16 | """ |
---|
| 17 | Try loading the configuration from the faster-to-load JSON cache at |
---|
| 18 | cache_path. If it doesn't exist or is outdated, load the configuration |
---|
| 19 | instead from the original YAML file at src_path and regenerate the cache. |
---|
| 20 | I assume I have the permissions to write to the cache directory. |
---|
| 21 | """ |
---|
[806] | 22 | |
---|
[807] | 23 | # Namespace container for state variables, so that they can be updated by |
---|
| 24 | # closures. |
---|
[793] | 25 | ns = struct() |
---|
| 26 | |
---|
[771] | 27 | if force_refresh: |
---|
[806] | 28 | do_refresh = True |
---|
[771] | 29 | else: |
---|
| 30 | src_mtime = getmtime(src_path) |
---|
[807] | 31 | try: cache_mtime = getmtime(cache_path) |
---|
| 32 | except OSError: do_refresh = True |
---|
| 33 | else: do_refresh = src_mtime + 1 >= cache_mtime |
---|
[771] | 34 | |
---|
[807] | 35 | # We chose not to simply say |
---|
| 36 | # |
---|
| 37 | # do_refresh = src_mtime >= cache_time |
---|
| 38 | # |
---|
| 39 | # because between the getmtime(src_path) and the time the cache is |
---|
| 40 | # rewritten, the master configuration may have been updated, so future |
---|
| 41 | # checks here would find a cache with a newer mtime than the master |
---|
| 42 | # (and thus treat the cache as containing the latest version of the |
---|
| 43 | # master). The +1 means that for at least a full second following the |
---|
| 44 | # update to the master, this function will refresh the cache, giving us |
---|
| 45 | # 1 second to write the cache. Note that if it takes longer than 1 |
---|
| 46 | # second to write the cache, then this situation could still arise. |
---|
| 47 | # |
---|
| 48 | # The getmtime calls should logically be part of the same transaction |
---|
| 49 | # as the rest of this function (cache read + conditional cache |
---|
| 50 | # refresh), but to wrap everything in an flock would cause the |
---|
| 51 | # following cache read to be less streamlined. |
---|
| 52 | |
---|
[806] | 53 | if not do_refresh: |
---|
[793] | 54 | # Try reading from the cache first. This must be transactionally |
---|
| 55 | # isolated from concurrent writes to prevent reading an incomplete |
---|
| 56 | # (changing) version of the data (but the transaction can share the |
---|
[806] | 57 | # lock with other concurrent reads). This isolation is accomplished |
---|
| 58 | # using an atomic filesystem rename in the refreshing stage. |
---|
[1197] | 59 | try: |
---|
| 60 | with closing(file(cache_path)) as f: |
---|
| 61 | ns.cfg = json.read(f.read()) |
---|
[806] | 62 | except: do_refresh = True |
---|
[778] | 63 | |
---|
[806] | 64 | if do_refresh: |
---|
[781] | 65 | # Atomically reload the source and regenerate the cache. The read and |
---|
| 66 | # write must be a single transaction, or a stale version may be |
---|
[806] | 67 | # written (if another read/write of a more recent configuration |
---|
| 68 | # is interleaved). The final atomic rename is to keep this |
---|
| 69 | # transactionally isolated from the above cache read. If we fail to |
---|
| 70 | # acquire the lock, just try to load the master configuration. |
---|
| 71 | import yaml |
---|
| 72 | try: loader = yaml.CSafeLoader |
---|
| 73 | except: loader = yaml.SafeLoader |
---|
| 74 | try: |
---|
[1197] | 75 | with lock_file(lock_path): |
---|
| 76 | with closing(file(src_path)) as f: |
---|
| 77 | ns.cfg = yaml.load(f, loader) |
---|
| 78 | try: |
---|
| 79 | with closing(file(cache_path + '.tmp', 'w')) as f: |
---|
| 80 | f.write(json.write(ns.cfg)) |
---|
[806] | 81 | except: pass # silent failure |
---|
[816] | 82 | else: rename(cache_path + '.tmp', cache_path) |
---|
[806] | 83 | except IOError: |
---|
[1197] | 84 | with closing(file(src_path)) as f: |
---|
| 85 | ns.cfg = yaml.load(f, loader) |
---|
[793] | 86 | return ns.cfg |
---|
[771] | 87 | |
---|
[778] | 88 | dicts = load() |
---|
| 89 | structs = dicts2struct(dicts) |
---|
| 90 | |
---|
[726] | 91 | # vim:et:sw=4:ts=4 |
---|