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 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
                             })
Exemple #5
0
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
                             })