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