Load balancer config

This commit is contained in:
Ian Southam 2014-11-27 14:16:17 +01:00 committed by wilderrodrigues
parent 7d0df32560
commit 84624091fd
5 changed files with 90 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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

View File

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