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
=== modified file 'hooks/common.py'
--- hooks/common.py 2014-08-28 09:54:11 +0000
+++ hooks/common.py 2014-09-24 07:49:01 +0000
@@ -30,6 +30,19 @@
30 """Get relation configuration from Juju."""30 """Get relation configuration from Juju."""
31 return self._run_juju_tool("relation-get")31 return self._run_juju_tool("relation-get")
3232
33 def get_machine_id(self):
34 """Return the Juju machine ID of this unit."""
35 # XXX once #1359714 is fixed this method can be dropped and we can use
36 # the JUJU_MACHINE_ID environment variable.
37
38 # Sniff the value of the machine ID by looking at the name of the
39 # directory where hooks live. It will be something like
40 # 'machine-0-lxc-1'.
41 pattern = "../../machine-*"
42 match = glob(pattern)[0]
43 dirname = os.path.basename(match)
44 return dirname.lstrip("machine-").replace("-", "/")
45
33 def get_service_config(self):46 def get_service_config(self):
34 """Get service configuration from Juju."""47 """Get service configuration from Juju."""
35 return self._run_juju_tool("config-get")48 return self._run_juju_tool("config-get")
@@ -88,7 +101,7 @@
88 is_configured_enough = all([101 is_configured_enough = all([
89 self.config.get("account_name"),102 self.config.get("account_name"),
90 self.config.get("computer_title"),103 self.config.get("computer_title"),
91 get_juju_info_filenames(self.juju_directory)])104 os.path.exists(self.juju_filename)])
92 if not try_to_register:105 if not try_to_register:
93 # We don't need to further check the configuration if we are106 # We don't need to further check the configuration if we are
94 # deleting it.107 # deleting it.
@@ -102,11 +115,11 @@
102 return 0115 return 0
103116
104 @property117 @property
105 def juju_directory(self):118 def juju_filename(self):
106 try:119 try:
107 return self.config.juju_directory120 return self.config.juju_filename
108 except AttributeError: # older landscape-client versions121 except AttributeError: # older landscape-client versions
109 return os.path.join(self.config.data_path, "juju-info.d")122 return os.path.join(self.config.data_path, "juju-info.json")
110123
111 def clear_registration(self):124 def clear_registration(self):
112 """125 """
@@ -187,7 +200,3 @@
187 """Dump content in JSON format to the specified file."""200 """Dump content in JSON format to the specified file."""
188 with open(filename, "w") as dumpfile:201 with open(filename, "w") as dumpfile:
189 json.dump(content, dumpfile)202 json.dump(content, dumpfile)
190
191
192def get_juju_info_filenames(juju_directory):
193 return glob("%s/*.json" % juju_directory)
194203
=== modified file 'hooks/hooks.py'
--- hooks/hooks.py 2014-08-28 09:54:11 +0000
+++ hooks/hooks.py 2014-09-24 07:49:01 +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
@@ -71,29 +70,15 @@
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()72 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 this73 # 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.74 # client with the unit it's managing, not its own unit.
77 juju_info = {75 juju_info = {
78 "environment-uuid": juju_broker.environment.get("JUJU_ENV_UUID"),76 "environment-uuid": juju_broker.environment.get("JUJU_ENV_UUID"),
79 "unit-name": remote_unit_name,77 "machine-id": juju_broker.get_machine_id(),
80 "api-addresses": juju_broker.environment.get("JUJU_API_ADDRESSES"),78 "api-addresses": juju_broker.environment.get("JUJU_API_ADDRESSES"),
81 "private-address": relation_conf.get("private-address")}79 "private-address": relation_conf.get("private-address")}
8280
83 # Let's not use any "/" in a filename.81 write_json_file(landscape_broker.juju_filename, juju_info)
84 unit_name = remote_unit_name.replace("/", "-")
85 juju_file = "%s.json" % unit_name
86
87 juju_info_dir = landscape_broker.juju_directory
88
89 try:
90 os.mkdir(juju_info_dir)
91 except OSError:
92 pass # The file already exists.
93
94 juju_info_file = os.path.join(juju_info_dir, juju_file)
95
96 write_json_file(juju_info_file, juju_info)
97 exit_code = landscape_broker.update_client_config(82 exit_code = landscape_broker.update_client_config(
98 {"computer-title": socket.gethostname()})83 {"computer-title": socket.gethostname()})
9984
@@ -111,9 +96,8 @@
111 landscape_broker.clear_registration()96 landscape_broker.clear_registration()
11297
113 # Delete all of the juju-info files.98 # Delete all of the juju-info files.
114 files_to_delete = get_juju_info_filenames(landscape_broker.juju_directory)99 if os.path.exists(landscape_broker.juju_filename):
115 for to_delete in files_to_delete:100 os.remove(landscape_broker.juju_filename)
116 os.remove(to_delete)
117 landscape_broker.update_client_config(101 landscape_broker.update_client_config(
118 {"computer-title": ""}, try_to_register=False)102 {"computer-title": ""}, try_to_register=False)
119103
120104
=== modified file 'hooks/install.py'
--- hooks/install.py 2014-08-28 10:09:11 +0000
+++ hooks/install.py 2014-09-24 07:49:01 +0000
@@ -8,6 +8,9 @@
8import os8import os
9import subprocess9import subprocess
10import sys10import sys
11
12from glob import glob
13
11from charmhelpers.core.hookenv import (14from charmhelpers.core.hookenv import (
12 Hooks, UnregisteredHookError, log)15 Hooks, UnregisteredHookError, log)
13from charmhelpers.fetch import (16from charmhelpers.fetch import (
@@ -43,6 +46,7 @@
43 data_path])46 data_path])
4447
4548
49
46def build_from_launchpad(url):50def build_from_launchpad(url):
47 """The charm will install the code from the passed lp branch.51 """The charm will install the code from the passed lp branch.
48 """52 """
@@ -55,8 +59,8 @@
55 subprocess.check_call(["make", "package"], env=env)59 subprocess.check_call(["make", "package"], env=env)
56 #TODO: The following call should be retried (potential race condition to60 #TODO: The following call should be retried (potential race condition to
57 # acquire the dpkg lock).61 # acquire the dpkg lock).
58 subprocess.call(["dpkg", "-i", "../landscape-client_*.deb",62 subprocess.call(["dpkg", "-i", glob("../landscape-client_*.deb")[0],
59 "../landscape-common_*.deb"])63 glob("../landscape-common_*.deb")[0]])
60 # The _run_apt_command will ensure the command is retried in case we cannot64 # The _run_apt_command will ensure the command is retried in case we cannot
61 # acquire the lock for some reason.65 # acquire the lock for some reason.
62 _run_apt_command(["apt-get", "-f", "install"], fatal=True)66 _run_apt_command(["apt-get", "-f", "install"], fatal=True)
6367
=== modified file 'hooks/test_hooks.py'
--- hooks/test_hooks.py 2014-08-28 09:54:11 +0000
+++ hooks/test_hooks.py 2014-09-24 07:49:01 +0000
@@ -30,6 +30,9 @@
30 def log(self, message):30 def log(self, message):
31 self.logs.append(message)31 self.logs.append(message)
3232
33 def get_machine_id(self):
34 return "1"
35
33 def _run_juju_tool(self, command):36 def _run_juju_tool(self, command):
34 """Override _run_juju_tool not not execute any commands."""37 """Override _run_juju_tool not not execute any commands."""
35 return self.commands[command]38 return self.commands[command]
@@ -64,12 +67,8 @@
64 self.landscape_broker.config.write()67 self.landscape_broker.config.write()
65 self.juju_broker = FakeJujuBroker()68 self.juju_broker = FakeJujuBroker()
6669
67 def touch_example_juju_info_file(self):70 def touch_juju_info_file(self):
68 if not os.path.exists(self.landscape_broker.juju_directory):71 write_json_file(self.landscape_broker.juju_filename, content={})
69 os.mkdir(self.landscape_broker.juju_directory)
70 filename = os.path.join(self.landscape_broker.juju_directory,
71 "whatever.json")
72 write_json_file(filename=filename, content={})
7372
74 def mkdtemp(self):73 def mkdtemp(self):
75 directory = mkdtemp(prefix=self.__class__.__name__)74 directory = mkdtemp(prefix=self.__class__.__name__)
@@ -99,6 +98,7 @@
99 single JSON file.98 single JSON file.
100 """99 """
101 juju_info = {"juju-env-uuid": "0afcd28d-6263-43fb-8131-afa696a984f8",100 juju_info = {"juju-env-uuid": "0afcd28d-6263-43fb-8131-afa696a984f8",
101 "juju-machine-id": "1",
102 "juju-unit-name": "postgresql/0",102 "juju-unit-name": "postgresql/0",
103 "juju-api-addresses": "10.0.3.1:17070",103 "juju-api-addresses": "10.0.3.1:17070",
104 "juju-private-address": "10.0.3.205"}104 "juju-private-address": "10.0.3.205"}
@@ -171,20 +171,17 @@
171 """171 """
172 self.juju_broker.environment.update({172 self.juju_broker.environment.update({
173 "JUJU_ENV_UUID": "uuid1",173 "JUJU_ENV_UUID": "uuid1",
174 "JUJU_REMOTE_UNIT": "unit/0",
175 "JUJU_API_ADDRESSES": "10.0.0.1:1234"})174 "JUJU_API_ADDRESSES": "10.0.0.1:1234"})
176 self.juju_broker.commands["relation-get"] = {175 self.juju_broker.commands["relation-get"] = {
177 "private-address": "10.0.0.99"}176 "private-address": "10.0.0.99"}
178 hooks.container_relation_joined(juju_broker=self.juju_broker,177 hooks.container_relation_joined(juju_broker=self.juju_broker,
179 landscape_broker=self.landscape_broker)178 landscape_broker=self.landscape_broker)
180179
181 juju_info_path = os.path.join(180 juju_info_path = self.landscape_broker.juju_filename
182 self.landscape_broker.juju_directory, "unit-0.json")
183
184 juju_info = json.loads(read_file(juju_info_path))181 juju_info = json.loads(read_file(juju_info_path))
185 self.assertEqual(182 self.assertEqual(
186 {"environment-uuid": "uuid1",183 {"environment-uuid": "uuid1",
187 "unit-name": "unit/0",184 "machine-id": "1",
188 "api-addresses": "10.0.0.1:1234",185 "api-addresses": "10.0.0.1:1234",
189 "private-address": "10.0.0.99"},186 "private-address": "10.0.0.99"},
190 juju_info)187 juju_info)
@@ -198,9 +195,7 @@
198 registration_attempts = []195 registration_attempts = []
199196
200 def record_registration_attempt():197 def record_registration_attempt():
201 juju_directory = self.landscape_broker.juju_directory198 if os.path.exists(self.landscape_broker.juju_filename):
202 juju_file_list = glob("%s/*.json" % juju_directory)
203 for _ in juju_file_list:
204 registration_attempts.append(True)199 registration_attempts.append(True)
205200
206 self.landscape_broker.try_to_register = record_registration_attempt201 self.landscape_broker.try_to_register = record_registration_attempt
@@ -249,14 +244,8 @@
249 since it contains information about the remove unit (which is244 since it contains information about the remove unit (which is
250 now gone).245 now gone).
251 """246 """
252 juju_info_path = os.path.join(247 juju_info_path = self.landscape_broker.juju_filename
253 self.landscape_broker.juju_directory, "unit-0.json")
254
255 if not os.path.exists(self.landscape_broker.juju_directory):
256 os.mkdir(self.landscape_broker.juju_directory)
257
258 juju_info = {"environment-uuid": "uuid1",248 juju_info = {"environment-uuid": "uuid1",
259 "unit-name": "unit/0",
260 "api-addresses": "10.0.0.1:1234",249 "api-addresses": "10.0.0.1:1234",
261 "private-address": "10.0.0.99"}250 "private-address": "10.0.0.99"}
262 write_json_file(juju_info_path, juju_info)251 write_json_file(juju_info_path, juju_info)
@@ -406,7 +395,7 @@
406 super(ConfigChangedTest, self).setUp()395 super(ConfigChangedTest, self).setUp()
407 self.landscape_broker.config.account_name = "account1"396 self.landscape_broker.config.account_name = "account1"
408 self.landscape_broker.config.computer_title = "title"397 self.landscape_broker.config.computer_title = "title"
409 self.touch_example_juju_info_file()398 self.touch_juju_info_file()
410 self.addCleanup(setattr, hooks, "CERT_FILE", hooks.CERT_FILE)399 self.addCleanup(setattr, hooks, "CERT_FILE", hooks.CERT_FILE)
411 tmp_dir = self.mkdtemp()400 tmp_dir = self.mkdtemp()
412 hooks.CERT_FILE = "%s/cert" % tmp_dir401 hooks.CERT_FILE = "%s/cert" % tmp_dir
@@ -435,7 +424,7 @@
435 super(RegistrationRelationJoinedTest, self).setUp()424 super(RegistrationRelationJoinedTest, self).setUp()
436 self.landscape_broker.config.account_name = "account1"425 self.landscape_broker.config.account_name = "account1"
437 self.landscape_broker.config.computer_title = "title"426 self.landscape_broker.config.computer_title = "title"
438 self.touch_example_juju_info_file()427 self.touch_juju_info_file()
439 self.addCleanup(setattr, hooks, "CERT_FILE", hooks.CERT_FILE)428 self.addCleanup(setattr, hooks, "CERT_FILE", hooks.CERT_FILE)
440 tmp_dir = self.mkdtemp()429 tmp_dir = self.mkdtemp()
441 hooks.CERT_FILE = "%s/cert" % tmp_dir430 hooks.CERT_FILE = "%s/cert" % tmp_dir

Subscribers

People subscribed via source and target branches