From ba3b6a19b1ff7fad828b9f56f05520021d6010b5 Mon Sep 17 00:00:00 2001 From: David Shrewsbury Date: Tue, 9 Oct 2012 10:02:37 -0400 Subject: [PATCH] Fix for CREATE if haproxy config does not exist. If the CREATE action follows a DELETE actions, it's possible that a configuration file may not exist, so we must allow for that. Also, stop the HAProxy process before deleting configuration files. --- libra/worker/drivers/haproxy/driver.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/libra/worker/drivers/haproxy/driver.py b/libra/worker/drivers/haproxy/driver.py index 4180040e..3e4d7866 100644 --- a/libra/worker/drivers/haproxy/driver.py +++ b/libra/worker/drivers/haproxy/driver.py @@ -12,6 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. +import os import subprocess from libra.worker.drivers.base import LoadBalancerDriver @@ -81,12 +82,20 @@ class HAProxyDriver(LoadBalancerDriver): fh.write(config_str) fh.close() - copy_cmd = "/usr/bin/sudo /bin/cp %s %s" % (self._config_file, - self._backup_config) - move_cmd = "/usr/bin/sudo /bin/mv %s %s" % (tmpfile, self._config_file) + # Copy any existing configuration file to a backup. + if os.path.exists(self._config_file): + copy_cmd = "/usr/bin/sudo /bin/cp %s %s" % (self._config_file, + self._backup_config) + try: + subprocess.check_output(copy_cmd.split(), + stderr=subprocess.STDOUT) + except subprocess.CalledProcessError as e: + raise Exception("Failed to copy configuration file: %s" % + e.output.rstrip('\n')) + # Move the temporary config file to production version. + move_cmd = "/usr/bin/sudo /bin/mv %s %s" % (tmpfile, self._config_file) try: - subprocess.check_output(copy_cmd.split(), stderr=subprocess.STDOUT) subprocess.check_output(move_cmd.split(), stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: raise Exception("Failed to write configuration file: %s" % @@ -156,5 +165,5 @@ class HAProxyDriver(LoadBalancerDriver): self._start() def delete(self): - self._delete_configs() self._stop() + self._delete_configs()