How to use the charmhelpers.core.hookenv.related_units function in charmhelpers

To help you get started, we’ve selected a few charmhelpers examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github openstack / charm-percona-cluster / hooks / percona_utils.py View on Github external
Having min-cluster-size set will guarantee is_bootstrapped will not
    return True until the expected number of peers are bootstrapped. If
    min-cluster-size is not set, it will check peer relations to estimate the
    expected cluster size. If min-cluster-size is not set and there are no
    peers it must assume the cluster is bootstrapped in order to allow for
    single unit deployments.

    @returns boolean
    """
    min_size = get_min_cluster_size()
    if not is_sufficient_peers():
        return False
    elif min_size > 1:
        uuids = []
        for relation_id in relation_ids('cluster'):
            units = related_units(relation_id) or []
            units.append(local_unit())
            for unit in units:
                if not relation_get(attribute='bootstrap-uuid',
                                    rid=relation_id,
                                    unit=unit):
                    log("{} is not yet clustered".format(unit),
                        DEBUG)
                    return False
                else:
                    bootstrap_uuid = relation_get(attribute='bootstrap-uuid',
                                                  rid=relation_id,
                                                  unit=unit)
                    if bootstrap_uuid:
                        uuids.append(bootstrap_uuid)

        if len(uuids) < min_size:
github openstack / charm-swift-proxy / charmhelpers / contrib / openstack / utils.py View on Github external
key = 'allowed_units'

    rel_name = rel_name or 'shared-db'
    this_unit = local_unit()

    if use_current_context:
        if relation_id() in relation_ids(rel_name):
            rids_units = [(None, None)]
        else:
            raise Exception("use_current_context=True but not in {} "
                            "rel hook contexts (currently in {})."
                            .format(rel_name, relation_id()))
    else:
        rids_units = [(r_id, u)
                      for r_id in relation_ids(rel_name)
                      for u in related_units(r_id)]

    for rid, unit in rids_units:
        allowed_units = relation_get(rid=rid, unit=unit, attribute=key)
        if allowed_units and this_unit in allowed_units.split():
            juju_log("This unit ({}) is in allowed unit list from {}".format(
                this_unit,
                unit), 'DEBUG')
            return True

    juju_log("This unit was not found in any allowed unit list")
    return False
github openstack / charm-ceph-proxy / hooks / ceph_hooks.py View on Github external
def client_relation_joined(relid=None, unit=None):
    if ready():
        service_name = None
        if relid is None:
            units = [remote_unit()]
            service_name = units[0].split('/')[0]
        else:
            units = related_units(relid)
            if len(units) > 0:
                service_name = units[0].split('/')[0]
        if unit is None:
            unit = units[0]
        if service_name is not None:
            ceph_addrs = config('monitor-hosts')
            data = {'key': ceph.get_named_key(service_name),
                    'auth': config('auth-supported'),
                    'ceph-public-address': ceph_addrs}

            settings = relation_get(rid=relid, unit=unit) or {}
            data_update = {}
            if 'broker_req' in settings:
                rsp = process_requests(settings['broker_req'])
                unit_id = unit.replace('/', '-')
                unit_response_key = 'broker-rsp-' + unit_id
github openstack / charm-ceph-proxy / charmhelpers / contrib / storage / linux / ceph.py View on Github external
def is_request_complete_for_rid(request, rid):
    """Check if a given request has been completed on the given relation

    @param request: A CephBrokerRq object
    @param rid: Relation ID
    """
    broker_key = get_broker_rsp_key()
    for unit in related_units(rid):
        rdata = relation_get(rid=rid, unit=unit)
        if rdata.get(broker_key):
            rsp = CephBrokerRsp(rdata.get(broker_key))
            if rsp.request_id == request.request_id:
                if not rsp.exit_code:
                    return True
        else:
            # The remote unit sent no reply targeted at this unit so either the
            # remote ceph cluster does not support unit targeted replies or it
            # has not processed our request yet.
            if rdata.get('broker_rsp'):
                request_data = json.loads(rdata['broker_rsp'])
                if request_data.get('request-id'):
                    log('Ignoring legacy broker_rsp without unit key as remote '
                        'service supports unit specific replies', level=DEBUG)
                else:
github openstack / charm-nova-cloud-controller / charmhelpers / contrib / openstack / utils.py View on Github external
for rel in expected_relations:
            if not rel.rel_id:
                juju_log(
                    'Expected to find {} relation, but it is missing'.format(
                        rel.rel_type),
                    'DEBUG')
                return False
            # Goal state returns every unit even for container scoped
            # relations but the charm only ever has a relation with
            # the local unit.
            if rel.rel_type in container_scoped_relations():
                expected_count = 1
            else:
                expected_count = len(
                    list(expected_related_units(reltype=rel.rel_type)))
            if len(related_units(relid=rel.rel_id)) < expected_count:
                juju_log(
                    ('Not at expected scale, not enough units on {} '
                     'relation'.format(rel.rel_type)),
                    'DEBUG')
                return False
    except NotImplementedError:
        return True
    juju_log('All checks have passed, unit is at expected scale', 'DEBUG')
    return True
github openstack / charm-swift-proxy / charmhelpers / contrib / hahelpers / cluster.py View on Github external
def is_clustered():
    for r_id in (relation_ids('ha') or []):
        for unit in (relation_list(r_id) or []):
            clustered = relation_get('clustered',
                                     rid=r_id,
                                     unit=unit)
            if clustered:
                return True
    return False
github openstack / charm-percona-cluster / hooks / percona_utils.py View on Github external
number of peers as expected for a complete cluster.

    If not defined assume a single unit.

    @returns boolean
    """

    min_size = config('min-cluster-size')
    if min_size:
        log("Checking for minimum of {} peer units".format(min_size),
            level=DEBUG)

        # Include this unit
        units = 1
        for rid in relation_ids('cluster'):
            units += len(related_units(rid))

        if units < min_size:
            log("Insufficient number of peer units to form cluster "
                "(expected={}, got={})".format(min_size, units), level=INFO)
            return False
        else:
            log("Sufficient number of peer units to form cluster {}"
                "".format(min_size, level=DEBUG))
            return True
    else:
        log("min-cluster-size is not defined, race conditions may occur if "
            "this is not a single unit deployment.", level=WARNING)
        return True
github jenkinsci / jenkins-charm / hooks / charmhelpers / contrib / openstack / context.py View on Github external
def canonical_names(self):
        """Figure out which canonical names clients will access this service.
        """
        cns = []
        for r_id in relation_ids('identity-service'):
            for unit in related_units(r_id):
                rdata = relation_get(rid=r_id, unit=unit)
                for k in rdata:
                    if k.startswith('ssl_key_'):
                        cns.append(k.lstrip('ssl_key_'))

        return sorted(list(set(cns)))
github openstack / charm-swift-proxy / charmhelpers / contrib / openstack / utils.py View on Github external
def get_endpoint_notifications(service_names, rel_name='identity-service'):
    """Return all notifications for the given services.

    :param service_names: List of service name.
    :type service_name: List
    :param rel_name: Name of the relation to query
    :type rel_name: str
    :returns: A dict containing the source of the notification and its nonce.
    :rtype: Dict[str, str]
    """
    notifications = {}
    for rid in relation_ids(rel_name):
        for unit in related_units(relid=rid):
            ep_changed_json = relation_get(
                rid=rid,
                unit=unit,
                attribute='ep_changed')
            if ep_changed_json:
                ep_changed = json.loads(ep_changed_json)
                for service in service_names:
                    if ep_changed.get(service):
                        key = get_endpoint_key(service, rid, unit)
                        notifications[key] = ep_changed[service]
    return notifications
github openstack / charm-nova-compute / hooks / nova_compute_utils.py View on Github external
def _network_config():
    '''
    Obtain all relevant network configuration settings from nova-c-c via
    cloud-compute interface.
    '''
    settings = ['network_manager', 'neutron_plugin', 'quantum_plugin']
    net_config = {}
    for rid in relation_ids('cloud-compute'):
        for unit in related_units(rid):
            for setting in settings:
                value = relation_get(setting, rid=rid, unit=unit)
                if value:
                    net_config[setting] = value
    return net_config