diff options
author | Michael DeHaan <michael.dehaan@gmail.com> | 2014-03-12 16:44:03 -0400 |
---|---|---|
committer | Michael DeHaan <michael@ansibleworks.com> | 2014-03-12 18:04:18 -0400 |
commit | 626f9629e224dcfe7d370fc1fcf8f3cd1ce330e2 (patch) | |
tree | f761c8f2e31e59114e148a47d99d9a258b286b20 | |
parent | 80df6e4197368bc3b9557342e52dbd3d00bc5315 (diff) | |
download | ansible-626f9629e224dcfe7d370fc1fcf8f3cd1ce330e2.tar.gz |
Various commits to enable no-shell or safe-shell usage, more to come.
Conflicts:
library/packaging/cpanm
-rw-r--r-- | library/packaging/apt_repository | 5 | ||||
-rw-r--r-- | library/packaging/cpanm | 145 | ||||
-rw-r--r-- | library/packaging/gem | 2 | ||||
-rw-r--r-- | library/packaging/macports | 6 | ||||
-rw-r--r-- | library/packaging/opkg | 3 | ||||
-rw-r--r-- | library/packaging/pacman | 6 |
6 files changed, 159 insertions, 8 deletions
diff --git a/library/packaging/apt_repository b/library/packaging/apt_repository index 4587d90ba7..7a19dabc16 100644 --- a/library/packaging/apt_repository +++ b/library/packaging/apt_repository @@ -352,7 +352,10 @@ def get_add_ppa_signing_key_callback(module): def _run_command(command): module.run_command(command, check_rc=True) - return _run_command if not module.check_mode else None + if module.check_mode: + return _run_command + else: + return None def main(): diff --git a/library/packaging/cpanm b/library/packaging/cpanm new file mode 100644 index 0000000000..1c73d2727c --- /dev/null +++ b/library/packaging/cpanm @@ -0,0 +1,145 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# (c) 2012, Franck Cuny <franck@lumberjaph.net> +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. +# + +DOCUMENTATION = ''' +--- +module: cpanm +short_description: Manages Perl library dependencies. +description: + - Manage Perl library dependencies. +version_added: "1.0" +options: + name: + description: + - The name of the Perl library to install + required: false + default: null + aliases: ["pkg"] + from_path: + description: + - The local directory from where to install + required: false + default: null + notest: + description: + - Do not run unit tests + required: false + default: false + locallib: + description: + - Specify the install base to install modules + required: false + default: false + mirror: + description: + - Specifies the base URL for the CPAN mirror to use + required: false + default: false +examples: + - code: "cpanm: name=Dancer" + description: Install I(Dancer) perl package. + - code: "cpanm: name=Dancer locallib=/srv/webapps/my_app/extlib" + description: "Install I(Dancer) (U(http://perldancer.org/)) into the specified I(locallib)" + - code: "cpanm: from_path=/srv/webapps/my_app/src/" + description: Install perl dependencies from local directory. + - code: "cpanm: name=Dancer notest=True locallib=/srv/webapps/my_app/extlib" + description: Install I(Dancer) perl package without running the unit tests in indicated I(locallib). + - code: "cpanm: name=Dancer mirror=http://cpan.cpantesters.org/" + description: Install I(Dancer) perl package from a specific mirror +notes: + - Please note that U(http://search.cpan.org/dist/App-cpanminus/bin/cpanm, cpanm) must be installed on the remote host. +author: Franck Cuny +''' + +def _is_package_installed(module, name, locallib, cpanm): + cmd = "" + if locallib: + os.environ["PERL5LIB"] = "%s/lib/perl5" % locallib + cmd = "%s perl -M%s -e '1'" % (cmd, name) + res, stdout, stderr = module.run_command(cmd, check_rc=False) + if res == 0 + return True + else + return False + +def _build_cmd_line(name, from_path, notest, locallib, mirror, cpanm): + # this code should use "%s" like everything else and just return early but not fixing all of it now. + # don't copy stuff like this + if from_path: + cmd = "{cpanm} {path}".format(cpanm=cpanm, path=from_path) + else: + cmd = "{cpanm} {name}".format(cpanm=cpanm, name=name) + + if notest is True: + cmd = "{cmd} -n".format(cmd=cmd) + + if locallib is not None: + cmd = "{cmd} -l {locallib}".format(cmd=cmd, locallib=locallib) + + if mirror is not None: + cmd = "{cmd} --mirror {mirror}".format(cmd=cmd, mirror=mirror) + + return cmd + + +def main(): + arg_spec = dict( + name=dict(default=None, required=False, aliases=['pkg']), + from_path=dict(default=None, required=False), + notest=dict(default=False, type='bool'), + locallib=dict(default=None, required=False), + mirror=dict(default=None, required=False) + ) + + module = AnsibleModule( + argument_spec=arg_spec, + required_one_of=[['name', 'from_path']], + ) + + cpanm = module.get_bin_path('cpanm', True) + name = module.params['name'] + from_path = module.params['from_path'] + notest = module.boolean(module.params.get('notest', False)) + locallib = module.params['locallib'] + mirror = module.params['mirror'] + + changed = False + + installed = _is_package_installed(module, name, locallib, cpanm) + + if not installed: + out_cpanm = err_cpanm = '' + cmd = _build_cmd_line(name, from_path, notest, locallib, mirror, cpanm) + + rc_cpanm, out_cpanm, err_cpanm = module.run_command(cmd, check_rc=False) + + if rc_cpanm != 0: + module.fail_json(msg=err_cpanm, cmd=cmd) + + if err_cpanm and 'is up to date' not in err_cpanm: + changed = True + + module.exit_json(changed=changed, binary=cpanm, name=name) + +# import module snippets +from ansible.module_utils.basic import * + +main() diff --git a/library/packaging/gem b/library/packaging/gem index 25fc337e14..079711fecf 100644 --- a/library/packaging/gem +++ b/library/packaging/gem @@ -89,7 +89,7 @@ def get_rubygems_path(module): return module.get_bin_path('gem', True) def get_rubygems_version(module): - cmd = [get_rubygems_path(module), '--version'] + cmd = [ get_rubygems_path(module), '--version' ] (rc, out, err) = module.run_command(cmd, check_rc=True) match = re.match(r'^(\d+)\.(\d+)\.(\d+)', out) diff --git a/library/packaging/macports b/library/packaging/macports index b58224b63f..ae7010b1cb 100644 --- a/library/packaging/macports +++ b/library/packaging/macports @@ -53,6 +53,7 @@ EXAMPLES = ''' - macports: name=foo state=inactive ''' +import pipes def update_package_db(module, port_path): """ Updates packages list. """ @@ -68,7 +69,7 @@ def query_package(module, port_path, name, state="present"): if state == "present": - rc, out, err = module.run_command("%s installed | grep -q ^.*%s" % (port_path, name)) + rc, out, err = module.run_command("%s installed | grep -q ^.*%s" % (pipes.quote(port_path), pipes.quote(name)), use_unsafe_shell=True) if rc == 0: return True @@ -76,7 +77,8 @@ def query_package(module, port_path, name, state="present"): elif state == "active": - rc, out, err = module.run_command("%s installed %s | grep -q active" % (port_path, name)) + rc, out, err = module.run_command("%s installed %s | grep -q active" % (pipes.quote(port_path), pipes.quote(name)), use_unsafe_shell=True) + if rc == 0: return True diff --git a/library/packaging/opkg b/library/packaging/opkg index 4a834cf1a3..0187abe56a 100644 --- a/library/packaging/opkg +++ b/library/packaging/opkg @@ -51,6 +51,7 @@ EXAMPLES = ''' - opkg: name=foo,bar state=absent ''' +import pipes def update_package_db(module, opkg_path): """ Updates packages list. """ @@ -66,7 +67,7 @@ def query_package(module, opkg_path, name, state="present"): if state == "present": - rc, out, err = module.run_command("%s list-installed | grep -q ^%s" % (opkg_path, name)) + rc, out, err = module.run_command("%s list-installed | grep -q ^%s" % (pipes.quote(opkg_path), pipes.quote(name)), use_unsafe_shell=True) if rc == 0: return True diff --git a/library/packaging/pacman b/library/packaging/pacman index a4a24ca5fd..46b7f4c755 100644 --- a/library/packaging/pacman +++ b/library/packaging/pacman @@ -100,7 +100,7 @@ def query_package(module, name, state="installed"): def update_package_db(module): - cmd = "pacman -Syy > /dev/null" + cmd = "pacman -Syy" rc, stdout, stderr = module.run_command(cmd, check_rc=False) if rc != 0: @@ -120,7 +120,7 @@ def remove_packages(module, packages): if not query_package(module, package): continue - cmd = "pacman -%s %s --noconfirm > /dev/null" % (args, package) + cmd = "pacman -%s %s --noconfirm" % (args, package) rc, stdout, stderr = module.run_command(cmd, check_rc=False) if rc != 0: @@ -148,7 +148,7 @@ def install_packages(module, packages, package_files): else: params = '-S %s' % package - cmd = "pacman %s --noconfirm > /dev/null" % (params) + cmd = "pacman %s --noconfirm" % (params) rc, stdout, stderr = module.run_command(cmd, check_rc=False) if rc != 0: |