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