| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2012, Matt Wright <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: easy_install |
| 11 |
short_description: Installs Python libraries |
| 12 |
description: |
| 13 |
- Installs Python libraries, optionally in a C(virtualenv). |
| 14 |
extends_documentation_fragment: |
| 15 |
- community.general._attributes |
| 16 |
attributes: |
| 17 |
check_mode: |
| 18 |
support: full |
| 19 |
diff_mode: |
| 20 |
support: none |
| 21 |
options: |
| 22 |
name: |
| 23 |
type: str |
| 24 |
description: |
| 25 |
- A Python library name. |
| 26 |
required: true |
| 27 |
virtualenv: |
| 28 |
type: str |
| 29 |
description: |
| 30 |
- An optional O(virtualenv) directory path to install into. If the O(virtualenv) does not exist, it is created automatically. |
| 31 |
virtualenv_site_packages: |
| 32 |
description: |
| 33 |
- Whether the virtual environment inherits packages from the global site-packages directory. Note that this setting |
| 34 |
has no effect on an already existing virtual environment, so if you want to change it, the environment must be deleted |
| 35 |
and newly created. |
| 36 |
type: bool |
| 37 |
default: false |
| 38 |
virtualenv_command: |
| 39 |
type: str |
| 40 |
description: |
| 41 |
- The command to create the virtual environment with. For example V(pyvenv), V(virtualenv), V(virtualenv2). |
| 42 |
default: virtualenv |
| 43 |
executable: |
| 44 |
type: str |
| 45 |
description: |
| 46 |
- The explicit executable or a pathname to the executable to be used to run easy_install for a specific version of Python |
| 47 |
installed in the system. For example V(easy_install-3.3), if there are both Python 2.7 and 3.3 installations in the |
| 48 |
system and you want to run easy_install for the Python 3.3 installation. |
| 49 |
default: easy_install |
| 50 |
state: |
| 51 |
type: str |
| 52 |
description: |
| 53 |
- The desired state of the library. V(latest) ensures that the latest version is installed. |
| 54 |
choices: [present, latest] |
| 55 |
default: present |
| 56 |
notes: |
| 57 |
- Please note that the C(easy_install) module can only install Python libraries. Thus this module is not able to remove |
| 58 |
libraries. It is generally recommended to use the M(ansible.builtin.pip) module which you can first install using M(community.general.easy_install). |
| 59 |
- Also note that C(virtualenv) must be installed on the remote host if the O(virtualenv) parameter is specified. |
| 60 |
requirements: ["virtualenv"] |
| 61 |
author: "Matt Wright (@mattupstate)" |
| 62 |
""" |
| 63 |
|
| 64 |
EXAMPLES = r""" |
| 65 |
- name: Install or update pip |
| 66 |
community.general.easy_install: |
| 67 |
name: pip |
| 68 |
state: latest |
| 69 |
|
| 70 |
- name: Install Bottle into the specified virtualenv |
| 71 |
community.general.easy_install: |
| 72 |
name: bottle |
| 73 |
virtualenv: /webapps/myapp/venv |
| 74 |
|
| 75 |
- name: Install a python package using pyvenv as the virtualenv tool |
| 76 |
community.general.easy_install: |
| 77 |
name: package_name |
| 78 |
virtualenv: /opt/myenv |
| 79 |
virtualenv_command: pyvenv |
| 80 |
""" |
| 81 |
|
| 82 |
import os |
| 83 |
import os.path |
| 84 |
import tempfile |
| 85 |
|
| 86 |
from ansible.module_utils.basic import AnsibleModule |
| 87 |
|
| 88 |
|
| 89 |
def install_package(module, name, easy_install, executable_arguments): |
| 90 |
cmd = [easy_install] + executable_arguments + [name] |
| 91 |
rc, out, err = module.run_command(cmd) |
| 92 |
return rc, out, err |
| 93 |
|
| 94 |
|
| 95 |
def _is_package_installed(module, name, easy_install, executable_arguments): |
| 96 |
# Copy and add to the arguments |
| 97 |
executable_arguments = executable_arguments[:] |
| 98 |
executable_arguments.append("--dry-run") |
| 99 |
rc, out, err = install_package(module, name, easy_install, executable_arguments) |
| 100 |
if rc: |
| 101 |
module.fail_json(msg=err) |
| 102 |
return "Downloading" not in out |
| 103 |
|
| 104 |
|
| 105 |
def _get_easy_install(module, env=None, executable=None): |
| 106 |
candidate_easy_inst_basenames = ["easy_install"] |
| 107 |
easy_install = None |
| 108 |
if executable is not None: |
| 109 |
if os.path.isabs(executable): |
| 110 |
easy_install = executable |
| 111 |
else: |
| 112 |
candidate_easy_inst_basenames.insert(0, executable) |
| 113 |
if easy_install is None: |
| 114 |
if env is None: |
| 115 |
opt_dirs = [] |
| 116 |
else: |
| 117 |
# Try easy_install with the virtualenv directory first. |
| 118 |
opt_dirs = [f"{env}/bin"] |
| 119 |
for basename in candidate_easy_inst_basenames: |
| 120 |
easy_install = module.get_bin_path(basename, False, opt_dirs) |
| 121 |
if easy_install is not None: |
| 122 |
break |
| 123 |
# easy_install should have been found by now. The final call to |
| 124 |
# get_bin_path will trigger fail_json. |
| 125 |
if easy_install is None: |
| 126 |
basename = candidate_easy_inst_basenames[0] |
| 127 |
easy_install = module.get_bin_path(basename, True, opt_dirs) |
| 128 |
return easy_install |
| 129 |
|
| 130 |
|
| 131 |
def main(): |
| 132 |
arg_spec = dict( |
| 133 |
name=dict(required=True), |
| 134 |
state=dict(default="present", choices=["present", "latest"], type="str"), |
| 135 |
virtualenv=dict(), |
| 136 |
virtualenv_site_packages=dict(default=False, type="bool"), |
| 137 |
virtualenv_command=dict(default="virtualenv"), |
| 138 |
executable=dict(default="easy_install"), |
| 139 |
) |
| 140 |
|
| 141 |
module = AnsibleModule(argument_spec=arg_spec, supports_check_mode=True) |
| 142 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 143 |
|
| 144 |
name = module.params["name"] |
| 145 |
env = module.params["virtualenv"] |
| 146 |
executable = module.params["executable"] |
| 147 |
site_packages = module.params["virtualenv_site_packages"] |
| 148 |
virtualenv_command = module.params["virtualenv_command"] |
| 149 |
executable_arguments = [] |
| 150 |
if module.params["state"] == "latest": |
| 151 |
executable_arguments.append("--upgrade") |
| 152 |
|
| 153 |
rc = 0 |
| 154 |
err = "" |
| 155 |
out = "" |
| 156 |
|
| 157 |
if env: |
| 158 |
virtualenv = module.get_bin_path(virtualenv_command, True) |
| 159 |
|
| 160 |
if not os.path.exists(os.path.join(env, "bin", "activate")): |
| 161 |
if module.check_mode: |
| 162 |
module.exit_json(changed=True) |
| 163 |
command = f"{virtualenv} {env}" |
| 164 |
if site_packages: |
| 165 |
command += " --system-site-packages" |
| 166 |
cwd = tempfile.gettempdir() |
| 167 |
rc_venv, out_venv, err_venv = module.run_command(command, cwd=cwd) |
| 168 |
|
| 169 |
rc += rc_venv |
| 170 |
out += out_venv |
| 171 |
err += err_venv |
| 172 |
|
| 173 |
easy_install = _get_easy_install(module, env, executable) |
| 174 |
|
| 175 |
cmd = None |
| 176 |
changed = False |
| 177 |
installed = _is_package_installed(module, name, easy_install, executable_arguments) |
| 178 |
|
| 179 |
if not installed: |
| 180 |
if module.check_mode: |
| 181 |
module.exit_json(changed=True) |
| 182 |
rc_easy_inst, out_easy_inst, err_easy_inst = install_package(module, name, easy_install, executable_arguments) |
| 183 |
|
| 184 |
rc += rc_easy_inst |
| 185 |
out += out_easy_inst |
| 186 |
err += err_easy_inst |
| 187 |
|
| 188 |
changed = True |
| 189 |
|
| 190 |
if rc != 0: |
| 191 |
module.fail_json(msg=err, cmd=cmd) |
| 192 |
|
| 193 |
module.exit_json(changed=changed, binary=easy_install, name=name, virtualenv=env) |
| 194 |
|
| 195 |
|
| 196 |
if __name__ == "__main__": |
| 197 |
main() |