def _create_lb_haproxy_cfg(self, msg, base_cfg_path='/etc/haproxy/haproxy.cfg'): try: haproxy_new_proxy_cfg = self._create_haproxy_listen_cfg( msg, base_cfg_path) except exception.HaproxyLBExists as e: LOG.warn('%s', e) raise exception.HaproxyCreateCfgError(explanation=str(e)) new_cfg_path = '/etc/haproxy/haproxy.cfg.new' cmd = 'cp %s %s' % (base_cfg_path, new_cfg_path) try: utils.execute(cmd) except exception.ProcessExecutionError as e: LOG.error("Failed to copy original configuration: %s", e) raise exception.HaproxyCreateCfgError(explanation=str(e)) try: with open(new_cfg_path, 'a') as cfile: cfile.write(haproxy_new_proxy_cfg) except IOError as e: LOG.error("Failed to open %s: %s" % (new_cfg_path, e)) raise exception.HaproxyCreateCfgError(explanation=str(e)) return new_cfg_path
def _test_haproxy_config(self, cfile_path): LOG.info('Testing the new haproxy configuration file') cmd = "haproxy -c -f %s" % cfile_path try: utils.execute(cmd) except exception.ProcessExecutionError as e: LOG.warn('Did not pass the new haproxy configuration test: %s', e) raise
def _replace_original_cfg_with_new(self, new_cfg_path): cmd = "cp %s /etc/haproxy/haproxy.cfg" % new_cfg_path try: utils.execute(cmd) except exception.ProcessExecutionError as e: LOG.error("Failed to replace the orignal configuration") return -1, str(e) return 0, None
def binding_ip(self, ips): dev = FLAGS.worker.service_interface for ip in ips: if ip in self.ips: continue if FLAGS.worker.service_interface == 'lo': cmd = 'ip addr add %s/32 scope link dev %s' % (ip, dev) else: cmd = 'ip addr add %s/32 scope global dev %s' % (ip, dev) utils.execute(cmd)
def _backup_original_cfg(self): now = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S-%f") backup_filename = 'haproxy.cfg_' + now backup_path = os.path.join(self.cfg_backup_dir, backup_filename) cmd = "cp /etc/haproxy/haproxy.cfg %s" % backup_path try: utils.execute(cmd) except exception.ProcessExecutionError as e: LOG.error("Failed to make a backup configuration") return -1, str(e) return 0, backup_path
def init_host(self): """Handle initialization if this is a standalone service. Child class should override this method """ dev = FLAGS.worker.service_interface cmd = 'ip addr show dev %s' % dev out = utils.execute(cmd) for line in out.split('\n'): fields = line.split() if fields and fields[0] == 'inet': self.ips.append(fields[1].split('/')[0])
def _reload_haproxy_cfg(self, backup_path): LOG.debug("Reloading haproxy") try: pid = self._get_haproxy_pid() except IOError: return -1 except ValueError: return -1 cmd = ("haproxy -f /etc/haproxy/haproxy.cfg -p " "/var/run/haproxy.pid -sf %s " % pid) try: utils.execute(cmd) except exception.ProcessExecutionError as e: LOG.error("Failed to reload haproxy(pid=%s): %s", pid, e) LOG.debug('Try to rollback the configuration') cmd = "cp %s /etc/haproxy/haproxy.cfg" % backup_path try: utils.execute(cmd) except exception.ProcessExecutionError as e: LOG.error('Failed to rollback the configuration') return -1 LOG.debug('Try to load the original configration') cmd = ("haproxy -f /etc/haproxy/haproxy.cfg -p " "/var/run/haproxy.pid -sf %s " % pid) try: utils.execute(cmd) except exception.ProcessExecutionError as e: LOG.error('Failed to load original configuration') return -1 LOG.debug("Reloaded haproxy successfully") return 0