~nottrobin/charms/precise/block-storage-broker/ensure-python-apt

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: Chad Smith
  • Date: 2014-02-11 19:59:05 UTC
  • Revision ID: chad.smith@canonical.com-20140211195905-q7daqsm1mhmg6orh
add _get_persisent_data and _persist_data functions to hooks.py to work around juju-core bug:1279018

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
from charmhelpers.core import hookenv
5
5
from charmhelpers.core.hookenv import ERROR, INFO
6
6
 
 
7
import json
7
8
import os
8
9
import sys
9
10
 
10
11
hooks = hookenv.Hooks()
11
12
 
 
13
PERSIST_DATA_FILE = "charm-data"
 
14
 
 
15
 
 
16
def _persist_data(key, value):
 
17
    """Save C{key} and C{value} pairs in a persistent file for reference"""
 
18
    data = {}
 
19
    filepath = "%s/%s" % (hookenv.charm_dir(), PERSIST_DATA_FILE)
 
20
    if os.path.exists(filepath):
 
21
        with open(filepath) as inputfile:
 
22
            data = json.load(inputfile)
 
23
    data[key] = value
 
24
    with open(filepath, "w") as outfile:
 
25
        outfile.write(unicode(json.dumps(data, ensure_ascii=False)))
 
26
 
 
27
 
 
28
def _get_persistent_data(key=None, remove_values=False):
 
29
    """Get a persistent data as specified by C{key} from the PERSIST_DATA_FILE
 
30
    If C{remove_values} is C{True} remove the specified C{key} from the data
 
31
    file.
 
32
    """
 
33
    data = {}
 
34
    filepath = "%s/%s" % (hookenv.charm_dir(), PERSIST_DATA_FILE)
 
35
    with open(filepath) as inputfile:
 
36
        data = json.load(inputfile)
 
37
    if key:
 
38
        value = data.get(key, None)
 
39
        if remove_values:
 
40
            data.pop(key, None)
 
41
            if data:
 
42
                with open(filepath, "w") as outfile:
 
43
                    outfile.write(
 
44
                        unicode(json.dumps(data, ensure_ascii=False)))
 
45
    else:
 
46
        value = data
 
47
        if remove_values:
 
48
            if os.path.exists(filepath):
 
49
                os.remove(filepath)
 
50
    return value
 
51
 
12
52
 
13
53
@hooks.hook()
14
54
def config_changed():
41
81
def block_storage_relation_departed():
42
82
    """Detach a nova volume from the related instance when relation departs"""
43
83
    import nova_util
44
 
    instance_id = hookenv.relation_get('instance-id')
 
84
    # XXX juju bug:1279018 for departed-hooks to see relation-data
 
85
    instance_id = _get_persistent_data(
 
86
        hookenv.remote_unit(), remove_values=True)
45
87
    if not instance_id:
46
88
        hookenv.log(
47
89
            "Cannot detach nova volume from instance without instance-id",
62
104
    import nova_util
63
105
    instance_id = hookenv.relation_get('instance-id')
64
106
    volume_label = hookenv.relation_get('volume-label')
 
107
    _persist_data(hookenv.remote_unit(), instance_id)
65
108
    if not instance_id:
66
109
        hookenv.log("Waiting for relation to define instance-id", INFO)
67
110
        return