From 84624091fd3ac373cf848809e5e009d67e9a368a Mon Sep 17 00:00:00 2001 From: Ian Southam Date: Thu, 27 Nov 2014 14:16:17 +0100 Subject: [PATCH] Load balancer config --- .../debian/config/opt/cloud/bin/configure.py | 4 ++ .../debian/config/opt/cloud/bin/cs/CsFile.py | 7 +++ .../config/opt/cloud/bin/cs/CsLoadBalancer.py | 46 +++++++++++++++++++ .../config/opt/cloud/bin/cs_loadbalancer.py | 27 +++++++++++ .../debian/config/opt/cloud/bin/merge.py | 6 +++ 5 files changed, 90 insertions(+) create mode 100644 systemvm/patches/debian/config/opt/cloud/bin/cs/CsLoadBalancer.py create mode 100644 systemvm/patches/debian/config/opt/cloud/bin/cs_loadbalancer.py diff --git a/systemvm/patches/debian/config/opt/cloud/bin/configure.py b/systemvm/patches/debian/config/opt/cloud/bin/configure.py index a67731b1853..799e999f45d 100755 --- a/systemvm/patches/debian/config/opt/cloud/bin/configure.py +++ b/systemvm/patches/debian/config/opt/cloud/bin/configure.py @@ -38,6 +38,7 @@ from cs.CsFile import CsFile from cs.CsAddress import CsAddress from cs.CsApp import CsApache, CsPasswdSvc, CsDnsmasq from cs.CsMonitor import CsMonitor +from cs.CsLoadBalancer import CsLoadBalancer class CsPassword(CsDataBag): @@ -568,6 +569,9 @@ def main(argv): dhcp = CsDhcp("dhcpentry", config) dhcp.process() + lb = CsLoadBalancer("loadbalancer", config) + lb.process() + mon = CsMonitor("monitorservice", config) mon.process() diff --git a/systemvm/patches/debian/config/opt/cloud/bin/cs/CsFile.py b/systemvm/patches/debian/config/opt/cloud/bin/cs/CsFile.py index 2943ac403c4..510b13f42cc 100644 --- a/systemvm/patches/debian/config/opt/cloud/bin/cs/CsFile.py +++ b/systemvm/patches/debian/config/opt/cloud/bin/cs/CsFile.py @@ -46,6 +46,10 @@ class CsFile: else: return False + def empty(self): + self.config = [] + self.new_config = [] + def commit(self): if not self.is_changed(): return @@ -104,3 +108,6 @@ class CsFile: self.new_config[index] = replace + "\n" if not found: self.new_config.append(replace + "\n") + + def compare(self, o): + return (isinstance(o, self.__class__) and set(self.config) == set(o.new_config)) diff --git a/systemvm/patches/debian/config/opt/cloud/bin/cs/CsLoadBalancer.py b/systemvm/patches/debian/config/opt/cloud/bin/cs/CsLoadBalancer.py new file mode 100644 index 00000000000..4199d706fd1 --- /dev/null +++ b/systemvm/patches/debian/config/opt/cloud/bin/cs/CsLoadBalancer.py @@ -0,0 +1,46 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +import logging +import os.path +import re +import shutil +from cs.CsDatabag import CsDataBag +from CsFile import CsFile +import CsHelper + +HAPROXY_CONF_T = "/etc/haproxy/haproxy.cfg.new" +HAPROXY_CONF_P = "/etc/haproxy/haproxy.cfg" + + +class CsLoadBalancer(CsDataBag): + """ Manage dhcp entries """ + + def process(self): + if "config" not in self.dbag.keys(): + return + if 'configuration' not in self.dbag['config'][0].keys(): + return + config = self.dbag['config'][0]['configuration'] + file1 = CsFile(HAPROXY_CONF_T) + file2 = CsFile(HAPROXY_CONF_P) + file1.empty() + for x in config: + [file1.append(w, -1) for w in x.split('\n')] + if not file2.compare(file1): + file1.commit() + shutil.copy2(HAPROXY_CONF_T, HAPROXY_CONF_P) + CsHelper.service("haproxy", "restart") diff --git a/systemvm/patches/debian/config/opt/cloud/bin/cs_loadbalancer.py b/systemvm/patches/debian/config/opt/cloud/bin/cs_loadbalancer.py new file mode 100644 index 00000000000..14b2732caa4 --- /dev/null +++ b/systemvm/patches/debian/config/opt/cloud/bin/cs_loadbalancer.py @@ -0,0 +1,27 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from pprint import pprint +import copy + + +def merge(dbag, data): + """ Simply overwrite the existsing bag as, the whole configuration is sent every time """ + if "rules" not in data: + return dbag + dbag['config'] = data['rules'] + return dbag diff --git a/systemvm/patches/debian/config/opt/cloud/bin/merge.py b/systemvm/patches/debian/config/opt/cloud/bin/merge.py index 36c543a2ceb..e3d54ac9390 100755 --- a/systemvm/patches/debian/config/opt/cloud/bin/merge.py +++ b/systemvm/patches/debian/config/opt/cloud/bin/merge.py @@ -26,6 +26,7 @@ import cs_cmdline import cs_vmp import cs_network_acl import cs_firewallrules +import cs_loadbalancer import cs_monitorservice import cs_vmdata import cs_dhcp @@ -106,6 +107,8 @@ class updateDataBag: dbag = self.process_network_acl(self.db.getDataBag()) elif self.qFile.type == 'firewallrules': dbag = self.process_firewallrules(self.db.getDataBag()) + elif self.qFile.type == 'loadbalancer': + dbag = self.process_loadbalancer(self.db.getDataBag()) elif self.qFile.type == 'monitorservice': dbag = self.process_monitorservice(self.db.getDataBag()) elif self.qFile.type == 'vmdata': @@ -150,6 +153,9 @@ class updateDataBag: def process_firewallrules(self, dbag): return cs_firewallrules.merge(dbag, self.qFile.data) + def process_loadbalancer(self, dbag): + return cs_loadbalancer.merge(dbag, self.qFile.data) + def process_monitorservice(self, dbag): return cs_monitorservice.merge(dbag, self.qFile.data)