Merge lp:~michael.nelson/charm-helpers/namespace-relation-data into lp:charm-helpers

Proposed by Michael Nelson
Status: Merged
Merged at revision: 62
Proposed branch: lp:~michael.nelson/charm-helpers/namespace-relation-data
Merge into: lp:charm-helpers
Prerequisite: lp:~michael.nelson/charm-helpers/ensure_etc_salt_exists
Diff against target: 99 lines (+56/-5)
2 files modified
charmhelpers/contrib/saltstack/__init__.py (+17/-2)
tests/contrib/saltstack/test_saltstates.py (+39/-3)
To merge this branch: bzr merge lp:~michael.nelson/charm-helpers/namespace-relation-data
Reviewer Review Type Date Requested Status
Matthew Wedgwood (community) Approve
Review via email: mp+170575@code.launchpad.net

Description of the change

This branch namespaces the relationship data stored in the salt grains file (for use in the machine states or template files).

It then additionally makes this data persistent. Rather than re-writing the grains each time update_machine_state() is called, it updates the grains instead (so the relation data is collected and/or updated over time, but always available to the templates/machine_states).

Finally, I just added a small host.apt_add_repository that automatically apt-updates by default to keep my own hooks cleaner. Let me know if this is done elsewhere.

I've QA'd this functionality on my charm.

Thanks!

To post a comment you must log in.
43. By Michael Nelson

Re-activated two tests that had been deactivated for debugging.

Revision history for this message
Matthew Wedgwood (mew) wrote :

Michael,

This looks to be based on your other MP with the hookenv changes. If you can re-base it to trunk, I'll be glad to take another look.

Revision history for this message
Matthew Wedgwood (mew) wrote :

Because of r42, this needs rebasing to trunk.

review: Needs Fixing
44. By Michael Nelson

Merged ensure_etc_salt_exists into namespace-relation-data.

Revision history for this message
Michael Nelson (michael.nelson) wrote :

Yes, the other branch/MP is listed above as the prerequisite (which is why the diff only includes the changes between that prereq and this one).

Anyway, I've remerged, so it now includes r42 and the updates to the prerequisite.

Thanks!

45. By Michael Nelson

Existing grains are updated from new config, rather than new config updated from existing grains.

Revision history for this message
Matthew Wedgwood (mew) wrote :

Sorry I'm just getting around to looking at this again.

As apt operations are related to installation of extra, normally external resources, that functionality lives in charmhelpers.fetch. This also keeps core leaning in the direction of being cross-platform (apt, RPM, Mac, Windows, etc.). There's already a related function in fetch. Can you see if that's good enough?

Also, on the subject of running apt-get update, I think it's best to have it default to False as there may be several repos added and running update each time would be inefficient. I would even prefer that it be a separate function.

review: Needs Fixing
46. By Michael Nelson

Merge trunk.

47. By Michael Nelson

Removed host.apt_add_repository

Revision history for this message
Matthew Wedgwood (mew) wrote :

Thanks for the changes! Looks good.

review: Approve
Revision history for this message
Michael Nelson (michael.nelson) wrote :

After chatting the other day, I just removed the apt_add_repository helper as I wasn't aware of the fetch module.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/contrib/saltstack/__init__.py'
2--- charmhelpers/contrib/saltstack/__init__.py 2013-06-20 07:37:17 +0000
3+++ charmhelpers/contrib/saltstack/__init__.py 2013-07-18 18:23:22 +0000
4@@ -115,7 +115,15 @@
5 # file resources etc.
6 config['charm_dir'] = charm_dir
7 config['local_unit'] = charmhelpers.core.hookenv.local_unit()
8- config.update(charmhelpers.core.hookenv.relation_get())
9+
10+ # Add any relation data prefixed with the relation type.
11+ relation_type = charmhelpers.core.hookenv.relation_type()
12+ if relation_type is not None:
13+ relation_data = charmhelpers.core.hookenv.relation_get()
14+ relation_data = dict(
15+ ("{}:{}".format(relation_type, key), val)
16+ for key, val in relation_data.items())
17+ config.update(relation_data)
18
19 # Don't use non-standard tags for unicode which will not
20 # work when salt uses yaml.load_safe.
21@@ -127,5 +135,12 @@
22 if not os.path.exists(grains_dir):
23 os.makedirs(grains_dir)
24
25+ if os.path.exists(salt_grains_path):
26+ with open(salt_grains_path, "r") as grain_file:
27+ grains = yaml.load(grain_file.read())
28+ else:
29+ grains = {}
30+
31+ grains.update(config)
32 with open(salt_grains_path, "w+") as fp:
33- fp.write(config.yaml())
34+ fp.write(yaml.dump(grains))
35
36=== modified file 'tests/contrib/saltstack/test_saltstates.py'
37--- tests/contrib/saltstack/test_saltstates.py 2013-06-20 07:34:15 +0000
38+++ tests/contrib/saltstack/test_saltstates.py 2013-07-18 18:23:22 +0000
39@@ -96,6 +96,10 @@
40 self.mock_relation_get = patcher.start()
41 self.mock_relation_get.return_value = {}
42 self.addCleanup(patcher.stop)
43+ patcher = mock.patch('charmhelpers.core.hookenv.relation_type')
44+ self.mock_relation_type = patcher.start()
45+ self.mock_relation_type.return_value = None
46+ self.addCleanup(patcher.stop)
47 patcher = mock.patch('charmhelpers.core.hookenv.local_unit')
48 self.mock_local_unit = patcher.start()
49 self.addCleanup(patcher.stop)
50@@ -137,6 +141,7 @@
51 'group_code_owner': 'webops_deploy',
52 'user_code_runner': 'ubunet',
53 })
54+ self.mock_relation_type.return_value = 'wsgi-file'
55 self.mock_relation_get.return_value = {
56 'relation_key1': 'relation_value1',
57 'relation_key2': 'relation_value2',
58@@ -151,7 +156,38 @@
59 "charm_dir": "/tmp/charm_dir",
60 "group_code_owner": "webops_deploy",
61 "user_code_runner": "ubunet",
62- "relation_key1": "relation_value1",
63- "relation_key2": "relation_value2",
64- "local_unit": "click-index/3",
65+ "wsgi-file:relation_key1": "relation_value1",
66+ "wsgi-file:relation_key2": "relation_value2",
67+ "local_unit": "click-index/3",
68+ }, result)
69+
70+ def test_updates_existing_values(self):
71+ """Data stored in grains is retained.
72+
73+ This may be helpful so that templates can access information
74+ from relations outside the current context.
75+ """
76+ os.makedirs(os.path.dirname(self.grain_path))
77+ with open(self.grain_path, 'w+') as grain_file:
78+ grain_file.write(yaml.dump({
79+ 'solr:hostname': 'example.com',
80+ 'user_code_runner': 'oldvalue',
81+ }))
82+
83+ self.mock_config.return_value = charmhelpers.core.hookenv.Serializable({
84+ 'group_code_owner': 'webops_deploy',
85+ 'user_code_runner': 'newvalue',
86+ })
87+ self.mock_local_unit.return_value = "click-index/3"
88+
89+ charmhelpers.contrib.saltstack.juju_config_2_grains()
90+
91+ with open(self.grain_path, 'r') as grain_file:
92+ result = yaml.load(grain_file.read())
93+ self.assertEqual({
94+ "charm_dir": "/tmp/charm_dir",
95+ "group_code_owner": "webops_deploy",
96+ "user_code_runner": "newvalue",
97+ "local_unit": "click-index/3",
98+ "solr:hostname": "example.com",
99 }, result)

Subscribers

People subscribed via source and target branches