Merge lp:~bjornt/charms/precise/landscape-client/save-juju-env into lp:~charmers/charms/precise/landscape-client/trunk

Proposed by Björn Tillenius
Status: Merged
Approved by: Mark Mims
Approved revision: 30
Merged at revision: 19
Proposed branch: lp:~bjornt/charms/precise/landscape-client/save-juju-env
Merge into: lp:~charmers/charms/precise/landscape-client/trunk
Prerequisite: lp:~bjornt/charms/precise/landscape-client/no-landscape-config-call
Diff against target: 138 lines (+58/-23)
4 files modified
hooks/common.py (+26/-20)
hooks/config-changed (+2/-1)
hooks/container-relation-joined (+29/-1)
revision (+1/-1)
To merge this branch: bzr merge lp:~bjornt/charms/precise/landscape-client/save-juju-env
Reviewer Review Type Date Requested Status
Mark Mims (community) Approve
charmers Pending
Kapil Thangavelu Pending
Review via email: mp+147617@code.launchpad.net

This proposal supersedes a proposal from 2013-02-05.

Description of the change

Save information about the Juju environment when joining the relation.

This makes the landscape-client send information about the Juju unit
it's connected to.

I had to change the function that tries to register the client
to return an error code, instead of exiting directly, so that
the juju information would be saved even if there were some
configuration error.

To post a comment you must log in.
Revision history for this message
Kapil Thangavelu (hazmat) wrote : Posted in a previous version of this proposal

lgtm, thanks.

review: Approve
Revision history for this message
Mark Mims (mark-mims) wrote : Posted in a previous version of this proposal

rejecting... the charm was incorrectly promulgated, so these MPs are against the wrong branch.

Please resubmit against lp:charms/landscape-client (which is currently correct)

30. By Björn Tillenius

Include the private address as well.

Revision history for this message
Björn Tillenius (bjornt) wrote :

I added the private address from relation-get to the things that we save
away. I'm also requesting a review from ~charmers, in case Mark or Kapil
don't have time to do the review.

Revision history for this message
Mark Mims (mark-mims) wrote :

me too

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 2013-01-24 12:45:44 +0000
3+++ hooks/common.py 2013-02-13 09:39:22 +0000
4@@ -22,21 +22,26 @@
5 for key, value in service_config.items():
6 setattr(client_config, key.replace("-", "_"), value)
7 client_config.write()
8- try_to_register()
9-
10-
11-def exit_with_error(exit_code):
12- """Disable the client and exit with the given exit code.
13-
14- The client is disabled, so that the config-changed hook can be
15- re-run after the configuration error has been resolve. If the client
16- wouldn't be disabled, try_to_register() would see that the client is
17- configured and wouldn't attempt to register the client again.
18- """
19- # Disable the client so that the config-change hook can be re-run
20- # after the error has been resolved.
21- stop_client_and_disable_init_script()
22- sys.exit(1)
23+ return try_to_register()
24+
25+
26+class ErrorHandler:
27+
28+ exit_code = 0
29+
30+ def flag_error(self, exit_code):
31+ """Disable the client and indicate an error was encountered.
32+
33+ The client is disabled, so that the config-changed hook can be
34+ re-run after the configuration error has been resolve. If the client
35+ wouldn't be disabled, try_to_register() would see that the client is
36+ configured and wouldn't attempt to register the client again.
37+ """
38+ # Disable the client so that the config-change hook can be re-run
39+ # after the error has been resolved.
40+ stop_client_and_disable_init_script()
41+ self.exit_code = exit_code
42+
43
44
45 def try_to_register():
46@@ -51,19 +56,20 @@
47 If an error is encountered, the client will be disabled and the hook
48 calling this function will exit with an error code.
49 """
50+ error_handler = ErrorHandler()
51+
52 sysvconfig = SysVConfig()
53 configured = sysvconfig.is_configured_to_run()
54 config = LandscapeSetupConfiguration()
55 config.load([])
56 config.silent = True
57 if configured or not config.get("computer_title"):
58- return
59+ return 0
60 try:
61 setup(config)
62 except ConfigurationError as error:
63 print >> sys.stderr, "Configuration error: %s" % (str(error),)
64- exit_with_error(1)
65+ error_handler.flag_error(1)
66 else:
67- register(config, on_error=exit_with_error)
68-
69-
70+ register(config, on_error=error_handler.flag_error)
71+ return error_handler.exit_code
72
73=== modified file 'hooks/config-changed'
74--- hooks/config-changed 2013-01-30 14:16:15 +0000
75+++ hooks/config-changed 2013-02-13 09:39:22 +0000
76@@ -2,11 +2,12 @@
77
78 from subprocess import check_output
79 import json
80+import sys
81
82 from common import update_client_config
83
84
85 configs = json.loads(check_output(["config-get", "--format=json"]))
86 if configs:
87- update_client_config(configs)
88+ sys.exit(update_client_config(configs))
89
90
91=== modified file 'hooks/container-relation-joined'
92--- hooks/container-relation-joined 2013-02-08 17:37:09 +0000
93+++ hooks/container-relation-joined 2013-02-13 09:39:22 +0000
94@@ -1,8 +1,36 @@
95 #!/usr/bin/python
96
97 import os
98+import sys
99+import json
100+from subprocess import check_output
101+
102+from landscape.deployment import Configuration
103
104 from common import update_client_config
105
106
107-update_client_config({"computer-title": os.environ["JUJU_REMOTE_UNIT"]})
108+exit_code = update_client_config(
109+ {"computer-title": os.environ["JUJU_REMOTE_UNIT"]})
110+juju_env = os.environ.get("JUJU_ENV_UUID")
111+# Older Juju versions didn't have JUJU_ENV_UUID.
112+if juju_env is not None:
113+ relation_get_output = check_output(["relation-get", "--format", "json"])
114+ if relation_get_output:
115+ relation_get = json.loads(relation_get_output)
116+ else:
117+ relation_get = {}
118+ print "RELATION GET: %r" % (relation_get,)
119+ client_config = Configuration()
120+ client_config.reload()
121+ # We use the remote unit for the unit name, since we want to
122+ # associate this client with the unit its managing, not its own
123+ # unit.
124+ juju_info = {"JUJU_ENV_UUID": juju_env,
125+ "JUJU_UNIT_NAME": os.environ["JUJU_REMOTE_UNIT"],
126+ "JUJU_PRIVATE_ADDRESS": relation_get.get("private-address")}
127+ juju_info_path = os.path.join(client_config.data_path, "juju-info")
128+ with open(juju_info_path, "w") as juju_info_file:
129+ juju_info_file.write(json.dumps(juju_info))
130+
131+sys.exit(exit_code)
132
133=== modified file 'revision'
134--- revision 2013-01-30 14:16:15 +0000
135+++ revision 2013-02-13 09:39:22 +0000
136@@ -1,1 +1,1 @@
137-6
138+7

Subscribers

People subscribed via source and target branches