From 781395be2cdd943897aa5295e8fa8cd5fef1520c Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Mon, 30 Jan 2012 10:15:30 +0100 Subject: [PATCH] Execute script if defined --- included.cfg | 1 + test.cfg | 1 + tuned/profile.py | 22 +++++++++++++++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/included.cfg b/included.cfg index 12a71a1..cdd22a6 100644 --- a/included.cfg +++ b/included.cfg @@ -1,4 +1,5 @@ [main] +script=included_script.sh [my_included_plugin] type=test diff --git a/test.cfg b/test.cfg index 4597f1c..4953369 100644 --- a/test.cfg +++ b/test.cfg @@ -1,5 +1,6 @@ [main] include=included.cfg +script=script.sh [sysctl] # ktune sysctl settings for EL 5 servers diff --git a/tuned/profile.py b/tuned/profile.py index dafb6b8..2b37cbe 100644 --- a/tuned/profile.py +++ b/tuned/profile.py @@ -33,6 +33,7 @@ class Profile(object): self._config_file = config_file self._sysctl = {} self._sysctl_original = {} + self._scripts = [] def _load_sysctl(self, cfg): if cfg.has_section("sysctl"): @@ -59,11 +60,24 @@ class Profile(object): self._sysctl_original[k] = v self._exec_sysctl(key + "=" + value, True) + return True def _revert_sysctl(self): for key, value in self._sysctl_original.iteritems(): self._exec_sysctl(key + "=" + value, True) + def _call_scripts(self, arg = "start"): + for script in self._scripts: + try: + proc = Popen([script, arg], stdout=PIPE, stderr=PIPE) + out, err = proc.communicate() + + if proc.returncode: + log.error("script %s error: %s" % (script, err[:-1])) + except OSError as e: + log.error("Script %s error: %s" % (script, e)) + return True + def _load_config(self, manager, config): if not os.path.exists(config): log.error("Config file %s does not exist" % (config)) @@ -75,6 +89,11 @@ class Profile(object): if cfg.has_option("main", "include"): self._load_config(manager, cfg.get("main", "include")) + if cfg.has_option("main", "script"): + script = os.path.abspath(cfg.get("main", "script")) + if not script in self._scripts: + self._scripts.append(script) + self._load_sysctl(cfg) for section in cfg.sections(): @@ -94,7 +113,8 @@ class Profile(object): return True def load(self): - return self._load_config(self._manager, self._config_file) and self._apply_sysctl() + return self._load_config(self._manager, self._config_file) and self._apply_sysctl() and self._call_scripts() def cleanup(self): self._revert_sysctl() + self._call_scripts("stop")