Merge lp:~mew/charm-helpers/no-host-magic into lp:charm-helpers

Proposed by Matthew Wedgwood
Status: Merged
Merged at revision: 59
Proposed branch: lp:~mew/charm-helpers/no-host-magic
Merge into: lp:charm-helpers
Diff against target: 190 lines (+26/-57)
2 files modified
charmhelpers/core/host.py (+7/-10)
tests/core/test_host.py (+19/-47)
To merge this branch: bzr merge lp:~mew/charm-helpers/no-host-magic
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Approve
Review via email: mp+174038@code.launchpad.net

Description of the change

Remove magic value interpolation from host.py. Similar functionality exists
in contrib.templating.

To post a comment you must log in.
Revision history for this message
Stuart Bishop (stub) wrote :

Is host.symlink() useful at all now? I guess os.symlink() doesn't force creation and no logging.

Looks good to me.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'charmhelpers/core/host.py'
--- charmhelpers/core/host.py 2013-07-09 10:43:41 +0000
+++ charmhelpers/core/host.py 2013-07-10 20:02:32 +0000
@@ -14,7 +14,7 @@
1414
15from collections import OrderedDict15from collections import OrderedDict
1616
17from hookenv import log, execution_environment17from hookenv import log
1818
1919
20def service_start(service_name):20def service_start(service_name):
@@ -74,36 +74,33 @@
7474
75def rsync(from_path, to_path, flags='-r', options=None):75def rsync(from_path, to_path, flags='-r', options=None):
76 """Replicate the contents of a path"""76 """Replicate the contents of a path"""
77 context = execution_environment()
78 options = options or ['--delete', '--executability']77 options = options or ['--delete', '--executability']
79 cmd = ['/usr/bin/rsync', flags]78 cmd = ['/usr/bin/rsync', flags]
80 cmd.extend(options)79 cmd.extend(options)
81 cmd.append(from_path.format(**context))80 cmd.append(from_path)
82 cmd.append(to_path.format(**context))81 cmd.append(to_path)
83 log(" ".join(cmd))82 log(" ".join(cmd))
84 return subprocess.check_output(cmd).strip()83 return subprocess.check_output(cmd).strip()
8584
8685
87def symlink(source, destination):86def symlink(source, destination):
88 """Create a symbolic link"""87 """Create a symbolic link"""
89 context = execution_environment()
90 log("Symlinking {} as {}".format(source, destination))88 log("Symlinking {} as {}".format(source, destination))
91 cmd = [89 cmd = [
92 'ln',90 'ln',
93 '-sf',91 '-sf',
94 source.format(**context),92 source,
95 destination.format(**context)93 destination,
96 ]94 ]
97 subprocess.check_call(cmd)95 subprocess.check_call(cmd)
9896
9997
100def mkdir(path, owner='root', group='root', perms=0555, force=False):98def mkdir(path, owner='root', group='root', perms=0555, force=False):
101 """Create a directory"""99 """Create a directory"""
102 context = execution_environment()
103 log("Making dir {} {}:{} {:o}".format(path, owner, group,100 log("Making dir {} {}:{} {:o}".format(path, owner, group,
104 perms))101 perms))
105 uid = pwd.getpwnam(owner.format(**context)).pw_uid102 uid = pwd.getpwnam(owner).pw_uid
106 gid = grp.getgrnam(group.format(**context)).gr_gid103 gid = grp.getgrnam(group).gr_gid
107 realpath = os.path.abspath(path)104 realpath = os.path.abspath(path)
108 if os.path.exists(realpath):105 if os.path.exists(realpath):
109 if force and not os.path.isdir(realpath):106 if force and not os.path.isdir(realpath):
110107
=== modified file 'tests/core/test_host.py'
--- tests/core/test_host.py 2013-07-09 10:43:41 +0000
+++ tests/core/test_host.py 2013-07-10 20:02:32 +0000
@@ -252,15 +252,10 @@
252 ])252 ])
253253
254 @patch('subprocess.check_output')254 @patch('subprocess.check_output')
255 @patch.object(host, 'execution_environment')
256 @patch.object(host, 'log')255 @patch.object(host, 'log')
257 def test_rsyncs_a_path(self, log, execution_environment, check_output):256 def test_rsyncs_a_path(self, log, check_output):
258 execution_environment.return_value = {257 from_path = '/from/this/path/foo'
259 'foo': 'FOO',258 to_path = '/to/this/path/bar'
260 'bar': 'BAR',
261 }
262 from_path = '/from/this/path/{foo}'
263 to_path = '/to/this/path/{bar}'
264 check_output.return_value = ' some output '259 check_output.return_value = ' some output '
265260
266 result = host.rsync(from_path, to_path)261 result = host.rsync(from_path, to_path)
@@ -268,42 +263,31 @@
268 self.assertEqual(result, 'some output')263 self.assertEqual(result, 'some output')
269 check_output.assert_called_with(['/usr/bin/rsync', '-r', '--delete',264 check_output.assert_called_with(['/usr/bin/rsync', '-r', '--delete',
270 '--executability',265 '--executability',
271 '/from/this/path/FOO',266 '/from/this/path/foo',
272 '/to/this/path/BAR'])267 '/to/this/path/bar'])
273268
274 @patch('subprocess.check_call')269 @patch('subprocess.check_call')
275 @patch.object(host, 'execution_environment')
276 @patch.object(host, 'log')270 @patch.object(host, 'log')
277 def test_creates_a_symlink(self, log, execution_environment, check_call):271 def test_creates_a_symlink(self, log, check_call):
278 execution_environment.return_value = {272 source = '/from/this/path/foo'
279 'foo': 'FOO',273 destination = '/to/this/path/bar'
280 'bar': 'BAR',
281 }
282 source = '/from/this/path/{foo}'
283 destination = '/to/this/path/{bar}'
284274
285 host.symlink(source, destination)275 host.symlink(source, destination)
286276
287 check_call.assert_called_with(['ln', '-sf',277 check_call.assert_called_with(['ln', '-sf',
288 '/from/this/path/FOO',278 '/from/this/path/foo',
289 '/to/this/path/BAR'])279 '/to/this/path/bar'])
290280
291 @patch('pwd.getpwnam')281 @patch('pwd.getpwnam')
292 @patch('grp.getgrnam')282 @patch('grp.getgrnam')
293 @patch.object(host, 'execution_environment')
294 @patch.object(host, 'log')283 @patch.object(host, 'log')
295 @patch.object(host, 'os')284 @patch.object(host, 'os')
296 def test_creates_a_directory_if_it_doesnt_exist(self, os_, log,285 def test_creates_a_directory_if_it_doesnt_exist(self, os_, log,
297 execution_environment,
298 getgrnam, getpwnam):286 getgrnam, getpwnam):
299 execution_environment.return_value = {
300 'foo': 'FOO',
301 'bar': 'BAR',
302 }
303 uid = 123287 uid = 123
304 gid = 234288 gid = 234
305 owner = 'some-user-{foo}'289 owner = 'some-user'
306 group = 'some-group-{bar}'290 group = 'some-group'
307 path = '/some/other/path/from/link'291 path = '/some/other/path/from/link'
308 realpath = '/some/path'292 realpath = '/some/path'
309 path_exists = False293 path_exists = False
@@ -316,22 +300,16 @@
316300
317 host.mkdir(path, owner=owner, group=group, perms=perms)301 host.mkdir(path, owner=owner, group=group, perms=perms)
318302
319 getpwnam.assert_called_with('some-user-FOO')303 getpwnam.assert_called_with('some-user')
320 getgrnam.assert_called_with('some-group-BAR')304 getgrnam.assert_called_with('some-group')
321 os_.path.abspath.assert_called_with(path)305 os_.path.abspath.assert_called_with(path)
322 os_.path.exists.assert_called_with(realpath)306 os_.path.exists.assert_called_with(realpath)
323 os_.makedirs.assert_called_with(realpath, perms)307 os_.makedirs.assert_called_with(realpath, perms)
324 os_.chown.assert_called_with(realpath, uid, gid)308 os_.chown.assert_called_with(realpath, uid, gid)
325309
326 @patch.object(host, 'execution_environment')
327 @patch.object(host, 'log')310 @patch.object(host, 'log')
328 @patch.object(host, 'os')311 @patch.object(host, 'os')
329 def test_creates_a_directory_with_defaults(self, os_, log,312 def test_creates_a_directory_with_defaults(self, os_, log):
330 execution_environment):
331 execution_environment.return_value = {
332 'foo': 'FOO',
333 'bar': 'BAR',
334 }
335 uid = 0313 uid = 0
336 gid = 0314 gid = 0
337 path = '/some/other/path/from/link'315 path = '/some/other/path/from/link'
@@ -351,20 +329,14 @@
351329
352 @patch('pwd.getpwnam')330 @patch('pwd.getpwnam')
353 @patch('grp.getgrnam')331 @patch('grp.getgrnam')
354 @patch.object(host, 'execution_environment')
355 @patch.object(host, 'log')332 @patch.object(host, 'log')
356 @patch.object(host, 'os')333 @patch.object(host, 'os')
357 def test_removes_file_with_same_path_before_mkdir(self, os_, log,334 def test_removes_file_with_same_path_before_mkdir(self, os_, log,
358 execution_environment,
359 getgrnam, getpwnam):335 getgrnam, getpwnam):
360 execution_environment.return_value = {
361 'foo': 'FOO',
362 'bar': 'BAR',
363 }
364 uid = 123336 uid = 123
365 gid = 234337 gid = 234
366 owner = 'some-user-{foo}'338 owner = 'some-user'
367 group = 'some-group-{bar}'339 group = 'some-group'
368 path = '/some/other/path/from/link'340 path = '/some/other/path/from/link'
369 realpath = '/some/path'341 realpath = '/some/path'
370 path_exists = True342 path_exists = True
@@ -380,8 +352,8 @@
380352
381 host.mkdir(path, owner=owner, group=group, perms=perms, force=force)353 host.mkdir(path, owner=owner, group=group, perms=perms, force=force)
382354
383 getpwnam.assert_called_with('some-user-FOO')355 getpwnam.assert_called_with('some-user')
384 getgrnam.assert_called_with('some-group-BAR')356 getgrnam.assert_called_with('some-group')
385 os_.path.abspath.assert_called_with(path)357 os_.path.abspath.assert_called_with(path)
386 os_.path.exists.assert_called_with(realpath)358 os_.path.exists.assert_called_with(realpath)
387 os_.unlink.assert_called_with(realpath)359 os_.unlink.assert_called_with(realpath)

Subscribers

People subscribed via source and target branches