4
4
from charmhelpers.core import hookenv
5
5
from charmhelpers.core.hookenv import ERROR, INFO
10
11
hooks = hookenv.Hooks()
13
PERSIST_DATA_FILE = "charm-data"
16
def _persist_data(key, value):
17
"""Save C{key} and C{value} pairs in a persistent file for reference"""
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)
24
with open(filepath, "w") as outfile:
25
outfile.write(unicode(json.dumps(data, ensure_ascii=False)))
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
34
filepath = "%s/%s" % (hookenv.charm_dir(), PERSIST_DATA_FILE)
35
with open(filepath) as inputfile:
36
data = json.load(inputfile)
38
value = data.get(key, None)
42
with open(filepath, "w") as outfile:
44
unicode(json.dumps(data, ensure_ascii=False)))
48
if os.path.exists(filepath):
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"""
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:
47
89
"Cannot detach nova volume from instance without instance-id",
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)