charm-neutron-openvswitch/hooks/neutron_ovs_hooks.py

147 lines
3.6 KiB
Python
Executable File

#!/usr/bin/python
import sys
from charmhelpers.contrib.openstack.utils import (
config_value_changed,
git_install_requested,
)
from charmhelpers.core.hookenv import (
Hooks,
UnregisteredHookError,
config,
log,
relation_set,
relation_ids,
)
from charmhelpers.core.host import (
restart_on_change
)
from charmhelpers.fetch import (
apt_install, apt_update, apt_purge
)
from charmhelpers.contrib.openstack.utils import (
os_requires_version,
)
from neutron_ovs_utils import (
DVR_PACKAGES,
configure_ovs,
determine_packages,
git_install,
get_topics,
determine_dvr_packages,
get_shared_secret,
register_configs,
restart_map,
use_dvr,
enable_metadata,
)
hooks = Hooks()
CONFIGS = register_configs()
@hooks.hook()
def install():
apt_update()
pkgs = determine_packages()
for pkg in pkgs:
apt_install(pkg, fatal=True)
git_install(config('openstack-origin-git'))
@hooks.hook('neutron-plugin-relation-changed')
@hooks.hook('config-changed')
@restart_on_change(restart_map())
def config_changed():
if determine_dvr_packages():
apt_update()
apt_install(determine_dvr_packages(), fatal=True)
if git_install_requested():
if config_value_changed('openstack-origin-git'):
git_install(config('openstack-origin-git'))
configure_ovs()
CONFIGS.write_all()
for rid in relation_ids('zeromq-configuration'):
zeromq_configuration_relation_joined(rid)
for rid in relation_ids('neutron-plugin'):
neutron_plugin_joined(relation_id=rid)
@hooks.hook('neutron-plugin-api-relation-changed')
@restart_on_change(restart_map())
def neutron_plugin_api_changed():
if use_dvr():
apt_update()
apt_install(DVR_PACKAGES, fatal=True)
else:
apt_purge(DVR_PACKAGES, fatal=True)
configure_ovs()
CONFIGS.write_all()
# If dvr setting has changed, need to pass that on
for rid in relation_ids('neutron-plugin'):
neutron_plugin_joined(relation_id=rid)
@hooks.hook('neutron-plugin-relation-joined')
def neutron_plugin_joined(relation_id=None):
print "Enable metadata: {}".format(enable_metadata())
if config('enable-local-dhcp-and-metadata'):
apt_install(['neutron-metadata-agent', 'neutron-dhcp-agent'] fatal=True)
secret = get_shared_secret() if enable_metadata() else None
rel_data = {
'metadata-shared-secret': secret,
}
print rel_data
relation_set(relation_id=relation_id, **rel_data)
@hooks.hook('amqp-relation-joined')
def amqp_joined(relation_id=None):
relation_set(relation_id=relation_id,
username=config('rabbit-user'),
vhost=config('rabbit-vhost'))
@hooks.hook('amqp-relation-changed')
@hooks.hook('amqp-relation-departed')
@restart_on_change(restart_map())
def amqp_changed():
if 'amqp' not in CONFIGS.complete_contexts():
log('amqp relation incomplete. Peer not ready?')
return
CONFIGS.write_all()
@hooks.hook('zeromq-configuration-relation-joined')
@os_requires_version('kilo', 'neutron-common')
def zeromq_configuration_relation_joined(relid=None):
relation_set(relation_id=relid,
topics=" ".join(get_topics()),
users="neutron")
@hooks.hook('zeromq-configuration-relation-changed')
@restart_on_change(restart_map(), stopstart=True)
def zeromq_configuration_relation_changed():
CONFIGS.write_all()
def main():
try:
hooks.execute(sys.argv)
except UnregisteredHookError as e:
log('Unknown hook {} - skipping.'.format(e))
if __name__ == '__main__':
main()