Module: check_mk
Branch: master
Commit: ca908cc2229ceef8584b255c8a1e8a7f9e3b1f2d
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=ca908cc2229ceeā¦
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Sat Aug 11 19:43:23 2018 +0200
Centralized site macro computation
Change-Id: I5d57c1d2430233fa510f8f014ac212d1d4c92abb
---
omd/packages/omd/omdlib/main.py | 36 +++++++++++++---------------------
tests/unit/omdlib/test_site_context.py | 7 +++++++
2 files changed, 21 insertions(+), 22 deletions(-)
diff --git a/omd/packages/omd/omdlib/main.py b/omd/packages/omd/omdlib/main.py
index 5a68cf2..821d24e 100644
--- a/omd/packages/omd/omdlib/main.py
+++ b/omd/packages/omd/omdlib/main.py
@@ -672,10 +672,7 @@ def file_owner_verify(path, user_id, group_id):
def create_skeleton_files(site, directory):
read_skel_permissions()
- replacements = {
- "###SITE###" : site.name,
- "###ROOT###" : site.dir,
- }
+ replacements = site.replacements
# Hack: exclude tmp if dir is '.'
exclude_tmp = directory == "."
skelroot = "/omd/versions/%s/skel" % omdlib.__version__
@@ -749,11 +746,7 @@ def try_chown(filename, user):
def instantiate_skel(path):
try:
t = file(path).read()
- replacements = {
- "###SITE###" : g_site.name,
- "###ROOT###" : g_site.dir,
- }
- return replace_tags(t, replacements)
+ return replace_tags(t, g_site.replacements)
except:
return "" # e.g. due to permission error
@@ -869,12 +862,8 @@ def patch_template_file(src, dst, old_site, new_site):
# Create patch from old instantiated skeleton file to new one
content = file(src).read()
for site in [ old_site, new_site ]:
- replacements = {
- "###SITE###" : site.name,
- "###ROOT###" : site.dir,
- }
filename = "%s.skel.%s" % (dst, site.name)
- file(filename, "w").write(replace_tags(content, replacements))
+ file(filename, "w").write(replace_tags(content, site.replacements))
try_chown(filename, new_site.name)
# If old and new skeleton file are identical, then do nothing
@@ -977,10 +966,7 @@ def patch_template_file(src, dst, old_site, new_site):
def merge_update_file(relpath, old_version, new_version):
fn = tty_bold + relpath + tty_normal
- replacements = {
- "###SITE###" : g_site.name,
- "###ROOT###" : g_site.dir,
- }
+ replacements = g_site.replacements
user_path = g_site.dir + "/" + relpath
permissions = os.stat(user_path).st_mode
@@ -1149,10 +1135,7 @@ def update_file(relpath, old_version, new_version, userdir,
old_perms):
old_skel = "/omd/versions/%s/skel" % old_version
new_skel = "/omd/versions/%s/skel" % new_version
- replacements = {
- "###SITE###" : g_site.name,
- "###ROOT###" : g_site.dir,
- }
+ replacements = g_site.replacements
old_path = old_skel + "/" + relpath
new_path = new_skel + "/" + relpath
@@ -4100,6 +4083,15 @@ class SiteContext(AbstractSiteContext):
return None
+ @property
+ def replacements(self):
+ """Dictionary of key/value for replacing macros in skel
files"""
+ return {
+ "###SITE###": self.name,
+ "###ROOT###": self.dir,
+ }
+
+
def load_config(self):
"""Load all variables from omd/sites.conf. These variables always
begin with
CONFIG_. The reason is that this file can be sources with the shell.
diff --git a/tests/unit/omdlib/test_site_context.py
b/tests/unit/omdlib/test_site_context.py
index 9e63446..0fa6658 100644
--- a/tests/unit/omdlib/test_site_context.py
+++ b/tests/unit/omdlib/test_site_context.py
@@ -22,6 +22,13 @@ def test_site_context_version(monkeypatch):
assert site.version == "2018.08.11.cee"
+def test_site_context_replacements(monkeypatch):
+ site = omdlib.main.SiteContext("dingeling")
+ assert site.replacements["###SITE###"] == "dingeling"
+ assert site.replacements["###ROOT###"] == "/omd/sites/dingeling"
+ assert len(site.replacements) == 2
+
+
def test_site_context_exists(monkeypatch):
monkeypatch.setattr(os.path, "exists", lambda p: p ==
"/omd/sites/dingeling")