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