Merge lp:~free.ekanayaka/charms/trusty/landscape-client/drop-unit-name-from-juju-info into lp:charms/trusty/landscape-client

Proposed by Free Ekanayaka
Status: Superseded
Proposed branch: lp:~free.ekanayaka/charms/trusty/landscape-client/drop-unit-name-from-juju-info
Merge into: lp:charms/trusty/landscape-client
Diff against target: 251 lines (+40/-54)
4 files modified
hooks/common.py (+17/-8)
hooks/hooks.py (+5/-21)
hooks/install.py (+6/-2)
hooks/test_hooks.py (+12/-23)
To merge this branch: bzr merge lp:~free.ekanayaka/charms/trusty/landscape-client/drop-unit-name-from-juju-info
Reviewer Review Type Date Requested Status
Landscape Pending
Landscape Pending
Review via email: mp+235756@code.launchpad.net

Description of the change

This branch drops the unit-name field from the content of the juju info file, and changes the code to generate a single json file as opposed to one per principal unit, since we now only need the machine->computer association.

To post a comment you must log in.
47. By Free Ekanayaka

Drop private-address from juju-info

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hooks/common.py'
2--- hooks/common.py 2014-08-28 09:54:11 +0000
3+++ hooks/common.py 2014-09-24 07:49:01 +0000
4@@ -30,6 +30,19 @@
5 """Get relation configuration from Juju."""
6 return self._run_juju_tool("relation-get")
7
8+ def get_machine_id(self):
9+ """Return the Juju machine ID of this unit."""
10+ # XXX once #1359714 is fixed this method can be dropped and we can use
11+ # the JUJU_MACHINE_ID environment variable.
12+
13+ # Sniff the value of the machine ID by looking at the name of the
14+ # directory where hooks live. It will be something like
15+ # 'machine-0-lxc-1'.
16+ pattern = "../../machine-*"
17+ match = glob(pattern)[0]
18+ dirname = os.path.basename(match)
19+ return dirname.lstrip("machine-").replace("-", "/")
20+
21 def get_service_config(self):
22 """Get service configuration from Juju."""
23 return self._run_juju_tool("config-get")
24@@ -88,7 +101,7 @@
25 is_configured_enough = all([
26 self.config.get("account_name"),
27 self.config.get("computer_title"),
28- get_juju_info_filenames(self.juju_directory)])
29+ os.path.exists(self.juju_filename)])
30 if not try_to_register:
31 # We don't need to further check the configuration if we are
32 # deleting it.
33@@ -102,11 +115,11 @@
34 return 0
35
36 @property
37- def juju_directory(self):
38+ def juju_filename(self):
39 try:
40- return self.config.juju_directory
41+ return self.config.juju_filename
42 except AttributeError: # older landscape-client versions
43- return os.path.join(self.config.data_path, "juju-info.d")
44+ return os.path.join(self.config.data_path, "juju-info.json")
45
46 def clear_registration(self):
47 """
48@@ -187,7 +200,3 @@
49 """Dump content in JSON format to the specified file."""
50 with open(filename, "w") as dumpfile:
51 json.dump(content, dumpfile)
52-
53-
54-def get_juju_info_filenames(juju_directory):
55- return glob("%s/*.json" % juju_directory)
56
57=== modified file 'hooks/hooks.py'
58--- hooks/hooks.py 2014-08-28 09:54:11 +0000
59+++ hooks/hooks.py 2014-09-24 07:49:01 +0000
60@@ -11,8 +11,7 @@
61 from subprocess import CalledProcessError
62
63 from common import (
64- write_json_file, JujuBroker, LandscapeBroker, chown,
65- get_juju_info_filenames)
66+ write_json_file, JujuBroker, LandscapeBroker, chown)
67 from ceph import (
68 get_ceph_client_path, write_ceph_client_keyring, write_ceph_client_config)
69
70@@ -71,29 +70,15 @@
71 "" % landscape_broker.local_unit)
72 landscape_broker.config.reload()
73 relation_conf = juju_broker.get_relation_config()
74- remote_unit_name = juju_broker.environment.get("JUJU_REMOTE_UNIT")
75 # We use the remote unit for the unit name, since we want to associate this
76 # client with the unit it's managing, not its own unit.
77 juju_info = {
78 "environment-uuid": juju_broker.environment.get("JUJU_ENV_UUID"),
79- "unit-name": remote_unit_name,
80+ "machine-id": juju_broker.get_machine_id(),
81 "api-addresses": juju_broker.environment.get("JUJU_API_ADDRESSES"),
82 "private-address": relation_conf.get("private-address")}
83
84- # Let's not use any "/" in a filename.
85- unit_name = remote_unit_name.replace("/", "-")
86- juju_file = "%s.json" % unit_name
87-
88- juju_info_dir = landscape_broker.juju_directory
89-
90- try:
91- os.mkdir(juju_info_dir)
92- except OSError:
93- pass # The file already exists.
94-
95- juju_info_file = os.path.join(juju_info_dir, juju_file)
96-
97- write_json_file(juju_info_file, juju_info)
98+ write_json_file(landscape_broker.juju_filename, juju_info)
99 exit_code = landscape_broker.update_client_config(
100 {"computer-title": socket.gethostname()})
101
102@@ -111,9 +96,8 @@
103 landscape_broker.clear_registration()
104
105 # Delete all of the juju-info files.
106- files_to_delete = get_juju_info_filenames(landscape_broker.juju_directory)
107- for to_delete in files_to_delete:
108- os.remove(to_delete)
109+ if os.path.exists(landscape_broker.juju_filename):
110+ os.remove(landscape_broker.juju_filename)
111 landscape_broker.update_client_config(
112 {"computer-title": ""}, try_to_register=False)
113
114
115=== modified file 'hooks/install.py'
116--- hooks/install.py 2014-08-28 10:09:11 +0000
117+++ hooks/install.py 2014-09-24 07:49:01 +0000
118@@ -8,6 +8,9 @@
119 import os
120 import subprocess
121 import sys
122+
123+from glob import glob
124+
125 from charmhelpers.core.hookenv import (
126 Hooks, UnregisteredHookError, log)
127 from charmhelpers.fetch import (
128@@ -43,6 +46,7 @@
129 data_path])
130
131
132+
133 def build_from_launchpad(url):
134 """The charm will install the code from the passed lp branch.
135 """
136@@ -55,8 +59,8 @@
137 subprocess.check_call(["make", "package"], env=env)
138 #TODO: The following call should be retried (potential race condition to
139 # acquire the dpkg lock).
140- subprocess.call(["dpkg", "-i", "../landscape-client_*.deb",
141- "../landscape-common_*.deb"])
142+ subprocess.call(["dpkg", "-i", glob("../landscape-client_*.deb")[0],
143+ glob("../landscape-common_*.deb")[0]])
144 # The _run_apt_command will ensure the command is retried in case we cannot
145 # acquire the lock for some reason.
146 _run_apt_command(["apt-get", "-f", "install"], fatal=True)
147
148=== modified file 'hooks/test_hooks.py'
149--- hooks/test_hooks.py 2014-08-28 09:54:11 +0000
150+++ hooks/test_hooks.py 2014-09-24 07:49:01 +0000
151@@ -30,6 +30,9 @@
152 def log(self, message):
153 self.logs.append(message)
154
155+ def get_machine_id(self):
156+ return "1"
157+
158 def _run_juju_tool(self, command):
159 """Override _run_juju_tool not not execute any commands."""
160 return self.commands[command]
161@@ -64,12 +67,8 @@
162 self.landscape_broker.config.write()
163 self.juju_broker = FakeJujuBroker()
164
165- def touch_example_juju_info_file(self):
166- if not os.path.exists(self.landscape_broker.juju_directory):
167- os.mkdir(self.landscape_broker.juju_directory)
168- filename = os.path.join(self.landscape_broker.juju_directory,
169- "whatever.json")
170- write_json_file(filename=filename, content={})
171+ def touch_juju_info_file(self):
172+ write_json_file(self.landscape_broker.juju_filename, content={})
173
174 def mkdtemp(self):
175 directory = mkdtemp(prefix=self.__class__.__name__)
176@@ -99,6 +98,7 @@
177 single JSON file.
178 """
179 juju_info = {"juju-env-uuid": "0afcd28d-6263-43fb-8131-afa696a984f8",
180+ "juju-machine-id": "1",
181 "juju-unit-name": "postgresql/0",
182 "juju-api-addresses": "10.0.3.1:17070",
183 "juju-private-address": "10.0.3.205"}
184@@ -171,20 +171,17 @@
185 """
186 self.juju_broker.environment.update({
187 "JUJU_ENV_UUID": "uuid1",
188- "JUJU_REMOTE_UNIT": "unit/0",
189 "JUJU_API_ADDRESSES": "10.0.0.1:1234"})
190 self.juju_broker.commands["relation-get"] = {
191 "private-address": "10.0.0.99"}
192 hooks.container_relation_joined(juju_broker=self.juju_broker,
193 landscape_broker=self.landscape_broker)
194
195- juju_info_path = os.path.join(
196- self.landscape_broker.juju_directory, "unit-0.json")
197-
198+ juju_info_path = self.landscape_broker.juju_filename
199 juju_info = json.loads(read_file(juju_info_path))
200 self.assertEqual(
201 {"environment-uuid": "uuid1",
202- "unit-name": "unit/0",
203+ "machine-id": "1",
204 "api-addresses": "10.0.0.1:1234",
205 "private-address": "10.0.0.99"},
206 juju_info)
207@@ -198,9 +195,7 @@
208 registration_attempts = []
209
210 def record_registration_attempt():
211- juju_directory = self.landscape_broker.juju_directory
212- juju_file_list = glob("%s/*.json" % juju_directory)
213- for _ in juju_file_list:
214+ if os.path.exists(self.landscape_broker.juju_filename):
215 registration_attempts.append(True)
216
217 self.landscape_broker.try_to_register = record_registration_attempt
218@@ -249,14 +244,8 @@
219 since it contains information about the remove unit (which is
220 now gone).
221 """
222- juju_info_path = os.path.join(
223- self.landscape_broker.juju_directory, "unit-0.json")
224-
225- if not os.path.exists(self.landscape_broker.juju_directory):
226- os.mkdir(self.landscape_broker.juju_directory)
227-
228+ juju_info_path = self.landscape_broker.juju_filename
229 juju_info = {"environment-uuid": "uuid1",
230- "unit-name": "unit/0",
231 "api-addresses": "10.0.0.1:1234",
232 "private-address": "10.0.0.99"}
233 write_json_file(juju_info_path, juju_info)
234@@ -406,7 +395,7 @@
235 super(ConfigChangedTest, self).setUp()
236 self.landscape_broker.config.account_name = "account1"
237 self.landscape_broker.config.computer_title = "title"
238- self.touch_example_juju_info_file()
239+ self.touch_juju_info_file()
240 self.addCleanup(setattr, hooks, "CERT_FILE", hooks.CERT_FILE)
241 tmp_dir = self.mkdtemp()
242 hooks.CERT_FILE = "%s/cert" % tmp_dir
243@@ -435,7 +424,7 @@
244 super(RegistrationRelationJoinedTest, self).setUp()
245 self.landscape_broker.config.account_name = "account1"
246 self.landscape_broker.config.computer_title = "title"
247- self.touch_example_juju_info_file()
248+ self.touch_juju_info_file()
249 self.addCleanup(setattr, hooks, "CERT_FILE", hooks.CERT_FILE)
250 tmp_dir = self.mkdtemp()
251 hooks.CERT_FILE = "%s/cert" % tmp_dir

Subscribers

People subscribed via source and target branches