~openstack-charmers-next/charms/wily/odl-controller/trunk

« back to all changes in this revision

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

  • Committer: Gerrit Code Review
  • Author(s): Jenkins
  • Date: 2016-03-18 16:10:08 UTC
  • mfrom: (21.1.1 trunk)
  • Revision ID: review@openstack.org-20160318161008-grzlrlzei667shdd
Merge "Add support for Ubuntu Xenial"

Show diffs side-by-side

added added

removed removed

Lines of Context:
492
492
 
493
493
@cached
494
494
def peer_relation_id():
495
 
    '''Get a peer relation id if a peer relation has been joined, else None.'''
 
495
    '''Get the peers relation id if a peers relation has been joined, else None.'''
496
496
    md = metadata()
497
497
    section = md.get('peers')
498
498
    if section:
517
517
def relation_to_role_and_interface(relation_name):
518
518
    """
519
519
    Given the name of a relation, return the role and the name of the interface
520
 
    that relation uses (where role is one of ``provides``, ``requires``, or ``peer``).
 
520
    that relation uses (where role is one of ``provides``, ``requires``, or ``peers``).
521
521
 
522
522
    :returns: A tuple containing ``(role, interface)``, or ``(None, None)``.
523
523
    """
524
524
    _metadata = metadata()
525
 
    for role in ('provides', 'requires', 'peer'):
 
525
    for role in ('provides', 'requires', 'peers'):
526
526
        interface = _metadata.get(role, {}).get(relation_name, {}).get('interface')
527
527
        if interface:
528
528
            return role, interface
534
534
    """
535
535
    Given a role and interface name, return a list of relation names for the
536
536
    current charm that use that interface under that role (where role is one
537
 
    of ``provides``, ``requires``, or ``peer``).
 
537
    of ``provides``, ``requires``, or ``peers``).
538
538
 
539
539
    :returns: A list of relation names.
540
540
    """
555
555
    :returns: A list of relation names.
556
556
    """
557
557
    results = []
558
 
    for role in ('provides', 'requires', 'peer'):
 
558
    for role in ('provides', 'requires', 'peers'):
559
559
        results.extend(role_and_interface_to_relations(role, interface_name))
560
560
    return results
561
561
 
637
637
 
638
638
 
639
639
@cached
640
 
def storage_get(attribute="", storage_id=""):
 
640
def storage_get(attribute=None, storage_id=None):
641
641
    """Get storage attributes"""
642
642
    _args = ['storage-get', '--format=json']
643
643
    if storage_id:
651
651
 
652
652
 
653
653
@cached
654
 
def storage_list(storage_name=""):
 
654
def storage_list(storage_name=None):
655
655
    """List the storage IDs for the unit"""
656
656
    _args = ['storage-list', '--format=json']
657
657
    if storage_name:
878
878
    subprocess.check_call(cmd)
879
879
 
880
880
 
 
881
@translate_exc(from_exc=OSError, to_exc=NotImplementedError)
 
882
def payload_register(ptype, klass, pid):
 
883
    """ is used while a hook is running to let Juju know that a
 
884
        payload has been started."""
 
885
    cmd = ['payload-register']
 
886
    for x in [ptype, klass, pid]:
 
887
        cmd.append(x)
 
888
    subprocess.check_call(cmd)
 
889
 
 
890
 
 
891
@translate_exc(from_exc=OSError, to_exc=NotImplementedError)
 
892
def payload_unregister(klass, pid):
 
893
    """ is used while a hook is running to let Juju know
 
894
    that a payload has been manually stopped. The <class> and <id> provided
 
895
    must match a payload that has been previously registered with juju using
 
896
    payload-register."""
 
897
    cmd = ['payload-unregister']
 
898
    for x in [klass, pid]:
 
899
        cmd.append(x)
 
900
    subprocess.check_call(cmd)
 
901
 
 
902
 
 
903
@translate_exc(from_exc=OSError, to_exc=NotImplementedError)
 
904
def payload_status_set(klass, pid, status):
 
905
    """is used to update the current status of a registered payload.
 
906
    The <class> and <id> provided must match a payload that has been previously
 
907
    registered with juju using payload-register. The <status> must be one of the
 
908
    follow: starting, started, stopping, stopped"""
 
909
    cmd = ['payload-status-set']
 
910
    for x in [klass, pid, status]:
 
911
        cmd.append(x)
 
912
    subprocess.check_call(cmd)
 
913
 
 
914
 
 
915
@translate_exc(from_exc=OSError, to_exc=NotImplementedError)
 
916
def resource_get(name):
 
917
    """used to fetch the resource path of the given name.
 
918
 
 
919
    <name> must match a name of defined resource in metadata.yaml
 
920
 
 
921
    returns either a path or False if resource not available
 
922
    """
 
923
    if not name:
 
924
        return False
 
925
 
 
926
    cmd = ['resource-get', name]
 
927
    try:
 
928
        return subprocess.check_output(cmd).decode('UTF-8')
 
929
    except subprocess.CalledProcessError:
 
930
        return False
 
931
 
 
932
 
881
933
@cached
882
934
def juju_version():
883
935
    """Full version string (eg. '1.23.3.1-trusty-amd64')"""
942
994
    for callback, args, kwargs in reversed(_atexit):
943
995
        callback(*args, **kwargs)
944
996
    del _atexit[:]
 
997
 
 
998
 
 
999
@translate_exc(from_exc=OSError, to_exc=NotImplementedError)
 
1000
def network_get_primary_address(binding):
 
1001
    '''
 
1002
    Retrieve the primary network address for a named binding
 
1003
 
 
1004
    :param binding: string. The name of a relation of extra-binding
 
1005
    :return: string. The primary IP address for the named binding
 
1006
    :raise: NotImplementedError if run on Juju < 2.0
 
1007
    '''
 
1008
    cmd = ['network-get', '--primary-address', binding]
 
1009
    return subprocess.check_output(cmd).strip()