~gnuoy/charms/trusty/nova-compute/1461192

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/core/hookenv.py

  • Committer: Liam Young
  • Date: 2015-07-31 13:11:21 UTC
  • Revision ID: liam.young@canonical.com-20150731131121-g7t8weto24y1t0ql
[gnuoy,trivial] Pre-release charmhelper sync to pickup cli module

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
import tempfile
35
35
from subprocess import CalledProcessError
36
36
 
 
37
from charmhelpers.cli import cmdline
 
38
 
37
39
import six
38
40
if not six.PY3:
39
41
    from UserDict import UserDict
173
175
    return os.environ.get('JUJU_RELATION', None)
174
176
 
175
177
 
176
 
def relation_id():
177
 
    """The relation ID for the current relation hook"""
178
 
    return os.environ.get('JUJU_RELATION_ID', None)
 
178
@cmdline.subcommand()
 
179
@cached
 
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:
 
189
                return relid
 
190
    else:
 
191
        raise ValueError('Must specify neither or both of relation_name and service_or_unit')
179
192
 
180
193
 
181
194
def local_unit():
188
201
    return os.environ.get('JUJU_REMOTE_UNIT', None)
189
202
 
190
203
 
 
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]
194
208
 
195
209
 
 
210
@cmdline.subcommand()
 
211
@cached
 
212
def remote_service_name(relid=None):
 
213
    """The remote service name for a given relation-id (or the current relation)"""
 
214
    if relid is None:
 
215
        unit = remote_unit()
 
216
    else:
 
217
        units = related_units(relid)
 
218
        unit = units[0] if units else None
 
219
    return unit.split('/')[0] if unit else None
 
220
 
 
221
 
196
222
def hook_name():
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]))
199
225
 
200
226
 
201
227
class Config(dict):
469
495
 
470
496
 
471
497
@cached
 
498
def relation_to_interface(relation_name):
 
499
    """
 
500
    Given the name of a relation, return the interface that relation uses.
 
501
 
 
502
    :returns: The interface name, or ``None``.
 
503
    """
 
504
    return relation_to_role_and_interface(relation_name)[1]
 
505
 
 
506
 
 
507
@cached
 
508
def relation_to_role_and_interface(relation_name):
 
509
    """
 
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``).
 
512
 
 
513
    :returns: A tuple containing ``(role, interface)``, or ``(None, None)``.
 
514
    """
 
515
    _metadata = metadata()
 
516
    for role in ('provides', 'requires', 'peer'):
 
517
        interface = _metadata.get(role, {}).get(relation_name, {}).get('interface')
 
518
        if interface:
 
519
            return role, interface
 
520
    return None, None
 
521
 
 
522
 
 
523
@cached
 
524
def role_and_interface_to_relations(role, interface_name):
 
525
    """
 
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``).
 
529
 
 
530
    :returns: A list of relation names.
 
531
    """
 
532
    _metadata = metadata()
 
533
    results = []
 
534
    for relation_name, relation in _metadata.get(role, {}).items():
 
535
        if relation['interface'] == interface_name:
 
536
            results.append(relation_name)
 
537
    return results
 
538
 
 
539
 
 
540
@cached
 
541
def interface_to_relations(interface_name):
 
542
    """
 
543
    Given an interface, return a list of relation names for the current
 
544
    charm that use that interface.
 
545
 
 
546
    :returns: A list of relation names.
 
547
    """
 
548
    results = []
 
549
    for role in ('provides', 'requires', 'peer'):
 
550
        results.extend(role_and_interface_to_relations(role, interface_name))
 
551
    return results
 
552
 
 
553
 
 
554
@cached
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])
645
728
 
646
729
 
 
730
def action_name():
 
731
    """Get the name of the currently executing action."""
 
732
    return os.environ.get('JUJU_ACTION_NAME')
 
733
 
 
734
 
 
735
def action_uuid():
 
736
    """Get the UUID of the currently executing action."""
 
737
    return os.environ.get('JUJU_ACTION_UUID')
 
738
 
 
739
 
 
740
def action_tag():
 
741
    """Get the tag for the currently executing action."""
 
742
    return os.environ.get('JUJU_ACTION_TAG')
 
743
 
 
744
 
647
745
def status_set(workload_state, message):
648
746
    """Set the workload state with a message
649
747