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
RevLine 
[1197]1from __future__ import with_statement
2
[784]3import json
[778]4from invirt.common import *
[816]5from os import rename
[781]6from os.path import getmtime
[1197]7from contextlib import closing
[726]8
[1420]9src_path   = '/etc/invirt/master.yaml'
10cache_path = '/var/lib/invirt/cache.json'
11lock_path  = '/var/lib/invirt/cache.lock'
[726]12
[1420]13def load(force_refresh = False):
[771]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    """
[806]20
[807]21    # Namespace container for state variables, so that they can be updated by
22    # closures.
[793]23    ns = struct()
24
[771]25    if force_refresh:
[806]26        do_refresh = True
[771]27    else:
28        src_mtime = getmtime(src_path)
[807]29        try:            cache_mtime = getmtime(cache_path)
30        except OSError: do_refresh  = True
31        else:           do_refresh  = src_mtime + 1 >= cache_mtime
[771]32
[807]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
[806]51    if not do_refresh:
[793]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
[806]55        # lock with other concurrent reads).  This isolation is accomplished
56        # using an atomic filesystem rename in the refreshing stage.
[1197]57        try: 
58            with closing(file(cache_path)) as f:
59                ns.cfg = json.read(f.read())
[806]60        except: do_refresh = True
[778]61
[806]62    if do_refresh:
[781]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
[806]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:
[1197]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))
[806]79                except: pass # silent failure
[816]80                else: rename(cache_path + '.tmp', cache_path)
[806]81        except IOError:
[1197]82            with closing(file(src_path)) as f:
83                ns.cfg = yaml.load(f, loader)
[793]84    return ns.cfg
[771]85
[778]86dicts = load()
87structs = dicts2struct(dicts)
88
[726]89# vim:et:sw=4:ts=4
Note: See TracBrowser for help on using the repository browser.