cead9267e8e01a419a999c05ca820b848f59ed94
[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.path import getmtime
4
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'
8
9 def load(src_path = default_src_path,
10          cache_path = default_cache_path,
11          force_refresh = False):
12     """
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.
17     """
18
19     # Namespace container for state variables, so that they can be updated by
20     # closures.
21     ns = struct()
22
23     if force_refresh:
24         do_refresh = True
25     else:
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
30
31         # We chose not to simply say
32         #
33         #   do_refresh = src_mtime >= cache_time
34         #
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.
43         #
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.
48
49     if not do_refresh:
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
58
59     if do_refresh:
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.
66         import yaml
67         try:    loader = yaml.CSafeLoader
68         except: loader = yaml.SafeLoader
69         try:
70             @with_lock_file(lock_file)
71             def refresh_cache():
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)
78         except IOError:
79             ns.cfg = with_closing(file(src_path)) (
80                     lambda f: yaml.load(f, loader))
81     return ns.cfg
82
83 dicts = load()
84 structs = dicts2struct(dicts)
85
86 # vim:et:sw=4:ts=4