Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method get_bin_path to module_common and update modules to use it #968

Merged
merged 5 commits into from Aug 30, 2012
Merged

Add method get_bin_path to module_common and update modules to use it #968

merged 5 commits into from Aug 30, 2012

Conversation

sfromm
Copy link
Contributor

@sfromm sfromm commented Aug 30, 2012

Following up on #903, this adds get_bin_path to module_common and updates modules to use it.

Please let me know if you have any comments/questions. Thanks. :-)

This is meant to assist all the modules that look for the full path of
an executable.  If it is found and is X_OK, returns the full path.
Otherwise, it returns None.
The optional list is prepended to PATH.
Fix get_bin_path() to use os.path.join().
* Migraed easy_install, pip, service, setup, and user.
* Updated fail_json message in apt_repository
* Fixed easy_install to not hardcode location of virtualenv in
  /usr/local/bin/.
* Made handling of virtualenv more consistent between easy_install and
  pip.
@mpdehaan
Copy link
Contributor

A few small things I found that would reduce the amount of boilerplate involved:

In a few places we have code like this in the module:

def get_bin_path(module, arg):
-    if os.path.exists('/usr/sbin/%s' % arg):
-        return '/usr/sbin/%s' % arg
-    elif os.path.exists('/sbin/%s' % arg):
-        return '/sbin/%s' % arg
-    else:
+    bin = module.get_bin_path(arg)  
+    if bin is None:
         module.fail_json(msg="Cannot find %s" % arg)
+    else:
+        return bin

Here, rather than just calling the get bin path inside another function, I think we'd use to add a module.get_bin_path(..., required=True) or something, such that we can eliminate this boilerplate as well across the board. If the path is not found it could automatically fail, which eliminates the need for the wrapper function as well as the "if bin is None" logic.

In the supervisorctl module:

  SUPERVISORCTL = _find_supervisorctl()
+    SUPERVISORCTL = module.get_bin_path('supervisorctl')
+    if SUPERVISORCTL is None:
+        module.fail_json(msg='supervisorctl is not installed')

It looks like the _find_supervisorctl function should be removed.

Other than that, looks solid.

@sfromm
Copy link
Contributor Author

sfromm commented Aug 30, 2012

On Thu, Aug 30, 2012 at 5:16 AM, Michael DeHaan notifications@github.comwrote:

A few small things I found that would reduce the amount of boilerplate
involved:

In a few places we have code like this in the module:

def get_bin_path(module, arg):

  • if os.path.exists('/usr/sbin/%s' % arg):
  • return '/usr/sbin/%s' % arg
  • elif os.path.exists('/sbin/%s' % arg):
  • return '/sbin/%s' % arg
  • else:
  • bin = module.get_bin_path(arg)
  • if bin is None: module.fail_json(msg="Cannot find %s" % arg)
  • else:
  • return bin

Here, rather than just calling the get bin path inside another function, I
think we'd use to add a module.get_bin_path(..., required=True) or
something, such that we can eliminate this boilerplate as well across the
board. If the path is not found it could automatically fail, which
eliminates the need for the wrapper function as well as the "if bin is
None" logic.

Sure, that will make the modules a bit cleaner. :-)

In the supervisorctl module:

SUPERVISORCTL = _find_supervisorctl()

  • SUPERVISORCTL = module.get_bin_path('supervisorctl')
  • if SUPERVISORCTL is None:
  • module.fail_json(msg='supervisorctl is not installed')

It looks like the _find_supervisorctl function should be removed.

Other than that, looks solid.

I'll prune that from the supervisorctl module. I tried to tread lightly
through some of the modules; this was one of them. :-/

sf

Added required as optional argument to get_bin_path(). It defaults to
false.  Updated following modules to use required=True when calling
get_bin_path():  apt_repository, easy_install, group, pip,
supervisorctl, and user.
Also removed _find_supervisorctl() from supervisorctl module and updated
_is_running() to not need it.
@sfromm
Copy link
Contributor Author

sfromm commented Aug 30, 2012

Commit 6742e9c should address the above issues.

@mpdehaan mpdehaan merged commit 6742e9c into ansible:devel Aug 30, 2012
@ansible ansible locked and limited conversation to collaborators Apr 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants