charm-nova-compute/hooks/nova_compute_hooks.py

206 lines
5.8 KiB
Python
Raw Normal View History

2013-07-18 19:37:30 -07:00
#!/usr/bin/python
2013-07-30 20:39:44 -07:00
import sys
2013-07-18 19:37:30 -07:00
from charmhelpers.core.hookenv import (
Hooks,
config,
log,
relation_ids,
relation_get,
2013-07-18 19:37:30 -07:00
relation_set,
service_name,
unit_get,
2013-07-30 20:39:44 -07:00
UnregisteredHookError,
2013-07-18 19:37:30 -07:00
)
from charmhelpers.core.host import (
apt_install,
apt_update,
filter_installed_packages,
2013-07-18 19:37:30 -07:00
restart_on_change,
)
from charmhelpers.contrib.openstack.utils import (
configure_installation_source,
openstack_upgrade_available,
)
2013-08-12 14:48:24 -07:00
from charmhelpers.contrib.openstack.neutron import neutron_plugin_attribute
2013-07-18 19:37:30 -07:00
from nova_compute_utils import (
create_libvirt_secret,
determine_packages,
2013-07-18 19:37:30 -07:00
import_authorized_keys,
import_keystone_ca_cert,
2013-07-30 20:39:44 -07:00
initialize_ssh_keys,
2013-07-18 19:37:30 -07:00
migration_enabled,
2013-08-12 14:48:24 -07:00
network_manager,
neutron_plugin,
2013-07-18 19:37:30 -07:00
do_openstack_upgrade,
public_ssh_key,
restart_map,
2013-07-18 19:37:30 -07:00
register_configs,
)
from nova_compute_context import CEPH_SECRET_UUID
2013-07-18 19:37:30 -07:00
from misc_utils import (
ensure_ceph_keyring,
)
hooks = Hooks()
CONFIGS = register_configs()
@hooks.hook()
def install():
configure_installation_source(config('openstack-origin'))
apt_update()
apt_install(determine_packages(), fatal=True)
2013-07-18 19:37:30 -07:00
@hooks.hook('config-changed')
@restart_on_change(restart_map())
2013-07-18 19:37:30 -07:00
def config_changed():
if openstack_upgrade_available('nova-common'):
do_openstack_upgrade(CONFIGS)
2013-07-18 19:37:30 -07:00
if migration_enabled() and config('migration-auth-type') == 'ssh':
# Check-in with nova-c-c and register new ssh key, if it has just been
# generated.
2013-07-30 20:39:44 -07:00
initialize_ssh_keys()
2013-07-18 19:37:30 -07:00
[compute_joined(rid) for rid in relation_ids('cloud-compute')]
CONFIGS.write_all()
2013-07-18 19:37:30 -07:00
@hooks.hook('amqp-relation-joined')
@restart_on_change(restart_map())
2013-07-18 19:37:30 -07:00
def amqp_joined():
relation_set(username=config('rabbit-user'), vhost=config('rabbit-vhost'))
@hooks.hook('amqp-relation-changed')
@restart_on_change(restart_map())
2013-07-18 19:37:30 -07:00
def amqp_changed():
if 'amqp' not in CONFIGS.complete_contexts():
log('amqp relation incomplete. Peer not ready?')
return
CONFIGS.write('/etc/nova/nova.conf')
2013-08-12 14:48:24 -07:00
if network_manager() == 'quantum':
2013-07-18 19:37:30 -07:00
CONFIGS.write('/etc/quantum/quantum.conf')
2013-08-12 14:48:24 -07:00
if network_manager() == 'neutron':
CONFIGS.write('/etc/neutron/neutron.conf')
2013-07-18 19:37:30 -07:00
@hooks.hook('shared-db-relation-joined')
def db_joined(rid=None):
relation_set(relation_id=rid,
nova_database=config('database'),
nova_username=config('database-user'),
nova_hostname=unit_get('private-address'))
2013-08-13 15:03:53 -07:00
if network_manager() in ['quantum', 'neutron']:
# XXX: Renaming relations from quantum_* to neutron_* here.
relation_set(relation_id=rid,
neutron_database=config('neutron-database'),
2013-08-13 15:03:53 -07:00
neutron_username=config('neutron-database-user'),
neutron_hostname=unit_get('private-address'))
2013-07-18 19:37:30 -07:00
@hooks.hook('shared-db-relation-changed')
@restart_on_change(restart_map())
2013-07-18 19:37:30 -07:00
def db_changed():
if 'shared-db' not in CONFIGS.complete_contexts():
log('shared-db relation incomplete. Peer not ready?')
return
CONFIGS.write('/etc/nova/nova.conf')
2013-08-12 14:48:24 -07:00
nm = network_manager()
if nm in ['quantum', 'neutron']:
plugin = neutron_plugin()
CONFIGS.write(neutron_plugin_attribute(plugin, 'config', nm))
2013-07-18 19:37:30 -07:00
@hooks.hook('image-service-relation-changed')
@restart_on_change(restart_map())
2013-07-18 19:37:30 -07:00
def image_service_changed():
if 'image-service' not in CONFIGS.complete_contexts():
log('image-service relation incomplete. Peer not ready?')
return
CONFIGS.write('/etc/nova/nova.conf')
@hooks.hook('cloud-compute-relation-joined')
def compute_joined(rid=None):
if not migration_enabled():
return
auth_type = config('migration-auth-type')
settings = {
'migration_auth_type': auth_type
}
if auth_type == 'ssh':
settings['ssh_public_key'] = public_ssh_key()
relation_set(relation_id=rid, **settings)
@hooks.hook('cloud-compute-relation-changed')
@restart_on_change(restart_map())
2013-07-18 19:37:30 -07:00
def compute_changed():
# rewriting all configs to pick up possible net or vol manager
# config advertised from controller.
CONFIGS.write_all()
2013-07-18 19:37:30 -07:00
import_authorized_keys()
import_keystone_ca_cert()
if network_manager() in ['quantum', 'neutron']:
# in case we already have a database relation, need to request
# access to the additional neutron database.
[db_joined(rid) for rid in relation_ids('shared-db')]
2013-07-18 19:37:30 -07:00
@hooks.hook('ceph-relation-joined')
@restart_on_change(restart_map())
2013-07-18 19:37:30 -07:00
def ceph_joined():
apt_install(filter_installed_packages(['ceph-common']), fatal=True)
2013-07-18 19:37:30 -07:00
@hooks.hook('ceph-relation-changed')
@restart_on_change(restart_map())
2013-07-18 19:37:30 -07:00
def ceph_changed():
if 'ceph' not in CONFIGS.complete_contexts():
log('ceph relation incomplete. Peer not ready?')
return
svc = service_name()
if not ensure_ceph_keyring(service=svc):
log('Could not create ceph keyring: peer not ready?')
return
CONFIGS.write('/etc/ceph/ceph.conf')
CONFIGS.write('/etc/ceph/secret.xml')
CONFIGS.write('/etc/nova/nova.conf')
2013-07-30 20:39:44 -07:00
# With some refactoring, this can move into NovaComputeCephContext
# and allow easily extended to support other compute flavors.
if config('virt-type') in ['kvm', 'qemu', 'lxc']:
create_libvirt_secret(secret_file='/etc/ceph/secret.xml',
secret_uuid=CEPH_SECRET_UUID,
key=relation_get('key'))
2013-07-30 20:39:44 -07:00
2013-08-20 12:10:36 -07:00
@hooks.hook('amqp-relation-broken',
'ceph-relation-broken',
'image-service-relation-broken',
'shared-db-relation-broken')
@restart_on_change(restart_map())
def relation_broken():
CONFIGS.write_all()
2013-07-30 20:39:44 -07:00
def main():
try:
hooks.execute(sys.argv)
except UnregisteredHookError as e:
log('Unknown hook {} - skipping.'.format(e))
if __name__ == '__main__':
main()