Added docstring, lint fixes and general tidyup

This commit is contained in:
Liam Young 2016-06-08 14:03:50 +00:00
parent ca1561dafd
commit 3e1adbf909
2 changed files with 48 additions and 14 deletions

View File

@ -14,6 +14,7 @@
import ipaddress import ipaddress
from six import string_types from six import string_types
class CRM(dict): class CRM(dict):
""" """
Configuration object for Pacemaker resources for the HACluster Configuration object for Pacemaker resources for the HACluster
@ -512,12 +513,24 @@ class ResourceDescriptor(object):
""" """
pass pass
class InitService(ResourceDescriptor): class InitService(ResourceDescriptor):
def __init__(self, service_name, init_service_name): def __init__(self, service_name, init_service_name):
"""Class for managing init resource
:param service_name: string - Name of service
:param init_service_name: string - Name service uses in init system
:returns: None
"""
self.service_name = service_name self.service_name = service_name
self.init_service_name = init_service_name self.init_service_name = init_service_name
def configure_resource(self, crm): def configure_resource(self, crm):
""""Configure new init system service resource in crm
:param crm: CRM() instance - Config object for Pacemaker resources
:returns: None
"""
res_key = 'res_{}_{}'.format( res_key = 'res_{}_{}'.format(
self.service_name.replace('-', '_'), self.service_name.replace('-', '_'),
self.init_service_name.replace('-', '_')) self.init_service_name.replace('-', '_'))
@ -530,12 +543,25 @@ class InitService(ResourceDescriptor):
class VirtualIP(ResourceDescriptor): class VirtualIP(ResourceDescriptor):
def __init__(self, service_name, vip, nic=None, cidr=None): def __init__(self, service_name, vip, nic=None, cidr=None):
"""Class for managing VIP resource
:param service_name: string - Name of service
:param vip: string - Virtual IP to be managed
:param nic: string - Network interface to bind vip to
:param cidr: string - Netmask for vip
:returns: None
"""
self.service_name = service_name self.service_name = service_name
self.vip = vip self.vip = vip
self.nic = nic self.nic = nic
self.cidr = cidr self.cidr = cidr
def configure_resource(self, crm): def configure_resource(self, crm):
"""Configure new vip resource in crm
:param crm: CRM() instance - Config object for Pacemaker resources
:returns: None
"""
vip_key = 'res_{}_{}_vip'.format(self.service_name, self.nic) vip_key = 'res_{}_{}_vip'.format(self.service_name, self.nic)
ipaddr = ipaddress.ip_address(self.vip) ipaddr = ipaddress.ip_address(self.vip)
if isinstance(ipaddr, ipaddress.IPv4Address): if isinstance(ipaddr, ipaddress.IPv4Address):

View File

@ -60,34 +60,41 @@ class HAClusterRequires(RelationBase):
hacluster.manage_resources(crm) hacluster.manage_resources(crm)
:param crm: CRM() instance - Config object for Pacemaker resources
:returns: None
""" """
relation_data = {k: v for k, v in crm.items() if v} relation_data = {k: v for k, v in crm.items() if v}
print(relation_data)
self.set_local(**relation_data) self.set_local(**relation_data)
self.set_remote(**relation_data) self.set_remote(**relation_data)
def bind_resources(self, iface, mcastport=None): def bind_resources(self, iface, mcastport=None):
"""Inform the ha subordinate about each service it should manage. The """Inform the ha subordinate about each service it should manage. The
child class specifies the services via self.ha_resources child class specifies the services via self.ha_resources
@param hacluster interface :param iface: string - Network interface to bind to
:param mcastport: int - Multicast port corosync should use for cluster
management traffic
""" """
if not mcastport: if mcastport is None:
mcastport=4440 mcastport = 4440
resources = self.get_local('resources') resources = self.get_local('resources')
self.bind_on(iface=iface, mcastport=mcastport) self.bind_on(iface=iface, mcastport=mcastport)
self.manage_resources(resources) self.manage_resources(resources)
def add_vip(self, name, vip, iface, netmask): def add_vip(self, name, vip, iface, netmask):
"""Add a VirtualIP object for each user specified vip to self.resources """Add a VirtualIP object for each user specified vip to self.resources
:param name: string - Name of service
:param vip: string - Virtual IP to be managed
:param iface: string - Network interface to bind vip to
:param netmask: string - Netmask for vip
:returns: None
""" """
resource_dict = self.get_local('resources') resource_dict = self.get_local('resources')
if resource_dict: if resource_dict:
resources=relations.hacluster.common.CRM(**resource_dict) resources = relations.hacluster.common.CRM(**resource_dict)
else: else:
resources=relations.hacluster.common.CRM() resources = relations.hacluster.common.CRM()
resources.add( resources.add(
relations.hacluster.common.VirtualIP( relations.hacluster.common.VirtualIP(
name, name,
@ -96,17 +103,18 @@ class HAClusterRequires(RelationBase):
cidr=netmask,)) cidr=netmask,))
self.set_local(resources=resources) self.set_local(resources=resources)
def add_init_service(self, name, service): def add_init_service(self, name, service):
"""Add a InitService object for haproxy to self.resources """Add a InitService object for haproxy to self.resources
:param name: string - Name of service
:param service: string - Name service uses in init system
:returns: None
""" """
resource_dict = self.get_local('resources') resource_dict = self.get_local('resources')
if resource_dict: if resource_dict:
resources=relations.hacluster.common.CRM(**resource_dict) resources = relations.hacluster.common.CRM(**resource_dict)
else: else:
resources=relations.hacluster.common.CRM() resources = relations.hacluster.common.CRM()
resources.add( resources.add(
relations.hacluster.common.InitService( relations.hacluster.common.InitService(name, service,))
name,
service,))
self.set_local(resources=resources) self.set_local(resources=resources)