Merge lp:~bac/charms/precise/juju-gui/1086790 into lp:~juju-gui/charms/precise/juju-gui/trunk

Proposed by Brad Crittenden
Status: Merged
Approved by: Gary Poster
Approved revision: 9
Merged at revision: 9
Proposed branch: lp:~bac/charms/precise/juju-gui/1086790
Merge into: lp:~juju-gui/charms/precise/juju-gui/trunk
Diff against target: 226 lines (+90/-14)
9 files modified
config.yaml (+8/-1)
hooks/install (+28/-12)
hooks/start (+1/-0)
hooks/stop (+1/-0)
hooks/utils.py (+27/-0)
revision (+1/-1)
tests/deploy.test (+1/-0)
tests/test_utils.py (+22/-0)
tests/unit.test (+1/-0)
To merge this branch: bzr merge lp:~bac/charms/precise/juju-gui/1086790
Reviewer Review Type Date Requested Status
Gary Poster (community) Approve
Benji York (community) code Approve
Review via email: mp+138823@code.launchpad.net

Description of the change

Add command-level logging in the hooks. Also for the run('make') command, stdout and stderr are redirected to a separate file so that progress can be monitored as it occurs. None of the other commands are long running to require that level of detail.

Sorry for not using Rietveld but I could not get lbox to work with this target.

To post a comment you must log in.
Revision history for this message
Benji York (benji) wrote :

The branch looks good. I only had small suggestions or questions.

The "#-*- python -*-" on line 28 surprised me. I am not against that
style of file type markers, but I just wanted to be sure it was
intentional. Oh, and it would be nice if there were a space after the
"#" character. :)

The import on line 130 could be a single-line import.

Line 164: hmm, prefixing the string with a newline will mean that the
log file starts with a blank line. That's not the end of the world, but
it is a bit odd. Maybe a comment to explain why would be in order.

Line 171 of the diff, the contents of the revision file: I /think/ I
saw a recent branch that bumped the revision higher than 13. You might
want to double-check what number the trunk is on.

Line 206: "The monkey patch is undone at the end of the test." Should
that read "The monkey patch is undone in the tearDown method."?

review: Approve (code)
9. By Brad Crittenden

Changes from review: formatting, adding comments, clarifications.

Revision history for this message
Gary Poster (gary) wrote :

Nice, Brad, thank you. Thank you for the test, also.

Your hooks/stop change added a tab (line 114 of current diff). I'm pretty sure you/we want it to be spaces.

I worry that the new logging function in hooks/utils.py will make fixing bug 1086507 harder still. Thinking it through, though, I think we will want to make that a separate task even for this change: to partially address it now will mean adding a config-changed hook, and that would be a bigger job than I want in this branch.

That's all I have from a visual review. I'll actually try it out now, and then follow up here.

review: Approve
10. By Brad Crittenden

