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