source: trunk/packages/invirt-base/python/invirt/config.py @ 1420

Last change on this file since 1420 was 1420, checked in by price, 16 years ago

eliminate choice of source and cache files in invirt config

File size: 3.6 KB
Line 
1from __future__ import with_statement
2
3import json
4from invirt.common import *
5from os import rename
6from os.path import getmtime
7from contextlib import closing
8
9src_path   = '/etc/invirt/master.yaml'
10cache_path = '/var/lib/invirt/cache.json'
11lock_path  = '/var/lib/invirt/cache.lock'
12
13def 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
86dicts = load()
87structs = dicts2struct(dicts)
88
89# vim:et:sw=4:ts=4
Note: See TracBrowser for help on using the repository browser.