Beispiel #1
0
    def _get_config(self):
        config = keepalived.KeepalivedConf()

        group1 = keepalived.KeepalivedGroup(1)
        group2 = keepalived.KeepalivedGroup(2)

        group1.set_notify('master', '/tmp/script.sh')

        instance1 = keepalived.KeepalivedInstance('MASTER',
                                                  'eth0',
                                                  1,
                                                  advert_int=5)
        instance1.set_authentication('AH', 'pass123')
        instance1.track_interfaces.append("eth0")

        vip_address1 = keepalived.KeepalivedVipAddress('192.168.1.0/24',
                                                       'eth1')

        vip_address2 = keepalived.KeepalivedVipAddress('192.168.2.0/24',
                                                       'eth2')

        vip_address3 = keepalived.KeepalivedVipAddress('192.168.3.0/24',
                                                       'eth2')

        vip_address_ex = keepalived.KeepalivedVipAddress(
            '192.168.55.0/24', 'eth10')

        instance1.vips.append(vip_address1)
        instance1.vips.append(vip_address2)
        instance1.vips.append(vip_address3)
        instance1.vips.append(vip_address_ex)

        virtual_route = keepalived.KeepalivedVirtualRoute(
            "0.0.0.0/0", "192.168.1.1", "eth1")
        instance1.virtual_routes.append(virtual_route)

        group1.add_instance(instance1)

        instance2 = keepalived.KeepalivedInstance('MASTER',
                                                  'eth4',
                                                  2,
                                                  mcast_src_ip='224.0.0.1')
        instance2.track_interfaces.append("eth4")

        vip_address1 = keepalived.KeepalivedVipAddress('192.168.3.0/24',
                                                       'eth6')

        instance2.vips.append(vip_address1)
        instance2.vips.append(vip_address2)
        instance2.vips.append(vip_address_ex)

        group2.add_instance(instance2)

        config.add_group(group1)
        config.add_instance(instance1)
        config.add_group(group2)
        config.add_instance(instance2)

        return config
Beispiel #2
0
    def _get_config(self):
        config = keepalived.KeepalivedConf()

        instance1 = keepalived.KeepalivedInstance('MASTER',
                                                  'eth0',
                                                  1, ['169.254.192.0/18'],
                                                  advert_int=5)
        instance1.set_authentication('AH', 'pass123')
        instance1.track_interfaces.append("eth0")

        vip_address1 = keepalived.KeepalivedVipAddress('192.168.1.0/24',
                                                       'eth1',
                                                       track=False)

        vip_address2 = keepalived.KeepalivedVipAddress('192.168.2.0/24',
                                                       'eth2',
                                                       track=False)

        vip_address3 = keepalived.KeepalivedVipAddress('192.168.3.0/24',
                                                       'eth2',
                                                       track=False)

        vip_address_ex = keepalived.KeepalivedVipAddress('192.168.55.0/24',
                                                         'eth10',
                                                         track=False)

        instance1.vips.append(vip_address1)
        instance1.vips.append(vip_address2)
        instance1.vips.append(vip_address3)
        instance1.vips.append(vip_address_ex)

        virtual_route = keepalived.KeepalivedVirtualRoute(
            n_consts.IPv4_ANY, "192.168.1.1", "eth1")
        instance1.virtual_routes.gateway_routes = [virtual_route]

        instance2 = keepalived.KeepalivedInstance('MASTER',
                                                  'eth4',
                                                  2, ['169.254.192.0/18'],
                                                  mcast_src_ip='224.0.0.1')
        instance2.track_interfaces.append("eth4")

        vip_address1 = keepalived.KeepalivedVipAddress('192.168.3.0/24',
                                                       'eth6',
                                                       track=False)

        instance2.vips.append(vip_address1)
        instance2.vips.append(vip_address2)
        instance2.vips.append(vip_address_ex)

        config.add_instance(instance1)
        config.add_instance(instance2)

        return config
Beispiel #3
0
    def _init_keepalived_manager(self, process_monitor):
        self.keepalived_manager = keepalived.KeepalivedManager(
            self.router['id'],
            keepalived.KeepalivedConf(),
            process_monitor,
            conf_path=self.agent_conf.ha_confs_path,
            namespace=self.ns_name)

        config = self.keepalived_manager.config

        interface_name = self.get_ha_device_name()
        subnets = self.ha_port.get('subnets', [])
        ha_port_cidrs = [subnet['cidr'] for subnet in subnets]
        instance = keepalived.KeepalivedInstance(
            'BACKUP',
            interface_name,
            self.ha_vr_id,
            ha_port_cidrs,
            nopreempt=True,
            advert_int=self.agent_conf.ha_vrrp_advert_int,
            priority=self.ha_priority)
        instance.track_interfaces.append(interface_name)

        if self.agent_conf.ha_vrrp_auth_password:
            # TODO(safchain): use oslo.config types when it will be available
            # in order to check the validity of ha_vrrp_auth_type
            instance.set_authentication(self.agent_conf.ha_vrrp_auth_type,
                                        self.agent_conf.ha_vrrp_auth_password)

        config.add_instance(instance)
