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

Proposed by Free Ekanayaka
Status: Merged
Approved by: Free Ekanayaka
Approved revision: 47
Merged at revision: 46
Proposed branch: lp:~free.ekanayaka/charms/trusty/landscape-client/drop-unit-name-from-juju-info
Merge into: lp:~landscape/charms/trusty/landscape-client/trunk
Diff against target: 229 lines (+25/-68)
3 files modified
hooks/common.py (+4/-8)
hooks/hooks.py (+11/-28)
hooks/test_hooks.py (+10/-32)
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
Chris Glass (community) Approve
Alberto Donato (community) Approve
Review via email: mp+235789@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.
Revision history for this message
Alberto Donato (ack) wrote :

LGTM, +1

One question inline.

#1:
I think we could drop "private-address" too. It's optional in the message and we don't use it on the server.

review: Approve
47. By Free Ekanayaka

Drop private-address from juju-info

Revision history for this message
Free Ekanayaka (free.ekanayaka) wrote :

#1

Fixed.

Revision history for this message
Chris Glass (tribaal) wrote :

+1! Ship it :)

review: Approve

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-09-02 13:55:31 +0000
3+++ hooks/common.py 2014-09-25 07:00:36 +0000
4@@ -101,7 +101,7 @@
5 is_configured_enough = all([
6 self.config.get("account_name"),
7 self.config.get("computer_title"),
8- get_juju_info_filenames(self.juju_directory)])
9+ os.path.exists(self.juju_filename)])
10 if not try_to_register:
11 # We don't need to further check the configuration if we are
12 # deleting it.
13@@ -115,11 +115,11 @@
14 return 0
15
16 @property
17- def juju_directory(self):
18+ def juju_filename(self):
19 try:
20- return self.config.juju_directory
21+ return self.config.juju_filename
22 except AttributeError: # older landscape-client versions
23- return os.path.join(self.config.data_path, "juju-info.d")
24+ return os.path.join(self.config.data_path, "juju-info.json")
25
26 def clear_registration(self):
27 """
28@@ -200,7 +200,3 @@
29 """Dump content in JSON format to the specified file."""
30 with open(filename, "w") as dumpfile:
31 json.dump(content, dumpfile)
32-
33-
34-def get_juju_info_filenames(juju_directory):
35- return glob("%s/*.json" % juju_directory)
36
37=== modified file 'hooks/hooks.py'
38--- hooks/hooks.py 2014-08-28 12:13:31 +0000
39+++ hooks/hooks.py 2014-09-25 07:00:36 +0000
40@@ -11,8 +11,7 @@
41 from subprocess import CalledProcessError
42
43 from common import (
44- write_json_file, JujuBroker, LandscapeBroker, chown,
45- get_juju_info_filenames)
46+ write_json_file, JujuBroker, LandscapeBroker, chown)
47 from ceph import (
48 get_ceph_client_path, write_ceph_client_keyring, write_ceph_client_config)
49
50@@ -70,31 +69,14 @@
51 juju_broker.log("In container-relation-joined for %s"
52 "" % landscape_broker.local_unit)
53 landscape_broker.config.reload()
54- relation_conf = juju_broker.get_relation_config()
55- remote_unit_name = juju_broker.environment.get("JUJU_REMOTE_UNIT")
56 # We use the remote unit for the unit name, since we want to associate this
57 # client with the unit it's managing, not its own unit.
58 juju_info = {
59 "environment-uuid": juju_broker.environment.get("JUJU_ENV_UUID"),
60- "unit-name": remote_unit_name,
61 "machine-id": juju_broker.get_machine_id(),
62- "api-addresses": juju_broker.environment.get("JUJU_API_ADDRESSES"),
63- "private-address": relation_conf.get("private-address")}
64-
65- # Let's not use any "/" in a filename.
66- unit_name = remote_unit_name.replace("/", "-")
67- juju_file = "%s.json" % unit_name
68-
69- juju_info_dir = landscape_broker.juju_directory
70-
71- try:
72- os.mkdir(juju_info_dir)
73- except OSError:
74- pass # The file already exists.
75-
76- juju_info_file = os.path.join(juju_info_dir, juju_file)
77-
78- write_json_file(juju_info_file, juju_info)
79+ "api-addresses": juju_broker.environment.get("JUJU_API_ADDRESSES")}
80+
81+ write_json_file(landscape_broker.juju_filename, juju_info)
82 exit_code = landscape_broker.update_client_config(
83 {"computer-title": socket.gethostname()})
84
85@@ -112,9 +94,8 @@
86 landscape_broker.clear_registration()
87
88 # Delete all of the juju-info files.
89- files_to_delete = get_juju_info_filenames(landscape_broker.juju_directory)
90- for to_delete in files_to_delete:
91- os.remove(to_delete)
92+ if os.path.exists(landscape_broker.juju_filename):
93+ os.remove(landscape_broker.juju_filename)
94 landscape_broker.update_client_config(
95 {"computer-title": ""}, try_to_register=False)
96
97@@ -180,16 +161,18 @@
98 juju_key_map = {"juju-env-uuid": "environment-uuid",
99 "juju-unit-name": "unit-name",
100 "juju-api-addresses": "api-addresses",
101- "juju-private-address": "private-address"}
102- juju_info = dict.fromkeys(juju_key_map.values())
103+ "juju-private-address": None}
104+ juju_info = dict.fromkeys(key for key in juju_key_map.values() if key)
105 files_to_delete = []
106 for filename, info_key in juju_key_map.iteritems():
107 file_path = os.path.join(meta_data_dir, filename)
108 if not os.path.exists(file_path):
109 continue
110+ files_to_delete.append(file_path)
111+ if info_key is None:
112+ continue
113 with open(file_path, "r") as juju_info_part_file:
114 juju_info[info_key] = juju_info_part_file.read().strip()
115- files_to_delete.append(file_path)
116 missing_values = [value is None for value in juju_info.itervalues()]
117 # If they're all missing it probably means we've already converted.
118 if all(missing_values):
119
120=== modified file 'hooks/test_hooks.py'
121--- hooks/test_hooks.py 2014-08-28 12:13:31 +0000
122+++ hooks/test_hooks.py 2014-09-25 07:00:36 +0000
123@@ -5,7 +5,6 @@
124 from tempfile import mkdtemp
125 from unittest import TestCase
126 from uuid import uuid4
127-from glob import glob
128
129 from landscape.deployment import Configuration
130 from landscape.lib.fs import read_file
131@@ -67,12 +66,8 @@
132 self.landscape_broker.config.write()
133 self.juju_broker = FakeJujuBroker()
134
135- def touch_example_juju_info_file(self):
136- if not os.path.exists(self.landscape_broker.juju_directory):
137- os.mkdir(self.landscape_broker.juju_directory)
138- filename = os.path.join(self.landscape_broker.juju_directory,
139- "whatever.json")
140- write_json_file(filename=filename, content={})
141+ def touch_juju_info_file(self):
142+ write_json_file(self.landscape_broker.juju_filename, content={})
143
144 def mkdtemp(self):
145 directory = mkdtemp(prefix=self.__class__.__name__)
146@@ -115,8 +110,7 @@
147 self.assertEqual(
148 {"environment-uuid": "0afcd28d-6263-43fb-8131-afa696a984f8",
149 "unit-name": "postgresql/0",
150- "api-addresses": "10.0.3.1:17070",
151- "private-address": "10.0.3.205"}, juju_json_info)
152+ "api-addresses": "10.0.3.1:17070"}, juju_json_info)
153
154 def test_old_juju_info_deleted_after_successful_migration(self):
155 """
156@@ -175,23 +169,16 @@
157 """
158 self.juju_broker.environment.update({
159 "JUJU_ENV_UUID": "uuid1",
160- "JUJU_REMOTE_UNIT": "unit/0",
161 "JUJU_API_ADDRESSES": "10.0.0.1:1234"})
162- self.juju_broker.commands["relation-get"] = {
163- "private-address": "10.0.0.99"}
164 hooks.container_relation_joined(juju_broker=self.juju_broker,
165 landscape_broker=self.landscape_broker)
166
167- juju_info_path = os.path.join(
168- self.landscape_broker.juju_directory, "unit-0.json")
169-
170+ juju_info_path = self.landscape_broker.juju_filename
171 juju_info = json.loads(read_file(juju_info_path))
172 self.assertEqual(
173 {"environment-uuid": "uuid1",
174 "machine-id": "1",
175- "unit-name": "unit/0",
176- "api-addresses": "10.0.0.1:1234",
177- "private-address": "10.0.0.99"},
178+ "api-addresses": "10.0.0.1:1234"},
179 juju_info)
180
181 def test_registers_after_juju_info_is_written(self):
182@@ -203,9 +190,7 @@
183 registration_attempts = []
184
185 def record_registration_attempt():
186- juju_directory = self.landscape_broker.juju_directory
187- juju_file_list = glob("%s/*.json" % juju_directory)
188- for _ in juju_file_list:
189+ if os.path.exists(self.landscape_broker.juju_filename):
190 registration_attempts.append(True)
191
192 self.landscape_broker.try_to_register = record_registration_attempt
193@@ -254,16 +239,9 @@
194 since it contains information about the remove unit (which is
195 now gone).
196 """
197- juju_info_path = os.path.join(
198- self.landscape_broker.juju_directory, "unit-0.json")
199-
200- if not os.path.exists(self.landscape_broker.juju_directory):
201- os.mkdir(self.landscape_broker.juju_directory)
202-
203+ juju_info_path = self.landscape_broker.juju_filename
204 juju_info = {"environment-uuid": "uuid1",
205- "unit-name": "unit/0",
206- "api-addresses": "10.0.0.1:1234",
207- "private-address": "10.0.0.99"}
208+ "api-addresses": "10.0.0.1:1234"}
209 write_json_file(juju_info_path, juju_info)
210 hooks.container_relation_departed(
211 landscape_broker=self.landscape_broker)
212@@ -411,7 +389,7 @@
213 super(ConfigChangedTest, self).setUp()
214 self.landscape_broker.config.account_name = "account1"
215 self.landscape_broker.config.computer_title = "title"
216- self.touch_example_juju_info_file()
217+ self.touch_juju_info_file()
218 self.addCleanup(setattr, hooks, "CERT_FILE", hooks.CERT_FILE)
219 tmp_dir = self.mkdtemp()
220 hooks.CERT_FILE = "%s/cert" % tmp_dir
221@@ -440,7 +418,7 @@
222 super(RegistrationRelationJoinedTest, self).setUp()
223 self.landscape_broker.config.account_name = "account1"
224 self.landscape_broker.config.computer_title = "title"
225- self.touch_example_juju_info_file()
226+ self.touch_juju_info_file()
227 self.addCleanup(setattr, hooks, "CERT_FILE", hooks.CERT_FILE)
228 tmp_dir = self.mkdtemp()
229 hooks.CERT_FILE = "%s/cert" % tmp_dir

Subscribers

People subscribed via source and target branches

to all changes: