| [1197] | 1 | from __future__ import with_statement | 
|---|
 | 2 |  | 
|---|
| [784] | 3 | import json | 
|---|
| [778] | 4 | from invirt.common import * | 
|---|
| [1422] | 5 | import os | 
|---|
| [816] | 6 | from os import rename | 
|---|
| [781] | 7 | from os.path import getmtime | 
|---|
| [1197] | 8 | from contextlib import closing | 
|---|
| [1421] | 9 | import yaml | 
|---|
| [1423] | 10 | import re | 
|---|
| [726] | 11 |  | 
|---|
| [1421] | 12 | try:    loader = yaml.CSafeLoader | 
|---|
 | 13 | except: loader = yaml.SafeLoader | 
|---|
 | 14 |  | 
|---|
| [1422] | 15 | src_path    = '/etc/invirt/master.yaml' | 
|---|
 | 16 | src_dirpath = '/etc/invirt/conf.d' | 
|---|
 | 17 | cache_path  = '/var/lib/invirt/cache.json' | 
|---|
 | 18 | lock_path   = '/var/lib/invirt/cache.lock' | 
|---|
| [726] | 19 |  | 
|---|
| [1422] | 20 | def augment(d1, d2): | 
|---|
 | 21 |     """Splice dict-tree d2 into d1.  Return d1. | 
|---|
 | 22 |  | 
|---|
| [1933] | 23 |     d2 may be None for an empty dict-tree, because yaml.load produces that. | 
|---|
 | 24 |  | 
|---|
| [1422] | 25 |     Example: | 
|---|
 | 26 |     >>> d = {'a': {'b': 1}, 'c': 2} | 
|---|
 | 27 |     >>> augment(d, {'a': {'d': 3}}) | 
|---|
 | 28 |     {'a': {'b', 1, 'd': 3}, 'c': 2} | 
|---|
 | 29 |     >>> d | 
|---|
 | 30 |     {'a': {'b', 1, 'd': 3}, 'c': 2} | 
|---|
 | 31 |     """ | 
|---|
| [1933] | 32 |     if d2 is None: | 
|---|
 | 33 |         return d1 | 
|---|
| [1422] | 34 |     for k in d2: | 
|---|
 | 35 |         if k in d1 and isinstance(d1[k], dict): | 
|---|
 | 36 |             augment(d1[k], d2[k]) | 
|---|
 | 37 |         else: | 
|---|
 | 38 |             d1[k] = d2[k] | 
|---|
 | 39 |     return d1 | 
|---|
 | 40 |  | 
|---|
| [1423] | 41 | def run_parts_list(dirname): | 
|---|
 | 42 |     """Reimplements Debian's run-parts --list. | 
|---|
 | 43 |  | 
|---|
 | 44 |     One difference from run-parts's behavior: run-parts --list /foo/ | 
|---|
| [1926] | 45 |     will give output like /foo//bar, but run_parts_list('/foo/') gives | 
|---|
 | 46 |     /foo/bar in deference to Python conventions. | 
|---|
| [1423] | 47 |  | 
|---|
 | 48 |     Matches documented behavior of run-parts in debianutils v2.28.2, dated 2007. | 
|---|
 | 49 |     """ | 
|---|
 | 50 |     # From run-parts(8). | 
|---|
 | 51 |     lanana_re   = re.compile('^[a-z0-9]+$') | 
|---|
 | 52 |     lsb_re      = re.compile('^_?([a-z0-9_.]+-)+[a-z0-9]+$') | 
|---|
 | 53 |     deb_cron_re = re.compile('^[a-z0-9][a-z0-9-]*$') | 
|---|
 | 54 |     for name in os.listdir(dirname): | 
|---|
 | 55 |         if lanana_re.match(name) or lsb_re.match(name) or deb_cron_re.match(name): | 
|---|
 | 56 |             yield os.path.join(dirname, name) | 
|---|
 | 57 |  | 
|---|
| [1422] | 58 | def list_files(): | 
|---|
 | 59 |     yield src_path | 
|---|
| [1423] | 60 |     for name in run_parts_list(src_dirpath): | 
|---|
 | 61 |         yield name | 
|---|
| [1422] | 62 |  | 
|---|
| [1421] | 63 | def load_master(): | 
|---|
| [1422] | 64 |     config = dict() | 
|---|
 | 65 |     for filename in list_files(): | 
|---|
 | 66 |         with closing(file(filename)) as f: | 
|---|
 | 67 |             augment(config, yaml.load(f, loader)) | 
|---|
 | 68 |     return config | 
|---|
| [1421] | 69 |  | 
|---|
 | 70 | def get_src_mtime(): | 
|---|
| [1422] | 71 |     return max(max(getmtime(filename) for filename in list_files()), | 
|---|
 | 72 |                getmtime(src_dirpath)) | 
