173
175
return os.environ.get('JUJU_RELATION', None)
177
"""The relation ID for the current relation hook"""
178
return os.environ.get('JUJU_RELATION_ID', None)
178
@cmdline.subcommand()
180
def relation_id(relation_name=None, service_or_unit=None):
181
"""The relation ID for the current or a specified relation"""
182
if not relation_name and not service_or_unit:
183
return os.environ.get('JUJU_RELATION_ID', None)
184
elif relation_name and service_or_unit:
185
service_name = service_or_unit.split('/')[0]
186
for relid in relation_ids(relation_name):
187
remote_service = remote_service_name(relid)
188
if remote_service == service_name:
191
raise ValueError('Must specify neither or both of relation_name and service_or_unit')
181
194
def local_unit():
188
201
return os.environ.get('JUJU_REMOTE_UNIT', None)
204
@cmdline.subcommand()
191
205
def service_name():
192
206
"""The name service group this unit belongs to"""
193
207
return local_unit().split('/')[0]
210
@cmdline.subcommand()
212
def remote_service_name(relid=None):
213
"""The remote service name for a given relation-id (or the current relation)"""
217
units = related_units(relid)
218
unit = units[0] if units else None
219
return unit.split('/')[0] if unit else None
197
223
"""The name of the currently executing hook"""
198
return os.path.basename(sys.argv[0])
224
return os.environ.get('JUJU_HOOK_NAME', os.path.basename(sys.argv[0]))
201
227
class Config(dict):
498
def relation_to_interface(relation_name):
500
Given the name of a relation, return the interface that relation uses.
502
:returns: The interface name, or ``None``.
504
return relation_to_role_and_interface(relation_name)[1]
508
def relation_to_role_and_interface(relation_name):
510
Given the name of a relation, return the role and the name of the interface
511
that relation uses (where role is one of ``provides``, ``requires``, or ``peer``).
513
:returns: A tuple containing ``(role, interface)``, or ``(None, None)``.
515
_metadata = metadata()
516
for role in ('provides', 'requires', 'peer'):
517
interface = _metadata.get(role, {}).get(relation_name, {}).get('interface')
519
return role, interface
524
def role_and_interface_to_relations(role, interface_name):
526
Given a role and interface name, return a list of relation names for the
527
current charm that use that interface under that role (where role is one
528
of ``provides``, ``requires``, or ``peer``).
530
:returns: A list of relation names.
532
_metadata = metadata()
534
for relation_name, relation in _metadata.get(role, {}).items():
535
if relation['interface'] == interface_name:
536
results.append(relation_name)
541
def interface_to_relations(interface_name):
543
Given an interface, return a list of relation names for the current
544
charm that use that interface.
546
:returns: A list of relation names.
549
for role in ('provides', 'requires', 'peer'):
550
results.extend(role_and_interface_to_relations(role, interface_name))
472
555
def charm_name():
473
556
"""Get the name of the current charm as is specified on metadata.yaml"""
474
557
return metadata().get('name')
644
727
subprocess.check_call(['action-fail', message])
731
"""Get the name of the currently executing action."""
732
return os.environ.get('JUJU_ACTION_NAME')
736
"""Get the UUID of the currently executing action."""
737
return os.environ.get('JUJU_ACTION_UUID')
741
"""Get the tag for the currently executing action."""
742
return os.environ.get('JUJU_ACTION_TAG')
647
745
def status_set(workload_state, message):
648
746
"""Set the workload state with a message