~openstack-charmers-archive/charms/trusty/neutron-gateway/trunk

« back to all changes in this revision

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

  • Committer: James Page
  • Date: 2015-10-22 13:23:58 UTC
  • Revision ID: james.page@ubuntu.com-20151022132358-qin1nvlnqn4aezaz
Tags: 15.10
15.10 Charm release

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
 
try:
38
 
    from charmhelpers.cli import cmdline
39
 
except ImportError as e:
40
 
    # due to the anti-pattern of partially synching charmhelpers directly
41
 
    # into charms, it's possible that charmhelpers.cli is not available;
42
 
    # if that's the case, they don't really care about using the cli anyway,
43
 
    # so mock it out
44
 
    if str(e) == 'No module named cli':
45
 
        class cmdline(object):
46
 
            @classmethod
47
 
            def subcommand(cls, *args, **kwargs):
48
 
                def _wrap(func):
49
 
                    return func
50
 
                return _wrap
51
 
    else:
52
 
        raise
53
 
 
54
37
import six
55
38
if not six.PY3:
56
39
    from UserDict import UserDict
91
74
        res = func(*args, **kwargs)
92
75
        cache[key] = res
93
76
        return res
 
77
    wrapper._wrapped = func
94
78
    return wrapper
95
79
 
96
80
 
190
174
    return os.environ.get('JUJU_RELATION', None)
191
175
 
192
176
 
193
 
@cmdline.subcommand()
194
177
@cached
195
178
def relation_id(relation_name=None, service_or_unit=None):
196
179
    """The relation ID for the current or a specified relation"""
216
199
    return os.environ.get('JUJU_REMOTE_UNIT', None)
217
200
 
218
201
 
219
 
@cmdline.subcommand()
220
202
def service_name():
221
203
    """The name service group this unit belongs to"""
222
204
    return local_unit().split('/')[0]
223
205
 
224
206
 
225
 
@cmdline.subcommand()
226
207
@cached
227
208
def remote_service_name(relid=None):
228
209
    """The remote service name for a given relation-id (or the current relation)"""
642
623
    return unit_get('private-address')
643
624
 
644
625
 
 
626
@cached
 
627
def storage_get(attribute="", storage_id=""):
 
628
    """Get storage attributes"""
 
629
    _args = ['storage-get', '--format=json']
 
630
    if storage_id:
 
631
        _args.extend(('-s', storage_id))
 
632
    if attribute:
 
633
        _args.append(attribute)
 
634
    try:
 
635
        return json.loads(subprocess.check_output(_args).decode('UTF-8'))
 
636
    except ValueError:
 
637
        return None
 
638
 
 
639
 
 
640
@cached
 
641
def storage_list(storage_name=""):
 
642
    """List the storage IDs for the unit"""
 
643
    _args = ['storage-list', '--format=json']
 
644
    if storage_name:
 
645
        _args.append(storage_name)
 
646
    try:
 
647
        return json.loads(subprocess.check_output(_args).decode('UTF-8'))
 
648
    except ValueError:
 
649
        return None
 
650
    except OSError as e:
 
651
        import errno
 
652
        if e.errno == errno.ENOENT:
 
653
            # storage-list does not exist
 
654
            return []
 
655
        raise
 
656
 
 
657
 
645
658
class UnregisteredHookError(Exception):
646
659
    """Raised when an undefined hook is called"""
647
660
    pass
786
799
 
787
800
 
788
801
def status_get():
789
 
    """Retrieve the previously set juju workload state
790
 
 
791
 
    If the status-set command is not found then assume this is juju < 1.23 and
792
 
    return 'unknown'
 
802
    """Retrieve the previously set juju workload state and message
 
803
 
 
804
    If the status-get command is not found then assume this is juju < 1.23 and
 
805
    return 'unknown', ""
 
806
 
793
807
    """
794
 
    cmd = ['status-get']
 
808
    cmd = ['status-get', "--format=json", "--include-data"]
795
809
    try:
796
 
        raw_status = subprocess.check_output(cmd, universal_newlines=True)
797
 
        status = raw_status.rstrip()
798
 
        return status
 
810
        raw_status = subprocess.check_output(cmd)
799
811
    except OSError as e:
800
812
        if e.errno == errno.ENOENT:
801
 
            return 'unknown'
 
813
            return ('unknown', "")
802
814
        else:
803
815
            raise
 
816
    else:
 
817
        status = json.loads(raw_status.decode("UTF-8"))
 
818
        return (status["status"], status["message"])
804
819
 
805
820
 
806
821
def translate_exc(from_exc, to_exc):