diff --git a/libra/tests/mock_objects.py b/libra/tests/mock_objects.py index 16588ec6..25cb4ffb 100644 --- a/libra/tests/mock_objects.py +++ b/libra/tests/mock_objects.py @@ -25,6 +25,12 @@ class FakeOSServices(ServicesBase): def remove_configs(self): pass + def sudo_rm(self, file): + pass + + def syslog_restart(self): + pass + class FakeFaultingOSServices(ServicesBase): def service_stop(self): diff --git a/libra/worker/drivers/haproxy/driver.py b/libra/worker/drivers/haproxy/driver.py index 9b7a7d1d..75a1ff1d 100644 --- a/libra/worker/drivers/haproxy/driver.py +++ b/libra/worker/drivers/haproxy/driver.py @@ -27,6 +27,7 @@ from libra.worker.drivers.haproxy.services_base import ServicesBase class HAProxyDriver(LoadBalancerDriver): def __init__(self, ossvc, user, group): + self.haproxy_log = '/mnt/log/haproxy.log' self.user = user self.group = group ossvc_driver = importutils.import_class(ossvc) @@ -124,14 +125,12 @@ class HAProxyDriver(LoadBalancerDriver): proto = proto.lower() - reallog = '/mnt/log/haproxy.log' - - if not os.path.exists(reallog): + if not os.path.exists(self.haproxy_log): raise Exception('No HAProxy logs found') # We need a copy we can read reallog_copy = '/tmp/haproxy.log' - self.ossvc.sudo_copy(reallog, reallog_copy) + self.ossvc.sudo_copy(self.haproxy_log, reallog_copy) self.ossvc.sudo_chown(reallog_copy, self.user, self.group) # Extract contents from the log based on protocol. This is @@ -257,6 +256,10 @@ class HAProxyDriver(LoadBalancerDriver): def delete(self): self.ossvc.service_stop() self.ossvc.remove_configs() + self.ossvc.sudo_rm(self.haproxy_log) + # Since haproxy should be logging via syslog, we need a syslog + # restart, otherwise the log file will be kept open and not reappear. + self.ossvc.syslog_restart() def get_stats(self, protocol): return self.ossvc.get_stats(protocol) diff --git a/libra/worker/drivers/haproxy/services_base.py b/libra/worker/drivers/haproxy/services_base.py index e6b6577a..0ce2367e 100644 --- a/libra/worker/drivers/haproxy/services_base.py +++ b/libra/worker/drivers/haproxy/services_base.py @@ -26,6 +26,10 @@ class ServicesBase: NOTE: All of these methods must be implemented. """ + def syslog_restart(self): + """ Restart syslog daemon. """ + raise NotImplementedError() + def service_stop(self): """ Stop the HAProxy service. """ raise NotImplementedError() @@ -53,3 +57,7 @@ class ServicesBase: def sudo_chown(self, file, user, group): """ Do a privileged file ownership change. """ raise NotImplementedError() + + def sudo_rm(self, file): + """ Do a privileged file delete. """ + raise NotImplementedError() diff --git a/libra/worker/drivers/haproxy/ubuntu_services.py b/libra/worker/drivers/haproxy/ubuntu_services.py index b35a4818..4826adb2 100644 --- a/libra/worker/drivers/haproxy/ubuntu_services.py +++ b/libra/worker/drivers/haproxy/ubuntu_services.py @@ -28,6 +28,13 @@ class UbuntuServices(ServicesBase): self._config_file = '/etc/haproxy/haproxy.cfg' self._backup_config = self._config_file + '.BKUP' + def syslog_restart(self): + cmd = '/usr/bin/sudo -n /usr/sbin/service rsyslog restart' + try: + subprocess.check_output(cmd.split()) + except subprocess.CalledProcessError as e: + raise Exception("Failed to restart rsyslog service: %s" % e) + def service_stop(self): """ Stop the HAProxy service on the local machine. """ cmd = '/usr/bin/sudo -n /usr/sbin/service haproxy stop' @@ -71,6 +78,17 @@ class UbuntuServices(ServicesBase): raise Exception("Failed to change file ownership: %s\n%s" % (e, e.output.rstrip('\n'))) + def sudo_rm(self, file): + if not os.path.exists(file): + return + cmd = '/usr/bin/sudo -n /bin/rm -f %s' % file + try: + subprocess.check_output(cmd.split(), + stderr=subprocess.STDOUT) + except subprocess.CalledProcessError as e: + raise Exception("Failed to delete %s\n%s" + % (file, e.output.rstrip('\n'))) + def write_config(self, config_str): """ Generate the new config and replace the current config file. @@ -108,12 +126,8 @@ class UbuntuServices(ServicesBase): def remove_configs(self): """ Delete current and backup configs on the local machine. """ - cmd = '/usr/bin/sudo -n /bin/rm -f %s %s' % (self._config_file, - self._backup_config) - try: - subprocess.check_output(cmd.split()) - except subprocess.CalledProcessError as e: - raise Exception("Failed to delete HAProxy config files: %s" % e) + self.sudo_rm(self._config_file) + self.sudo_rm(self._backup_config) def get_stats(self, protocol): """