2 from invirt.common import *
3 from os.path import getmtime
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'
9 def load(src_path = default_src_path,
10 cache_path = default_cache_path,
11 force_refresh = False):
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.
19 # Namespace container for state variables, so that they can be updated by
26 src_mtime = getmtime(src_path)
27 try: cache_mtime = getmtime(cache_path)
28 except OSError: do_refresh = True
29 else: do_refresh = src_mtime + 1 >= cache_mtime
31 # We chose not to simply say
33 # do_refresh = src_mtime >= cache_time
35 # because between the getmtime(src_path) and the time the cache is
36 # rewritten, the master configuration may have been updated, so future
37 # checks here would find a cache with a newer mtime than the master
38 # (and thus treat the cache as containing the latest version of the
39 # master). The +1 means that for at least a full second following the
40 # update to the master, this function will refresh the cache, giving us
41 # 1 second to write the cache. Note that if it takes longer than 1
42 # second to write the cache, then this situation could still arise.
44 # The getmtime calls should logically be part of the same transaction
45 # as the rest of this function (cache read + conditional cache
46 # refresh), but to wrap everything in an flock would cause the
47 # following cache read to be less streamlined.
50 # Try reading from the cache first. This must be transactionally
51 # isolated from concurrent writes to prevent reading an incomplete
52 # (changing) version of the data (but the transaction can share the
53 # lock with other concurrent reads). This isolation is accomplished
54 # using an atomic filesystem rename in the refreshing stage.
55 try: ns.cfg = with_closing(file(cache_path)) (
56 lambda f: json.read(f.read()))
57 except: do_refresh = True
60 # Atomically reload the source and regenerate the cache. The read and
61 # write must be a single transaction, or a stale version may be
62 # written (if another read/write of a more recent configuration
63 # is interleaved). The final atomic rename is to keep this
64 # transactionally isolated from the above cache read. If we fail to
65 # acquire the lock, just try to load the master configuration.
67 try: loader = yaml.CSafeLoader
68 except: loader = yaml.SafeLoader
70 @with_lock_file(lock_file)
72 ns.cfg = with_closing(file(src_path)) (
73 lambda f: yaml.load(f, loader))
74 try: with_closing(file(cache_path + '.tmp', 'w')) (
75 lambda f: f.write(json.write(ns.cfg)))
76 except: pass # silent failure
77 else: os.rename(cache_path + '.tmp', cache_path)
79 ns.cfg = with_closing(file(src_path)) (
80 lambda f: yaml.load(f, loader))
84 structs = dicts2struct(dicts)