mirror of
				https://github.com/apache/cloudstack.git
				synced 2025-11-04 00:02:37 +01:00 
			
		
		
		
	Sorting out redundancy
some new unit testing stupid bug in CsCmdLine refactor
This commit is contained in:
		
							parent
							
								
									014b47f6e6
								
							
						
					
					
						commit
						406af7e855
					
				@ -47,7 +47,7 @@ class CsAddress(CsDataBag):
 | 
			
		||||
 | 
			
		||||
    def get_guest_if(self):
 | 
			
		||||
        """
 | 
			
		||||
        Return CsIp object for the first guest interface
 | 
			
		||||
        Return CsInterface object for the first guest interface
 | 
			
		||||
        """
 | 
			
		||||
        for ip in self.get_ips():
 | 
			
		||||
            if ip.is_guest():
 | 
			
		||||
@ -199,6 +199,11 @@ class CsInterface:
 | 
			
		||||
            return True
 | 
			
		||||
        return False
 | 
			
		||||
 | 
			
		||||
    def is_public(self):
 | 
			
		||||
        if "nw_type" in self.address and self.address['nw_type'] in ['public']:
 | 
			
		||||
            return True
 | 
			
		||||
        return False
 | 
			
		||||
 | 
			
		||||
    def to_str(self):
 | 
			
		||||
        pprint(self.address)
 | 
			
		||||
 | 
			
		||||
@ -294,7 +299,7 @@ class CsIP:
 | 
			
		||||
            if " DOWN " in i:
 | 
			
		||||
                cmd2 = "ip link set %s up" % self.getDevice()
 | 
			
		||||
                # Do not change the state of ips on a redundant router that are managed by vrrp or CsRedundant
 | 
			
		||||
                if self.config.cmdline().is_redundant() and self.needs_vrrp():
 | 
			
		||||
                if self.config.cmdline().is_redundant() and self.is_public():
 | 
			
		||||
                    pass
 | 
			
		||||
                else:
 | 
			
		||||
                    CsHelper.execute(cmd2)
 | 
			
		||||
@ -448,6 +453,11 @@ class CsIP:
 | 
			
		||||
            return True
 | 
			
		||||
        return False
 | 
			
		||||
 | 
			
		||||
    def is_public(self):
 | 
			
		||||
        if "nw_type" in self.address and self.address['nw_type'] in ['public']:
 | 
			
		||||
            return True
 | 
			
		||||
        return False
 | 
			
		||||
 | 
			
		||||
    def ip(self):
 | 
			
		||||
        return str(self.address['cidr'])
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -52,16 +52,26 @@ class CsCmdLine(CsDataBag):
 | 
			
		||||
    """ Get cmdline config parameters """
 | 
			
		||||
 | 
			
		||||
    def idata(self):
 | 
			
		||||
        if "config" in self.dbag:
 | 
			
		||||
            return self.dbag['config']
 | 
			
		||||
        else:
 | 
			
		||||
            return {}
 | 
			
		||||
        if "config" not in self.dbag:
 | 
			
		||||
            self.dbag['config'] = {}
 | 
			
		||||
        return self.dbag['config']
 | 
			
		||||
 | 
			
		||||
    def get_priority(self):
 | 
			
		||||
        if "router_pr" in self.idata():
 | 
			
		||||
            return self.idata()['router_pr']
 | 
			
		||||
        return 99
 | 
			
		||||
 | 
			
		||||
    def is_redundant(self):
 | 
			
		||||
        if "redundant_router" in self.idata():
 | 
			
		||||
            return self.idata()['redundant_router'] == "true"
 | 
			
		||||
        return False
 | 
			
		||||
 | 
			
		||||
    def set_redundant(self, val="true"):
 | 
			
		||||
        self.idata()['redundant_router'] = val
 | 
			
		||||
 | 
			
		||||
    def set_guest_gw(self, val):
 | 
			
		||||
        self.idata()['guestgw'] = val
 | 
			
		||||
 | 
			
		||||
    def get_guest_gw(self):
 | 
			
		||||
        if "guestgw" in self.idata():
 | 
			
		||||
            return self.idata()['guestgw']
 | 
			
		||||
@ -95,7 +105,7 @@ class CsCmdLine(CsDataBag):
 | 
			
		||||
            return "unknown"
 | 
			
		||||
 | 
			
		||||
    def get_domain(self):
 | 
			
		||||
        if "domain" in self.config:
 | 
			
		||||
        if "domain" in self.idata();
 | 
			
		||||
            return self.idata()['domain']
 | 
			
		||||
        else:
 | 
			
		||||
            return "cloudnine.internal"
 | 
			
		||||
 | 
			
		||||
@ -88,7 +88,7 @@ class CsRedundant(object):
 | 
			
		||||
        # keepalived configuration
 | 
			
		||||
        file = CsFile("/etc/keepalived/keepalived.conf")
 | 
			
		||||
        file.search(" router_id ", "    router_id %s" % self.cl.get_name())
 | 
			
		||||
        file.search(" priority ", "    priority %s" % 20)
 | 
			
		||||
        file.search(" priority ", "    priority %s" % self.cl.get_priority())
 | 
			
		||||
        file.search(" weight ", "    weight %s" % 2)
 | 
			
		||||
        file.greplace("[RROUTER_BIN_PATH]", self.CS_ROUTER_DIR)
 | 
			
		||||
        file.section("virtual_ipaddress {", "}", self._collect_ips())
 | 
			
		||||
