charm-nova-compute/hooks/nova_compute_hooks.py

467 lines
14 KiB
Python
Raw Normal View History

2015-09-22 15:19:29 +01:00
#!/usr/bin/python
import platform
2013-07-30 20:39:44 -07:00
import sys
import uuid
2013-07-18 19:37:30 -07:00
from charmhelpers.core.hookenv import (
Hooks,
config,
2014-03-25 10:12:03 +01:00
is_relation_made,
2013-07-18 19:37:30 -07:00
log,
2014-03-25 10:12:03 +01:00
ERROR,
2013-07-18 19:37:30 -07:00
relation_ids,
related_units,
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,
2015-09-25 15:12:01 -07:00
status_set,
2013-07-18 19:37:30 -07:00
)
from charmhelpers.core.host import (
service_restart,
2013-09-20 17:40:54 +01:00
)
from charmhelpers.core.strutils import (
bool_from_string,
)
2013-09-20 17:40:54 +01:00
from charmhelpers.fetch import (
2013-07-18 19:37:30 -07:00
apt_install,
apt_purge,
2013-07-18 19:37:30 -07:00
apt_update,
filter_installed_packages,
2013-07-18 19:37:30 -07:00
)
from charmhelpers.contrib.openstack.utils import (
2015-04-15 14:21:42 +00:00
config_value_changed,
2013-07-18 19:37:30 -07:00
configure_installation_source,
2015-04-15 14:21:42 +00:00
git_install_requested,
2013-07-18 19:37:30 -07:00
openstack_upgrade_available,
os_requires_version,
is_unit_paused_set,
pausable_restart_on_change as restart_on_change,
2013-07-18 19:37:30 -07:00
)
2014-11-19 17:53:16 -06:00
from charmhelpers.contrib.storage.linux.ceph import (
ensure_ceph_keyring,
CephBrokerRq,
delete_keyring,
send_request_if_needed,
2015-09-07 09:43:08 +01:00
is_request_complete,
2014-11-19 17:53:16 -06:00
)
2013-09-27 17:20:42 +01:00
from charmhelpers.payload.execd import execd_preinstall
2013-07-18 19:37:30 -07:00
from nova_compute_utils import (
create_libvirt_secret,
determine_packages,
2015-04-15 14:21:42 +00:00
git_install,
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,
do_openstack_upgrade,
public_ssh_key,
restart_map,
services,
2013-07-18 19:37:30 -07:00
register_configs,
NOVA_CONF,
ceph_config_file, CEPH_SECRET,
enable_shell, disable_shell,
2015-02-11 13:20:35 -05:00
configure_lxd,
2014-05-06 21:58:56 +00:00
fix_path_ownership,
2014-09-09 10:56:37 +00:00
get_topics,
2014-10-15 06:51:47 +00:00
assert_charm_supports_ipv6,
2015-08-13 10:11:23 +01:00
install_hugepages,
get_hugepage_number,
assess_status,
set_ppc64_cpu_smt_state,
2013-07-18 19:37:30 -07:00
)
2014-09-17 14:49:43 +08:00
from charmhelpers.contrib.network.ip import (
2014-09-20 00:14:00 +08:00
get_ipv6_addr
2013-07-18 19:37:30 -07:00
)
from charmhelpers.core.unitdata import kv
2014-11-11 22:28:08 +00:00
from nova_compute_context import (
CEPH_SECRET_UUID,
assert_libvirt_imagebackend_allowed
2014-11-11 22:28:08 +00:00
)
2015-01-12 12:04:00 +00:00
from charmhelpers.contrib.charmsupport import nrpe
2014-11-26 10:39:42 -03:00
from charmhelpers.core.sysctl import create as create_sysctl
from charmhelpers.contrib.hardening.harden import harden
2014-11-26 10:39:42 -03:00
from socket import gethostname
2013-07-18 19:37:30 -07:00
hooks = Hooks()
CONFIGS = register_configs()
@hooks.hook('install.real')
@harden()
2013-07-18 19:37:30 -07:00
def install():
2015-09-25 15:12:01 -07:00
status_set('maintenance', 'Executing pre-install')
2013-09-27 17:20:42 +01:00
execd_preinstall()
2013-07-18 19:37:30 -07:00
configure_installation_source(config('openstack-origin'))
2015-04-15 14:21:42 +00:00
2015-09-25 15:12:01 -07:00
status_set('maintenance', 'Installing apt packages')
2013-07-18 19:37:30 -07:00
apt_update()
apt_install(determine_packages(), fatal=True)
2013-07-18 19:37:30 -07:00
2015-09-25 15:12:01 -07:00
status_set('maintenance', 'Git install')
2015-04-15 14:21:42 +00:00
git_install(config('openstack-origin-git'))
2013-07-18 19:37:30 -07:00
@hooks.hook('config-changed')
@restart_on_change(restart_map())
@harden()
2013-07-18 19:37:30 -07:00
def config_changed():
if config('prefer-ipv6'):
2015-09-25 15:12:01 -07:00
status_set('maintenance', 'configuring ipv6')
2014-09-26 19:20:20 +01:00
assert_charm_supports_ipv6()
2014-04-01 17:01:37 +01:00
global CONFIGS
send_remote_restart = False
2015-04-15 14:21:42 +00:00
if git_install_requested():
if config_value_changed('openstack-origin-git'):
2015-09-25 15:12:01 -07:00
status_set('maintenance', 'Running Git install')
2015-04-15 14:21:42 +00:00
git_install(config('openstack-origin-git'))
elif not config('action-managed-upgrade'):
2015-04-15 14:21:42 +00:00
if openstack_upgrade_available('nova-common'):
2015-09-25 15:12:01 -07:00
status_set('maintenance', 'Running openstack upgrade')
do_openstack_upgrade(CONFIGS)
send_remote_restart = True
2013-07-18 19:37:30 -07:00
2014-11-26 10:39:42 -03:00
sysctl_dict = config('sysctl')
if sysctl_dict:
2014-12-10 15:52:15 -03:00
create_sysctl(sysctl_dict, '/etc/sysctl.d/50-nova-compute.conf')
2014-11-26 10:39:42 -03:00
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.
2015-09-25 15:12:01 -07:00
status_set('maintenance', 'SSH key exchange')
2013-07-30 20:39:44 -07:00
initialize_ssh_keys()
2014-04-03 17:18:05 +01:00
import_authorized_keys()
if config('enable-resize') is True:
enable_shell(user='nova')
2015-09-25 15:12:01 -07:00
status_set('maintenance', 'SSH key exchange')
initialize_ssh_keys(user='nova')
import_authorized_keys(user='nova', prefix='nova')
else:
disable_shell(user='nova')
2014-03-31 16:30:14 +01:00
if config('instances-path') is not None:
fp = config('instances-path')
fix_path_ownership(fp, user='nova')
[compute_joined(rid) for rid in relation_ids('cloud-compute')]
for rid in relation_ids('zeromq-configuration'):
zeromq_configuration_relation_joined(rid)
2013-07-18 19:37:30 -07:00
for rid in relation_ids('neutron-plugin'):
neutron_plugin_joined(rid, remote_restart=send_remote_restart)
2015-04-24 10:43:58 -03:00
if is_relation_made("nrpe-external-master"):
update_nrpe_config()
2014-10-29 22:30:36 -05:00
if config('hugepages'):
install_hugepages()
2015-08-13 10:11:23 +01:00
# Disable smt for ppc64, required for nova/libvirt/kvm
arch = platform.machine()
log('CPU architecture: {}'.format(arch))
if arch in ['ppc64el', 'ppc64le']:
set_ppc64_cpu_smt_state('off')
if (config('libvirt-image-backend') == 'rbd' and
assert_libvirt_imagebackend_allowed()):
for rid in relation_ids('ceph'):
for unit in related_units(rid):
ceph_changed(rid=rid, unit=unit)
CONFIGS.write_all()
2013-07-18 19:37:30 -07:00
@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'))
2013-07-18 19:37:30 -07:00
@hooks.hook('amqp-relation-changed')
2014-01-30 12:46:46 +01:00
@hooks.hook('amqp-relation-departed')
@restart_on_change(restart_map())
2014-02-13 09:49:17 +01:00
def amqp_changed():
2014-01-30 12:46:46 +01:00
if 'amqp' not in CONFIGS.complete_contexts():
log('amqp relation incomplete. Peer not ready?')
return
CONFIGS.write(NOVA_CONF)
2013-07-18 19:37:30 -07:00
@hooks.hook('shared-db-relation-joined')
def db_joined(rid=None):
2014-03-25 10:12:03 +01:00
if is_relation_made('pgsql-db'):
# error, postgresql is used
e = ('Attempting to associate a mysql database when there is already '
2014-03-25 10:35:58 +01:00
'associated a postgresql one')
2014-03-25 10:12:03 +01:00
log(e, level=ERROR)
raise Exception(e)
relation_set(relation_id=rid,
nova_database=config('database'),
nova_username=config('database-user'),
nova_hostname=unit_get('private-address'))
2013-07-18 19:37:30 -07:00
2014-03-25 11:27:22 +01:00
@hooks.hook('pgsql-db-relation-joined')
def pgsql_db_joined():
2014-03-25 10:12:03 +01:00
if is_relation_made('shared-db'):
# raise error
2014-04-10 17:09:53 +01:00
e = ('Attempting to associate a postgresql database when'
' there is already associated a mysql one')
2014-03-25 10:12:03 +01:00
log(e, level=ERROR)
raise Exception(e)
relation_set(database=config('database'))
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(NOVA_CONF)
2013-07-18 19:37:30 -07:00
2014-03-25 11:27:22 +01:00
@hooks.hook('pgsql-db-relation-changed')
@restart_on_change(restart_map())
def postgresql_db_changed():
if 'pgsql-db' not in CONFIGS.complete_contexts():
log('pgsql-db relation incomplete. Peer not ready?')
return
CONFIGS.write(NOVA_CONF)
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(NOVA_CONF)
2013-07-18 19:37:30 -07:00
@hooks.hook('cloud-compute-relation-joined')
def compute_joined(rid=None):
# NOTE(james-page) in MAAS environments the actual hostname is a CNAME
# record so won't get scanned based on private-address which is an IP
# add the hostname configured locally to the relation.
settings = {
'hostname': gethostname()
}
2014-09-17 14:49:43 +08:00
if config('prefer-ipv6'):
2014-09-20 00:14:00 +08:00
settings = {'private-address': get_ipv6_addr()[0]}
if migration_enabled():
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)
if config('enable-resize'):
settings['nova_ssh_public_key'] = public_ssh_key(user='nova')
relation_set(relation_id=rid, **settings)
2013-07-18 19:37:30 -07:00
@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_authorized_keys(user='nova', prefix='nova')
2013-07-18 19:37:30 -07:00
import_keystone_ca_cert()
@hooks.hook('ceph-relation-joined')
@restart_on_change(restart_map())
2013-07-18 19:37:30 -07:00
def ceph_joined():
2015-09-25 15:12:01 -07:00
status_set('maintenance', 'Installing apt packages')
apt_install(filter_installed_packages(['ceph-common']), fatal=True)
# Bug 1427660
if not is_unit_paused_set():
service_restart('libvirt-bin')
2013-07-18 19:37:30 -07:00
def get_ceph_request():
rq = CephBrokerRq()
replicas = config('ceph-osd-replication-count')
rq.add_op_create_pool(name=config('rbd-pool'), replica_count=replicas)
return rq
2013-07-18 19:37:30 -07:00
@hooks.hook('ceph-relation-changed')
@restart_on_change(restart_map())
def ceph_changed(rid=None, unit=None):
2013-07-18 19:37:30 -07:00
if 'ceph' not in CONFIGS.complete_contexts():
log('ceph relation incomplete. Peer not ready?')
return
if not ensure_ceph_keyring(service=service_name(), user='nova',
group='nova'):
2013-07-18 19:37:30 -07:00
log('Could not create ceph keyring: peer not ready?')
return
2013-07-30 20:39:44 -07:00
CONFIGS.write(ceph_config_file())
CONFIGS.write(CEPH_SECRET)
CONFIGS.write(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.
key = relation_get(attribute='key', rid=rid, unit=unit)
if config('virt-type') in ['kvm', 'qemu', 'lxc'] and key:
create_libvirt_secret(secret_file=CEPH_SECRET,
secret_uuid=CEPH_SECRET_UUID, key=key)
2014-11-11 22:28:08 +00:00
if (config('libvirt-image-backend') == 'rbd' and
assert_libvirt_imagebackend_allowed()):
2015-09-07 09:43:08 +01:00
if is_request_complete(get_ceph_request()):
log('Request complete')
# Ensure that nova-compute is restarted since only now can we
# guarantee that ceph resources are ready, but only if not paused.
if not is_unit_paused_set():
service_restart('nova-compute')
else:
send_request_if_needed(get_ceph_request())
2013-07-30 20:39:44 -07:00
@hooks.hook('ceph-relation-broken')
def ceph_broken():
service = service_name()
delete_keyring(service=service)
CONFIGS.write_all()
2013-08-20 12:10:36 -07:00
@hooks.hook('amqp-relation-broken',
'image-service-relation-broken',
2014-03-25 10:12:03 +01:00
'shared-db-relation-broken',
'pgsql-db-relation-broken')
2013-08-20 12:10:36 -07:00
@restart_on_change(restart_map())
def relation_broken():
CONFIGS.write_all()
@hooks.hook('upgrade-charm')
@harden()
def upgrade_charm():
2015-08-13 10:16:35 +01:00
# NOTE: ensure psutil install for hugepages configuration
2015-09-25 15:12:01 -07:00
status_set('maintenance', 'Installing apt packages')
2015-08-13 10:16:35 +01:00
apt_install(filter_installed_packages(['python-psutil']))
for r_id in relation_ids('amqp'):
amqp_joined(relation_id=r_id)
2015-04-24 10:43:58 -03:00
if is_relation_made('nrpe-external-master'):
update_nrpe_config()
@hooks.hook('nova-ceilometer-relation-changed')
@restart_on_change(restart_map())
def nova_ceilometer_relation_changed():
2013-11-29 15:57:59 +00:00
CONFIGS.write_all()
2014-09-09 09:47:14 +00:00
@hooks.hook('zeromq-configuration-relation-joined')
2015-03-16 16:14:33 +00:00
@os_requires_version('kilo', 'nova-common')
2014-09-09 09:47:14 +00:00
def zeromq_configuration_relation_joined(relid=None):
2014-09-09 09:58:06 +00:00
relation_set(relation_id=relid,
2014-09-09 10:56:37 +00:00
topics=" ".join(get_topics()),
2014-09-09 09:58:06 +00:00
users="nova")
2014-09-09 09:47:14 +00:00
@hooks.hook('zeromq-configuration-relation-changed')
@restart_on_change(restart_map())
2014-09-09 09:47:14 +00:00
def zeromq_configuration_relation_changed():
CONFIGS.write(NOVA_CONF)
@hooks.hook('nrpe-external-master-relation-joined',
'nrpe-external-master-relation-changed')
2014-10-29 22:30:36 -05:00
def update_nrpe_config():
2015-01-12 12:04:00 +00:00
# python-dbus is used by check_upstart_job
2014-10-29 22:30:36 -05:00
apt_install('python-dbus')
2015-01-12 12:04:00 +00:00
hostname = nrpe.get_nagios_hostname()
current_unit = nrpe.get_nagios_unit_name()
nrpe_setup = nrpe.NRPE(hostname=hostname)
nrpe.add_init_service_checks(nrpe_setup, services(), current_unit)
nrpe_setup.write()
2014-10-29 22:30:36 -05:00
@hooks.hook('neutron-plugin-relation-joined')
def neutron_plugin_joined(relid=None, remote_restart=False):
rel_settings = {
'hugepage_number': get_hugepage_number()
}
if remote_restart:
rel_settings['restart-trigger'] = str(uuid.uuid4())
relation_set(relation_id=relid,
**rel_settings)
@hooks.hook('neutron-plugin-relation-changed')
@restart_on_change(restart_map())
def neutron_plugin_changed():
settings = relation_get()
if settings.get('enable-metadata'):
enable_metadata = bool_from_string(settings['enable-metadata'])
else:
enable_metadata = False
if 'metadata-shared-secret' in settings or enable_metadata:
apt_update()
apt_install(filter_installed_packages(['nova-api-metadata']),
fatal=True)
else:
apt_purge('nova-api-metadata', fatal=True)
CONFIGS.write(NOVA_CONF)
@hooks.hook('lxd-relation-joined')
def lxd_joined(relid=None):
relation_set(relation_id=relid,
user='nova')
@hooks.hook('lxd-relation-changed')
def lxc_changed():
nonce = relation_get('nonce')
db = kv()
if nonce and db.get('lxd-nonce') != nonce:
db.set('lxd-nonce', nonce)
configure_lxd(user='nova')
if not is_unit_paused_set():
service_restart('nova-compute')
2016-01-22 10:34:03 +00:00
@hooks.hook('nova-designate-relation-changed')
@restart_on_change(restart_map())
def designate_changed():
CONFIGS.write(NOVA_CONF)
@hooks.hook('update-status')
@harden()
def update_status():
log('Updating status.')
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))
assess_status(CONFIGS)
2013-07-30 20:39:44 -07:00
if __name__ == '__main__':
main()