2011-12-08 09:52:12 -08:00
|
|
|
#!/usr/bin/python
|
2012-02-29 11:59:37 -08:00
|
|
|
|
2014-02-25 12:34:13 +01:00
|
|
|
import os
|
|
|
|
import sys
|
2014-02-26 16:54:26 +00:00
|
|
|
import time
|
2014-02-25 12:34:13 +01:00
|
|
|
|
|
|
|
from subprocess import check_call
|
|
|
|
|
|
|
|
from charmhelpers.contrib import unison
|
|
|
|
|
|
|
|
from charmhelpers.core.hookenv import (
|
|
|
|
Hooks,
|
|
|
|
UnregisteredHookError,
|
|
|
|
config,
|
2014-03-31 10:35:19 +02:00
|
|
|
is_relation_made,
|
2014-02-25 12:34:13 +01:00
|
|
|
log,
|
2014-03-31 10:35:19 +02:00
|
|
|
ERROR,
|
2014-02-25 12:34:13 +01:00
|
|
|
relation_get,
|
|
|
|
relation_ids,
|
|
|
|
relation_set,
|
2014-03-27 22:00:08 +00:00
|
|
|
related_units,
|
2014-02-25 12:34:13 +01:00
|
|
|
unit_get,
|
|
|
|
)
|
|
|
|
|
|
|
|
from charmhelpers.core.host import (
|
|
|
|
mkdir,
|
|
|
|
restart_on_change,
|
|
|
|
)
|
|
|
|
|
|
|
|
from charmhelpers.fetch import (
|
2014-02-26 16:54:26 +00:00
|
|
|
apt_install, apt_update,
|
|
|
|
filter_installed_packages
|
2014-02-25 12:34:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
from charmhelpers.contrib.openstack.utils import (
|
|
|
|
configure_installation_source,
|
|
|
|
openstack_upgrade_available,
|
|
|
|
)
|
2012-10-02 17:36:25 -07:00
|
|
|
|
2013-03-18 12:56:57 +00:00
|
|
|
from keystone_utils import (
|
2014-02-25 12:34:13 +01:00
|
|
|
add_service_to_keystone,
|
|
|
|
determine_packages,
|
|
|
|
do_openstack_upgrade,
|
2013-03-18 12:56:57 +00:00
|
|
|
ensure_initial_admin,
|
2014-02-25 12:34:13 +01:00
|
|
|
migrate_database,
|
|
|
|
save_script_rc,
|
2014-03-28 10:39:49 +00:00
|
|
|
synchronize_ca,
|
2014-02-25 12:34:13 +01:00
|
|
|
register_configs,
|
|
|
|
relation_list,
|
|
|
|
restart_map,
|
2013-03-19 13:41:27 +00:00
|
|
|
CLUSTER_RES,
|
2014-02-25 12:34:13 +01:00
|
|
|
KEYSTONE_CONF,
|
|
|
|
SSH_USER,
|
2014-05-06 15:22:01 +01:00
|
|
|
STORED_PASSWD,
|
2014-02-25 12:34:13 +01:00
|
|
|
)
|
2011-12-08 09:52:12 -08:00
|
|
|
|
2014-02-25 12:34:13 +01:00
|
|
|
from charmhelpers.contrib.hahelpers.cluster import (
|
|
|
|
eligible_leader,
|
|
|
|
is_leader,
|
2014-07-16 14:33:47 +01:00
|
|
|
get_hacluster_config,
|
2014-02-25 12:34:13 +01:00
|
|
|
)
|
2013-11-12 16:28:10 +00:00
|
|
|
|
2014-02-25 12:34:13 +01:00
|
|
|
from charmhelpers.payload.execd import execd_preinstall
|
2014-03-28 10:39:49 +00:00
|
|
|
from charmhelpers.contrib.peerstorage import peer_echo
|
2014-07-15 16:55:39 +01:00
|
|
|
from charmhelpers.contrib.network.ip import (
|
|
|
|
get_iface_for_address,
|
|
|
|
get_netmask_for_address
|
|
|
|
)
|
2011-12-08 09:52:12 -08:00
|
|
|
|
2014-02-25 12:34:13 +01:00
|
|
|
hooks = Hooks()
|
|
|
|
CONFIGS = register_configs()
|
2013-03-18 12:56:57 +00:00
|
|
|
|
2014-02-26 16:54:26 +00:00
|
|
|
|
2014-02-25 12:34:13 +01:00
|
|
|
@hooks.hook()
|
|
|
|
def install():
|
2013-11-12 16:28:10 +00:00
|
|
|
execd_preinstall()
|
2014-02-25 12:34:13 +01:00
|
|
|
configure_installation_source(config('openstack-origin'))
|
|
|
|
apt_update()
|
|
|
|
apt_install(determine_packages(), fatal=True)
|
|
|
|
|
2014-02-26 16:54:26 +00:00
|
|
|
|
2014-02-25 12:34:13 +01:00
|
|
|
@hooks.hook('config-changed')
|
2014-02-26 17:05:40 +00:00
|
|
|
@restart_on_change(restart_map())
|
2014-02-25 12:34:13 +01:00
|
|
|
def config_changed():
|
2014-02-26 16:54:26 +00:00
|
|
|
unison.ensure_user(user=SSH_USER, group='keystone')
|
2014-02-25 12:34:13 +01:00
|
|
|
homedir = unison.get_homedir(SSH_USER)
|
|
|
|
if not os.path.isdir(homedir):
|
2014-03-03 09:14:09 +00:00
|
|
|
mkdir(homedir, SSH_USER, 'keystone', 0o775)
|
2014-02-26 16:54:26 +00:00
|
|
|
|
2014-02-25 12:34:13 +01:00
|
|
|
if openstack_upgrade_available('keystone'):
|
|
|
|
do_openstack_upgrade(configs=CONFIGS)
|
2014-02-26 16:54:26 +00:00
|
|
|
|
|
|
|
check_call(['chmod', '-R', 'g+wrx', '/var/lib/keystone/'])
|
|
|
|
|
2014-02-25 12:34:13 +01:00
|
|
|
save_script_rc()
|
|
|
|
configure_https()
|
|
|
|
CONFIGS.write_all()
|
|
|
|
if eligible_leader(CLUSTER_RES):
|
|
|
|
migrate_database()
|
|
|
|
ensure_initial_admin(config)
|
|
|
|
log('Firing identity_changed hook for all related services.')
|
|
|
|
# HTTPS may have been set - so fire all identity relations
|
|
|
|
# again
|
|
|
|
for r_id in relation_ids('identity-service'):
|
2014-02-26 16:54:26 +00:00
|
|
|
for unit in relation_list(r_id):
|
|
|
|
identity_changed(relation_id=r_id,
|
|
|
|
remote_unit=unit)
|
2011-12-08 09:52:12 -08:00
|
|
|
|
2013-01-30 16:48:51 -08:00
|
|
|
|
2014-02-25 12:34:13 +01:00
|
|
|
@hooks.hook('shared-db-relation-joined')
|
2011-12-08 09:52:12 -08:00
|
|
|
def db_joined():
|
2014-03-31 10:35:19 +02:00
|
|
|
if is_relation_made('pgsql-db'):
|
|
|
|
# error, postgresql is used
|
|
|
|
e = ('Attempting to associate a mysql database when there is already '
|
|
|
|
'associated a postgresql one')
|
|
|
|
log(e, level=ERROR)
|
|
|
|
raise Exception(e)
|
|
|
|
|
2014-02-25 12:34:13 +01:00
|
|
|
relation_set(database=config('database'),
|
|
|
|
username=config('database-user'),
|
|
|
|
hostname=unit_get('private-address'))
|
2013-03-18 12:56:57 +00:00
|
|
|
|
2011-12-08 09:52:12 -08:00
|
|
|
|
2014-03-31 10:35:19 +02:00
|
|
|
@hooks.hook('pgsql-db-relation-joined')
|
|
|
|
def pgsql_db_joined():
|
|
|
|
if is_relation_made('shared-db'):
|
|
|
|
# raise error
|
2014-04-10 17:00:28 +01:00
|
|
|
e = ('Attempting to associate a postgresql database when there'
|
|
|
|
' is already associated a mysql one')
|
2014-03-31 10:35:19 +02:00
|
|
|
log(e, level=ERROR)
|
|
|
|
raise Exception(e)
|
|
|
|
|
|
|
|
relation_set(database=config('database'))
|
|
|
|
|
|
|
|
|
2014-02-25 12:34:13 +01:00
|
|
|
@hooks.hook('shared-db-relation-changed')
|
|
|
|
@restart_on_change(restart_map())
|
2011-12-08 09:52:12 -08:00
|
|
|
def db_changed():
|
2014-02-25 12:34:13 +01:00
|
|
|
if 'shared-db' not in CONFIGS.complete_contexts():
|
|
|
|
log('shared-db relation incomplete. Peer not ready?')
|
2014-03-31 10:35:19 +02:00
|
|
|
else:
|
|
|
|
CONFIGS.write(KEYSTONE_CONF)
|
|
|
|
if eligible_leader(CLUSTER_RES):
|
|
|
|
migrate_database()
|
|
|
|
ensure_initial_admin(config)
|
|
|
|
# Ensure any existing service entries are updated in the
|
|
|
|
# new database backend
|
|
|
|
for rid in relation_ids('identity-service'):
|
|
|
|
for unit in related_units(rid):
|
|
|
|
identity_changed(relation_id=rid, remote_unit=unit)
|
|
|
|
|
|
|
|
|
|
|
|
@hooks.hook('pgsql-db-relation-changed')
|
|
|
|
@restart_on_change(restart_map())
|
|
|
|
def pgsql_db_changed():
|
|
|
|
if 'pgsql-db' not in CONFIGS.complete_contexts():
|
|
|
|
log('pgsql-db relation incomplete. Peer not ready?')
|
2014-02-26 16:54:26 +00:00
|
|
|
else:
|
|
|
|
CONFIGS.write(KEYSTONE_CONF)
|
|
|
|
if eligible_leader(CLUSTER_RES):
|
|
|
|
migrate_database()
|
|
|
|
ensure_initial_admin(config)
|
2014-03-27 22:00:08 +00:00
|
|
|
# Ensure any existing service entries are updated in the
|
|
|
|
# new database backend
|
|
|
|
for rid in relation_ids('identity-service'):
|
2014-03-27 22:02:13 +00:00
|
|
|
for unit in related_units(rid):
|
2014-03-27 22:00:08 +00:00
|
|
|
identity_changed(relation_id=rid, remote_unit=unit)
|
2013-03-18 12:56:57 +00:00
|
|
|
|
2013-02-07 21:03:44 -08:00
|
|
|
|
2014-02-25 12:34:13 +01:00
|
|
|
@hooks.hook('identity-service-relation-changed')
|
2014-02-26 16:54:26 +00:00
|
|
|
def identity_changed(relation_id=None, remote_unit=None):
|
2014-02-25 12:34:13 +01:00
|
|
|
if eligible_leader(CLUSTER_RES):
|
2014-02-26 16:54:26 +00:00
|
|
|
add_service_to_keystone(relation_id, remote_unit)
|
2014-03-28 10:39:49 +00:00
|
|
|
synchronize_ca()
|
2014-02-26 16:54:26 +00:00
|
|
|
else:
|
|
|
|
log('Deferring identity_changed() to service leader.')
|
2012-12-17 13:45:58 +00:00
|
|
|
|
|
|
|
|
2014-02-25 12:34:13 +01:00
|
|
|
@hooks.hook('cluster-relation-joined')
|
2013-02-12 21:56:39 -08:00
|
|
|
def cluster_joined():
|
|
|
|
unison.ssh_authorized_peers(user=SSH_USER,
|
2014-02-25 12:34:13 +01:00
|
|
|
group='juju_keystone',
|
2013-02-12 21:56:39 -08:00
|
|
|
peer_interface='cluster',
|
2013-04-09 13:16:44 +01:00
|
|
|
ensure_local_user=True)
|
2013-03-18 12:56:57 +00:00
|
|
|
|
2012-12-17 13:45:58 +00:00
|
|
|
|
2014-02-25 12:34:13 +01:00
|
|
|
@hooks.hook('cluster-relation-changed',
|
|
|
|
'cluster-relation-departed')
|
|
|
|
@restart_on_change(restart_map(), stopstart=True)
|
2012-12-17 13:45:58 +00:00
|
|
|
def cluster_changed():
|
2014-03-28 11:04:08 +00:00
|
|
|
# NOTE(jamespage) re-echo passwords for peer storage
|
|
|
|
peer_echo(includes=['_passwd'])
|
2013-02-12 21:56:39 -08:00
|
|
|
unison.ssh_authorized_peers(user=SSH_USER,
|
2014-02-26 16:54:26 +00:00
|
|
|
group='keystone',
|
2013-03-18 12:56:57 +00:00
|
|
|
peer_interface='cluster',
|
2013-04-09 13:16:44 +01:00
|
|
|
ensure_local_user=True)
|
2014-03-28 10:39:49 +00:00
|
|
|
synchronize_ca()
|
2014-02-25 12:34:13 +01:00
|
|
|
CONFIGS.write_all()
|
|
|
|
|
|
|
|
|
|
|
|
@hooks.hook('ha-relation-joined')
|
|
|
|
def ha_joined():
|
2014-07-16 14:33:47 +01:00
|
|
|
config = get_hacluster_config()
|
2014-07-29 08:42:45 +01:00
|
|
|
|
2012-12-17 13:45:58 +00:00
|
|
|
resources = {
|
2014-02-25 12:34:13 +01:00
|
|
|
'res_ks_haproxy': 'lsb:haproxy',
|
|
|
|
}
|
2012-12-17 13:45:58 +00:00
|
|
|
resource_params = {
|
2013-03-18 12:56:57 +00:00
|
|
|
'res_ks_haproxy': 'op monitor interval="5s"'
|
2014-02-25 12:34:13 +01:00
|
|
|
}
|
2014-07-15 16:55:39 +01:00
|
|
|
|
|
|
|
vip_group = []
|
2014-07-16 14:33:47 +01:00
|
|
|
for vip in config['vip'].split():
|
2014-07-15 16:55:39 +01:00
|
|
|
iface = get_iface_for_address(vip)
|
|
|
|
if iface is not None:
|
|
|
|
vip_key = 'res_ks_{}_vip'.format(iface)
|
|
|
|
resources[vip_key] = 'ocf:heartbeat:IPaddr2'
|
|
|
|
resource_params[vip_key] = (
|
|
|
|
'params ip="{vip}" cidr_netmask="{netmask}"'
|
|
|
|
' nic="{iface}"'.format(vip=vip,
|
|
|
|
iface=iface,
|
|
|
|
netmask=get_netmask_for_address(vip))
|
|
|
|
)
|
|
|
|
vip_group.append(vip_key)
|
|
|
|
|
|
|
|
if len(vip_group) > 1:
|
2014-07-16 14:17:03 +01:00
|
|
|
relation_set(groups={'grp_ks_vips': ' '.join(vip_group)})
|
2014-07-15 16:57:34 +01:00
|
|
|
|
2012-12-17 13:45:58 +00:00
|
|
|
init_services = {
|
2013-03-18 12:56:57 +00:00
|
|
|
'res_ks_haproxy': 'haproxy'
|
2014-02-25 12:34:13 +01:00
|
|
|
}
|
2013-03-18 12:56:57 +00:00
|
|
|
clones = {
|
2013-03-18 15:49:00 +00:00
|
|
|
'cl_ks_haproxy': 'res_ks_haproxy'
|
2014-02-25 12:34:13 +01:00
|
|
|
}
|
|
|
|
relation_set(init_services=init_services,
|
2014-07-16 14:33:47 +01:00
|
|
|
corosync_bindiface=config['ha-bindiface'],
|
|
|
|
corosync_mcastport=config['ha-mcastport'],
|
2014-02-25 12:34:13 +01:00
|
|
|
resources=resources,
|
|
|
|
resource_params=resource_params,
|
|
|
|
clones=clones)
|
|
|
|
|
|
|
|
|
|
|
|
@hooks.hook('ha-relation-changed')
|
2014-03-03 09:13:00 +00:00
|
|
|
@restart_on_change(restart_map())
|
2014-02-25 12:34:13 +01:00
|
|
|
def ha_changed():
|
|
|
|
clustered = relation_get('clustered')
|
2014-03-03 09:13:00 +00:00
|
|
|
CONFIGS.write_all()
|
2014-02-26 16:54:26 +00:00
|
|
|
if (clustered is not None and
|
2014-03-03 09:14:09 +00:00
|
|
|
is_leader(CLUSTER_RES)):
|
2014-02-26 16:54:26 +00:00
|
|
|
ensure_initial_admin(config)
|
|
|
|
log('Cluster configured, notifying other services and updating '
|
|
|
|
'keystone endpoint configuration')
|
|
|
|
for rid in relation_ids('identity-service'):
|
2014-03-31 16:00:14 +01:00
|
|
|
relation_set(relation_id=rid,
|
2014-02-26 16:54:26 +00:00
|
|
|
auth_host=config('vip'),
|
|
|
|
service_host=config('vip'))
|
2014-02-25 12:34:13 +01:00
|
|
|
|
|
|
|
|
2014-05-06 14:13:30 +01:00
|
|
|
@hooks.hook('identity-admin-relation-changed')
|
2014-03-25 17:20:13 +11:00
|
|
|
def admin_relation_changed():
|
|
|
|
relation_data = {
|
2014-06-25 17:17:59 +12:00
|
|
|
'service_hostname': unit_get('private-address'),
|
2014-05-06 14:13:30 +01:00
|
|
|
'service_port': config('service-port'),
|
|
|
|
'service_username': config('admin-user'),
|
|
|
|
'service_tenant_name': config('admin-role'),
|
|
|
|
'service_region': config('region'),
|
2014-03-25 17:20:13 +11:00
|
|
|
}
|
2014-05-06 15:22:01 +01:00
|
|
|
if os.path.isfile(STORED_PASSWD):
|
|
|
|
with open(STORED_PASSWD) as f:
|
2014-05-06 14:13:30 +01:00
|
|
|
relation_data['service_password'] = f.readline().strip('\n')
|
|
|
|
relation_set(**relation_data)
|
|
|
|
|
|
|
|
|
2014-02-25 12:34:13 +01: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)
|
|
|
|
|
|
|
|
|
|
|
|
@hooks.hook('upgrade-charm')
|
2014-02-26 16:54:26 +00:00
|
|
|
@restart_on_change(restart_map(), stopstart=True)
|
2014-02-25 12:34:13 +01:00
|
|
|
def upgrade_charm():
|
2014-02-26 16:54:26 +00:00
|
|
|
apt_install(filter_installed_packages(determine_packages()))
|
2014-03-28 10:43:32 +00:00
|
|
|
unison.ssh_authorized_peers(user=SSH_USER,
|
|
|
|
group='keystone',
|
|
|
|
peer_interface='cluster',
|
|
|
|
ensure_local_user=True)
|
|
|
|
synchronize_ca()
|
2014-02-26 16:54:26 +00:00
|
|
|
if eligible_leader(CLUSTER_RES):
|
|
|
|
log('Cluster leader - ensuring endpoint configuration'
|
|
|
|
' is up to date')
|
2014-02-26 17:30:34 +00:00
|
|
|
time.sleep(10)
|
2014-02-26 16:54:26 +00:00
|
|
|
ensure_initial_admin(config)
|
2014-04-02 12:28:40 +01:00
|
|
|
# Deal with interface changes for icehouse
|
|
|
|
for r_id in relation_ids('identity-service'):
|
|
|
|
for unit in relation_list(r_id):
|
|
|
|
identity_changed(relation_id=r_id,
|
|
|
|
remote_unit=unit)
|
2014-02-25 12:34:13 +01:00
|
|
|
CONFIGS.write_all()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
try:
|
|
|
|
hooks.execute(sys.argv)
|
|
|
|
except UnregisteredHookError as e:
|
|
|
|
log('Unknown hook {} - skipping.'.format(e))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|