Merge lp:~therve/landscape-client/juju-env-info-2 into lp:~landscape/landscape-client/trunk

Proposed by Thomas Herve
Status: Merged
Merged at revision: 609
Proposed branch: lp:~therve/landscape-client/juju-env-info-2
Merge into: lp:~landscape/landscape-client/trunk
Diff against target: 112 lines (+64/-2)
3 files modified
landscape/message_schemas.py (+5/-2)
landscape/monitor/computerinfo.py (+19/-0)
landscape/monitor/tests/test_computerinfo.py (+40/-0)
To merge this branch: bzr merge lp:~therve/landscape-client/juju-env-info-2
Reviewer Review Type Date Requested Status
Alberto Donato (community) Approve
Chris Glass (community) Approve
Review via email: mp+143651@code.launchpad.net

Description of the change

Here's another try: making it part of a plugin instead of registration means that the data will be used *after* the computer is registered, instead during registration where I may not have a computer yet. I'll write the server branch right away to make sure it's better.

To post a comment you must log in.
Revision history for this message
Chris Glass (tribaal) wrote :

Nice! +1

[nitpick]
+ fd = open(juju_info, "w")
+ fd.write(json.dumps({"JUJU_ENV_UUID": "uuid1",
+ "JUJU_UNIT_NAME": "unit/0"}))
+ fd.close()

Would probably benefit from a context instead, just in case:

with open(juju_info, "w") as fd:
    fd.write(juju.dumps({...}))

But yeah, nitpick, feel free to ignore.

review: Approve
Revision history for this message
Alberto Donato (ack) wrote :

Looks good! +1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'landscape/message_schemas.py'
2--- landscape/message_schemas.py 2013-01-17 08:52:48 +0000
3+++ landscape/message_schemas.py 2013-01-17 09:24:03 +0000
4@@ -68,10 +68,13 @@
5 "computer-info",
6 {"hostname": utf8,
7 "total-memory": Int(),
8- "total-swap": Int()},
9+ "total-swap": Int(),
10+ "juju-env-uuid": utf8,
11+ "juju-unit-name": utf8},
12 # Not sure why these are all optional, but it's explicitly tested
13 # in the server
14- optional=["hostname", "total-memory", "total-swap"])
15+ optional=["hostname", "total-memory", "total-swap", "juju-env-uuid",
16+ "juju-unit-name"])
17
18 DISTRIBUTION_INFO = Message(
19 "distribution-info",
20
21=== modified file 'landscape/monitor/computerinfo.py'
22--- landscape/monitor/computerinfo.py 2011-06-11 00:32:22 +0000
23+++ landscape/monitor/computerinfo.py 2013-01-17 09:24:03 +0000
24@@ -1,3 +1,5 @@
25+import os
26+import json
27 import logging
28
29 from landscape.lib.lsb_release import LSB_RELEASE_FILENAME, parse_lsb_release
30@@ -25,6 +27,8 @@
31
32 def register(self, registry):
33 super(ComputerInfo, self).register(registry)
34+ self._juju_info_path = os.path.join(registry.config.data_path,
35+ "juju-info")
36 self.call_on_accepted("computer-info",
37 self.send_computer_message, True)
38 self.call_on_accepted("distribution-info",
39@@ -59,6 +63,21 @@
40 self._add_if_new(message, "total-memory",
41 total_memory)
42 self._add_if_new(message, "total-swap", total_swap)
43+ if os.path.exists(self._juju_info_path):
44+ fd = None
45+ try:
46+ fd = open(self._juju_info_path)
47+ data = fd.read()
48+ juju_info = json.loads(data)
49+ env_uuid = juju_info.get("JUJU_ENV_UUID")
50+ unit_name = juju_info.get("JUJU_UNIT_NAME")
51+ self._add_if_new(message, "juju-env-uuid", env_uuid)
52+ self._add_if_new(message, "juju-unit-name", unit_name)
53+ except Exception:
54+ pass
55+ finally:
56+ if fd is not None:
57+ fd.close()
58 return message
59
60 def _add_if_new(self, message, key, value):
61
62=== modified file 'landscape/monitor/tests/test_computerinfo.py'
63--- landscape/monitor/tests/test_computerinfo.py 2011-07-05 05:09:11 +0000
64+++ landscape/monitor/tests/test_computerinfo.py 2013-01-17 09:24:03 +0000
65@@ -1,3 +1,5 @@
66+import os
67+import json
68 import re
69
70 from landscape.monitor.computerinfo import ComputerInfo
71@@ -325,3 +327,41 @@
72
73 self.mstore.set_accepted_types(["distribution-info", "computer-info"])
74 self.assertMessages(list(self.mstore.get_pending_messages()), [])
75+
76+ def test_juju_info(self):
77+ """
78+ L{ComputerInfo} sends data from the juju environment if the juju info
79+ file is present.
80+ """
81+ juju_info = os.path.join(self.monitor.config.data_path, "juju-info")
82+ fd = open(juju_info, "w")
83+ fd.write(json.dumps({"JUJU_ENV_UUID": "uuid1",
84+ "JUJU_UNIT_NAME": "unit/0"}))
85+ fd.close()
86+ self.mstore.set_accepted_types(["computer-info"])
87+
88+ plugin = ComputerInfo()
89+ self.monitor.add(plugin)
90+ plugin.exchange()
91+ messages = self.mstore.get_pending_messages()
92+ self.assertEqual(1, len(messages))
93+ self.assertEqual("uuid1", messages[0]["juju-env-uuid"])
94+ self.assertEqual("unit/0", messages[0]["juju-unit-name"])
95+
96+ def test_juju_info_error(self):
97+ """
98+ L{ComputerInfo} is resilient to issues reading the juju info file.
99+ """
100+ juju_info = os.path.join(self.monitor.config.data_path, "juju-info")
101+ fd = open(juju_info, "w")
102+ fd.write("foo")
103+ fd.close()
104+ os.chmod(juju_info, 0)
105+ self.mstore.set_accepted_types(["computer-info"])
106+
107+ plugin = ComputerInfo()
108+ self.monitor.add(plugin)
109+ plugin.exchange()
110+ messages = self.mstore.get_pending_messages()
111+ self.assertEqual(1, len(messages))
112+ self.assertNotIn("juju-env-uuid", messages[0])

Subscribers

People subscribed via source and target branches

to all changes: