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
=== modified file 'hooks/common.py'
--- hooks/common.py 2014-09-02 13:55:31 +0000
+++ hooks/common.py 2014-09-25 07:00:36 +0000
@@ -101,7 +101,7 @@
101 is_configured_enough = all([101 is_configured_enough = all([
102 self.config.get("account_name"),102 self.config.get("account_name"),
103 self.config.get("computer_title"),103 self.config.get("computer_title"),
104 get_juju_info_filenames(self.juju_directory)])104 os.path.exists(self.juju_filename)])
105 if not try_to_register:105 if not try_to_register:
106 # We don't need to further check the configuration if we are106 # We don't need to further check the configuration if we are
107 # deleting it.107 # deleting it.
@@ -115,11 +115,11 @@
115 return 0115 return 0
116116
117 @property117 @property
118 def juju_directory(self):118 def juju_filename(self):
119 try:119 try:
120 return self.config.juju_directory120 return self.config.juju_filename
121 except AttributeError: # older landscape-client versions121 except AttributeError: # older landscape-client versions
122 return os.path.join(self.config.data_path, "juju-info.d")122 return os.path.join(self.config.data_path, "juju-info.json")
123123
124 def clear_registration(self):124 def clear_registration(self):
125 """125 """
@@ -200,7 +200,3 @@
200 """Dump content in JSON format to the specified file."""200 """Dump content in JSON format to the specified file."""
201 with open(filename, "w") as dumpfile:201 with open(filename, "w") as dumpfile:
202 json.dump(content, dumpfile)202 json.dump(content, dumpfile)
203
204
205def get_juju_info_filenames(juju_directory):
206 return glob("%s/*.json" % juju_directory)
207203
=== modified file 'hooks/hooks.py'
--- hooks/hooks.py 2014-08-28 12:13:31 +0000
+++ hooks/hooks.py 2014-09-25 07:00:36 +0000
@@ -11,8 +11,7 @@
11from subprocess import CalledProcessError11from subprocess import CalledProcessError
1212
13from common import (13from common import (
14 write_json_file, JujuBroker, LandscapeBroker, chown,14 write_json_file, JujuBroker, LandscapeBroker, chown)
15 get_juju_info_filenames)
16from ceph import (15from ceph import (
17 get_ceph_client_path, write_ceph_client_keyring, write_ceph_client_config)16 get_ceph_client_path, write_ceph_client_keyring, write_ceph_client_config)
1817
@@ -70,31 +69,14 @@
70 juju_broker.log("In container-relation-joined for %s"69 juju_broker.log("In container-relation-joined for %s"
71 "" % landscape_broker.local_unit)70 "" % landscape_broker.local_unit)
72 landscape_broker.config.reload()71 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 this72 # 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.73 # client with the unit it's managing, not its own unit.
77 juju_info = {74 juju_info = {
78 "environment-uuid": juju_broker.environment.get("JUJU_ENV_UUID"),75 "environment-uuid": juju_broker.environment.get("JUJU_ENV_UUID"),
79 "unit-name": remote_unit_name,
80 "machine-id": juju_broker.get_machine_id(),76 "machine-id": juju_broker.get_machine_id(),
81 "api-addresses": juju_broker.environment.get("JUJU_API_ADDRESSES"),77 "api-addresses": juju_broker.environment.get("JUJU_API_ADDRESSES")}
82 "private-address": relation_conf.get("private-address")}78
8379 write_json_file(landscape_broker.juju_filename, juju_info)
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 exit_code = landscape_broker.update_client_config(80 exit_code = landscape_broker.update_client_config(
99 {"computer-title": socket.gethostname()})81 {"computer-title": socket.gethostname()})
10082
@@ -112,9 +94,8 @@
112 landscape_broker.clear_registration()94 landscape_broker.clear_registration()
11395
114 # Delete all of the juju-info files.96 # Delete all of the juju-info files.
115 files_to_delete = get_juju_info_filenames(landscape_broker.juju_directory)97 if os.path.exists(landscape_broker.juju_filename):
116 for to_delete in files_to_delete:98 os.remove(landscape_broker.juju_filename)
117 os.remove(to_delete)
118 landscape_broker.update_client_config(99 landscape_broker.update_client_config(
119 {"computer-title": ""}, try_to_register=False)100 {"computer-title": ""}, try_to_register=False)
120101
@@ -180,16 +161,18 @@
180 juju_key_map = {"juju-env-uuid": "environment-uuid",161 juju_key_map = {"juju-env-uuid": "environment-uuid",
181 "juju-unit-name": "unit-name",162 "juju-unit-name": "unit-name",
182 "juju-api-addresses": "api-addresses",163 "juju-api-addresses": "api-addresses",
183 "juju-private-address": "private-address"}164 "juju-private-address": None}
184 juju_info = dict.fromkeys(juju_key_map.values())165 juju_info = dict.fromkeys(key for key in juju_key_map.values() if key)
185 files_to_delete = []166 files_to_delete = []
186 for filename, info_key in juju_key_map.iteritems():167 for filename, info_key in juju_key_map.iteritems():
187 file_path = os.path.join(meta_data_dir, filename)168 file_path = os.path.join(meta_data_dir, filename)
188 if not os.path.exists(file_path):169 if not os.path.exists(file_path):
189 continue170 continue
171 files_to_delete.append(file_path)
172 if info_key is None:
173 continue
190 with open(file_path, "r") as juju_info_part_file:174 with open(file_path, "r") as juju_info_part_file:
191 juju_info[info_key] = juju_info_part_file.read().strip()175 juju_info[info_key] = juju_info_part_file.read().strip()
192 files_to_delete.append(file_path)
193 missing_values = [value is None for value in juju_info.itervalues()]176 missing_values = [value is None for value in juju_info.itervalues()]
194 # If they're all missing it probably means we've already converted.177 # If they're all missing it probably means we've already converted.
195 if all(missing_values):178 if all(missing_values):
196179
=== modified file 'hooks/test_hooks.py'
--- hooks/test_hooks.py 2014-08-28 12:13:31 +0000
+++ hooks/test_hooks.py 2014-09-25 07:00:36 +0000
@@ -5,7 +5,6 @@
5from tempfile import mkdtemp5from tempfile import mkdtemp
6from unittest import TestCase6from unittest import TestCase
7from uuid import uuid47from uuid import uuid4
8from glob import glob
98
10from landscape.deployment import Configuration9from landscape.deployment import Configuration
11from landscape.lib.fs import read_file10from landscape.lib.fs import read_file
@@ -67,12 +66,8 @@
67 self.landscape_broker.config.write()66 self.landscape_broker.config.write()
68 self.juju_broker = FakeJujuBroker()67 self.juju_broker = FakeJujuBroker()
6968
70 def touch_example_juju_info_file(self):69 def touch_juju_info_file(self):
71 if not os.path.exists(self.landscape_broker.juju_directory):70 write_json_file(self.landscape_broker.juju_filename, content={})
72 os.mkdir(self.landscape_broker.juju_directory)
73 filename = os.path.join(self.landscape_broker.juju_directory,
74 "whatever.json")
75 write_json_file(filename=filename, content={})
7671
77 def mkdtemp(self):72 def mkdtemp(self):
78 directory = mkdtemp(prefix=self.__class__.__name__)73 directory = mkdtemp(prefix=self.__class__.__name__)
@@ -115,8 +110,7 @@
115 self.assertEqual(110 self.assertEqual(
116 {"environment-uuid": "0afcd28d-6263-43fb-8131-afa696a984f8",111 {"environment-uuid": "0afcd28d-6263-43fb-8131-afa696a984f8",
117 "unit-name": "postgresql/0",112 "unit-name": "postgresql/0",
118 "api-addresses": "10.0.3.1:17070",113 "api-addresses": "10.0.3.1:17070"}, juju_json_info)
119 "private-address": "10.0.3.205"}, juju_json_info)
120114
121 def test_old_juju_info_deleted_after_successful_migration(self):115 def test_old_juju_info_deleted_after_successful_migration(self):
122 """116 """
@@ -175,23 +169,16 @@
175 """169 """
176 self.juju_broker.environment.update({170 self.juju_broker.environment.update({
177 "JUJU_ENV_UUID": "uuid1",171 "JUJU_ENV_UUID": "uuid1",
178 "JUJU_REMOTE_UNIT": "unit/0",
179 "JUJU_API_ADDRESSES": "10.0.0.1:1234"})172 "JUJU_API_ADDRESSES": "10.0.0.1:1234"})
180 self.juju_broker.commands["relation-get"] = {
181 "private-address": "10.0.0.99"}
182 hooks.container_relation_joined(juju_broker=self.juju_broker,173 hooks.container_relation_joined(juju_broker=self.juju_broker,
183 landscape_broker=self.landscape_broker)174 landscape_broker=self.landscape_broker)
184175
185 juju_info_path = os.path.join(176 juju_info_path = self.landscape_broker.juju_filename
186 self.landscape_broker.juju_directory, "unit-0.json")
187
188 juju_info = json.loads(read_file(juju_info_path))177 juju_info = json.loads(read_file(juju_info_path))
189 self.assertEqual(178 self.assertEqual(
190 {"environment-uuid": "uuid1",179 {"environment-uuid": "uuid1",
191 "machine-id": "1",180 "machine-id": "1",
192 "unit-name": "unit/0",181 "api-addresses": "10.0.0.1:1234"},
193 "api-addresses": "10.0.0.1:1234",
194 "private-address": "10.0.0.99"},
195 juju_info)182 juju_info)
196183
197 def test_registers_after_juju_info_is_written(self):184 def test_registers_after_juju_info_is_written(self):
@@ -203,9 +190,7 @@
203 registration_attempts = []190 registration_attempts = []
204191
205 def record_registration_attempt():192 def record_registration_attempt():
206 juju_directory = self.landscape_broker.juju_directory193 if os.path.exists(self.landscape_broker.juju_filename):
207 juju_file_list = glob("%s/*.json" % juju_directory)
208 for _ in juju_file_list:
209 registration_attempts.append(True)194 registration_attempts.append(True)
210195
211 self.landscape_broker.try_to_register = record_registration_attempt196 self.landscape_broker.try_to_register = record_registration_attempt
@@ -254,16 +239,9 @@
254 since it contains information about the remove unit (which is239 since it contains information about the remove unit (which is
255 now gone).240 now gone).
256 """241 """
257 juju_info_path = os.path.join(242 juju_info_path = self.landscape_broker.juju_filename
258 self.landscape_broker.juju_directory, "unit-0.json")
259
260 if not os.path.exists(self.landscape_broker.juju_directory):
261 os.mkdir(self.landscape_broker.juju_directory)
262
263 juju_info = {"environment-uuid": "uuid1",243 juju_info = {"environment-uuid": "uuid1",
264 "unit-name": "unit/0",244 "api-addresses": "10.0.0.1:1234"}
265 "api-addresses": "10.0.0.1:1234",
266 "private-address": "10.0.0.99"}
267 write_json_file(juju_info_path, juju_info)245 write_json_file(juju_info_path, juju_info)
268 hooks.container_relation_departed(246 hooks.container_relation_departed(
269 landscape_broker=self.landscape_broker)247 landscape_broker=self.landscape_broker)
@@ -411,7 +389,7 @@
411 super(ConfigChangedTest, self).setUp()389 super(ConfigChangedTest, self).setUp()
412 self.landscape_broker.config.account_name = "account1"390 self.landscape_broker.config.account_name = "account1"
413 self.landscape_broker.config.computer_title = "title"391 self.landscape_broker.config.computer_title = "title"
414 self.touch_example_juju_info_file()392 self.touch_juju_info_file()
415 self.addCleanup(setattr, hooks, "CERT_FILE", hooks.CERT_FILE)393 self.addCleanup(setattr, hooks, "CERT_FILE", hooks.CERT_FILE)
416 tmp_dir = self.mkdtemp()394 tmp_dir = self.mkdtemp()
417 hooks.CERT_FILE = "%s/cert" % tmp_dir395 hooks.CERT_FILE = "%s/cert" % tmp_dir
@@ -440,7 +418,7 @@
440 super(RegistrationRelationJoinedTest, self).setUp()418 super(RegistrationRelationJoinedTest, self).setUp()
441 self.landscape_broker.config.account_name = "account1"419 self.landscape_broker.config.account_name = "account1"
442 self.landscape_broker.config.computer_title = "title"420 self.landscape_broker.config.computer_title = "title"
443 self.touch_example_juju_info_file()421 self.touch_juju_info_file()
444 self.addCleanup(setattr, hooks, "CERT_FILE", hooks.CERT_FILE)422 self.addCleanup(setattr, hooks, "CERT_FILE", hooks.CERT_FILE)
445 tmp_dir = self.mkdtemp()423 tmp_dir = self.mkdtemp()
446 hooks.CERT_FILE = "%s/cert" % tmp_dir424 hooks.CERT_FILE = "%s/cert" % tmp_dir

Subscribers

People subscribed via source and target branches

to all changes: