def leader_init_db_if_ready(skip_acl_check=False, db_rid=None, unit=None): """Initialise db if leader and db not yet intialised. NOTE: must be called from database context. """ if not hookenv.is_leader(): hookenv.log("Not leader - skipping db init", level=hookenv.DEBUG) return if ncc_utils.is_db_initialised(): hookenv.log("Database already initialised - skipping db init", level=hookenv.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 = hookenv.relation_get('nova_allowed_units', rid=db_rid, unit=unit) if skip_acl_check or (allowed_units and hookenv.local_unit() in allowed_units.split()): hookenv.status_set('maintenance', 'Running nova db migration') ncc_utils.migrate_nova_databases() hookenv.log('Triggering remote restarts.') update_nova_relation(remote_restart=True) else: hookenv.log('allowed_units either not presented, or local unit ' 'not in acl list: %s' % repr(allowed_units))
def add_hosts_to_cell_when_ready(): """Helper function to call add_hosts_to_cell() when the unit is the leader and the cellv2 and database are ready. :raises: subprocess.CalledProcessError if cells command fails """ if (hookenv.is_leader() and ncc_utils.is_cellv2_init_ready() and ncc_utils.is_db_initialised()): ncc_utils.add_hosts_to_cell()
def update_cell_db_if_ready(skip_acl_check=False, db_rid=None, unit=None): """Update the cells db if leader and db's are already intialised""" if not hookenv.is_leader(): return if not ncc_utils.is_db_initialised(): hookenv.log("Database not initialised - skipping cell db update", level=hookenv.DEBUG) return if not ncc_utils.is_cellv2_init_ready(): return allowed_units = hookenv.relation_get('nova_allowed_units', rid=db_rid, unit=unit) if skip_acl_check or (allowed_units and hookenv.local_unit() in allowed_units.split()): ncc_utils.update_cell_database() else: hookenv.log('allowed_units either not presented, or local unit ' 'not in acl list: %s' % repr(allowed_units))
def compute_changed(rid=None, unit=None): for r_id in hookenv.relation_ids('nova-api'): nova_api_relation_joined(rid=r_id) rel_settings = hookenv.relation_get(rid=rid, unit=unit) if not rel_settings.get('region', None) == hookenv.config('region'): hookenv.relation_set(relation_id=rid, region=hookenv.config('region')) if (hookenv.is_leader() and ncc_utils.is_cellv2_init_ready() and ncc_utils.is_db_initialised()): ncc_utils.add_hosts_to_cell() if 'migration_auth_type' not in rel_settings: return if rel_settings['migration_auth_type'] == 'ssh': hookenv.status_set('maintenance', 'configuring live migration') key = rel_settings.get('ssh_public_key') if not key: hookenv.log('SSH migration set but peer did not publish key.') return ncc_utils.ssh_compute_add(key, rid=rid, unit=unit) index = 0 for line in ncc_utils.ssh_known_hosts_lines(unit=unit): hookenv.relation_set(relation_id=rid, relation_settings={ 'known_hosts_{}'.format(index): line }) index += 1 hookenv.relation_set(relation_id=rid, known_hosts_max_index=index) index = 0 for line in ncc_utils.ssh_authorized_keys_lines(unit=unit): hookenv.relation_set(relation_id=rid, relation_settings={ 'authorized_keys_{}'.format(index): line }) index += 1 hookenv.relation_set(relation_id=rid, authorized_keys_max_index=index) if 'nova_ssh_public_key' not in rel_settings: return if rel_settings['nova_ssh_public_key']: ncc_utils.ssh_compute_add(rel_settings['nova_ssh_public_key'], rid=rid, unit=unit, user='******') index = 0 for line in ncc_utils.ssh_known_hosts_lines(unit=unit, user='******'): hookenv.relation_set(relation_id=rid, relation_settings={ '{}_known_hosts_{}'.format('nova', index): line }) index += 1 hookenv.relation_set(relation_id=rid, relation_settings={ ('{}_known_hosts_max_index' .format('nova')): index }) index = 0 for line in ncc_utils.ssh_authorized_keys_lines( unit=unit, user='******'): hookenv.relation_set(relation_id=rid, relation_settings={ '{}_authorized_keys_{}'.format( 'nova', index): line }) index += 1 hookenv.relation_set(relation_id=rid, relation_settings={ '{}_authorized_keys_max_index'.format( 'nova'): index })
def compute_changed(rid=None, unit=None): for r_id in hookenv.relation_ids('nova-api'): nova_api_relation_joined(rid=r_id) rel_settings = hookenv.relation_get(rid=rid, unit=unit) if not rel_settings.get('region', None) == hookenv.config('region'): hookenv.relation_set(relation_id=rid, region=hookenv.config('region')) if (hookenv.is_leader() and ncc_utils.is_cellv2_init_ready() and ncc_utils.is_db_initialised()): ncc_utils.add_hosts_to_cell() if 'migration_auth_type' not in rel_settings: return if rel_settings['migration_auth_type'] == 'ssh': hookenv.status_set('maintenance', 'configuring live migration') key = rel_settings.get('ssh_public_key') if not key: hookenv.log('SSH migration set but peer did not publish key.') return ncc_utils.ssh_compute_add(key, rid=rid, unit=unit) index = 0 for line in ncc_utils.ssh_known_hosts_lines(unit=unit): hookenv.relation_set( relation_id=rid, relation_settings={'known_hosts_{}'.format(index): line}) index += 1 hookenv.relation_set(relation_id=rid, known_hosts_max_index=index) index = 0 for line in ncc_utils.ssh_authorized_keys_lines(unit=unit): hookenv.relation_set( relation_id=rid, relation_settings={'authorized_keys_{}'.format(index): line}) index += 1 hookenv.relation_set(relation_id=rid, authorized_keys_max_index=index) if 'nova_ssh_public_key' not in rel_settings: return if rel_settings['nova_ssh_public_key']: ncc_utils.ssh_compute_add(rel_settings['nova_ssh_public_key'], rid=rid, unit=unit, user='******') index = 0 for line in ncc_utils.ssh_known_hosts_lines(unit=unit, user='******'): hookenv.relation_set(relation_id=rid, relation_settings={ '{}_known_hosts_{}'.format('nova', index): line }) index += 1 hookenv.relation_set(relation_id=rid, relation_settings={ ('{}_known_hosts_max_index'.format('nova')): index }) index = 0 for line in ncc_utils.ssh_authorized_keys_lines(unit=unit, user='******'): hookenv.relation_set(relation_id=rid, relation_settings={ '{}_authorized_keys_{}'.format( 'nova', index): line }) index += 1 hookenv.relation_set(relation_id=rid, relation_settings={ '{}_authorized_keys_max_index'.format('nova'): index })