diff --git a/tuned/controller.py b/tuned/controller.py index ef46cba..1626632 100644 --- a/tuned/controller.py +++ b/tuned/controller.py @@ -41,7 +41,8 @@ class Controller(exports.interfaces.ExportableInterface): def __init__(self, config_file, debug): super(self.__class__, self).__init__() exports.register_object(self) - self._daemon = daemon.Daemon(config_file) + self._config_file = config_file + self._daemon = daemon.Daemon() self._terminate = threading.Event() def run(self): @@ -89,8 +90,13 @@ class Controller(exports.interfaces.ExportableInterface): @exports.export("s", "b") def switch_profile(self, profile): - # TODO - return False + config_file = "/etc/tune-profiles/%s/tuned.conf" % (profile) + try: + self._daemon.config_file = config_file + except ValueError as e: + log.error("Unable to open profile's config file %s" % (config_file) ) + return False + return self.reload() @exports.export("", "s") def active_profile(self): diff --git a/tuned/daemon.py b/tuned/daemon.py index 3f8de1f..f87432d 100644 --- a/tuned/daemon.py +++ b/tuned/daemon.py @@ -15,27 +15,34 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # +import os import units import utils import logs import monitors import plugins import threading +import ConfigParser log = logs.get() DEFAULT_CONFIG_FILE = "/etc/tuned/tuned.conf" class Daemon(object): - def __init__(self, config_file = None): + def __init__(self): log.info("initializing Daemon") - if config_file is None: - config_file = DEFAULT_CONFIG_FILE - self._config_file = config_file + self._config_file = DEFAULT_CONFIG_FILE self._thread = None self._terminate = threading.Event() + def _load_plugins(self, manager): + cfg = ConfigParser.SafeConfigParser() + cfg.read(self._config_file) + + test_a = manager.create("test", "test", None) + test_b = manager.create("foo", "cpu", None) + def _thread_code(self): self._terminate.clear() @@ -44,8 +51,7 @@ class Daemon(object): monitors_repo = monitors.get_repository() plugins_repo = plugins.get_repository() - test_a = manager.create("test", "test", None) - test_b = manager.create("foo", "cpu", None) + self._load_plugins(manager) while not self._terminate.wait(10): log.debug("updating monitors") @@ -55,6 +61,18 @@ class Daemon(object): manager.delete_all() + @property + def config_file(self): + return self._config_file + + @config_file.setter + def config_file(self, value): + if not os.path.exists(value): + raise ValueError("Config file does not exist") + + self._config_file = value + # TODO: Maybe restart the daemon here? + def is_running(self): return self._thread is not None and self._thread.is_alive()