Add option required=(True|False) to get_bin_path and update modules

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.
This commit is contained in:
Stephen Fromm 2012-08-30 10:31:23 -07:00
parent e5a635672c
commit 6742e9c3f4
7 changed files with 21 additions and 49 deletions

View file

@ -202,9 +202,12 @@ class AnsibleModule(object):
log_args = re.sub(r'login_password=.+ (.*)', r"login_password=NOT_LOGGING_PASSWORD \1", log_args) log_args = re.sub(r'login_password=.+ (.*)', r"login_password=NOT_LOGGING_PASSWORD \1", log_args)
syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % log_args) syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % log_args)
def get_bin_path(self, arg, opt_dirs=[]): def get_bin_path(self, arg, required=False, opt_dirs=[]):
''' '''
find system executable in PATH. find system executable in PATH.
Optional arguments:
- required: if executable is not found and required is true, fail_json
- opt_dirs: optional list of directories to search in addition to PATH
if found return full path; otherwise return None if found return full path; otherwise return None
''' '''
sbin_paths = ['/sbin', '/usr/sbin', '/usr/local/sbin'] sbin_paths = ['/sbin', '/usr/sbin', '/usr/local/sbin']
@ -223,6 +226,8 @@ class AnsibleModule(object):
if os.path.exists(path) and os.access(path, os.X_OK): if os.path.exists(path) and os.access(path, os.X_OK):
bin_path = path bin_path = path
break break
if required and bin_path is None:
self.fail_json(msg='Failed to find required executable %s' % arg)
return bin_path return bin_path
def boolean(self, arg): def boolean(self, arg):

View file

@ -46,9 +46,7 @@ def main():
module = AnsibleModule(argument_spec=arg_spec) module = AnsibleModule(argument_spec=arg_spec)
add_apt_repository = module.get_bin_path(ADD_APT_REPO) add_apt_repository = module.get_bin_path(ADD_APT_REPO, True)
if add_apt_repository is None:
module.fail_json(msg='Unable to find executable %s' % ADD_APT_REPO)
repo = module.params['repo'] repo = module.params['repo']
state = module.params['state'] state = module.params['state']

View file

@ -50,16 +50,14 @@ def main():
name = module.params['name'] name = module.params['name']
env = module.params['virtualenv'] env = module.params['virtualenv']
easy_install = module.get_bin_path('easy_install', ['%s/bin' % env]) easy_install = module.get_bin_path('easy_install', True, ['%s/bin' % env])
if easy_install is None:
module.fail_json(msg='easy_install is not installed')
rc = 0 rc = 0
err = '' err = ''
out = '' out = ''
if env: if env:
virtualenv = module.get_bin_path('virtualenv') virtualenv = module.get_bin_path('virtualenv', True)
if virtualenv is None: if virtualenv is None:
module.fail_json(msg='virtualenv is not installed') module.fail_json(msg='virtualenv is not installed')

View file

@ -20,22 +20,15 @@
import grp import grp
def get_bin_path(module, arg):
bin = module.get_bin_path(arg)
if bin is None:
module.fail_json(msg="Cannot find %s" % arg)
else:
return bin
def group_del(module, group): def group_del(module, group):
cmd = [get_bin_path(module, 'groupdel'), group] cmd = [module.get_bin_path('groupdel', True), group]
p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = p.communicate() (out, err) = p.communicate()
rc = p.returncode rc = p.returncode
return (rc, out, err) return (rc, out, err)
def group_add(module, group, **kwargs): def group_add(module, group, **kwargs):
cmd = [get_bin_path(module, 'groupadd')] cmd = [module.get_bin_path('groupadd', True)]
for key in kwargs: for key in kwargs:
if key == 'gid' and kwargs[key] is not None: if key == 'gid' and kwargs[key] is not None:
cmd.append('-g') cmd.append('-g')
@ -49,7 +42,7 @@ def group_add(module, group, **kwargs):
return (rc, out, err) return (rc, out, err)
def group_mod(module, group, **kwargs): def group_mod(module, group, **kwargs):
cmd = [get_bin_path(module, 'groupmod')] cmd = [module.get_bin_path('groupmod', True)]
info = group_info(group) info = group_info(group)
for key in kwargs: for key in kwargs:
if key == 'gid': if key == 'gid':

