source: trunk/packages/sipb-xen-base/files/usr/share/python-support/sipb-xen-base/invirt/config.py @ 816

Last change on this file since 816 was 816, checked in by y_z, 16 years ago
  • use invirt.config to get hostnames
  • refactoring: extracted bcast() function into invirt.remote package
  • fixed os.rename import bug
  • using correct default paths in invirt-getconf
File size: 3.7 KB
RevLine 
[784]1import json
[778]2from invirt.common import *
[816]3from os import rename
[781]4from os.path import getmtime
[726]5
[771]6default_src_path   = '/etc/invirt/master.yaml'
[781]7default_cache_path = '/var/lib/invirt/cache.json'
[793]8lock_file          = '/var/lib/invirt/cache.lock'
[726]9
[771]10def 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    """
[806]19
[807]20    # Namespace container for state variables, so that they can be updated by
21    # closures.
[793]22    ns = struct()
23
[771]24    if force_refresh:
[806]25        do_refresh = True
[771]26    else:
27        src_mtime = getmtime(src_path)
[807]28        try:            cache_mtime = getmtime(cache_path)
29        except OSError: do_refresh  = True
30        else:           do_refresh  = src_mtime + 1 >= cache_mtime
[771]31
[807]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
[806]50    if not do_refresh:
[793]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
[806]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)) (
[796]57                lambda f: json.read(f.read()))
[806]58        except: do_refresh = True
[778]59
[806]60    if do_refresh:
[781]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
[806]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
[816]78                else: rename(cache_path + '.tmp', cache_path)
[806]79        except IOError:
[796]80            ns.cfg = with_closing(file(src_path)) (
[806]81                    lambda f: yaml.load(f, loader))
[793]82    return ns.cfg
[771]83
[778]84dicts = load()
85structs = dicts2struct(dicts)
86
[726]87# vim:et:sw=4:ts=4
Note: See TracBrowser for help on using the repository browser.