113 lines
3.8 KiB
Python
113 lines
3.8 KiB
Python
#!/usr/bin/python
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import relations.hacluster.common
|
|
from charms.reactive import hook
|
|
from charms.reactive import RelationBase
|
|
from charms.reactive import scopes
|
|
|
|
|
|
class HAClusterRequires(RelationBase):
|
|
# The hacluster charm is a subordinate charm and really only works
|
|
# for a single service to the HA Cluster relation, therefore set the
|
|
# expected scope to be GLOBAL.
|
|
scope = scopes.GLOBAL
|
|
|
|
@hook('{requires:hacluster}-relation-joined')
|
|
def joined(self):
|
|
self.set_state('{relation_name}.connected')
|
|
|
|
@hook('{requires:hacluster}-relation-changed')
|
|
def changed(self):
|
|
self.set_state('{relation_name}.available')
|
|
|
|
@hook('{requires:hacluster}-relation-{broken,departed}')
|
|
def departed(self):
|
|
self.remove_state('{relation_name}.available')
|
|
self.remove_state('{relation_name}.connected')
|
|
|
|
def bind_on(self, iface=None, mcastport=None):
|
|
relation_data = {}
|
|
if iface:
|
|
relation_data['corosync_bindiface'] = iface
|
|
if mcastport:
|
|
relation_data['corosync_mcastport'] = mcastport
|
|
|
|
if relation_data:
|
|
self.set_local(**relation_data)
|
|
self.set_remote(**relation_data)
|
|
|
|
def manage_resources(self, crm):
|
|
"""
|
|
Request for the hacluster to manage the resources defined in the
|
|
crm object.
|
|
|
|
res = CRM()
|
|
res.primitive('res_neutron_haproxy', 'lsb:haproxy',
|
|
op='monitor interval="5s"')
|
|
res.init_services('haproxy')
|
|
res.clone('cl_nova_haproxy', 'res_neutron_haproxy')
|
|
|
|
hacluster.manage_resources(crm)
|
|
|
|
"""
|
|
relation_data = {k: v for k, v in crm.items() if v}
|
|
print(relation_data)
|
|
self.set_local(**relation_data)
|
|
self.set_remote(**relation_data)
|
|
|
|
|
|
def bind_resources(self, iface, mcastport=None):
|
|
"""Inform the ha subordinate about each service it should manage. The
|
|
child class specifies the services via self.ha_resources
|
|
|
|
@param hacluster interface
|
|
"""
|
|
if not mcastport:
|
|
mcastport=4440
|
|
resources = self.get_local('resources')
|
|
self.bind_on(iface=iface, mcastport=mcastport)
|
|
self.manage_resources(resources)
|
|
|
|
|
|
def add_vip(self, name, vip, iface, netmask):
|
|
"""Add a VirtualIP object for each user specified vip to self.resources
|
|
"""
|
|
resource_dict = self.get_local('resources')
|
|
if resource_dict:
|
|
resources=relations.hacluster.common.CRM(**resource_dict)
|
|
else:
|
|
resources=relations.hacluster.common.CRM()
|
|
resources.add(
|
|
relations.hacluster.common.VirtualIP(
|
|
name,
|
|
vip,
|
|
nic=iface,
|
|
cidr=netmask,))
|
|
self.set_local(resources=resources)
|
|
|
|
|
|
def add_init_service(self, name, service):
|
|
"""Add a InitService object for haproxy to self.resources
|
|
"""
|
|
resource_dict = self.get_local('resources')
|
|
if resource_dict:
|
|
resources=relations.hacluster.common.CRM(**resource_dict)
|
|
else:
|
|
resources=relations.hacluster.common.CRM()
|
|
resources.add(
|
|
relations.hacluster.common.InitService(
|
|
name,
|
|
service,))
|
|
self.set_local(resources=resources)
|