Merge lp:~hopem/charm-helpers/lp1694963 into lp:charm-helpers

Proposed by Edward Hope-Morley
Status: Merged
Merged at revision: 751
Proposed branch: lp:~hopem/charm-helpers/lp1694963
Merge into: lp:charm-helpers
Diff against target: 128 lines (+94/-0)
2 files modified
charmhelpers/contrib/storage/linux/ceph.py (+42/-0)
tests/contrib/storage/test_linux_ceph.py (+52/-0)
To merge this branch: bzr merge lp:~hopem/charm-helpers/lp1694963
Reviewer Review Type Date Requested Status
Alex Kavanagh Pending
Review via email: mp+325400@code.launchpad.net

This proposal supersedes a proposal from 2017-06-09.

To post a comment you must log in.
Revision history for this message
Alex Kavanagh (ajkavanagh) wrote : Posted in a previous version of this proposal

I'm not sure I understand the rationale of using another store for this persistent data? why not just use the regular kv() store and just use the same 'namespace' that it already being proposed; it's unlikely to clash with anything else. Plus, the decorator will not be needed, and it simplifies the code.

review: Needs Fixing
Revision history for this message
Billy Olsen (billy-olsen) wrote : Posted in a previous version of this proposal

This looks good to me.

review: Approve
Revision history for this message
Alex Kavanagh (ajkavanagh) wrote : Posted in a previous version of this proposal

Sorry, probably already merged, but please take notice of these comments for the future.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/contrib/storage/linux/ceph.py'
2--- charmhelpers/contrib/storage/linux/ceph.py 2017-03-29 13:20:58 +0000
3+++ charmhelpers/contrib/storage/linux/ceph.py 2017-06-09 15:20:01 +0000
4@@ -63,6 +63,7 @@
5 from charmhelpers.fetch import (
6 apt_install,
7 )
8+from charmhelpers.core.unitdata import kv
9
10 from charmhelpers.core.kernel import modprobe
11 from charmhelpers.contrib.openstack.utils import config_flags_parser
12@@ -1314,6 +1315,47 @@
13 relation_set(relation_id=rid, broker_req=request.request)
14
15
16+def is_broker_action_done(action, rid=None, unit=None):
17+ """Check whether broker action has completed yet.
18+
19+ @param action: name of action to be performed
20+ @returns True if action complete otherwise False
21+ """
22+ rdata = relation_get(rid, unit) or {}
23+ broker_rsp = rdata.get(get_broker_rsp_key())
24+ if not broker_rsp:
25+ return False
26+
27+ rsp = CephBrokerRsp(broker_rsp)
28+ unit_name = local_unit().partition('/')[2]
29+ key = "unit_{}_ceph_broker_action.{}".format(unit_name, action)
30+ kvstore = kv()
31+ val = kvstore.get(key=key)
32+ if val and val == rsp.request_id:
33+ return True
34+
35+ return False
36+
37+
38+def mark_broker_action_done(action, rid=None, unit=None):
39+ """Mark action as having been completed.
40+
41+ @param action: name of action to be performed
42+ @returns None
43+ """
44+ rdata = relation_get(rid, unit) or {}
45+ broker_rsp = rdata.get(get_broker_rsp_key())
46+ if not broker_rsp:
47+ return
48+
49+ rsp = CephBrokerRsp(broker_rsp)
50+ unit_name = local_unit().partition('/')[2]
51+ key = "unit_{}_ceph_broker_action.{}".format(unit_name, action)
52+ kvstore = kv()
53+ kvstore.set(key=key, value=rsp.request_id)
54+ kvstore.flush()
55+
56+
57 class CephConfContext(object):
58 """Ceph config (ceph.conf) context.
59
60
61=== modified file 'tests/contrib/storage/test_linux_ceph.py'
62--- tests/contrib/storage/test_linux_ceph.py 2017-03-29 13:20:58 +0000
63+++ tests/contrib/storage/test_linux_ceph.py 2017-06-09 15:20:01 +0000
64@@ -7,8 +7,11 @@
65 from testtools import TestCase
66 import json
67 import copy
68+import shutil
69
70 import charmhelpers.contrib.storage.linux.ceph as ceph_utils
71+
72+from charmhelpers.core.unitdata import Storage
73 from subprocess import CalledProcessError
74 from tests.helpers import patch_open, FakeRelation
75 import nose.plugins.attrib
76@@ -1397,3 +1400,52 @@
77 mock_config.return_value = ("{'osd': {'foo': 1},"
78 "'unknown': {'blah': 1}}")
79 self.assertEqual({'osd': {'foo': 1}}, ctxt)
80+
81+ @patch.object(ceph_utils, 'local_unit', lambda: "nova-compute/0")
82+ def test_is_broker_action_done(self):
83+ tmpdir = mkdtemp()
84+ try:
85+ db_path = '{}/kv.db'.format(tmpdir)
86+ with patch('charmhelpers.core.unitdata._KV', Storage(db_path)):
87+ rq_id = "3d03e9f6-4c36-11e7-89ba-fa163e7c7ec6"
88+ broker_key = ceph_utils.get_broker_rsp_key()
89+ self.relation_get.return_value = {broker_key:
90+ json.dumps({"request-id":
91+ rq_id,
92+ "exit-code": 0})}
93+ action = 'restart_nova_compute'
94+ ret = ceph_utils.is_broker_action_done(action, rid="ceph:1",
95+ unit="ceph/0")
96+ self.relation_get.assert_has_calls([call('ceph:1', 'ceph/0')])
97+ self.assertFalse(ret)
98+
99+ ceph_utils.mark_broker_action_done(action)
100+ self.assertTrue(os.path.exists(tmpdir))
101+ ret = ceph_utils.is_broker_action_done(action, rid="ceph:1",
102+ unit="ceph/0")
103+ self.assertTrue(ret)
104+ finally:
105+ if os.path.exists(tmpdir):
106+ shutil.rmtree(tmpdir)
107+
108+ @patch.object(ceph_utils, 'local_unit', lambda: "nova-compute/0")
109+ def test_mark_broker_action_done(self):
110+ tmpdir = mkdtemp()
111+ try:
112+ db_path = '{}/kv.db'.format(tmpdir)
113+ with patch('charmhelpers.core.unitdata._KV', Storage(db_path)):
114+ rq_id = "3d03e9f6-4c36-11e7-89ba-fa163e7c7ec6"
115+ broker_key = ceph_utils.get_broker_rsp_key()
116+ self.relation_get.return_value = {broker_key:
117+ json.dumps({"request-id":
118+ rq_id})}
119+ action = 'restart_nova_compute'
120+ ceph_utils.mark_broker_action_done(action, rid="ceph:1",
121+ unit="ceph/0")
122+ key = 'unit_0_ceph_broker_action.{}'.format(action)
123+ self.relation_get.assert_has_calls([call('ceph:1', 'ceph/0')])
124+ kvstore = Storage(db_path)
125+ self.assertEqual(kvstore.get(key=key), rq_id)
126+ finally:
127+ if os.path.exists(tmpdir):
128+ shutil.rmtree(tmpdir)

Subscribers

People subscribed via source and target branches