diff --git a/tuned/admin/admin.py b/tuned/admin/admin.py index 98f2702..ad4acd6 100644 --- a/tuned/admin/admin.py +++ b/tuned/admin/admin.py @@ -1,3 +1,7 @@ +import tuned.utils.commands +from tuned.profiles import Locator as profiles_locator +from exceptions import TunedAdminDBusException +import tuned.consts as consts import sys class Admin(object): @@ -7,37 +11,68 @@ class Admin(object): def _error(self, message): print >>sys.stderr, message - def list(self): - profiles = self._controller.profiles() + def list(self, dbus_warn = True): + try: + profile_names = self._controller.profiles() + except TunedAdminDBusException as e: + if dbus_warn: + print >> sys.stderr, e + profile_names = profiles_locator(consts.LOAD_DIRECTORIES).get_known_names() print "Available profiles:" - for profile in profiles: + for profile in profile_names: print "- %s" % profile - self.active() + self.active(False) - def active(self): - profile = self._controller.active_profile() - if profile is not None: - print "Current active profile: %s" % profile + def active(self, dbus_warn = True): + try: + profile_name = self._controller.active_profile() + except TunedAdminDBusException as e: + if dbus_warn: + print >> sys.stderr, e + profile_name = tuned.utils.commands.read_file(consts.ACTIVE_PROFILE_FILE, None) + if profile_name is not None and profile_name != "": + print "Current active profile: %s" % profile_name return True else: print "No current active profile." return False - def profile(self, profiles): + def profile(self, profiles, dbus_warn = True): + fallback = False profile_name = " ".join(profiles) - if not self._controller.switch_profile(profile_name): - self._error("Cannot switch the profile.") + if profile_name == "": return False + try: + ret = self._controller.switch_profile(profile_name) + except TunedAdminDBusException as e: + fallback = True + if dbus_warn: + print >> sys.stderr, e + if profile_name in profiles_locator(consts.LOAD_DIRECTORIES).get_known_names(): + ret = tuned.utils.commands.write_to_file(consts.ACTIVE_PROFILE_FILE, profile_name) + else: + ret = False + self._error("Requested profile '%s' doesn't exist." % profile_name) + if ret: + if fallback: + print "You need to (re)start the tuned daemon by hand for changes to apply." + else: + if not self._controller.is_running() and not self._controller.start(): + self._error("Cannot enable the tuning.") + ret = False + else: + self._error("Cannot switch the profile.") - if not self._controller.is_running(): - if not self._controller.start(): - self._error("Cannot enable the tuning.") - return False + return ret - return True - - def recommend_profile(self): - print self._controller.recommend_profile() + def recommend_profile(self, dbus_warn = True): + try: + profile = self._controller.recommend_profile() + except TunedAdminDBusException as e: + if dbus_warn: + print >> sys.stderr, e + profile = tuned.utils.commands.recommend_profile() + print profile def off(self): result = self._controller.off() diff --git a/tuned/admin/dbus_controller.py b/tuned/admin/dbus_controller.py index 808a159..3523bdd 100644 --- a/tuned/admin/dbus_controller.py +++ b/tuned/admin/dbus_controller.py @@ -1,7 +1,6 @@ import dbus import dbus.exceptions -import tuned.utils.commands -from exceptions import TunedAdminException +from exceptions import TunedAdminDBusException __all__ = ["DBusController"] @@ -21,13 +20,13 @@ class DBusController(object): try: self._init_proxy() except dbus.exceptions.DBusException: - raise TunedAdminException("Cannot talk to Tuned daemon via DBus.") + raise TunedAdminDBusException("Cannot talk to Tuned daemon via DBus.") try: method = self._proxy.get_dbus_method(method_name) return method(*args, **kwargs) except dbus.exceptions.DBusException as dbus_exception: - raise TunedAdminException("DBus call to Tuned daemon failed (%s)." % str(dbus_exception)) + raise TunedAdminDBusException("DBus call to Tuned daemon failed (%s)." % str(dbus_exception)) def is_running(self): return self._call("is_running") @@ -42,24 +41,15 @@ class DBusController(object): return self._call("profiles") def active_profile(self): - profile_name = self._call("active_profile") - if profile_name != "": - return profile_name - else: - return None + return self._call("active_profile") def switch_profile(self, new_profile): - if new_profile != "": - return self._call("switch_profile", new_profile) - else: + if new_profile == "": return False + return self._call("switch_profile", new_profile) def recommend_profile(self): - try: - profile = self._call("recommend_profile") - except TunedAdminException: - profile = tuned.utils.commands.recommend_profile() - return profile + return self._call("recommend_profile") def off(self): return self._call("disable") diff --git a/tuned/admin/exceptions.py b/tuned/admin/exceptions.py index 11d3a66..39b8d8a 100644 --- a/tuned/admin/exceptions.py +++ b/tuned/admin/exceptions.py @@ -2,3 +2,6 @@ import tuned.exceptions class TunedAdminException(tuned.exceptions.TunedException): pass + +class TunedAdminDBusException(tuned.exceptions.TunedException): + pass