@ -146,11 +146,13 @@ class CsRedundant(object):
 | 
			
		||||
        if not self.cl.is_redundant():
 | 
			
		||||
            logging.error("Set backup called on non-redundant router")
 | 
			
		||||
            return
 | 
			
		||||
        """ 
 | 
			
		||||
        if not self.cl.is_master():
 | 
			
		||||
            logging.error("Set backup called on node that is already backup")
 | 
			
		||||
            return
 | 
			
		||||
        """
 | 
			
		||||
        logging.info("Router switched to backup mode")
 | 
			
		||||
        ads = [o for o in self.address.get_ips() if o.needs_vrrp()]
 | 
			
		||||
        ads = [o for o in self.address.get_ips() if o.is_public()]
 | 
			
		||||
        for o in ads:
 | 
			
		||||
            CsHelper.execute("ifconfig %s down" % o.get_device())
 | 
			
		||||
        cmd = "%s -C %s" % (self.CONNTRACKD_BIN, self.CONNTRACKD_CONFIG)
 | 
			
		||||
@ -160,6 +162,7 @@ class CsRedundant(object):
 | 
			
		||||
        CsHelper.service("cloud-passwd-srvr", "stop")
 | 
			
		||||
        CsHelper.service("dnsmasq", "stop")
 | 
			
		||||
        self.cl.dbag['config']['redundant_master'] = "false"
 | 
			
		||||
        CsHelper.service("keepalived", "restart")
 | 
			
		||||
        self.cl.save()
 | 
			
		||||
 | 
			
		||||
    def set_master(self):
 | 
			
		||||
@ -167,10 +170,12 @@ class CsRedundant(object):
 | 
			
		||||
        if not self.cl.is_redundant():
 | 
			
		||||
            logging.error("Set master called on non-redundant router")
 | 
			
		||||
            return
 | 
			
		||||
        """
 | 
			
		||||
        if self.cl.is_master():
 | 
			
		||||
            logging.error("Set master called on master node")
 | 
			
		||||
            return
 | 
			
		||||
        ads = [o for o in self.address.get_ips() if o.needs_vrrp()]
 | 
			
		||||
        """
 | 
			
		||||
        ads = [o for o in self.address.get_ips() if o.is_public()]
 | 
			
		||||
        for o in ads:
 | 
			
		||||
            # cmd2 = "ip link set %s up" % self.getDevice()
 | 
			
		||||
            CsHelper.execute("ifconfig %s down" % o.get_device())
 | 
			
		||||
@ -189,6 +194,7 @@ class CsRedundant(object):
 | 
			
		||||
        CsHelper.service("dnsmasq", "restart")
 | 
			
		||||
        self.cl.dbag['config']['redundant_master'] = "true"
 | 
			
		||||
        self.cl.save()
 | 
			
		||||
        CsHelper.service("keepalived", "restart")
 | 
			
		||||
        logging.info("Router switched to master mode")
 | 
			
		||||
 | 
			
		||||
    def _collect_ignore_ips(self):
 | 
			
		||||
 | 
			
		||||
@ -21,6 +21,7 @@ from cs.CsRedundant import CsRedundant
 | 
			
		||||
from cs.CsDatabag import CsCmdLine
 | 
			
		||||
from cs.CsAddress import CsAddress
 | 
			
		||||
from cs.CsConfig import CsConfig
 | 
			
		||||
import logging
 | 
			
		||||
from optparse import OptionParser
 | 
			
		||||
 | 
			
		||||
parser = OptionParser()
 | 
			
		||||
@ -35,15 +36,15 @@ parser.add_option("-f", "--fault",
 | 
			
		||||
                  help="Notify Fault")
 | 
			
		||||
(options, args) = parser.parse_args()
 | 
			
		||||
 | 
			
		||||
config = CsConfig(False)
 | 
			
		||||
config = CsConfig()
 | 
			
		||||
logging.basicConfig(filename=config.get_logger(),
 | 
			
		||||
                    level=config.get_level(),
 | 
			
		||||
                    format=config.get_format())
 | 
			
		||||
config.set_cl()
 | 
			
		||||
config.cmdline()
 | 
			
		||||
cl = CsCmdLine("cmdline", config)
 | 
			
		||||
 | 
			
		||||
address = CsAddress("ips")
 | 
			
		||||
red = CsRedundant(config, address)
 | 
			
		||||
config.set_address()
 | 
			
		||||
red = CsRedundant(config)
 | 
			
		||||
 | 
			
		||||
if options.master:
 | 
			
		||||
    red.set_master()
 | 
			
		||||
 | 
			
		||||
@ -33,7 +33,7 @@ parser.add_option("-d", "--disable",
 | 
			
		||||
 | 
			
		||||
(options, args) = parser.parse_args()
 | 
			
		||||
 | 
			
		||||
config = CsConfig(False)
 | 
			
		||||
config = CsConfig()
 | 
			
		||||
logging.basicConfig(filename=config.get_logger(),
 | 
			
		||||
                    level=config.get_level(),
 | 
			
		||||
                    format=config.get_format())
 | 
			
		||||
 | 
			
		||||
@ -17,5 +17,8 @@ class TestCsInterface(unittest.TestCase):
 | 
			
		||||
    def test_get_gateway(self):
 | 
			
		||||
        self.assertTrue(self.csinterface.get_gateway() == "1.2.3.4")
 | 
			
		||||
 | 
			
		||||
    def test_is_public(self):
 | 
			
		||||
        self.assertTrue(self.csinterface.is_public() is False)
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    unittest.main()
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user