From 78d930149075479849424e4d4eee9c4fdfab2d71 Mon Sep 17 00:00:00 2001 From: Augustin Trancart Date: Sun, 9 Feb 2020 16:28:19 +0100 Subject: [PATCH] Configure location of lock files Before this commit, moulinette was using lockfiles in /var/run. However this prevents it to run as non-root user. To allow this and to facilitate unit testing (typically run as regular user), this commit adds a MOULINETTE_RUN_DIR env variable. --- moulinette/actionsmap.py | 3 ++- moulinette/core.py | 4 ++-- moulinette/globals.py | 1 + pytest.ini | 1 + 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/moulinette/actionsmap.py b/moulinette/actionsmap.py index a8a3d271..24563ff7 100644 --- a/moulinette/actionsmap.py +++ b/moulinette/actionsmap.py @@ -419,6 +419,7 @@ class ActionsMap(object): moulinette_env = init_moulinette_env() DATA_DIR = moulinette_env["DATA_DIR"] CACHE_DIR = moulinette_env["CACHE_DIR"] + self.run_dir = moulinette_env["RUN_DIR"] if len(namespaces) == 0: namespaces = self.get_namespaces() @@ -545,7 +546,7 @@ class ActionsMap(object): full_action_name = "%s.%s.%s" % (namespace, category, action) # Lock the moulinette for the namespace - with MoulinetteLock(namespace, timeout): + with MoulinetteLock(namespace, timeout, lock_dir=self.run_dir): start = time() try: mod = __import__( diff --git a/moulinette/core.py b/moulinette/core.py index 5d47ae84..2e44db43 100644 --- a/moulinette/core.py +++ b/moulinette/core.py @@ -454,12 +454,12 @@ class MoulinetteLock(object): """ - def __init__(self, namespace, timeout=None, interval=0.5): + def __init__(self, namespace, timeout=None, interval=0.5, lock_dir="/var/run"): self.namespace = namespace self.timeout = timeout self.interval = interval - self._lockfile = "/var/run/moulinette_%s.lock" % namespace + self._lockfile = "{}/moulinette_{}.lock".format(lock_dir, namespace) self._stale_checked = False self._locked = False diff --git a/moulinette/globals.py b/moulinette/globals.py index 39f45d93..ab70adbb 100644 --- a/moulinette/globals.py +++ b/moulinette/globals.py @@ -11,4 +11,5 @@ def init_moulinette_env(): "MOULINETTE_LOCALES_DIR", "/usr/share/moulinette/locale" ), "CACHE_DIR": environ.get("MOULINETTE_CACHE_DIR", "/var/cache/moulinette"), + "RUN_DIR": environ.get("MOULINETTE_RUN_DIR", "/var/run") } diff --git a/pytest.ini b/pytest.ini index 55416dc0..c6ffe03e 100644 --- a/pytest.ini +++ b/pytest.ini @@ -5,3 +5,4 @@ testpaths = test/ env = MOULINETTE_LOCALES_DIR = {PWD}/locales TESTS_RUN = True + MOULINETTE_RUN_DIR=.