Remove HAProxy logs on LB delete.

Since a node can be reused when load balancers are cycled through
creation and deletion, we need to remove any logs left from HAProxy
so that an ARCHIVE message will not pick up logs from the previous
LB.

Change-Id: I4c26fc5894dc8df7c6a069d612e7c91f96298d8d
This commit is contained in:
David Shrewsbury
2013-05-12 00:00:50 -04:00
parent 6f80f47d71
commit ca6716d572
4 changed files with 41 additions and 10 deletions

View File

@@ -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):

View File

@@ -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)

View File

@@ -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()

View File

@@ -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):
"""