How to use the charmhelpers.core.hookenv.relation_ids 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 juju / charm-tools / tests / layers / mysql / hooks / charmhelpers / core / services / helpers.py View on Github external
like::

            {% for unit in interface -%}
                {{ unit['key'] }}{% if not loop.last %},{% endif %}
            {%- endfor %}

        Note that since all sets of relation data from all related services and
        units are in a single list, if you need to know which service or unit a
        set of data came from, you'll need to extend this class to preserve
        that information.
        """
        if not hookenv.relation_ids(self.name):
            return

        ns = self.setdefault(self.name, [])
        for rid in sorted(hookenv.relation_ids(self.name)):
            for unit in sorted(hookenv.related_units(rid)):
                reldata = hookenv.relation_get(rid=rid, unit=unit)
                if self._is_ready(reldata):
                    ns.append(reldata)
github openstack / charm-swift-proxy / charmhelpers / contrib / storage / linux / ceph.py View on Github external
def get_ceph_nodes(relation='ceph'):
    """Query named relation to determine current nodes."""
    hosts = []
    for r_id in relation_ids(relation):
        for unit in related_units(r_id):
            hosts.append(relation_get('private-address', unit=unit, rid=r_id))

    return hosts
github jenkinsci / jenkins-charm / hooks / charmhelpers / contrib / openstack / context.py View on Github external
def __call__(self):
        log('Generating template context for identity-service', level=DEBUG)
        ctxt = {}
        for rid in relation_ids('identity-service'):
            for unit in related_units(rid):
                rdata = relation_get(rid=rid, unit=unit)
                serv_host = rdata.get('service_host')
                serv_host = format_ipv6_addr(serv_host) or serv_host
                auth_host = rdata.get('auth_host')
                auth_host = format_ipv6_addr(auth_host) or auth_host
                svc_protocol = rdata.get('service_protocol') or 'http'
                auth_protocol = rdata.get('auth_protocol') or 'http'
                ctxt = {'service_port': rdata.get('service_port'),
                        'service_host': serv_host,
                        'auth_host': auth_host,
                        'auth_port': rdata.get('auth_port'),
                        'admin_tenant_name': rdata.get('service_tenant'),
                        'admin_user': rdata.get('service_username'),
                        'admin_password': rdata.get('service_password'),
                        'service_protocol': svc_protocol,
github openstack / charm-openstack-dashboard / charmhelpers / contrib / openstack / context.py View on Github external
user_setting = 'rabbit-user'
            vhost_setting = 'rabbit-vhost'

        try:
            username = conf[user_setting]
            vhost = conf[vhost_setting]
        except KeyError as e:
            log('Could not generate shared_db context. Missing required charm '
                'config options: %s.' % e, level=ERROR)
            raise OSContextError

        ctxt = {}
        if self.relation_id:
            rids = [self.relation_id]
        else:
            rids = relation_ids(self.rel_name)
        for rid in rids:
            ha_vip_only = False
            self.related = True
            transport_hosts = None
            rabbitmq_port = '5672'
            for unit in related_units(rid):
                if relation_get('clustered', rid=rid, unit=unit):
                    ctxt['clustered'] = True
                    vip = relation_get('vip', rid=rid, unit=unit)
                    vip = format_ipv6_addr(vip) or vip
                    ctxt['rabbitmq_host'] = vip
                    transport_hosts = [vip]
                else:
                    host = relation_get('private-address', rid=rid, unit=unit)
                    host = format_ipv6_addr(host) or host
                    ctxt['rabbitmq_host'] = host
github openstack / charm-nova-compute / hooks / nova_compute_utils.py View on Github external
def get_optional_relations():
    """Return a dictionary of optional relations.

    @returns {relation: relation_name}
    """
    optional_interfaces = {}
    if relation_ids('ceph'):
        optional_interfaces['storage-backend'] = ['ceph']
    if relation_ids('neutron-plugin'):
        optional_interfaces['neutron-plugin'] = ['neutron-plugin']
    if config('encrypt'):
        optional_interfaces['vault'] = ['secrets-storage']

    return optional_interfaces
github openstack / charm-nova-compute / hooks / charmhelpers / contrib / openstack / utils.py View on Github external
def get_peers_unit_state(relation_name='cluster'):
    """Get the state of all peers.

    :param relation_name: Name of relation to check peers on.
    :type relation_name: string
    :returns: Unit states keyed on unit name.
    :rtype: dict
    :raises: ValueError
    """
    r_ids = relation_ids(relation_name)
    rids_units = [(r, u) for r in r_ids for u in related_units(r)]
    unit_states = {}
    for r_id, unit in rids_units:
        settings = relation_get(unit=unit, rid=r_id)
        unit_states[unit] = settings.get(get_peer_key(unit), UNIT_UNKNOWN)
        if unit_states[unit] not in UNIT_STATES:
            raise ValueError(
                "Unit in unknown state {}".format(unit_states[unit]))
    return unit_states
github openstack / charm-nova-cloud-controller / charmhelpers / contrib / charmsupport / nrpe.py View on Github external
monitors = {"monitors": {"remote": {"nrpe": nrpe_monitors}}}
        for nrpecheck in self.checks:
            nrpecheck.write(self.nagios_context, self.hostname,
                            self.nagios_servicegroups)
            nrpe_monitors[nrpecheck.shortname] = {
                "command": nrpecheck.command,
            }

        # update-status hooks are configured to firing every 5 minutes by
        # default. When nagios-nrpe-server is restarted, the nagios server
        # reports checks failing causing unneccessary alerts. Let's not restart
        # on update-status hooks.
        if not hook_name() == 'update-status':
            service('restart', 'nagios-nrpe-server')

        monitor_ids = relation_ids("local-monitors") + \
            relation_ids("nrpe-external-master")
        for rid in monitor_ids:
            relation_set(relation_id=rid, monitors=yaml.dump(monitors))
github openstack / charm-nova-cloud-controller / hooks / nova_cc_hooks.py View on Github external
def update_nova_relation(remote_restart=False):
    for rid in hookenv.relation_ids('cloud-compute'):
        compute_joined(rid=rid, remote_restart=remote_restart)
    for rid in hookenv.relation_ids('quantum-network-service'):
        quantum_joined(rid=rid, remote_restart=remote_restart)
    for rid in hookenv.relation_ids('nova-cell-api'):
        nova_cell_api_relation_joined(rid=rid, remote_restart=remote_restart)
github openstack / charm-percona-cluster / charmhelpers / contrib / peerstorage / __init__.py View on Github external
def relation_set(relation_id=None, relation_settings=None, **kwargs):
    """Attempt to use leader-set if supported in the current version of Juju,
    otherwise falls back on relation-set.

    Note that we only attempt to use leader-set if the provided relation_id is
    a peer relation id or no relation id is provided (in which case we assume
    we are within the peer relation context).
    """
    try:
        if relation_id in relation_ids('cluster'):
            return leader_set(settings=relation_settings, **kwargs)
        else:
            raise NotImplementedError
    except NotImplementedError:
        return _relation_set(relation_id=relation_id,
                             relation_settings=relation_settings, **kwargs)
github jenkinsci / jenkins-charm / hooks / charmhelpers / contrib / openstack / context.py View on Github external
def __call__(self):
        ctxt = {'sections': {}}
        for rid in relation_ids(self.interface):
            for unit in related_units(rid):
                sub_config = relation_get('subordinate_configuration',
                                          rid=rid, unit=unit)
                if sub_config and sub_config != '':
                    try:
                        sub_config = json.loads(sub_config)
                    except:
                        log('Could not parse JSON from subordinate_config '
                            'setting from %s' % rid, level=ERROR)
                        continue

                    if self.service not in sub_config:
                        log('Found subordinate_config on %s but it contained'
                            'nothing for %s service' % (rid, self.service),
                            level=INFO)
                        continue