return output
-class KeepalivedGroup(object):
- """Group section of a keepalived configuration."""
-
- def __init__(self, ha_vr_id):
- self.ha_vr_id = ha_vr_id
- self.name = 'VG_%s' % ha_vr_id
- self.instance_names = set()
- self.notifiers = []
-
- def add_instance(self, instance):
- self.instance_names.add(instance.name)
-
- def set_notify(self, state, path):
- if state not in VALID_NOTIFY_STATES:
- raise InvalidNotifyStateException(state=state)
- self.notifiers.append((state, path))
-
- def build_config(self):
- return itertools.chain(['vrrp_sync_group %s {' % self.name,
- ' group {'],
- (' %s' % i for i in self.instance_names),
- [' }'],
- (' notify_%s "%s"' % (state, path)
- for state, path in self.notifiers),
- ['}'])
-
-
class KeepalivedInstance(object):
"""Instance section of a keepalived configuration."""
self.vips = []
self.virtual_routes = []
self.authentication = None
+ self.notifiers = []
metadata_cidr = '169.254.169.254/32'
self.primary_vip_range = get_free_range(
parent_range='169.254.0.0/16',
return [vip.ip_address for vip in self.vips
if vip.interface_name == interface_name]
+ def set_notify(self, state, path):
+ if state not in VALID_NOTIFY_STATES:
+ raise InvalidNotifyStateException(state=state)
+ self.notifiers.append((state, path))
+
def _build_track_interface_config(self):
return itertools.chain(
[' track_interface {'],
for route in self.virtual_routes),
[' }'])
+ def _build_notify_scripts(self):
+ return itertools.chain((' notify_%s "%s"' % (state, path)
+ for state, path in self.notifiers))
+
def build_config(self):
config = ['vrrp_instance %s {' % self.name,
' state %s' % self.state,
if self.virtual_routes:
config.extend(self._build_virtual_routes_config())
+ if self.notifiers:
+ config.extend(self._build_notify_scripts())
+
config.append('}')
return config
self.reset()
def reset(self):
- self.groups = {}
self.instances = {}
- def add_group(self, group):
- self.groups[group.ha_vr_id] = group
-
- def get_group(self, ha_vr_id):
- return self.groups.get(ha_vr_id)
-
def add_instance(self, instance):
self.instances[instance.vrouter_id] = instance
def build_config(self):
config = []
- for group in self.groups.values():
- config.extend(group.build_config())
-
for instance in self.instances.values():
config.extend(instance.build_config())
state_path = self._get_full_config_file_path('state')
return '%s\necho -n %s > %s' % (script, state, state_path)
- def add_notifier(self, script, state, ha_vr_id):
+ def add_notifier(self, script, state, vrouter_id):
"""Add a master, backup or fault notifier.
These notifiers are executed when keepalived invokes a state
full_script = self._append_state(script_with_prefix, state)
self._write_notify_script(state, full_script)
- group = self.config.get_group(ha_vr_id)
- group.set_notify(state, self._get_notifier_path(state))
+ vr_instance = self.config.get_instance(vrouter_id)
+ vr_instance.set_notify(state, self._get_notifier_path(state))
def get_conf_dir(self):
confs_dir = os.path.abspath(os.path.normpath(self.conf_path))
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,
'169.254.192.0/18',
advert_int=5)
instance1.set_authentication('AH', 'pass123')
instance1.track_interfaces.append("eth0")
+ instance1.set_notify('master', '/tmp/script.sh')
vip_address1 = keepalived.KeepalivedVipAddress('192.168.1.0/24',
'eth1')
"eth1")
instance1.virtual_routes.append(virtual_route)
- group1.add_instance(instance1)
-
instance2 = keepalived.KeepalivedInstance('MASTER', 'eth4', 2,
'169.254.192.0/18',
mcast_src_ip='224.0.0.1')
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
class KeepalivedConfTestCase(base.BaseTestCase,
KeepalivedConfBaseMixin):
- expected = """vrrp_sync_group VG_1 {
- group {
- VR_1
- }
- notify_master "/tmp/script.sh"
-}
-vrrp_sync_group VG_2 {
- group {
- VR_2
- }
-}
-vrrp_instance VR_1 {
+ expected = """vrrp_instance VR_1 {
state MASTER
interface eth0
virtual_router_id 1
virtual_routes {
0.0.0.0/0 via 192.168.1.1 dev eth1
}
+ notify_master "/tmp/script.sh"
}
vrrp_instance VR_2 {
state MASTER
class KeepalivedStateExceptionTestCase(base.BaseTestCase):
def test_state_exception(self):
- group = keepalived.KeepalivedGroup('group2')
+ instance = keepalived.KeepalivedInstance('MASTER', 'eth0', 1,
+ '169.254.192.0/18')
invalid_notify_state = 'a seal walks'
self.assertRaises(keepalived.InvalidNotifyStateException,
- group.set_notify,
+ instance.set_notify,
invalid_notify_state, '/tmp/script.sh')
invalid_vrrp_state = 'into a club'
instance.remove_vips_vroutes_by_interface('eth2')
instance.remove_vips_vroutes_by_interface('eth10')
- expected = """vrrp_sync_group VG_1 {
- group {
- VR_1
- }
- notify_master "/tmp/script.sh"
-}
-vrrp_sync_group VG_2 {
- group {
- VR_2
- }
-}
-vrrp_instance VR_1 {
+ expected = """vrrp_instance VR_1 {
state MASTER
interface eth0
virtual_router_id 1
virtual_routes {
0.0.0.0/0 via 192.168.1.1 dev eth1
}
+ notify_master "/tmp/script.sh"
}
vrrp_instance VR_2 {
state MASTER