8648c70ebdafd35235ba87e05a97f2d603ff3843
[invirt/packages/invirt-base.git] / files / usr / share / python-support / sipb-xen-base / invirt / config.py
1 import json
2 from invirt.common import *
3 from os import rename
4 from os.path import getmtime
5
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'
9
10 def load(src_path = default_src_path,
11          cache_path = default_cache_path,
12          force_refresh = False):
13     """
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.
18     """
19
20     # Namespace container for state variables, so that they can be updated by
21     # closures.
22     ns = struct()
23
24     if force_refresh:
25         do_refresh = True
26     else:
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
31
32         # We chose not to simply say
33         #
34         #   do_refresh = src_mtime >= cache_time
35         #
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.
44         #
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.
49
50     if not do_refresh:
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
59
60     if do_refresh:
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.
67         import yaml
68         try:    loader = yaml.CSafeLoader
69         except: loader = yaml.SafeLoader
70         try:
71             @with_lock_file(lock_file)
72             def refresh_cache():
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)
79         except IOError:
80             ns.cfg = with_closing(file(src_path)) (
81                     lambda f: yaml.load(f, loader))
82     return ns.cfg
83
84 dicts = load()
85 structs = dicts2struct(dicts)
86
87 # vim:et:sw=4:ts=4