View file

@ -77,9 +77,7 @@ def main():
env = module.params['virtualenv'] env = module.params['virtualenv']
if env: if env:
virtualenv = module.get_bin_path('virtualenv') virtualenv = module.get_bin_path('virtualenv', True)
if virtualenv is None:
module.fail_json(msg='virtualenv is not installed')
rc_venv, out_venv, err_venv = _ensure_virtualenv(module, env, virtualenv) rc_venv, out_venv, err_venv = _ensure_virtualenv(module, env, virtualenv)
@ -87,9 +85,7 @@ def main():
out += out_venv out += out_venv
err += err_venv err += err_venv
pip = module.get_bin_path('pip', ['%s/bin' % env]) pip = module.get_bin_path('pip', True, ['%s/bin' % env])
if pip is None:
module.fail_json(msg='pip is not installed')
state = module.params['state'] state = module.params['state']
name = module.params['name'] name = module.params['name']

View file

@ -19,17 +19,8 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# #
def _find_supervisorctl(): def _is_running(name, supervisorctl):
paths = ['/usr/local/bin', '/usr/bin'] rc, out, err = _run('%s status %s' % (supervisorctl, name))
for p in paths:
e = p + '/supervisorctl'
if os.path.exists(e):
return e
def _is_running(name):
rc, out, err = _run('%s status %s' % (_find_supervisorctl(), name))
return 'RUNNING' in out return 'RUNNING' in out
@ -52,14 +43,12 @@ def main():
name = module.params['name'] name = module.params['name']
state = module.params['state'] state = module.params['state']
SUPERVISORCTL = module.get_bin_path('supervisorctl') SUPERVISORCTL = module.get_bin_path('supervisorctl', True)
if SUPERVISORCTL is None:
module.fail_json(msg='supervisorctl is not installed')
if SUPERVISORCTL is None: if SUPERVISORCTL is None:
module.fail_json(msg='supervisorctl is not installed') module.fail_json(msg='supervisorctl is not installed')
running = _is_running(name) running = _is_running(name, SUPERVISORCTL)
if running and state == 'started': if running and state == 'started':
module.exit_json(changed=False, name=name, state=state) module.exit_json(changed=False, name=name, state=state)

View file

@ -35,15 +35,8 @@ if os.path.exists('/etc/master.passwd'):
# invoke adduser in lieu of useradd, nor pw in lieu of usermod. # invoke adduser in lieu of useradd, nor pw in lieu of usermod.
# That is, this won't work on FreeBSD. # That is, this won't work on FreeBSD.
def get_bin_path(module, arg):
bin = module.get_bin_path(arg)
if bin is None:
module.fail_json(msg="Cannot find %s" % arg)
else:
return bin
def user_del(module, user, **kwargs): def user_del(module, user, **kwargs):
cmd = [get_bin_path(module, 'userdel')] cmd = [module.get_bin_path('userdel', True)]
for key in kwargs: for key in kwargs:
if key == 'force' and kwargs[key] == 'yes': if key == 'force' and kwargs[key] == 'yes':
cmd.append('-f') cmd.append('-f')
@ -56,7 +49,7 @@ def user_del(module, user, **kwargs):
return (rc, out, err) return (rc, out, err)
def user_add(module, user, **kwargs): def user_add(module, user, **kwargs):
cmd = [get_bin_path(module, 'useradd')] cmd = [module.get_bin_path('useradd', True)]
for key in kwargs: for key in kwargs:
if key == 'uid' and kwargs[key] is not None: if key == 'uid' and kwargs[key] is not None:
cmd.append('-u') cmd.append('-u')
@ -103,7 +96,7 @@ Without spwd, we would have to resort to reading /etc/shadow
to get the encrypted string. For now, punt on idempotent password changes. to get the encrypted string. For now, punt on idempotent password changes.
""" """
def user_mod(module, user, **kwargs): def user_mod(module, user, **kwargs):
cmd = [get_bin_path(module, 'usermod')] cmd = [module.get_bin_path('usermod', True)]
info = user_info(user) info = user_info(user)
for key in kwargs: for key in kwargs:
if key == 'uid': if key == 'uid':