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.
This commit is contained in:
Augustin Trancart 2020-02-09 16:28:19 +01:00
parent ec36c8a579
commit 78d9301490
4 changed files with 6 additions and 3 deletions

View file

@ -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__(

View file

@ -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

View file

@ -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")
}

View file

@ -5,3 +5,4 @@ testpaths = test/
env =
MOULINETTE_LOCALES_DIR = {PWD}/locales
TESTS_RUN = True
MOULINETTE_RUN_DIR=.