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