Beispiel #4
0
    def _init_keepalived_manager(self):
        # TODO(Carl) This looks a bit funny, doesn't it?
        self.keepalived_manager = self.get_keepalived_manager()

        config = self.keepalived_manager.config

        interface_name = self.get_ha_device_name(self.ha_port['id'])
        ha_port_cidr = self.ha_port['subnet']['cidr']
        instance = keepalived.KeepalivedInstance(
            'BACKUP',
            interface_name,
            self.ha_vr_id,
            ha_port_cidr,
            nopreempt=True,
            advert_int=self.agent_conf.ha_vrrp_advert_int,
            priority=self.ha_priority)
        instance.track_interfaces.append(interface_name)

        if self.agent_conf.ha_vrrp_auth_password:
            # TODO(safchain): use oslo.config types when it will be available
            # in order to check the validity of ha_vrrp_auth_type
            instance.set_authentication(self.agent_conf.ha_vrrp_auth_type,
                                        self.agent_conf.ha_vrrp_auth_password)

        config.add_instance(instance)
Beispiel #5
0
    def _init_keepalived_manager(self, ri):
        ri.keepalived_manager = keepalived.KeepalivedManager(
            ri.router['id'],
            keepalived.KeepalivedConf(),
            conf_path=self.conf.ha_confs_path,
            namespace=ri.ns_name,
            root_helper=self.root_helper)

        config = ri.keepalived_manager.config

        interface_name = self.get_ha_device_name(ri.ha_port['id'])
        ha_port_cidr = ri.ha_port['subnet']['cidr']
        instance = keepalived.KeepalivedInstance(
            'BACKUP',
            interface_name,
            ri.ha_vr_id,
            ha_port_cidr,
            nopreempt=True,
            advert_int=self.conf.ha_vrrp_advert_int,
            priority=ri.ha_priority)
        instance.track_interfaces.append(interface_name)

        if self.conf.ha_vrrp_auth_password:
            # TODO(safchain): use oslo.config types when it will be available
            # in order to check the validity of ha_vrrp_auth_type
            instance.set_authentication(self.conf.ha_vrrp_auth_type,
                                        self.conf.ha_vrrp_auth_password)

        group = keepalived.KeepalivedGroup(ri.ha_vr_id)
        group.add_instance(instance)

        config.add_group(group)
        config.add_instance(instance)
Beispiel #6
0
 def test_add_vip_returns_exception_on_duplicate_ip(self):
     instance = keepalived.KeepalivedInstance('MASTER', 'eth0', 1,
                                              ['169.254.192.0/18'])
     instance.add_vip('192.168.222.1/32', 'eth11', None)
     self.assertRaises(keepalived.VIPDuplicateAddressException,
                       instance.add_vip, '192.168.222.1/32', 'eth12',
                       'link')
    def test_state_exception(self):
        instance = keepalived.KeepalivedInstance('MASTER', 'eth0', 1,
                                                 '169.254.192.0/18')

        invalid_notify_state = 'a seal walks'
        self.assertRaises(keepalived.InvalidNotifyStateException,
                          instance.set_notify, invalid_notify_state,
                          '/tmp/script.sh')

        invalid_vrrp_state = 'into a club'
        self.assertRaises(keepalived.InvalidInstanceStateException,
                          keepalived.KeepalivedInstance, invalid_vrrp_state,
                          'eth0', 33, '169.254.192.0/18')

        invalid_auth_type = '[hip, hip]'
        instance = keepalived.KeepalivedInstance('MASTER', 'eth0', 1,
                                                 '169.254.192.0/18')
        self.assertRaises(keepalived.InvalidAuthenticationTypeExecption,
                          instance.set_authentication, invalid_auth_type,
                          'some_password')
Beispiel #8
0
    def test_state_exception(self):
        invalid_vrrp_state = 'a seal walks'
        self.assertRaises(keepalived.InvalidInstanceStateException,
                          keepalived.KeepalivedInstance, invalid_vrrp_state,
                          'eth0', 33, ['169.254.192.0/18'])

        invalid_auth_type = 'into a club'
        instance = keepalived.KeepalivedInstance('MASTER', 'eth0', 1,
                                                 ['169.254.192.0/18'])
        self.assertRaises(keepalived.InvalidAuthenticationTypeException,
                          instance.set_authentication, invalid_auth_type,
                          'some_password')
Beispiel #9
0
    def test_build_config_no_vips(self):
        expected = """vrrp_instance VR_1 {
    state MASTER
    interface eth0
    virtual_router_id 1
    priority 50
    virtual_ipaddress {
        169.254.0.1/24 dev eth0
    }
}"""
        instance = keepalived.KeepalivedInstance('MASTER', 'eth0', 1,
                                                 ['169.254.192.0/18'])
        self.assertEqual(expected, '\n'.join(instance.build_config()))