|---|
| [1421] | 73 |  | 
|---|
| [1420] | 74 | def load(force_refresh = False): | 
|---|
| [771] | 75 |     """ | 
|---|
 | 76 |     Try loading the configuration from the faster-to-load JSON cache at | 
|---|
 | 77 |     cache_path.  If it doesn't exist or is outdated, load the configuration | 
|---|
 | 78 |     instead from the original YAML file at src_path and regenerate the cache. | 
|---|
 | 79 |     I assume I have the permissions to write to the cache directory. | 
|---|
 | 80 |     """ | 
|---|
| [806] | 81 |  | 
|---|
| [807] | 82 |     # Namespace container for state variables, so that they can be updated by | 
|---|
 | 83 |     # closures. | 
|---|
| [793] | 84 |     ns = struct() | 
|---|
 | 85 |  | 
|---|
| [771] | 86 |     if force_refresh: | 
|---|
| [806] | 87 |         do_refresh = True | 
|---|
| [771] | 88 |     else: | 
|---|
| [1421] | 89 |         src_mtime = get_src_mtime() | 
|---|
| [807] | 90 |         try:            cache_mtime = getmtime(cache_path) | 
|---|
 | 91 |         except OSError: do_refresh  = True | 
|---|
 | 92 |         else:           do_refresh  = src_mtime + 1 >= cache_mtime | 
|---|
| [771] | 93 |  | 
|---|
| [807] | 94 |         # We chose not to simply say | 
|---|
 | 95 |         # | 
|---|
 | 96 |         #   do_refresh = src_mtime >= cache_time | 
|---|
 | 97 |         # | 
|---|
 | 98 |         # because between the getmtime(src_path) and the time the cache is | 
|---|
 | 99 |         # rewritten, the master configuration may have been updated, so future | 
|---|
 | 100 |         # checks here would find a cache with a newer mtime than the master | 
|---|
 | 101 |         # (and thus treat the cache as containing the latest version of the | 
|---|
 | 102 |         # master).  The +1 means that for at least a full second following the | 
|---|
 | 103 |         # update to the master, this function will refresh the cache, giving us | 
|---|
 | 104 |         # 1 second to write the cache.  Note that if it takes longer than 1 | 
|---|
 | 105 |         # second to write the cache, then this situation could still arise. | 
|---|
 | 106 |         # | 
|---|
 | 107 |         # The getmtime calls should logically be part of the same transaction | 
|---|
 | 108 |         # as the rest of this function (cache read + conditional cache | 
|---|
 | 109 |         # refresh), but to wrap everything in an flock would cause the | 
|---|
 | 110 |         # following cache read to be less streamlined. | 
|---|
 | 111 |  | 
|---|
| [806] | 112 |     if not do_refresh: | 
|---|
| [793] | 113 |         # Try reading from the cache first.  This must be transactionally | 
|---|
 | 114 |         # isolated from concurrent writes to prevent reading an incomplete | 
|---|
 | 115 |         # (changing) version of the data (but the transaction can share the | 
|---|
| [806] | 116 |         # lock with other concurrent reads).  This isolation is accomplished | 
|---|
 | 117 |         # using an atomic filesystem rename in the refreshing stage. | 
|---|
| [1197] | 118 |         try:  | 
|---|
 | 119 |             with closing(file(cache_path)) as f: | 
|---|
 | 120 |                 ns.cfg = json.read(f.read()) | 
|---|
| [806] | 121 |         except: do_refresh = True | 
|---|
| [778] | 122 |  | 
|---|
| [806] | 123 |     if do_refresh: | 
|---|
| [781] | 124 |         # Atomically reload the source and regenerate the cache.  The read and | 
|---|
 | 125 |         # write must be a single transaction, or a stale version may be | 
|---|
| [806] | 126 |         # written (if another read/write of a more recent configuration | 
|---|
 | 127 |         # is interleaved).  The final atomic rename is to keep this | 
|---|
 | 128 |         # transactionally isolated from the above cache read.  If we fail to | 
|---|
 | 129 |         # acquire the lock, just try to load the master configuration. | 
|---|
 | 130 |         try: | 
|---|
| [1197] | 131 |             with lock_file(lock_path): | 
|---|
| [1421] | 132 |                 ns.cfg = load_master() | 
|---|
| [1197] | 133 |                 try:  | 
|---|
 | 134 |                     with closing(file(cache_path + '.tmp', 'w')) as f: | 
|---|
 | 135 |                         f.write(json.write(ns.cfg)) | 
|---|
| [806] | 136 |                 except: pass # silent failure | 
|---|
| [816] | 137 |                 else: rename(cache_path + '.tmp', cache_path) | 
|---|
| [806] | 138 |         except IOError: | 
|---|
| [1421] | 139 |             ns.cfg = load_master() | 
|---|
| [793] | 140 |     return ns.cfg | 
|---|
| [771] | 141 |  | 
|---|
| [778] | 142 | dicts = load() | 
|---|
| [1934] | 143 | structs = dicts2struct(dicts, '') | 
|---|
| [778] | 144 |  | 
|---|
| [726] | 145 | # vim:et:sw=4:ts=4 | 
|---|