charm-trilio-dm-api/src/reactive/dmapi_handlers.py

83 lines
2.7 KiB
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 charms_openstack.charm as charm
import charms.reactive as reactive
# This charm's library contains all of the handler code associated with
# dmapi
import charm.openstack.dmapi as dmapi # noqa
charm.use_defaults(
"charm.installed",
"amqp.connected",
"shared-db.connected",
"identity-service.connected",
"identity-service.available", # enables SSL support
"config.changed",
"update-status",
"certificates.available",
"cluster.available",
)
@reactive.when("dmapi-db.ready")
@reactive.when("shared-db.available")
@reactive.when("identity-service.available")
@reactive.when("amqp.available")
def render_config(*args):
"""Render the configuration for charm when all the interfaces are
available.
"""
with charm.provide_charm_instance() as charm_class:
charm_class.upgrade_if_available(args)
with charm.provide_charm_instance() as charm_class:
charm_class.render_with_interfaces(args)
charm_class.assess_status()
reactive.set_state("config.rendered")
@reactive.when("config.rendered")
def init_db():
"""Perform DB Sync"""
with charm.provide_charm_instance() as charm_class:
charm_class.db_sync()
@reactive.when("ha.connected")
def cluster_connected(hacluster):
"""Configure HA resources in corosync"""
with charm.provide_charm_instance() as charm_class:
charm_class.configure_ha_resources(hacluster)
charm_class.assess_status()
@reactive.when("shared-db.available")
@reactive.when_not("dmapi-db.ready")
def check_dmapi_db():
db_ep = reactive.endpoint_from_flag('shared-db.available')
if db_ep:
if db_ep.password(prefix='dmapi'):
reactive.set_state("dmapi-db.ready")
else:
with charm.provide_charm_instance() as instance:
for db in instance.get_database_setup():
db_ep.configure(**db)
@reactive.when_any("config.changed.triliovault-pkg-source",
"config.changed.openstack-origin")
def install_source_changed():
"""Trigger re-install of charm if source configuration options change"""
reactive.clear_flag("charm.installed")
reactive.set_flag("upgrade.triliovault")