Beispiel #10
0
 def test_build_config_no_vips(self):
     expected = textwrap.dedent("""\
         vrrp_instance VR_1 {
             state MASTER
             interface eth0
             virtual_router_id 1
             priority 50
             garp_master_delay 60
             virtual_ipaddress {
                 169.254.0.1/24 dev eth0
             }
         }""")
     instance = keepalived.KeepalivedInstance('MASTER', 'eth0', VRRP_ID,
                                              ['169.254.192.0/18'])
     self.assertEqual(expected, os.linesep.join(instance.build_config()))
Beispiel #11
0
    def configure(self):
        config = keepalived.KeepalivedConf()
        instance1 = keepalived.KeepalivedInstance('MASTER',
                                                  self.ha_port,
                                                  1, ['169.254.192.0/18'],
                                                  advert_int=5)
        instance1.track_interfaces.append(self.ha_port)

        # Configure keepalived with an IPv6 address (gw_vip) on gw_port.
        vip_addr1 = keepalived.KeepalivedVipAddress(self.gw_vip, self.gw_port)
        instance1.vips.append(vip_addr1)

        # Configure keepalived with an IPv6 default route on gw_port.
        gateway_route = keepalived.KeepalivedVirtualRoute(
            n_consts.IPv6_ANY, self.default_gw, self.gw_port)
        instance1.virtual_routes.gateway_routes = [gateway_route]
        config.add_instance(instance1)
        self.config = config
Beispiel #12
0
    def _init_keepalived_manager(self, process_monitor):
        self.keepalived_manager = keepalived.KeepalivedManager(
            self.router['id'],
            keepalived.KeepalivedConf(),
            process_monitor,
            conf_path=self.agent_conf.ha_confs_path,
            namespace=self.ha_namespace,
            throttle_restart_value=(
                self.agent_conf.ha_vrrp_advert_int * THROTTLER_MULTIPLIER))

        # The following call is required to ensure that if the state path does
        # not exist it gets created.
        self.keepalived_manager.get_full_config_file_path('test')

        config = self.keepalived_manager.config

        interface_name = self.get_ha_device_name()
        subnets = self.ha_port.get('subnets', [])
        ha_port_cidrs = [subnet['cidr'] for subnet in subnets]
        instance = keepalived.KeepalivedInstance(
            'BACKUP',
            interface_name,
            self.ha_vr_id,
            ha_port_cidrs,
            nopreempt=True,
            advert_int=self.agent_conf.ha_vrrp_advert_int,
            priority=self.ha_priority,
            vrrp_health_check_interval=(
                self.agent_conf.ha_vrrp_health_check_interval),
            ha_conf_dir=self.keepalived_manager.get_conf_dir())
        instance.track_interfaces.append(interface_name)

        if self.agent_conf.ha_vrrp_auth_password:
            # TODO(safchain): use oslo.config types when it will be available
            # in order to check the validity of ha_vrrp_auth_type
            instance.set_authentication(self.agent_conf.ha_vrrp_auth_type,
                                        self.agent_conf.ha_vrrp_auth_password)

        config.add_instance(instance)
Beispiel #13
0
    def test_build_config_no_vips_track_script(self):
        expected = """
vrrp_script ha_health_check_1 {
    script "/etc/ha_confs/qrouter-x/ha_check_script_1.sh"
    interval 5
    fall 2
    rise 2
}

vrrp_instance VR_1 {
    state MASTER
    interface eth0
    virtual_router_id 1
    priority 50
    garp_master_delay 60
    virtual_ipaddress {
        169.254.0.1/24 dev eth0
    }
}"""
        instance = keepalived.KeepalivedInstance('MASTER', 'eth0', VRRP_ID,
                                                 ['169.254.192.0/18'])
        instance.track_script = keepalived.KeepalivedTrackScript(
            VRRP_INTERVAL, '/etc/ha_confs/qrouter-x', VRRP_ID)
        self.assertEqual(expected, '\n'.join(instance.build_config()))
Beispiel #14
0
 def test_add_vip_idempotent(self):
     instance = keepalived.KeepalivedInstance('MASTER', 'eth0', 1,
                                              ['169.254.192.0/18'])
     instance.add_vip('192.168.222.1/32', 'eth11', None)
     instance.add_vip('192.168.222.1/32', 'eth12', 'link')
     self.assertEqual(1, len(instance.vips))
Beispiel #15
0
 def test_get_primary_vip(self):
     instance = keepalived.KeepalivedInstance('MASTER', 'ha0', 42,
                                              ['169.254.192.0/18'])
     self.assertEqual('169.254.0.42/24', instance.get_primary_vip())
Beispiel #16
0
 def test_generate_primary_vip(self):
     instance = keepalived.KeepalivedInstance('MASTER', 'ha0', 42,
                                              '169.254.192.0/18')
     self.assertEqual('169.254.0.42/24',
                      str(instance._generate_primary_vip()))