charm-octavia/src/reactive/octavia_handlers.py

106 lines
3.6 KiB
Python

# Copyright 2018 Canonical Ltd
#
# 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 uuid
import charms.reactive as reactive
import charms.leadership as leadership
import charms_openstack.charm as charm
import charms_openstack.ip as os_ip
import charmhelpers.core as ch_core
import charm.openstack.octavia as octavia # noqa
import charm.openstack.api_crud as api_crud
charm.use_defaults(
'charm.installed',
'amqp.connected',
'shared-db.connected',
'identity-service.connected',
'identity-service.available',
'config.changed',
'update-status')
@reactive.when('leadership.is_leader')
@reactive.when_not('leadership.set.heartbeat-key')
def generate_heartbeat_key():
"""Generate a unique key for ``heartbeat_key`` configuration option."""
leadership.leader_set({'heartbeat-key': str(uuid.uuid4())})
@reactive.when('neutron-load-balancer.available')
def setup_neutron_lbaas_proxy():
neutron = reactive.endpoint_from_flag('neutron-load-balancer.available')
with charm.provide_charm_instance() as octavia_charm:
octavia_url = '{}:{}'.format(
os_ip.canonical_url(endpoint_type=os_ip.INTERNAL),
octavia_charm.api_port('octavia-api'))
neutron.publish_load_balancer_info('octavia', octavia_url)
@reactive.when('leadership.is_leader')
@reactive.when('identity-service.available')
@reactive.when('config.default.custom-amp-flavor-id')
def get_nova_flavor():
"""Get or create private Nova flavor for use with Octavia."""
identity_service = reactive.endpoint_from_flag(
'identity-service.available')
try:
flavor = api_crud.get_nova_flavor(identity_service)
except api_crud.APIUnavailable as e:
ch_core.hookenv.log('Nova API not available yet, deferring '
'flavor discovery/creation. ("{}")'
.format(e),
level=ch_core.hookenv.DEBUG)
else:
leadership.leader_set({'amp-flavor-id': flavor.id})
@reactive.when('shared-db.available')
@reactive.when('identity-service.available')
@reactive.when('amqp.available')
@reactive.when('leadership.set.heartbeat-key')
def render(*args):
"""
Render the configuration for Octavia when all interfaces are available.
"""
with charm.provide_charm_instance() as octavia_charm:
octavia_charm.render_with_interfaces(args)
octavia_charm.enable_webserver_site()
octavia_charm.assess_status()
reactive.set_state('config.rendered')
@reactive.when_not('db.synced')
@reactive.when('config.rendered')
def init_db():
"""Run initial DB migrations when config is rendered."""
with charm.provide_charm_instance() as octavia_charm:
octavia_charm.db_sync()
octavia_charm.restart_all()
reactive.set_state('db.synced')
octavia_charm.assess_status()
@reactive.when('ha.connected')
@reactive.when_not('ha.available')
def cluster_connected(hacluster):
"""Configure HA resources in corosync."""
with charm.provide_charm_instance() as octavia_charm:
octavia_charm.configure_ha_resources(hacluster)
octavia_charm.assess_status()