Detabify

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'config.yaml'
2--- config.yaml 2012-12-03 10:02:45 +0000
3+++ config.yaml 2012-12-10 14:55:21 +0000
4@@ -29,6 +29,13 @@
5 default: sample
6 juju-gui-console-enabled:
7 description: |
8- Whether or not the browser's console should be enabled.
9+ Whether or not the console should be enabled for the browser.
10 type: boolean
11 default: false
12+ command-log-file:
13+ description: |
14+ The log file where stdout and stderr should be sent for all
15+ commands that are run by charm hooks.
16+ type: string
17+ default: /var/log/juju/juju-gui.log
18+
19
20=== modified file 'hooks/install'
21--- hooks/install 2012-11-30 17:00:51 +0000
22+++ hooks/install 2012-12-10 14:55:21 +0000
23@@ -1,7 +1,12 @@
24 #!/usr/bin/env python2
25-
26-from subprocess import check_call
27-
28+# -*- python -*-
29+
30+import os.path
31+from subprocess import (
32+ CalledProcessError,
33+ check_call,
34+ )
35+import tempfile
36
37 # python-shelltoolbox is installed as a dependency of python-charmhelpers.
38 check_call(['apt-get', 'install', '-y', 'python-charmhelpers'])
39@@ -23,34 +28,45 @@
40 run,
41 )
42
43+from utils import cmd_log
44+
45
46 def fetch(juju_gui_branch, juju_api_branch):
47 """Install required dependencies and retrieve Juju/Juju GUI branches."""
48 log('Installing dependencies.')
49- install_extra_repositories('ppa:chris-lea/node.js')
50- apt_get_install('bzr', 'imagemagick', 'make', 'nodejs', 'npm', 'zookeeper')
51+ cmd_log(install_extra_repositories('ppa:chris-lea/node.js'))
52+ cmd_log(
53+ apt_get_install('bzr', 'imagemagick', 'make',
54+ 'nodejs', 'npm', 'zookeeper'))
55 log('Retrieving source checkouts.')
56 bzr_checkout = command('bzr', 'co', '--lightweight')
57- bzr_checkout(juju_gui_branch, 'juju-gui')
58- bzr_checkout(juju_api_branch, 'juju')
59-
60-
61-def build():
62+ cmd_log(bzr_checkout(juju_gui_branch, 'juju-gui'))
63+ cmd_log(bzr_checkout(juju_api_branch, 'juju'))
64+
65+
66+def build(logpath):
67 """Set up Juju GUI."""
68 log('Building Juju GUI.')
69 with cd('juju-gui'):
70- run('make')
71+ logdir = os.path.dirname(logpath)
72+ fd, name = tempfile.mkstemp(prefix='make-', dir=logdir)
73+ log('Output from "make" sent to', name)
74+ run('make', stdout=fd, stderr=fd)
75
76
77 def main():
78 config = get_config()
79 fetch(config['juju-gui-branch'], config['juju-api-branch'])
80- build()
81+ build(config['command-log-file'])
82
83
84 if __name__ == '__main__':
85 log_entry()
86 try:
87 main()
88+ except CalledProcessError as e:
89+ log('Exception caught:')
90+ log(e.output)
91+ raise
92 finally:
93 log_exit()
94
95=== modified file 'hooks/start'
96--- hooks/start 2012-12-03 10:02:45 +0000
97+++ hooks/start 2012-12-10 14:55:21 +0000
98@@ -1,4 +1,5 @@
99 #!/usr/bin/env python2
100+#-*- python -*-
101
102 import json
103 import os
104
105=== modified file 'hooks/stop'
106--- hooks/stop 2012-12-03 10:02:45 +0000
107+++ hooks/stop 2012-12-10 14:55:21 +0000
108@@ -1,4 +1,5 @@
109 #!/usr/bin/env python2
110+#-*- python -*-
111
112 from charmhelpers import (
113 get_config,
114
115=== modified file 'hooks/utils.py'
116--- hooks/utils.py 2012-11-29 14:49:41 +0000
117+++ hooks/utils.py 2012-12-10 14:55:21 +0000
118@@ -1,8 +1,10 @@
119 """Juju GUI charm utilities."""
120
121 import os
122+import logging
123
124 from shelltoolbox import search_file
125+from charmhelpers import get_config
126
127
128 def get_zookeeper_address(agent_file_path):
129@@ -30,3 +32,28 @@
130 contents = open(template_path).read()
131 with open(destination, 'w') as stream:
132 stream.write(contents % context)
133+
134+
135+results_log = None
136+
137+def _setupLogging():
138+ global results_log
139+ if results_log is not None:
140+ return
141+ config = get_config()
142+ logging.basicConfig(
143+ filename=config['command-log-file'],
144+ level=logging.INFO,
145+ format="%(asctime)s: %(name)s@%(levelname)s %(message)s")
146+ results_log = logging.getLogger('juju-gui')
147+
148+
149+def cmd_log(results):
150+ global results_log
151+ if not results:
152+ return
153+ if results_log is None:
154+ _setupLogging()
155+ # Since 'results' may be multi-line output, start it on a separate line
156+ # from the logger timestamp, etc.
157+ results_log.info('\n' + results)
158
159=== modified file 'revision'
160--- revision 2012-11-30 17:36:15 +0000
161+++ revision 2012-12-10 14:55:21 +0000
162@@ -1,1 +1,1 @@
163-12
164+13
165
166=== modified file 'tests/deploy.test'
167--- tests/deploy.test 2012-11-30 17:00:51 +0000
168+++ tests/deploy.test 2012-12-10 14:55:21 +0000
169@@ -1,4 +1,5 @@
170 #!/usr/bin/env python2
171+#-*- python -*-
172
173 import time
174 import unittest
175
176=== modified file 'tests/test_utils.py'
177--- tests/test_utils.py 2012-11-29 14:49:41 +0000
178+++ tests/test_utils.py 2012-12-10 14:55:21 +0000
179@@ -3,8 +3,11 @@
180 import os
181 import tempfile
182 import unittest
183+import charmhelpers
184+from simplejson import dumps
185
186 from utils import (
187+ cmd_log,
188 get_zookeeper_address,
189 render_to_file,
190 )
191@@ -44,6 +47,25 @@
192 expected = self.template_contents % context
193 self.assertEqual(expected, self.destination_file.read())
194
195+class CmdLogTest(unittest.TestCase):
196+ def setUp(self):
197+ # Patch the charmhelpers 'command', which powers get_config. The
198+ # result of this is the mock_config dictionary will be returned.
199+ # The monkey patch is undone in the tearDown.
200+ self.command = charmhelpers.command
201+ fd, self.log_file_name = tempfile.mkstemp()
202+ os.close(fd)
203+ mock_config = {'command-log-file': self.log_file_name}
204+ charmhelpers.command = lambda *args: lambda: dumps(mock_config)
205+
206+ def tearDown(self):
207+ charmhelpers.command = self.command
208+
209+ def test_contents_logged(self):
210+ cmd_log('foo')
211+ line = open(self.log_file_name, 'r').read()
212+ self.assertTrue(line.endswith(': juju-gui@INFO \nfoo\n'))
213+
214
215 if __name__ == '__main__':
216 unittest.main(verbosity=2)
217
218=== modified file 'tests/unit.test'
219--- tests/unit.test 2012-11-21 14:51:05 +0000
220+++ tests/unit.test 2012-12-10 14:55:21 +0000
221@@ -1,4 +1,5 @@
222 #!/usr/bin/env python2
223+# -*- python -*-
224
225 import os
226 import sys

Subscribers

People subscribed via source and target branches