import invirt.common
-src_path = '/etc/invirt/master.yaml'
-src_dirpath = '/etc/invirt/conf.d'
-cache_path = '/var/lib/invirt/cache.json'
-lock_path = '/var/lib/invirt/cache.lock'
+SRC_PATH = '/etc/invirt/master.yaml'
+SRC_DIRPATH = '/etc/invirt/conf.d'
+CACHE_PATH = '/var/lib/invirt/cache.json'
+LOCK_PATH = '/var/lib/invirt/cache.lock'
def augment(d1, d2):
"""
yield os.path.join(dirname, name)
def list_files():
- yield src_path
- for name in run_parts_list(src_dirpath):
+ yield SRC_PATH
+ for name in run_parts_list(SRC_DIRPATH):
yield name
def load_master():
def get_src_mtime():
return max(max(os.path.getmtime(filename) for filename in list_files()),
- os.path.getmtime(src_dirpath))
+ os.path.getmtime(SRC_DIRPATH))
def load(force_refresh=False):
"""
Try loading the configuration from the faster-to-load JSON cache at
- cache_path. If it doesn't exist or is outdated, load the configuration
- instead from the original YAML file at src_path and regenerate the cache.
+ CACHE_PATH. If it doesn't exist or is outdated, load the configuration
+ instead from the original YAML file at SRC_PATH and regenerate the cache.
I assume I have the permissions to write to the cache directory.
"""
else:
src_mtime = get_src_mtime()
try:
- cache_mtime = os.path.getmtime(cache_path)
+ cache_mtime = os.path.getmtime(CACHE_PATH)
except OSError:
do_refresh = True
else:
#
# do_refresh = src_mtime >= cache_time
#
- # because between the getmtime(src_path) and the time the cache is
+ # because between the getmtime(SRC_PATH) and the time the cache is
# rewritten, the master configuration may have been updated, so future
# checks here would find a cache with a newer mtime than the master
# (and thus treat the cache as containing the latest version of the
# lock with other concurrent reads). This isolation is accomplished
# using an atomic filesystem rename in the refreshing stage.
try:
- with open(cache_path) as f:
+ with open(CACHE_PATH) as f:
ns.cfg = json.read(f.read())
except:
do_refresh = True
# transactionally isolated from the above cache read. If we fail to
# acquire the lock, just try to load the master configuration.
try:
- with invirt.common.open_locked(lock_path):
+ with invirt.common.open_locked(LOCK_PATH):
ns.cfg = load_master()
try:
- with open(cache_path + '.tmp', 'w') as f:
+ with open(CACHE_PATH + '.tmp', 'w') as f:
f.write(json.write(ns.cfg))
except:
pass # silent failure
else:
- os.rename(cache_path + '.tmp', cache_path)
+ os.rename(CACHE_PATH + '.tmp', CACHE_PATH)
except IOError:
ns.cfg = load_master()
return ns.cfg