0

I am new to Juju and trying to write a charm. I need ip address of all units deployed in a service. I am using

e.g.I have 3 units of wordpress with-- juju deploy -n 3 cs:wordpress

I deployed my charm from local repo -- juju deploy local:trusty/X

I add relation between the two-- juju add-relation X wordpress

Now, I need list of all 3 ip addresses of wordpress service in one of the hook in my charm(X)

I set relation-keys in wordpress and tried using relation-ids -> relation-list -> relation-get hook tools in my charm. But it gives me just one ip and not all three.

relation-ids --gives one id. relation-list id --gives just one unit. relation-get --gives corresponding ip.

what should I do to get expected result?

mayuri
  • 1
  • 2

2 Answers2

1

Try something like this:

from charmhelpers.contrib.openstack.utils import get_host_ip
from charmhelpers.core.hookenv import (
    relation_ids,
    related_units,
    relation_get,
)
def _get_ips(rel_name):
    return [get_host_ip(rdata['private-address'])
            for rid in relation_ids(rel_name)
            for rdata in
            (relation_get(rid=rid, unit=unit) for unit in related_units(rid))
            if rdata]

ips = _get_ips("wordpress")

The relation name might be different in your case. Do confirm that.

Bilal Baqar
  • 208
  • 2
  • 12
0

you can get a unit's address using

get_host_ip(unit_get('private-address')

where get_host_ip can be imported from charmhelpers

from charmhelpers.contrib.openstack.utils import get_host_ip

To get IP addresses of all units of charms, you have to add peer relation. You can read about relations from here

Junaid
  • 3,477
  • 1
  • 24
  • 24