Charm Interface - HA Cluster
Go to file
Liam Young 67511b3787 Resource parameter order is important on Eoan+
Port of https://github.com/juju/charm-helpers/pull/373 to this
interface for reactive charms. From the ch pull request:

On Eoan we saw errors like:

ERROR: syntax in primitive: Attribute order error: timeout must appear
before any instance attribute parsing 'primitive res_ks_cf9dea1_vip
ocfheartbeatIPaddr2 params ip=10.5.253.30 op monitor depth=0
timeout=20s interval=10s'

It would appear, that ordering matters, update resource config function
to the correct order.

Change-Id: I1f8a440fb0ad62192307946de42b9b176b3ef4c1
Partial-Bug: #1843830
2020-04-22 08:54:02 +00:00
unit_tests Resource parameter order is important on Eoan+ 2020-04-22 08:54:02 +00:00
.gitignore Add unit tests and fix for _parse bug exposed 2018-12-17 16:28:35 +00:00
.gitreview OpenDev Migration Patch 2019-04-19 19:48:31 +00:00
.testr.conf Add unit tests and fix for _parse bug exposed 2018-12-17 16:28:35 +00:00
.zuul.yaml Switch to Ussuri jobs 2019-10-31 17:51:43 +08:00
README.md Add .gitreview, update tox.ini, update readme. 2016-07-06 12:56:24 +00:00
common.py Resource parameter order is important on Eoan+ 2020-04-22 08:54:02 +00:00
copyright First commit of the proposed hacluster interface. 2016-01-05 21:22:27 -07:00
interface.yaml Add interface build ignore rules 2018-12-18 09:50:44 +01:00
requires.py Control access to crm in cases when the unit is yet not 2020-03-03 19:43:50 +02:00
test-requirements.txt Add unit tests and fix for _parse bug exposed 2018-12-17 16:28:35 +00:00
tox.ini Avoid pollution and false positives with tox.ini 2019-08-27 08:54:47 -04:00

README.md

Overview

This interface handles the communication with the hacluster subordinate charm using the ha interface protocol.

Usage

Requires

The interface layer will set the following reactive states, as appropriate:

  • {relation_name}.connected The relation is established and ready for the local charm to configure the hacluster subordinate charm. The configuration of the resources to manage for the hacluster charm can be managed via one of the following methods:

    • manage_resources method
    • bind_on method

    Configuration of the managed resources within the hacluster can be managed by passing common.CRM object definitions to the manage_resources method.

  • {relation_name}.available The hacluster is up and ready.

For example:

from charms.reactive import when, when_not
from charms.reactive import set_state, remove_state

from relations.hacluster.common import CRM


@when('ha.connected')
def cluster_connected(hacluster):

    resources = CRM()
    resources.primitive('res_vip', 'ocf:IPAddr2',
                        params='ip=10.0.3.100 nic=eth0',
                        op='monitor interval="10s"')
    resources.clone('cl_res_vip', 'res_vip')

    hacluster.bind_on(iface='eth0', mcastport=4430)
    hacluster.manage_resources(resources)

Additionally, for more code clarity a custom object implements the interface defined in common.ResourceDescriptor can be used to simplify the code for reuse.

For example:

import ipaddress

from relation.hacluster.common import CRM
from relation.hacluster.common import ResourceDescriptor

class VirtualIP(ResourceDescriptor):
    def __init__(self, vip, nic='eth0'):
        self.vip = vip
        self.nic = 'eth0'

    def configure_resource(self, crm):
        ipaddr = ipaddress.ip_address(self.vip)
        if isinstance(ipaddr, ipaddress.IPv4Address):
            res_type = 'ocf:heartbeat:IPAddr2'
            res_parms = 'ip={ip} nic={nic}'.format(ip=self.vip,
                                                   nic=self.nic)
        else:
            res_type = 'ocf:heartbeat:IPv6addr'
            res_params = 'ipv6addr={ip} nic={nic}'.format(ip=self.vip,
                                                          nic=self.nic)

        crm.primitive('res_vip', res_type, params=res_params,
                      op='monitor interval="10s"')
        crm.clone('cl_res_vip', 'res_vip')

Once the VirtualIP class above has been defined in charm code, it can make the code a bit cleaner. The example above can thusly be written as:

@when('ha.connected')
def cluster_connected(hacluster):
    resources = CRM()
    resources.add(VirtualIP('10.0.3.100'))

    hacluster.bind_on(iface='eth0', mcastport=4430)
    hacluster.manage_resources(resources)