1111 lines
37 KiB
Python
Raw Normal View History

2013-08-01 20:42:16 -07:00
#!/usr/bin/python
2013-08-15 14:57:06 -07:00
import os
import shutil
2013-08-01 20:42:16 -07:00
import sys
import uuid
2013-08-01 20:42:16 -07:00
from subprocess import (
check_call,
)
2013-08-01 20:42:16 -07:00
from urlparse import urlparse
from charmhelpers.core.hookenv import (
Hooks,
UnregisteredHookError,
config,
2013-08-15 14:57:06 -07:00
charm_dir,
2014-03-20 11:18:23 +01:00
is_relation_made,
2013-08-01 20:42:16 -07:00
log,
local_unit,
DEBUG,
2014-03-24 13:01:12 +01:00
ERROR,
2013-08-01 20:42:16 -07:00
relation_get,
relation_ids,
relation_set,
related_units,
2013-08-01 20:42:16 -07:00
open_port,
unit_get,
2015-10-07 09:32:28 +00:00
status_set,
2013-08-01 20:42:16 -07:00
)
from charmhelpers.core.host import (
restart_on_change,
service_running,
service_stop,
2015-03-30 18:26:30 +01:00
service_reload,
2014-09-26 15:46:53 +00:00
service_restart,
2013-09-20 17:29:50 +01:00
)
from charmhelpers.fetch import (
apt_install,
apt_update,
filter_installed_packages
2013-08-01 20:42:16 -07:00
)
from charmhelpers.contrib.openstack.utils import (
2015-04-15 14:17:56 +00:00
config_value_changed,
2013-08-01 20:42:16 -07:00
configure_installation_source,
2015-04-15 14:17:56 +00:00
git_install_requested,
2013-08-01 20:42:16 -07:00
openstack_upgrade_available,
os_release,
os_requires_version,
2015-10-07 09:32:28 +00:00
sync_db_with_multi_ipv6_addresses,
2015-10-08 14:31:01 +01:00
set_os_workload_status,
2013-08-01 20:42:16 -07:00
)
from charmhelpers.contrib.openstack.neutron import (
2013-08-12 13:55:41 -07:00
network_manager,
neutron_plugin_attribute,
)
from nova_cc_context import (
NeutronAPIContext,
NovaCellContext,
)
2014-08-11 15:37:20 +00:00
from charmhelpers.contrib.peerstorage import (
peer_retrieve,
peer_echo,
)
2013-08-01 20:42:16 -07:00
from nova_cc_utils import (
api_port,
2013-08-01 20:42:16 -07:00
auth_token_config,
cmd_all_services,
2013-08-01 20:42:16 -07:00
determine_endpoints,
determine_packages,
determine_ports,
disable_services,
2013-08-01 20:42:16 -07:00
do_openstack_upgrade,
enable_services,
2015-04-15 14:17:56 +00:00
git_install,
2013-08-01 20:42:16 -07:00
keystone_ca_cert_b64,
2014-09-26 15:46:53 +00:00
migrate_neutron_database,
migrate_nova_database,
neutron_plugin,
2013-08-01 20:42:16 -07:00
save_script_rc,
2014-09-09 13:01:37 +00:00
services,
2013-08-01 20:42:16 -07:00
ssh_compute_add,
2013-08-10 09:49:47 +01:00
ssh_compute_remove,
ssh_known_hosts_lines,
ssh_authorized_keys_lines,
2013-08-01 20:42:16 -07:00
register_configs,
restart_map,
volume_service,
CLUSTER_RES,
2013-09-25 14:12:21 +01:00
NOVA_CONF,
QUANTUM_CONF,
NEUTRON_CONF,
QUANTUM_API_PASTE,
2014-07-09 14:30:59 +01:00
console_attributes,
service_guard,
guard_map,
2014-09-09 13:01:37 +00:00
get_topics,
2014-10-15 06:49:03 +00:00
setup_ipv6,
2015-10-07 09:32:28 +00:00
REQUIRED_INTERFACES,
check_optional_relations,
is_db_initialised,
2013-08-01 20:42:16 -07:00
)
from charmhelpers.contrib.hahelpers.cluster import (
is_elected_leader,
2013-08-01 20:42:16 -07:00
get_hacluster_config,
2015-07-22 20:01:24 +08:00
https,
2013-08-01 20:42:16 -07:00
)
2013-09-27 17:18:25 +01:00
from charmhelpers.payload.execd import execd_preinstall
2014-07-04 12:49:08 +01:00
from charmhelpers.contrib.openstack.ip import (
canonical_url,
2015-07-22 13:33:03 +08:00
PUBLIC, INTERNAL, ADMIN,
resolve_address,
2014-07-04 12:49:08 +01:00
)
2013-09-27 17:18:25 +01:00
from charmhelpers.contrib.network.ip import (
format_ipv6_addr,
get_iface_for_address,
get_netmask_for_address,
get_address_in_network,
2014-09-30 14:48:02 +08:00
get_ipv6_addr,
2015-07-22 20:01:24 +08:00
is_ipv6,
)
from charmhelpers.contrib.openstack.context import ADDRESS_TYPES
2015-01-12 12:04:00 +00:00
from charmhelpers.contrib.charmsupport import nrpe
2014-10-29 22:30:36 -05:00
try:
FileNotFoundError
except NameError:
# python3 compatibility
FileNotFoundError = OSError
2013-08-01 20:42:16 -07:00
hooks = Hooks()
CONFIGS = register_configs()
COLO_CONSOLEAUTH = 'inf: res_nova_consoleauth grp_nova_vips'
AGENT_CONSOLEAUTH = 'ocf:openstack:nova-consoleauth'
AGENT_CA_PARAMS = 'op monitor interval="5s"'
NOVA_CONSOLEAUTH_OVERRIDE = '/etc/init/nova-consoleauth.override'
2013-08-01 20:42:16 -07:00
def leader_init_db_if_ready(skip_acl_check=False, skip_cells_restarts=False,
db_rid=None, unit=None):
"""Initialise db if leader and db not yet intialised.
NOTE: must be called from database context.
"""
if not is_elected_leader(CLUSTER_RES):
log("Not leader - skipping db init", level=DEBUG)
return
if is_db_initialised():
log("Database already initialised - skipping db init", level=DEBUG)
return
# Bugs 1353135 & 1187508. Dbs can appear to be ready before the units
# acl entry has been added. So, if the db supports passing a list of
# permitted units then check if we're in the list.
allowed_units = relation_get('nova_allowed_units', rid=db_rid, unit=unit)
if skip_acl_check or (allowed_units and local_unit() in
allowed_units.split()):
status_set('maintenance', 'Running nova db migration')
migrate_nova_database()
log('Triggering remote cloud-compute restarts.')
[compute_joined(rid=rid, remote_restart=True)
for rid in relation_ids('cloud-compute')]
if not skip_cells_restarts:
log('Triggering remote cell restarts.')
[nova_cell_relation_joined(rid=rid, remote_restart=True)
for rid in relation_ids('cell')]
conditional_neutron_migration()
else:
log('allowed_units either not presented, or local unit '
'not in acl list: %s' % repr(allowed_units))
@hooks.hook('install.real')
2013-08-01 20:42:16 -07:00
def install():
2015-10-07 09:32:28 +00:00
status_set('maintenance', 'Executing pre-install')
2013-09-27 17:18:25 +01:00
execd_preinstall()
2013-08-01 20:42:16 -07:00
configure_installation_source(config('openstack-origin'))
2015-04-15 14:17:56 +00:00
2015-10-07 09:32:28 +00:00
status_set('maintenance', 'Installing apt packages')
2013-08-01 20:42:16 -07:00
apt_update()
apt_install(determine_packages(), fatal=True)
2013-08-15 14:57:06 -07:00
2015-04-15 14:17:56 +00:00
git_install(config('openstack-origin-git'))
2013-08-15 14:57:06 -07:00
_files = os.path.join(charm_dir(), 'files')
if os.path.isdir(_files):
for f in os.listdir(_files):
f = os.path.join(_files, f)
if os.path.isfile(f):
log('Installing %s to /usr/bin' % f)
shutil.copy2(f, '/usr/bin')
2013-08-01 20:42:16 -07:00
[open_port(port) for port in determine_ports()]
2015-10-07 09:32:28 +00:00
msg = 'Disabling services into db relation joined'
log(msg)
status_set('maintenance', msg)
disable_services()
cmd_all_services('stop')
2013-08-01 20:42:16 -07:00
2013-08-12 10:50:39 -07:00
@hooks.hook('config-changed')
2014-07-29 12:50:07 +01:00
@service_guard(guard_map(), CONFIGS,
active=config('service-guard'))
@restart_on_change(restart_map(), stopstart=True)
2013-08-01 20:42:16 -07:00
def config_changed():
# neutron-server runs if < juno. Neutron-server creates mysql tables
# which will subsequently cause db migratoins to fail if >= juno.
# Disable neutron-server if >= juno
if os_release('nova-common') >= 'juno':
with open('/etc/init/neutron-server.override', 'wb') as out:
out.write('manual\n')
2014-09-18 20:23:54 +08:00
if config('prefer-ipv6'):
2015-10-07 09:32:28 +00:00
status_set('maintenance', 'configuring ipv6')
2014-09-18 20:23:54 +08:00
setup_ipv6()
2014-09-26 20:15:49 +01:00
sync_db_with_multi_ipv6_addresses(config('database'),
2014-09-30 14:59:57 +08:00
config('database-user'),
2014-09-30 13:42:04 +01:00
relation_prefix='nova')
2014-09-18 20:23:54 +08:00
global CONFIGS
2015-04-15 14:17:56 +00:00
if git_install_requested():
2015-10-07 09:32:28 +00:00
status_set('maintenance', 'Running Git install')
2015-04-15 14:17:56 +00:00
if config_value_changed('openstack-origin-git'):
git_install(config('openstack-origin-git'))
2015-09-21 16:06:54 -07:00
elif not config('action-managed-upgrade'):
2015-04-15 14:17:56 +00:00
if openstack_upgrade_available('nova-common'):
2015-10-07 09:32:28 +00:00
status_set('maintenance', 'Running openstack upgrade')
2015-09-21 16:06:54 -07:00
CONFIGS = do_openstack_upgrade(CONFIGS)
2015-04-15 14:17:56 +00:00
[neutron_api_relation_joined(rid=rid, remote_restart=True)
for rid in relation_ids('neutron-api')]
2013-08-01 20:42:16 -07:00
save_script_rc()
configure_https()
2013-08-10 18:35:37 +01:00
CONFIGS.write_all()
if console_attributes('protocol'):
2015-04-15 14:17:56 +00:00
if not git_install_requested():
2015-10-07 09:32:28 +00:00
status_set('maintenance', 'Configuring guest console access')
2015-04-15 14:17:56 +00:00
apt_update()
packages = console_attributes('packages') or []
filtered = filter_installed_packages(packages)
if filtered:
apt_install(filtered, fatal=True)
[compute_joined(rid=rid)
for rid in relation_ids('cloud-compute')]
for r_id in relation_ids('identity-service'):
2014-07-16 14:19:17 +01:00
identity_joined(rid=r_id)
2014-09-09 13:01:37 +00:00
for rid in relation_ids('zeromq-configuration'):
zeromq_configuration_relation_joined(rid)
[cluster_joined(rid) for rid in relation_ids('cluster')]
2014-10-29 22:30:36 -05:00
update_nrpe_config()
2013-08-01 20:42:16 -07:00
# If the region value has changed, notify the cloud-compute relations
# to ensure the value is propagated to the compute nodes.
if config_value_changed('region'):
for rid in relation_ids('cloud-compute'):
compute_changed(rid)
update_nova_consoleauth_config()
2013-08-01 20:42:16 -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-08-01 20:42:16 -07:00
@hooks.hook('amqp-relation-changed')
2014-01-30 12:26:36 +01:00
@hooks.hook('amqp-relation-departed')
2014-07-29 12:50:07 +01:00
@service_guard(guard_map(), CONFIGS,
active=config('service-guard'))
2013-08-01 20:42:16 -07:00
@restart_on_change(restart_map())
def amqp_changed():
if 'amqp' not in CONFIGS.complete_contexts():
log('amqp relation incomplete. Peer not ready?')
return
2013-09-25 14:12:21 +01:00
CONFIGS.write(NOVA_CONF)
if not is_relation_made('neutron-api'):
if network_manager() == 'quantum':
CONFIGS.write(QUANTUM_CONF)
if network_manager() == 'neutron':
CONFIGS.write(NEUTRON_CONF)
2014-04-25 16:46:42 +00:00
[nova_cell_relation_joined(rid=rid)
for rid in relation_ids('cell')]
2013-08-01 20:42:16 -07:00
2014-09-26 15:46:53 +00:00
def conditional_neutron_migration():
if os_release('nova-common') <= 'icehouse':
2015-04-22 14:39:04 +00:00
log('Not running neutron database migration as migrations are handled '
'by the neutron-server process.')
elif os_release('nova-common') >= 'kilo':
2015-04-22 14:39:04 +00:00
log('Not running neutron database migration as migrations are by '
2015-04-22 14:36:20 +00:00
'the neutron-api charm.')
2014-09-26 15:46:53 +00:00
else:
if not config('quantum-plugin') == 'vsp':
status_set('maintenance', 'Running neutron db migration')
migrate_neutron_database()
2015-06-18 11:24:16 -07:00
else:
log('Not running neutron database migration as migrations are '
2015-08-21 05:05:24 -07:00
'handled by the neutron-api charm in case of VSP(juno).')
2014-09-26 15:46:53 +00:00
# neutron-api service may have appeared while the migration was
# running so prod it just in case
[neutron_api_relation_joined(rid=rid, remote_restart=True)
for rid in relation_ids('neutron-api')]
if 'neutron-server' in services():
service_restart('neutron-server')
2013-08-01 20:42:16 -07:00
@hooks.hook('shared-db-relation-joined')
def db_joined():
2014-04-10 17:05:53 +01:00
if is_relation_made('pgsql-nova-db') or \
is_relation_made('pgsql-neutron-db'):
2014-03-20 11:18:23 +01:00
# error, postgresql is used
e = ('Attempting to associate a mysql database when there is already '
'associated a postgresql one')
2014-03-24 12:36:04 +01:00
log(e, level=ERROR)
2014-03-24 13:01:12 +01:00
raise Exception(e)
2014-03-24 15:31:59 +01:00
2013-08-10 14:35:17 +01:00
if network_manager() in ['quantum', 'neutron']:
2014-09-30 13:42:04 +01:00
config_neutron = True
else:
config_neutron = False
if config('prefer-ipv6'):
2014-09-26 20:15:49 +01:00
sync_db_with_multi_ipv6_addresses(config('database'),
2014-09-30 14:48:02 +08:00
config('database-user'),
2014-09-30 13:42:04 +01:00
relation_prefix='nova')
if config_neutron:
sync_db_with_multi_ipv6_addresses(config('neutron-database'),
config('neutron-database-user'),
relation_prefix='neutron')
else:
host = unit_get('private-address')
2014-09-27 01:17:47 +08:00
relation_set(nova_database=config('database'),
nova_username=config('database-user'),
nova_hostname=host)
2014-09-30 13:42:04 +01:00
if config_neutron:
# XXX: Renaming relations from quantum_* to neutron_* here.
relation_set(neutron_database=config('neutron-database'),
neutron_username=config('neutron-database-user'),
neutron_hostname=host)
2013-08-01 20:42:16 -07:00
2014-03-20 11:18:23 +01:00
@hooks.hook('pgsql-nova-db-relation-joined')
def pgsql_nova_db_joined():
if is_relation_made('shared-db'):
# raise error
2014-04-10 17:05:53 +01:00
e = ('Attempting to associate a postgresql database'
' when there is already associated a mysql one')
2014-03-24 12:36:04 +01:00
log(e, level=ERROR)
2014-03-24 13:01:12 +01:00
raise Exception(e)
2014-03-20 11:18:23 +01:00
2014-03-25 10:00:50 +01:00
relation_set(database=config('database'))
2014-03-20 11:18:23 +01:00
@hooks.hook('pgsql-neutron-db-relation-joined')
def pgsql_neutron_db_joined():
if is_relation_made('shared-db'):
# raise error
2014-04-10 17:05:53 +01:00
e = ('Attempting to associate a postgresql database'
' when there is already associated a mysql one')
2014-03-24 12:36:04 +01:00
log(e, level=ERROR)
2014-03-24 13:01:12 +01:00
raise Exception(e)
2014-03-20 11:18:23 +01:00
2014-03-25 10:00:50 +01:00
relation_set(database=config('neutron-database'))
2014-03-20 11:18:23 +01:00
2013-08-01 20:42:16 -07:00
@hooks.hook('shared-db-relation-changed')
2014-07-29 12:50:07 +01:00
@service_guard(guard_map(), CONFIGS,
active=config('service-guard'))
2013-08-01 20:42:16 -07:00
@restart_on_change(restart_map())
def db_changed():
if 'shared-db' not in CONFIGS.complete_contexts():
log('shared-db relation incomplete. Peer not ready?')
return
CONFIGS.write_all()
leader_init_db_if_ready()
2013-08-01 20:42:16 -07:00
2014-03-20 11:18:23 +01:00
@hooks.hook('pgsql-nova-db-relation-changed')
2014-07-29 12:50:07 +01:00
@service_guard(guard_map(), CONFIGS,
active=config('service-guard'))
2014-03-20 11:18:23 +01:00
@restart_on_change(restart_map())
2014-03-24 12:36:04 +01:00
def postgresql_nova_db_changed():
if 'pgsql-nova-db' not in CONFIGS.complete_contexts():
2014-03-27 12:46:44 +01:00
log('pgsql-nova-db relation incomplete. Peer not ready?')
2014-03-20 11:18:23 +01:00
return
CONFIGS.write_all()
leader_init_db_if_ready(skip_acl_check=True, skip_cells_restarts=True)
2014-03-20 11:18:23 +01:00
2014-03-24 12:36:04 +01:00
@hooks.hook('pgsql-neutron-db-relation-changed')
2014-07-29 12:50:07 +01:00
@service_guard(guard_map(), CONFIGS,
active=config('service-guard'))
2014-03-24 12:36:04 +01:00
@restart_on_change(restart_map())
def postgresql_neutron_db_changed():
if network_manager() in ['neutron', 'quantum']:
plugin = neutron_plugin()
# DB config might have been moved to main neutron.conf in H?
CONFIGS.write(neutron_plugin_attribute(plugin, 'config'))
2013-08-01 20:42:16 -07:00
@hooks.hook('image-service-relation-changed')
2014-07-29 12:50:07 +01:00
@service_guard(guard_map(), CONFIGS,
active=config('service-guard'))
2013-08-01 20:42:16 -07:00
@restart_on_change(restart_map())
def image_service_changed():
if 'image-service' not in CONFIGS.complete_contexts():
log('image-service relation incomplete. Peer not ready?')
return
2013-09-25 14:12:21 +01:00
CONFIGS.write(NOVA_CONF)
2013-08-01 20:42:16 -07:00
# TODO: special case config flag for essex (strip protocol)
@hooks.hook('identity-service-relation-joined')
def identity_joined(rid=None):
2014-07-04 12:49:08 +01:00
public_url = canonical_url(CONFIGS, PUBLIC)
internal_url = canonical_url(CONFIGS, INTERNAL)
admin_url = canonical_url(CONFIGS, ADMIN)
relation_set(relation_id=rid, **determine_endpoints(public_url,
2014-06-27 11:37:18 +01:00
internal_url,
admin_url))
2013-08-01 20:42:16 -07:00
@hooks.hook('identity-service-relation-changed')
2014-07-29 12:50:07 +01:00
@service_guard(guard_map(), CONFIGS,
active=config('service-guard'))
2013-08-01 20:42:16 -07:00
@restart_on_change(restart_map())
def identity_changed():
if 'identity-service' not in CONFIGS.complete_contexts():
log('identity-service relation incomplete. Peer not ready?')
return
CONFIGS.write('/etc/nova/api-paste.ini')
2013-09-25 14:12:21 +01:00
CONFIGS.write(NOVA_CONF)
if not is_relation_made('neutron-api'):
if network_manager() == 'quantum':
CONFIGS.write(QUANTUM_API_PASTE)
CONFIGS.write(QUANTUM_CONF)
save_novarc()
if network_manager() == 'neutron':
CONFIGS.write(NEUTRON_CONF)
[compute_joined(rid) for rid in relation_ids('cloud-compute')]
[quantum_joined(rid) for rid in relation_ids('quantum-network-service')]
[nova_vmware_relation_joined(rid) for rid in relation_ids('nova-vmware')]
[neutron_api_relation_joined(rid) for rid in relation_ids('neutron-api')]
configure_https()
2013-08-01 20:42:16 -07:00
@hooks.hook('nova-volume-service-relation-joined',
'cinder-volume-service-relation-joined')
2014-07-29 12:50:07 +01:00
@service_guard(guard_map(), CONFIGS,
active=config('service-guard'))
2013-08-01 20:42:16 -07:00
@restart_on_change(restart_map())
def volume_joined():
2013-09-25 14:12:21 +01:00
CONFIGS.write(NOVA_CONF)
2013-08-01 20:42:16 -07:00
# kick identity_joined() to publish possibly new nova-volume endpoint.
[identity_joined(rid) for rid in relation_ids('identity-service')]
def _auth_config():
'''Grab all KS auth token config from api-paste.ini, or return empty {}'''
ks_auth_host = auth_token_config('auth_host')
if not ks_auth_host:
# if there is no auth_host set, identity-service changed hooks
# have not fired, yet.
return {}
cfg = {
'auth_host': ks_auth_host,
'auth_port': auth_token_config('auth_port'),
'auth_protocol': auth_token_config('auth_protocol'),
'service_protocol': auth_token_config('service_protocol'),
2013-08-01 20:42:16 -07:00
'service_port': auth_token_config('service_port'),
'service_username': auth_token_config('admin_user'),
'service_password': auth_token_config('admin_password'),
'service_tenant_name': auth_token_config('admin_tenant_name'),
'auth_uri': auth_token_config('auth_uri'),
2013-08-15 14:57:06 -07:00
# quantum-gateway interface deviates a bit.
'keystone_host': ks_auth_host,
'service_tenant': auth_token_config('admin_tenant_name'),
2013-08-01 20:42:16 -07:00
}
return cfg
def save_novarc():
auth = _auth_config()
# XXX hard-coded http
ks_url = '%s://%s:%s/v2.0' % (auth['auth_protocol'],
auth['auth_host'], auth['auth_port'])
with open('/etc/quantum/novarc', 'wb') as out:
out.write('export OS_USERNAME=%s\n' % auth['service_username'])
out.write('export OS_PASSWORD=%s\n' % auth['service_password'])
out.write('export OS_TENANT_NAME=%s\n' % auth['service_tenant_name'])
out.write('export OS_AUTH_URL=%s\n' % ks_url)
out.write('export OS_REGION_NAME=%s\n' % config('region'))
def neutron_settings():
neutron_settings = {}
if is_relation_made('neutron-api', 'neutron-plugin'):
neutron_api_info = NeutronAPIContext()()
neutron_settings.update({
# XXX: Rename these relations settings?
'quantum_plugin': neutron_api_info['neutron_plugin'],
'region': config('region'),
2014-06-23 12:07:35 +01:00
'quantum_security_groups':
neutron_api_info['neutron_security_groups'],
'quantum_url': neutron_api_info['neutron_url'],
})
else:
neutron_settings.update({
# XXX: Rename these relations settings?
'quantum_plugin': neutron_plugin(),
'region': config('region'),
'quantum_security_groups': config('quantum-security-groups'),
2014-07-04 12:49:08 +01:00
'quantum_url': "{}:{}".format(canonical_url(CONFIGS, INTERNAL),
str(api_port('neutron-server'))),
})
neutron_url = urlparse(neutron_settings['quantum_url'])
neutron_settings['quantum_host'] = neutron_url.hostname
neutron_settings['quantum_port'] = neutron_url.port
return neutron_settings
2014-06-20 09:59:07 +01:00
2013-10-29 19:28:46 -07:00
def keystone_compute_settings():
2013-08-01 20:42:16 -07:00
ks_auth_config = _auth_config()
2013-10-29 19:28:46 -07:00
rel_settings = {}
2013-08-01 20:42:16 -07:00
2013-08-10 14:35:17 +01:00
if network_manager() in ['quantum', 'neutron']:
2013-08-01 20:42:16 -07:00
if ks_auth_config:
rel_settings.update(ks_auth_config)
rel_settings.update(neutron_settings())
2013-08-01 20:42:16 -07:00
ks_ca = keystone_ca_cert_b64()
if ks_auth_config and ks_ca:
rel_settings['ca_cert'] = ks_ca
2013-10-29 19:28:46 -07:00
return rel_settings
2014-07-09 14:09:15 +01:00
def console_settings():
rel_settings = {}
2014-07-10 08:37:39 +01:00
proto = console_attributes('protocol')
if not proto:
2014-07-09 14:09:15 +01:00
return {}
2014-07-10 08:37:39 +01:00
rel_settings['console_keymap'] = config('console-keymap')
rel_settings['console_access_protocol'] = proto
2015-07-22 13:33:03 +08:00
2015-07-22 20:01:24 +08:00
console_ssl = False
if config('console-ssl-cert') and config('console-ssl-key'):
2015-07-22 20:01:24 +08:00
console_ssl = True
2015-07-22 13:33:03 +08:00
if config('console-proxy-ip') == 'local':
2015-07-22 20:01:24 +08:00
if console_ssl:
address = resolve_address(endpoint_type=PUBLIC)
address = format_ipv6_addr(address) or address
proxy_base_addr = 'https://%s' % address
else:
# canonical_url will only return 'https:' if API SSL are enabled.
proxy_base_addr = canonical_url(CONFIGS, PUBLIC)
2014-07-09 14:09:15 +01:00
else:
2015-07-22 20:01:24 +08:00
if console_ssl or https():
schema = "https"
else:
schema = "http"
2015-07-22 20:01:24 +08:00
proxy_base_addr = "%s://%s" % (schema, config('console-proxy-ip'))
2014-07-10 08:37:39 +01:00
if proto == 'vnc':
protocols = ['novnc', 'xvpvnc']
else:
protocols = [proto]
for _proto in protocols:
rel_settings['console_proxy_%s_address' % (_proto)] = \
"%s:%s%s" % (proxy_base_addr,
console_attributes('proxy-port', proto=_proto),
console_attributes('proxy-page', proto=_proto))
rel_settings['console_proxy_%s_host' % (_proto)] = \
urlparse(proxy_base_addr).hostname
2014-07-10 08:37:39 +01:00
rel_settings['console_proxy_%s_port' % (_proto)] = \
2014-07-10 09:07:28 +01:00
console_attributes('proxy-port', proto=_proto)
2014-07-10 08:37:39 +01:00
2014-07-09 14:09:15 +01:00
return rel_settings
2013-10-29 19:28:46 -07:00
@hooks.hook('cloud-compute-relation-joined')
def compute_joined(rid=None, remote_restart=False):
cons_settings = console_settings()
relation_set(relation_id=rid, **cons_settings)
2013-10-29 19:28:46 -07:00
rel_settings = {
'network_manager': network_manager(),
'volume_service': volume_service(),
# (comment from bash vers) XXX Should point to VIP if clustered, or
# this may not even be needed.
'ec2_host': unit_get('private-address'),
'region': config('region')
2013-10-29 19:28:46 -07:00
}
# update relation setting if we're attempting to restart remote
# services
if remote_restart:
rel_settings['restart_trigger'] = str(uuid.uuid4())
2013-10-29 19:28:46 -07:00
rel_settings.update(keystone_compute_settings())
relation_set(relation_id=rid, **rel_settings)
2013-08-01 20:42:16 -07:00
2013-08-12 13:53:00 -07:00
@hooks.hook('cloud-compute-relation-changed')
def compute_changed(rid=None, unit=None):
rel_settings = relation_get(rid=rid, unit=unit)
if not rel_settings.get('region', None) == config('region'):
relation_set(relation_id=rid, region=config('region'))
if 'migration_auth_type' not in rel_settings:
return
if rel_settings['migration_auth_type'] == 'ssh':
2015-10-07 09:32:28 +00:00
status_set('maintenance', 'configuring live migration')
key = rel_settings.get('ssh_public_key')
2013-08-10 09:49:47 +01:00
if not key:
log('SSH migration set but peer did not publish key.')
return
ssh_compute_add(key, rid=rid, unit=unit)
index = 0
for line in ssh_known_hosts_lines(unit=unit):
relation_set(
relation_id=rid,
relation_settings={
'known_hosts_{}'.format(index): line})
index += 1
relation_set(relation_id=rid, known_hosts_max_index=index)
index = 0
for line in ssh_authorized_keys_lines(unit=unit):
relation_set(
relation_id=rid,
relation_settings={
'authorized_keys_{}'.format(index): line})
index += 1
relation_set(relation_id=rid, authorized_keys_max_index=index)
2014-05-27 15:43:49 +02:00
if 'nova_ssh_public_key' not in rel_settings:
return
if rel_settings['nova_ssh_public_key']:
ssh_compute_add(rel_settings['nova_ssh_public_key'],
rid=rid, unit=unit, user='nova')
index = 0
for line in ssh_known_hosts_lines(unit=unit, user='nova'):
relation_set(
relation_id=rid,
relation_settings={
'{}_known_hosts_{}'.format(
'nova',
index): line})
index += 1
relation_set(
relation_id=rid,
relation_settings={
'{}_known_hosts_max_index'.format('nova'): index})
index = 0
for line in ssh_authorized_keys_lines(unit=unit, user='nova'):
relation_set(
relation_id=rid,
relation_settings={
'{}_authorized_keys_{}'.format(
'nova',
index): line})
index += 1
relation_set(
relation_id=rid,
relation_settings={
'{}_authorized_keys_max_index'.format('nova'): index})
2013-08-01 20:42:16 -07:00
@hooks.hook('cloud-compute-relation-departed')
2013-08-10 09:49:47 +01:00
def compute_departed():
ssh_compute_remove(public_key=relation_get('ssh_public_key'))
2013-08-10 09:49:47 +01:00
2013-08-10 14:35:17 +01:00
@hooks.hook('neutron-network-service-relation-joined',
'quantum-network-service-relation-joined')
2013-08-01 20:42:16 -07:00
def quantum_joined(rid=None):
rel_settings = neutron_settings()
2013-08-01 20:42:16 -07:00
# inform quantum about local keystone auth config
ks_auth_config = _auth_config()
rel_settings.update(ks_auth_config)
# must pass the keystone CA cert, if it exists.
ks_ca = keystone_ca_cert_b64()
if ks_auth_config and ks_ca:
rel_settings['ca_cert'] = ks_ca
relation_set(relation_id=rid, **rel_settings)
2013-08-01 20:42:16 -07:00
@hooks.hook('cluster-relation-joined')
def cluster_joined(relation_id=None):
for addr_type in ADDRESS_TYPES:
address = get_address_in_network(
config('os-{}-network'.format(addr_type))
)
if address:
relation_set(
relation_id=relation_id,
relation_settings={'{}-address'.format(addr_type): address}
)
2014-09-30 14:48:02 +08:00
if config('prefer-ipv6'):
private_addr = get_ipv6_addr(exc_list=[config('vip')])[0]
2014-10-02 10:22:36 +01:00
relation_set(relation_id=relation_id,
relation_settings={'private-address': private_addr})
2013-08-01 20:42:16 -07:00
@hooks.hook('cluster-relation-changed',
2015-03-13 11:51:30 +00:00
'cluster-relation-departed',
'leader-settings-changed')
2014-07-29 12:50:07 +01:00
@service_guard(guard_map(), CONFIGS,
active=config('service-guard'))
@restart_on_change(restart_map(), stopstart=True)
2013-08-01 20:42:16 -07:00
def cluster_changed():
CONFIGS.write_all()
if relation_ids('cluster'):
peer_echo(includes=['dbsync_state'])
dbsync_state = peer_retrieve('dbsync_state')
if dbsync_state == 'complete':
enable_services()
cmd_all_services('start')
else:
log('Database sync not ready. Shutting down services')
disable_services()
cmd_all_services('stop')
2013-08-01 20:42:16 -07:00
@hooks.hook('ha-relation-joined')
def ha_joined():
cluster_config = get_hacluster_config()
2013-08-01 20:42:16 -07:00
resources = {
'res_nova_haproxy': 'lsb:haproxy',
}
resource_params = {
2015-02-17 12:57:15 -03:00
'res_nova_haproxy': 'op monitor interval="5s"',
2013-08-01 20:42:16 -07:00
}
vip_group = []
for vip in cluster_config['vip'].split():
2014-09-30 14:48:02 +08:00
if is_ipv6(vip):
res_nova_vip = 'ocf:heartbeat:IPv6addr'
vip_params = 'ipv6addr'
else:
res_nova_vip = 'ocf:heartbeat:IPaddr2'
vip_params = 'ip'
iface = (get_iface_for_address(vip) or
config('vip_iface'))
netmask = (get_netmask_for_address(vip) or
config('vip_cidr'))
if iface is not None:
vip_key = 'res_nova_{}_vip'.format(iface)
resources[vip_key] = res_nova_vip
resource_params[vip_key] = (
'params {ip}="{vip}" cidr_netmask="{netmask}"'
' nic="{iface}"'.format(ip=vip_params,
vip=vip,
iface=iface,
netmask=netmask)
)
vip_group.append(vip_key)
if len(vip_group) >= 1:
relation_set(groups={'grp_nova_vips': ' '.join(vip_group)})
2014-07-16 14:19:17 +01:00
2013-08-01 20:42:16 -07:00
init_services = {
'res_nova_haproxy': 'haproxy'
2013-08-01 20:42:16 -07:00
}
clones = {
'cl_nova_haproxy': 'res_nova_haproxy'
}
2015-02-18 15:22:26 -03:00
colocations = {}
if config('single-nova-consoleauth') and console_attributes('protocol'):
colocations['vip_consoleauth'] = COLO_CONSOLEAUTH
init_services['res_nova_consoleauth'] = 'nova-consoleauth'
resources['res_nova_consoleauth'] = AGENT_CONSOLEAUTH
resource_params['res_nova_consoleauth'] = AGENT_CA_PARAMS
2013-08-01 20:42:16 -07:00
relation_set(init_services=init_services,
corosync_bindiface=cluster_config['ha-bindiface'],
corosync_mcastport=cluster_config['ha-mcastport'],
2013-08-01 20:42:16 -07:00
resources=resources,
resource_params=resource_params,
2015-02-17 12:57:15 -03:00
clones=clones,
colocations=colocations)
2013-08-01 20:42:16 -07:00
@hooks.hook('ha-relation-changed')
def ha_changed():
clustered = relation_get('clustered')
if not clustered or clustered in [None, 'None', '']:
2013-08-01 20:42:16 -07:00
log('ha_changed: hacluster subordinate not fully clustered.')
return
CONFIGS.write(NOVA_CONF)
if not is_relation_made('neutron-api'):
if network_manager() == 'quantum':
CONFIGS.write(QUANTUM_CONF)
if network_manager() == 'neutron':
CONFIGS.write(NEUTRON_CONF)
2013-08-01 20:42:16 -07:00
log('Cluster configured, notifying other services and updating '
'keystone endpoint configuration')
for rid in relation_ids('identity-service'):
identity_joined(rid=rid)
update_nova_consoleauth_config()
2013-08-01 20:42:16 -07:00
2014-08-12 06:50:23 +00:00
@hooks.hook('shared-db-relation-broken',
'pgsql-nova-db-relation-broken')
@service_guard(guard_map(), CONFIGS,
active=config('service-guard'))
def db_departed():
CONFIGS.write_all()
for r_id in relation_ids('cluster'):
relation_set(relation_id=r_id, dbsync_state='incomplete')
disable_services()
cmd_all_services('stop')
2014-08-12 06:50:23 +00:00
2013-08-20 12:06:15 -07:00
@hooks.hook('amqp-relation-broken',
'cinder-volume-service-relation-broken',
'identity-service-relation-broken',
'image-service-relation-broken',
'nova-volume-service-relation-broken',
2014-03-20 11:18:23 +01:00
'pgsql-neutron-db-relation-broken',
2013-08-20 12:06:15 -07:00
'quantum-network-service-relation-broken')
@service_guard(guard_map(), CONFIGS,
active=config('service-guard'))
2013-08-20 12:06:15 -07:00
def relation_broken():
CONFIGS.write_all()
2014-04-25 16:46:42 +00:00
[nova_cell_relation_joined(rid=rid)
for rid in relation_ids('cell')]
2013-08-20 12:06:15 -07:00
2013-08-01 20:42:16 -07:00
def configure_https():
'''
Enables SSL API Apache config if appropriate and kicks identity-service
with any required api updates.
'''
# need to write all to ensure changes to the entire request pipeline
# propagate (c-api, haprxy, apache)
CONFIGS.write_all()
if 'https' in CONFIGS.complete_contexts():
cmd = ['a2ensite', 'openstack_https_frontend']
check_call(cmd)
else:
cmd = ['a2dissite', 'openstack_https_frontend']
check_call(cmd)
# TODO: improve this by checking if local CN certs are available
# first then checking reload status (see LP #1433114).
2015-03-30 18:26:30 +01:00
service_reload('apache2', restart_on_failure=True)
2013-08-01 20:42:16 -07:00
for rid in relation_ids('identity-service'):
identity_joined(rid=rid)
2013-10-08 17:51:27 -07:00
@hooks.hook()
def nova_vmware_relation_joined(rid=None):
2013-10-29 19:28:46 -07:00
rel_settings = {'network_manager': network_manager()}
ks_auth = _auth_config()
if ks_auth:
rel_settings.update(ks_auth)
rel_settings.update({
'quantum_plugin': neutron_plugin(),
'quantum_security_groups': config('quantum-security-groups'),
2014-07-04 12:49:08 +01:00
'quantum_url': "{}:{}".format(canonical_url(CONFIGS, INTERNAL),
str(api_port('neutron-server')))})
2013-10-29 19:28:46 -07:00
relation_set(relation_id=rid, **rel_settings)
2013-10-08 17:51:27 -07:00
@hooks.hook('nova-vmware-relation-changed')
2014-07-29 12:50:07 +01:00
@service_guard(guard_map(), CONFIGS,
active=config('service-guard'))
2013-10-10 09:04:40 -07:00
@restart_on_change(restart_map())
def nova_vmware_relation_changed():
CONFIGS.write('/etc/nova/nova.conf')
@hooks.hook('upgrade-charm')
def upgrade_charm():
apt_install(filter_installed_packages(determine_packages()),
fatal=True)
for r_id in relation_ids('amqp'):
amqp_joined(relation_id=r_id)
2014-03-04 17:06:08 +00:00
for r_id in relation_ids('identity-service'):
2014-03-04 17:07:06 +00:00
identity_joined(rid=r_id)
for r_id in relation_ids('cloud-compute'):
for unit in related_units(r_id):
compute_changed(r_id, unit)
rels = ['shared-db', 'pgsql-nova-db']
for rname in rels:
for rid in relation_ids(rname):
for unit in related_units(rid):
if rname == 'pgsql-nova-db':
leader_init_db_if_ready(skip_acl_check=True,
skip_cells_restarts=True,
db_rid=rid, unit=unit)
else:
2016-01-08 13:46:56 +00:00
leader_init_db_if_ready(db_rid=rid, unit=unit)
2014-10-29 22:30:36 -05:00
update_nrpe_config()
update_nova_consoleauth_config()
# remote_restart is defaulted to true as nova-cells may have started the
2014-09-28 14:11:26 +00:00
# nova-cell process before the db migration was run so it will need a
# kick
@hooks.hook('cell-relation-joined')
def nova_cell_relation_joined(rid=None, remote_restart=True):
rel_settings = {
'nova_url': "%s:8774/v2" % canonical_url(CONFIGS, INTERNAL)
}
if remote_restart:
rel_settings['restart_trigger'] = str(uuid.uuid4())
relation_set(relation_id=rid, **rel_settings)
2014-04-25 16:46:42 +00:00
@hooks.hook('cell-relation-changed')
@restart_on_change(restart_map())
2014-04-25 16:46:42 +00:00
def nova_cell_relation_changed():
CONFIGS.write(NOVA_CONF)
2014-07-11 10:14:57 +01:00
def get_cell_type():
2014-09-28 14:11:26 +00:00
cell_info = NovaCellContext()()
if 'cell_type' in cell_info:
return cell_info['cell_type']
return None
2014-09-28 14:11:26 +00:00
@hooks.hook('neutron-api-relation-joined')
2014-09-26 15:46:53 +00:00
def neutron_api_relation_joined(rid=None, remote_restart=False):
with open('/etc/init/neutron-server.override', 'wb') as out:
out.write('manual\n')
2014-05-16 10:52:19 +00:00
if os.path.isfile(NEUTRON_CONF):
os.rename(NEUTRON_CONF, NEUTRON_CONF + '_unused')
if service_running('neutron-server'):
service_stop('neutron-server')
2014-05-16 10:52:19 +00:00
for id_rid in relation_ids('identity-service'):
identity_joined(rid=id_rid)
2014-09-26 15:46:53 +00:00
rel_settings = {
'nova_url': canonical_url(CONFIGS, INTERNAL) + ":8774/v2"
}
if get_cell_type():
2014-09-28 14:11:26 +00:00
rel_settings['cell_type'] = get_cell_type()
2014-09-26 15:46:53 +00:00
if remote_restart:
rel_settings['restart_trigger'] = str(uuid.uuid4())
relation_set(relation_id=rid, **rel_settings)
2014-04-25 16:46:42 +00:00
2014-06-20 09:59:07 +01:00
2014-05-08 19:36:17 +00:00
@hooks.hook('neutron-api-relation-changed')
2014-07-29 12:50:07 +01:00
@service_guard(guard_map(), CONFIGS,
active=config('service-guard'))
@restart_on_change(restart_map())
2014-05-08 19:36:17 +00:00
def neutron_api_relation_changed():
CONFIGS.write(NOVA_CONF)
for rid in relation_ids('cloud-compute'):
compute_joined(rid=rid)
for rid in relation_ids('quantum-network-service'):
quantum_joined(rid=rid)
2014-05-08 19:36:17 +00:00
2014-06-20 09:59:07 +01:00
@hooks.hook('neutron-api-relation-broken')
2014-07-29 12:50:07 +01:00
@service_guard(guard_map(), CONFIGS,
active=config('service-guard'))
@restart_on_change(restart_map())
def neutron_api_relation_broken():
if os.path.isfile('/etc/init/neutron-server.override'):
os.remove('/etc/init/neutron-server.override')
CONFIGS.write_all()
for rid in relation_ids('cloud-compute'):
compute_joined(rid=rid)
for rid in relation_ids('quantum-network-service'):
quantum_joined(rid=rid)
2014-06-20 09:59:07 +01:00
2014-09-09 13:01:37 +00:00
@hooks.hook('zeromq-configuration-relation-joined')
2015-03-16 16:13:02 +00:00
@os_requires_version('kilo', 'nova-common')
2014-09-09 13:01:37 +00:00
def zeromq_configuration_relation_joined(relid=None):
relation_set(relation_id=relid,
topics=" ".join(get_topics()),
users="nova")
@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)
2015-02-19 14:29:22 +10:00
nrpe.copy_nrpe_checks()
2015-01-12 12:04:00 +00:00
nrpe.add_init_service_checks(nrpe_setup, services(), current_unit)
2015-02-19 14:29:22 +10:00
nrpe.add_haproxy_checks(nrpe_setup, current_unit)
2015-01-12 12:04:00 +00:00
nrpe_setup.write()
2014-06-20 09:59:07 +01:00
2015-01-09 15:50:01 +00:00
@hooks.hook('memcache-relation-joined',
'memcache-relation-departed',
'memcache-relation-changed',
'memcache-relation-broken')
@restart_on_change(restart_map())
def memcached_joined():
CONFIGS.write(NOVA_CONF)
2014-09-09 13:01:37 +00:00
@hooks.hook('zeromq-configuration-relation-changed')
@restart_on_change(restart_map(), stopstart=True)
def zeromq_configuration_relation_changed():
CONFIGS.write(NOVA_CONF)
def update_nova_consoleauth_config():
"""
Configure nova-consoleauth pacemaker resources
"""
relids = relation_ids('ha')
if len(relids) == 0:
log('Related to {} ha services'.format(len(relids)), level='DEBUG')
ha_relid = None
data = {}
else:
ha_relid = relids[0]
data = relation_get(rid=ha_relid) or {}
# initialize keys in case this is a new dict
data.setdefault('delete_resources', [])
for k in ['colocations', 'init_services', 'resources', 'resource_params']:
data.setdefault(k, {})
if config('single-nova-consoleauth') and console_attributes('protocol'):
for item in ['vip_consoleauth', 'res_nova_consoleauth']:
try:
data['delete_resources'].remove(item)
except ValueError:
pass # nothing to remove, we are good
# the new pcmkr resources have to be added to the existing ones
data['colocations']['vip_consoleauth'] = COLO_CONSOLEAUTH
data['init_services']['res_nova_consoleauth'] = 'nova-consoleauth'
data['resources']['res_nova_consoleauth'] = AGENT_CONSOLEAUTH
data['resource_params']['res_nova_consoleauth'] = AGENT_CA_PARAMS
for rid in relation_ids('ha'):
relation_set(rid, **data)
# nova-consoleauth will be managed by pacemaker, so mark it as manual
if relation_ids('ha'):
with open(NOVA_CONSOLEAUTH_OVERRIDE, 'w') as fp:
fp.write('manual\n')
fp.flush()
elif (not config('single-nova-consoleauth') and
console_attributes('protocol')):
for item in ['vip_consoleauth', 'res_nova_consoleauth']:
if item not in data['delete_resources']:
data['delete_resources'].append(item)
# remove them from the rel, so they aren't recreated when the hook
# is recreated
data['colocations'].pop('vip_consoleauth', None)
data['init_services'].pop('res_nova_consoleauth', None)
data['resources'].pop('res_nova_consoleauth', None)
data['resource_params'].pop('res_nova_consoleauth', None)
for rid in relation_ids('ha'):
relation_set(rid, **data)
try:
os.remove(NOVA_CONSOLEAUTH_OVERRIDE)
except FileNotFoundError as e:
log(str(e), level='DEBUG')
2013-08-01 20:42:16 -07:00
def main():
try:
hooks.execute(sys.argv)
except UnregisteredHookError as e:
log('Unknown hook {} - skipping.'.format(e))
2015-10-08 14:31:01 +01:00
set_os_workload_status(CONFIGS, REQUIRED_INTERFACES,
charm_func=check_optional_relations)
2013-08-01 20:42:16 -07:00
if __name__ == '__main__':
main()