1 from __future__ import with_statement
4 from invirt.common import *
6 from os.path import getmtime
7 from contextlib import closing
9 default_src_path = '/etc/invirt/master.yaml'
10 default_cache_path = '/var/lib/invirt/cache.json'
11 lock_path = '/var/lib/invirt/cache.lock'
13 def load(src_path = default_src_path,
14 cache_path = default_cache_path,
15 force_refresh = False):
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.
23 # Namespace container for state variables, so that they can be updated by
30 src_mtime = getmtime(src_path)
31 try: cache_mtime = getmtime(cache_path)
32 except OSError: do_refresh = True
33 else: do_refresh = src_mtime + 1 >= cache_mtime
35 # We chose not to simply say
37 # do_refresh = src_mtime >= cache_time
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.
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.
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
57 # lock with other concurrent reads). This isolation is accomplished
58 # using an atomic filesystem rename in the refreshing stage.
60 with closing(file(cache_path)) as f:
61 ns.cfg = json.read(f.read())
62 except: do_refresh = True
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
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.
72 try: loader = yaml.CSafeLoader
73 except: loader = yaml.SafeLoader
75 with lock_file(lock_path):
76 with closing(file(src_path)) as f:
77 ns.cfg = yaml.load(f, loader)
79 with closing(file(cache_path + '.tmp', 'w')) as f:
80 f.write(json.write(ns.cfg))
81 except: pass # silent failure
82 else: rename(cache_path + '.tmp', cache_path)
84 with closing(file(src_path)) as f:
85 ns.cfg = yaml.load(f, loader)
89 structs = dicts2struct(dicts)