From bd745ff87e5db591ef586d43346759b2693e31ef Mon Sep 17 00:00:00 2001 From: xichengc Date: Wed, 6 Aug 2014 11:26:25 -0700 Subject: [PATCH] Make Health Check work with new db and config Change-Id: Ic4c9913b45e09add559adcae6c441b55ef356731 --- compass/actions/health_check/base.py | 28 +++++++++++++++++++ compass/actions/health_check/check.py | 4 +-- compass/actions/health_check/check_dhcp.py | 7 ++--- compass/actions/health_check/check_dns.py | 7 ++--- .../health_check/check_os_installer.py | 7 ++--- .../health_check/check_package_installer.py | 3 +- compass/actions/health_check/check_tftp.py | 7 ++--- compass/actions/health_check/utils.py | 21 ++++++++++++++ 8 files changed, 64 insertions(+), 20 deletions(-) diff --git a/compass/actions/health_check/base.py b/compass/actions/health_check/base.py index a64fcd84..7829139d 100644 --- a/compass/actions/health_check/base.py +++ b/compass/actions/health_check/base.py @@ -14,6 +14,8 @@ """Base class for Compass Health Check.""" from compass.actions.health_check import utils as health_check_utils +from compass.db.api import database +from compass.db import models from compass.utils import setting_wrapper as setting @@ -25,6 +27,32 @@ class BaseCheck(object): self.code = 1 self.messages = [] self.dist, self.version, self.release = health_check_utils.get_dist() + self.os_installer = self._get_os_installer() + self.package_installer = self._get_package_installer() + + def _get_os_installer(self): + database.init() + os_installer = {} + with database.session() as session: + installer = session.query( + models.OSInstaller + ).first() + os_installer['name'] = health_check_utils.strip_name( + installer.name) + os_installer.update(installer.settings) + return os_installer + + def _get_package_installer(self): + database.init() + package_installer = {} + with database.session() as session: + installer = session.query( + models.PackageInstaller + ).first() + package_installer['name'] = health_check_utils.strip_name( + installer.name) + package_installer.update(installer.settings) + return package_installer def _set_status(self, code, message): """set status.""" diff --git a/compass/actions/health_check/check.py b/compass/actions/health_check/check.py index e50b83a5..c1adbc6b 100644 --- a/compass/actions/health_check/check.py +++ b/compass/actions/health_check/check.py @@ -36,12 +36,12 @@ class BootCheck(base.BaseCheck): status['celery'] = self._check_celery() status['dhcp'] = self._check_dhcp() status['dns'] = self._check_dns() -# status['hds'] = self._check_hds() + status['hds'] = self._check_hds() status['os_installer'] = self._check_os_installer() status['package_installer'] = self._check_package_installer() status['squid'] = self._check_squid() status['tftp'] = self._check_tftp() -# status['other'] = self._check_misc() + status['other'] = self._check_misc() return status diff --git a/compass/actions/health_check/check_dhcp.py b/compass/actions/health_check/check_dhcp.py index d9a3f875..cb3429fe 100644 --- a/compass/actions/health_check/check_dhcp.py +++ b/compass/actions/health_check/check_dhcp.py @@ -29,8 +29,7 @@ class DhcpCheck(base.BaseCheck): def run(self): """do health check.""" - installer = self.config.OS_INSTALLER - method_name = "self.check_" + installer + "_dhcp()" + method_name = "self.check_" + self.os_installer['name'] + "_dhcp()" return eval(method_name) def check_cobbler_dhcp(self): @@ -38,10 +37,10 @@ class DhcpCheck(base.BaseCheck): try: remote = xmlrpclib.Server( - self.config.COBBLER_INSTALLER_URL, + self.os_installer['url'], allow_none=True) remote.login( - *self.config.COBBLER_INSTALLER_TOKEN) + *self.os_installer['token']) except Exception: self._set_status( 0, diff --git a/compass/actions/health_check/check_dns.py b/compass/actions/health_check/check_dns.py index 98313acf..67ee970d 100644 --- a/compass/actions/health_check/check_dns.py +++ b/compass/actions/health_check/check_dns.py @@ -28,18 +28,17 @@ class DnsCheck(base.BaseCheck): def run(self): """do health check.""" - installer = self.config.OS_INSTALLER - method_name = "self.check_" + installer + "_dns()" + method_name = "self.check_" + self.os_installer['name'] + "_dns()" return eval(method_name) def check_cobbler_dns(self): """Checks if Cobbler has taken over DNS service.""" try: remote = xmlrpclib.Server( - self.config.COBBLER_INSTALLER_URL, + self.os_installer['url'], allow_none=True) remote.login( - *self.config.COBBLER_INSTALLER_TOKEN) + *self.os_installer['token']) except Exception: self._set_status(0, "[%s]Error: Cannot login to Cobbler " diff --git a/compass/actions/health_check/check_os_installer.py b/compass/actions/health_check/check_os_installer.py index b9fe1f41..7e4e25fb 100644 --- a/compass/actions/health_check/check_os_installer.py +++ b/compass/actions/health_check/check_os_installer.py @@ -26,18 +26,17 @@ class OsInstallerCheck(base.BaseCheck): def run(self): """do health check.""" - installer = self.config.OS_INSTALLER - method_name = 'self.' + installer + '_check()' + method_name = 'self.' + self.os_installer['name'] + '_check()' return eval(method_name) def cobbler_check(self): """Runs cobbler check from xmlrpc client.""" try: remote = xmlrpclib.Server( - self.config.COBBLER_INSTALLER_URL, + self.os_installer['url'], allow_none=True) token = remote.login( - *self.config.COBBLER_INSTALLER_TOKEN) + *self.os_installer['token']) except Exception: self.code = 0 self.messages.append( diff --git a/compass/actions/health_check/check_package_installer.py b/compass/actions/health_check/check_package_installer.py index dd47e1e6..04ec9b1e 100644 --- a/compass/actions/health_check/check_package_installer.py +++ b/compass/actions/health_check/check_package_installer.py @@ -28,8 +28,7 @@ class PackageInstallerCheck(base.BaseCheck): def run(self): """do health check.""" - installer = self.config.PACKAGE_INSTALLER - method_name = "self." + installer + "_check()" + method_name = "self." + self.package_installer['name'] + "_check()" return eval(method_name) def chef_check(self): diff --git a/compass/actions/health_check/check_tftp.py b/compass/actions/health_check/check_tftp.py index f254edfd..efbcbc8b 100644 --- a/compass/actions/health_check/check_tftp.py +++ b/compass/actions/health_check/check_tftp.py @@ -27,8 +27,7 @@ class TftpCheck(base.BaseCheck): def run(self): """do health check.""" - installer = self.config.OS_INSTALLER - method_name = "self.check_" + installer + "_tftp()" + method_name = "self.check_" + self.os_installer['name'] + "_tftp()" return eval(method_name) def check_cobbler_tftp(self): @@ -40,10 +39,10 @@ class TftpCheck(base.BaseCheck): try: remote = xmlrpclib.Server( - self.config.COBBLER_INSTALLER_URL, + self.os_installer['name'], allow_none=True) remote.login( - *self.config.COBBLER_INSTALLER_TOKEN) + *self.os_installer['token']) except Exception: self._set_status( 0, diff --git a/compass/actions/health_check/utils.py b/compass/actions/health_check/utils.py index 18cfa86a..369c5b61 100644 --- a/compass/actions/health_check/utils.py +++ b/compass/actions/health_check/utils.py @@ -16,6 +16,7 @@ import commands import os import platform +import re def validate_setting(module, setting, param): @@ -91,3 +92,23 @@ def check_chkconfig(service_name): break return chk_on + + +def strip_name(name): + """Reformats names.""" + if not any([s in name for s in "(,),-,_".split(',')]): + return name + + paren_regex = re.compile("(.*?)\s*\(") + dash_regex = re.compile("(.*?)\s*\-") + under_dash_regex = re.compile("(.*?)\s*\_") + + r1 = paren_regex.match(name) + r2 = dash_regex.match(name) + r3 = under_dash_regex.match(name) + shortest = 'AVeryLongStringForDefualt' + for r in [r1, r2, r3]: + if r and len(r.group(1)) < len(shortest): + shortest = r.group(1) + + return shortest