示例#1
0
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())
示例#2
0
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())
示例#3
0
def main(argv):
    parser = argparse.ArgumentParser()
    parser.add_argument('-s',
                        '--service',
                        default=None,
                        help='service to dump')
    parser.add_argument('-d',
                        '--dest',
                        default=None,
                        help='destination to dump')
    args = parser.parse_args(argv[1:])
    pools = IpvsClient().get_pools()
    for p in pools:
        s = p.service()
        if args.service is not None or args.dest is not None:
            if (args.service is not None
                    and not match_arg(args.service, s.vip(), s.port())):
                continue
            if (args.dest is not None and not any(
                [match_arg(args.dest, d.ip(), s.port()) for d in p.dests()])):
                continue
        print(s)
        for d in p.dests():
            if args.dest is None or match_arg(args.dest, d.ip(), s.port()):
                print('->', d)
示例#4
0
文件: ipvssync.py 项目: akamac/ipvs
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())
示例#5
0
文件: ipvssync.py 项目: akamac/ipvs
def main():  # pylint: disable=missing-docstring
    client = IpvsClient()
    reload_ipvs(client, load_pools())

    signal.signal(signal.SIGHUP,
                  lambda s, f: reload_ipvs(client, load_pools()))
    signal.signal(signal.SIGINT, lambda s, f: flush_n_exit(client))  # Nomad
    signal.signal(signal.SIGTERM, lambda s, f: flush_n_exit(client))  # Docker
    while True:
        print('Waiting for SIGHUP to reload ipvs config')
        signal.pause()
示例#6
0
文件: ipvs.py 项目: saks/nora
def main():
    module = AnsibleModule(
        argument_spec=dict(
            clear=dict(required=False, default=False, type='bool'),
            pools=dict(required=False, default=[], type='list'),
            verbose=dict(default=False, type='bool'),
        ),
    )

    try:
        pools_spec = module.params.get('pools')
        client = IpvsClient(verbose=module.params.get('verbose'))

        if module.params.get('clear'):
            client.flush()

        pools = ipvs.Pool.load_pools_from_json_list(pools_spec)
        changed = False

        for pool in pools:
            service = pool.service()
            service_attrs_list = service.to_attr_list()
            existing_service = client.get_service(service_attrs_list)

            if not existing_service:
                client.add_service(service.vip(), service.port(),
                                   sched_name=service.sched())

            dests = pool.dests()
            existing_dests = client.get_dests(service_attrs_list)

            if 0 == len(existing_dests):
                for dest in dests:
                    changed = True
                    client.add_dest(service.vip(), service.port(), dest.ip(),
                                    rport=int(dest.port()), weight=int(dest.weight()),
                                    method=ipvs.IPVS_MASQUERADING)

            for dest in dests:
                if not present_in_existing_dests(dest, existing_dests):
                    changed = True
                    client.update_dest(service.vip(), service.port(), dest.ip(),
                                       rport=int(dest.port()), weight=int(dest.weight()),
                                       method=ipvs.IPVS_MASQUERADING)

        module.exit_json(changed=changed,
                         msg="manager.get_summary_message()",
                         summary="manager.counters",
                         reload_reasons="manager.get_reload_reason_message()",
                         ansible_facts={"service": 'x[0].to_dict()'})

    except True as e:
        raise e
示例#7
0
 def setUp(self):
     '''
     Set up environment by creating a client and cleaning up services
     '''
     self.client = IpvsClient(verbose=BaseIpvsTestCase.verbose)
     self.cleanup()
示例#8
0
 def setUp(self):
     '''
     Set up environment by creating a client and cleaning up services
     '''
     self.client = IpvsClient(verbose=BaseIpvsTestCase.verbose)
     self.cleanup()
示例#9
0
def ipvs_client():
    return IpvsClient()
示例#10
0
文件: ipvssync.py 项目: akamac/ipvs
def flush_n_exit(client: IpvsClient) -> None:
    """Flush IPVS config and exit"""
    print('Received termination request. Flushing config')
    client.flush()
    raise SystemExit