class BaseIpvsTestCase(unittest.TestCase): ''' Base class allowing to setup and tear down environment. This class will correctly tear down fwmark based services. ''' verbose = False def setUp(self): ''' Set up environment by creating a client and cleaning up services ''' self.client = IpvsClient(verbose=BaseIpvsTestCase.verbose) self.cleanup() def tearDown(self): ''' Clean up services when tearing down test ''' self.cleanup() def cleanup(self): ''' helper function that clear ALL services from ipvs ''' pools = self.client.get_pools() for pool in pools: service = pool.service() if service.fwmark() is None: self.client.del_service(service.vip(), service.port(), protocol=service.proto_num()) else: self.client.del_fwm_service(service.fwmark(), af=service.af())
class BaseIpvsTestCase(unittest.TestCase): ''' Base class allowing to setup and tear down environment. This class will correctly tear down fwmark based services. ''' verbose = False def setUp(self): ''' Set up environment by creating a client and cleaning up services ''' self.client = IpvsClient(verbose=BaseIpvsTestCase.verbose) self.cleanup() def tearDown(self): ''' Clean up services when tearing down test ''' self.cleanup() def cleanup(self): ''' helper function that clear ALL services from ipvs ''' pools = self.client.get_pools() for pool in pools: service = pool.service() if service.fwmark() is None: self.client.del_service( service.vip(), service.port(), protocol=service.proto_num()) else: self.client.del_fwm_service(service.fwmark(), af=service.af())
def reload_ipvs(client: IpvsClient, pools: list) -> None: """Reload IPVS configuration given the passed list of pools""" print('Updating IPVS configuration') try: pools_to_load = Pool.load_pools_from_json_list(pools) except: # pylint: disable=bare-except print('Invalid pool configuration') return existing_pools = client.get_pools() services_to_load = [p.service() for p in pools_to_load] existing_services = [p.service() for p in existing_pools] for service in services_to_load: if service not in existing_services: print(f"Adding VIP {service.vip()}:{service.port()}") client.add_service(service.vip(), service.port(), protocol=service.proto_num(), sched_name=service.sched()) for service in existing_services: if service not in services_to_load: client.del_service(service.vip(), service.port(), protocol=service.proto_num()) existing_pools = client.get_pools() for pool in pools_to_load: existing_pool = next( filter(lambda p: p.service() == pool.service(), existing_pools)) for dest in pool.dests(): if dest.ip() not in [d.ip() for d in existing_pool.dests()]: client.add_dest(pool.service().vip(), pool.service().port(), protocol=pool.service().proto_num(), rip=dest.ip(), weight=dest.weight(), method=dest.fwd_method()) for dest in existing_pool.dests(): if dest.ip() not in [d.ip() for d in pool.dests()]: client.del_dest(pool.service().vip(), pool.service().port(), protocol=pool.service().proto_num(), rip=dest.ip())