| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2021 Radek Sprta <[email protected]> |
| 3 |
# Copyright (c) 2024 Colin Nolan <[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: cargo |
| 11 |
short_description: Manage Rust packages with cargo |
| 12 |
version_added: 4.3.0 |
| 13 |
description: |
| 14 |
- Manage Rust packages with cargo. |
| 15 |
author: "Radek Sprta (@radek-sprta)" |
| 16 |
extends_documentation_fragment: |
| 17 |
- community.general._attributes |
| 18 |
attributes: |
| 19 |
check_mode: |
| 20 |
support: full |
| 21 |
diff_mode: |
| 22 |
support: none |
| 23 |
options: |
| 24 |
executable: |
| 25 |
description: |
| 26 |
- Path to the C(cargo) installed in the system. |
| 27 |
- If not specified, the module looks for C(cargo) in E(PATH). |
| 28 |
type: path |
| 29 |
version_added: 7.5.0 |
| 30 |
name: |
| 31 |
description: |
| 32 |
- The name of a Rust package to install. |
| 33 |
type: list |
| 34 |
elements: str |
| 35 |
required: true |
| 36 |
path: |
| 37 |
description: The base path where to install the Rust packages. Cargo automatically appends V(/bin). In other words, V(/usr/local) |
| 38 |
becomes V(/usr/local/bin). |
| 39 |
type: path |
| 40 |
version: |
| 41 |
description: The version to install. If O(name) contains multiple values, the module tries to install all of them in this |
| 42 |
version. |
| 43 |
type: str |
| 44 |
locked: |
| 45 |
description: |
| 46 |
- Install with locked dependencies. |
| 47 |
- This is only used when installing packages. |
| 48 |
type: bool |
| 49 |
default: false |
| 50 |
version_added: 7.5.0 |
| 51 |
state: |
| 52 |
description: |
| 53 |
- The state of the Rust package. |
| 54 |
type: str |
| 55 |
default: present |
| 56 |
choices: ["present", "absent", "latest"] |
| 57 |
directory: |
| 58 |
description: |
| 59 |
- Path to the source directory to install the Rust package from. |
| 60 |
- This is only used when installing packages. |
| 61 |
type: path |
| 62 |
version_added: 9.1.0 |
| 63 |
features: |
| 64 |
description: |
| 65 |
- List of features to activate. |
| 66 |
- This is only used when installing packages. |
| 67 |
type: list |
| 68 |
elements: str |
| 69 |
default: [] |
| 70 |
version_added: 11.0.0 |
| 71 |
requirements: |
| 72 |
- cargo installed |
| 73 |
""" |
| 74 |
|
| 75 |
EXAMPLES = r""" |
| 76 |
- name: Install "ludusavi" Rust package |
| 77 |
community.general.cargo: |
| 78 |
name: ludusavi |
| 79 |
|
| 80 |
- name: Install "ludusavi" Rust package with locked dependencies |
| 81 |
community.general.cargo: |
| 82 |
name: ludusavi |
| 83 |
locked: true |
| 84 |
|
| 85 |
- name: Install "ludusavi" Rust package in version 0.10.0 |
| 86 |
community.general.cargo: |
| 87 |
name: ludusavi |
| 88 |
version: '0.10.0' |
| 89 |
|
| 90 |
- name: Install "ludusavi" Rust package to global location |
| 91 |
community.general.cargo: |
| 92 |
name: ludusavi |
| 93 |
path: /usr/local |
| 94 |
|
| 95 |
- name: Remove "ludusavi" Rust package |
| 96 |
community.general.cargo: |
| 97 |
name: ludusavi |
| 98 |
state: absent |
| 99 |
|
| 100 |
- name: Update "ludusavi" Rust package its latest version |
| 101 |
community.general.cargo: |
| 102 |
name: ludusavi |
| 103 |
state: latest |
| 104 |
|
| 105 |
- name: Install "ludusavi" Rust package from source directory |
| 106 |
community.general.cargo: |
| 107 |
name: ludusavi |
| 108 |
directory: /path/to/ludusavi/source |
| 109 |
|
| 110 |
- name: Install "serpl" Rust package with ast_grep feature |
| 111 |
community.general.cargo: |
| 112 |
name: serpl |
| 113 |
features: |
| 114 |
- ast_grep |
| 115 |
""" |
| 116 |
|
| 117 |
import json |
| 118 |
import os |
| 119 |
import re |
| 120 |
|
| 121 |
from ansible.module_utils.basic import AnsibleModule |
| 122 |
|
| 123 |
|
| 124 |
class Cargo: |
| 125 |
def __init__(self, module, **kwargs): |
| 126 |
self.module = module |
| 127 |
self.executable = [kwargs["executable"] or module.get_bin_path("cargo", True)] |
| 128 |
self.name = kwargs["name"] |
| 129 |
self.path = kwargs["path"] |
| 130 |
self.state = kwargs["state"] |
| 131 |
self.version = kwargs["version"] |
| 132 |
self.locked = kwargs["locked"] |
| 133 |
self.directory = kwargs["directory"] |
| 134 |
self.features = kwargs["features"] |
| 135 |
|
| 136 |
@property |
| 137 |
def path(self): |
| 138 |
return self._path |
| 139 |
|
| 140 |
@path.setter |
| 141 |
def path(self, path): |
| 142 |
if path is not None and not os.path.isdir(path): |
| 143 |
self.module.fail_json(msg=f"Path {path} is not a directory") |
| 144 |
self._path = path |
| 145 |
|
| 146 |
def _exec(self, args, run_in_check_mode=False, check_rc=True, add_package_name=True): |
| 147 |
if not self.module.check_mode or (self.module.check_mode and run_in_check_mode): |
| 148 |
cmd = self.executable + args |
| 149 |
rc, out, err = self.module.run_command(cmd, check_rc=check_rc) |
| 150 |
return out, err |
| 151 |
return "", "" |
| 152 |
|
| 153 |
def get_installed(self): |
| 154 |
cmd = ["install", "--list"] |
| 155 |
if self.path: |
| 156 |
cmd.append("--root") |
| 157 |
cmd.append(self.path) |
| 158 |
|
| 159 |
data, dummy = self._exec(cmd, True, False, False) |
| 160 |
|
| 161 |
package_regex = re.compile(r"^([\w\-]+) v(\S+).*:$") |
| 162 |
installed = {} |
| 163 |
for line in data.splitlines(): |
| 164 |
package_info = package_regex.match(line) |
| 165 |
if package_info: |
| 166 |
installed[package_info.group(1)] = package_info.group(2) |
| 167 |
|
| 168 |
return installed |
| 169 |
|
| 170 |
def install(self, packages=None): |
| 171 |
cmd = ["install"] |
| 172 |
cmd.extend(packages or self.name) |
| 173 |
if self.locked: |
| 174 |
cmd.append("--locked") |
| 175 |
if self.path: |
| 176 |
cmd.append("--root") |
| 177 |
cmd.append(self.path) |
| 178 |
if self.version: |
| 179 |
cmd.append("--version") |
| 180 |
cmd.append(self.version) |
| 181 |
if self.directory: |
| 182 |
cmd.append("--path") |
| 183 |
cmd.append(self.directory) |
| 184 |
if self.features: |
| 185 |
cmd += ["--features", ",".join(self.features)] |
| 186 |
return self._exec(cmd) |
| 187 |
|
| 188 |
def is_outdated(self, name): |
| 189 |
installed_version = self.get_installed().get(name) |
| 190 |
latest_version = ( |
| 191 |
self.get_latest_published_version(name) if not self.directory else self.get_source_directory_version(name) |
| 192 |
) |
| 193 |
return installed_version != latest_version |
| 194 |
|
| 195 |
def get_latest_published_version(self, name): |
| 196 |
cmd = ["search", name, "--limit", "1"] |
| 197 |
data, dummy = self._exec(cmd, True, False, False) |
| 198 |
|
| 199 |
match = re.search(r"^" + re.escape(name) + r'\s*=\s*"([^"]+)"', data, re.MULTILINE) |
| 200 |
if not match: |
| 201 |
self.module.fail_json(msg=f"No published version for package {name} found") |
| 202 |
return match.group(1) |
| 203 |
|
| 204 |
def get_source_directory_version(self, name): |
| 205 |
cmd = [ |
| 206 |
"metadata", |
| 207 |
"--format-version", |
| 208 |
"1", |
| 209 |
"--no-deps", |
| 210 |
"--manifest-path", |
| 211 |
os.path.join(self.directory, "Cargo.toml"), |
| 212 |
] |
| 213 |
data, dummy = self._exec(cmd, True, False, False) |
| 214 |
manifest = json.loads(data) |
| 215 |
|
| 216 |
package = next( |
| 217 |
(package for package in manifest["packages"] if package["name"] == name), |
| 218 |
None, |
| 219 |
) |
| 220 |
if not package: |
| 221 |
self.module.fail_json( |
| 222 |
msg=f"Package {name} not defined in source, found: {[x['name'] for x in manifest['packages']]}" |
| 223 |
) |
| 224 |
return package["version"] |
| 225 |
|
| 226 |
def uninstall(self, packages=None): |
| 227 |
cmd = ["uninstall"] |
| 228 |
cmd.extend(packages or self.name) |
| 229 |
return self._exec(cmd) |
| 230 |
|
| 231 |
|
| 232 |
def main(): |
| 233 |
arg_spec = dict( |
| 234 |
executable=dict(type="path"), |
| 235 |
name=dict(required=True, type="list", elements="str"), |
| 236 |
path=dict(type="path"), |
| 237 |
state=dict(default="present", choices=["present", "absent", "latest"]), |
| 238 |
version=dict(type="str"), |
| 239 |
locked=dict(default=False, type="bool"), |
| 240 |
directory=dict(type="path"), |
| 241 |
features=dict(default=[], type="list", elements="str"), |
| 242 |
) |
| 243 |
module = AnsibleModule(argument_spec=arg_spec, supports_check_mode=True) |
| 244 |
|
| 245 |
name = module.params["name"] |
| 246 |
state = module.params["state"] |
| 247 |
version = module.params["version"] |
| 248 |
directory = module.params["directory"] |
| 249 |
|
| 250 |
if not name: |
| 251 |
module.fail_json(msg="Package name must be specified") |
| 252 |
|
| 253 |
if directory is not None and not os.path.isdir(directory): |
| 254 |
module.fail_json(msg="Source directory does not exist") |
| 255 |
|
| 256 |
# Set LANG env since we parse stdout |
| 257 |
module.run_command_environ_update = dict(LANGUAGE="C", LC_ALL="C") |
| 258 |
|
| 259 |
cargo = Cargo(module, **module.params) |
| 260 |
changed, out, err = False, None, None |
| 261 |
installed_packages = cargo.get_installed() |
| 262 |
if state == "present": |
| 263 |
to_install = [ |
| 264 |
n for n in name if (n not in installed_packages) or (version and version != installed_packages[n]) |
| 265 |
] |
| 266 |
if to_install: |
| 267 |
changed = True |
| 268 |
out, err = cargo.install(to_install) |
| 269 |
elif state == "latest": |
| 270 |
to_update = [n for n in name if n not in installed_packages or cargo.is_outdated(n)] |
| 271 |
if to_update: |
| 272 |
changed = True |
| 273 |
out, err = cargo.install(to_update) |
| 274 |
else: # absent |
| 275 |
to_uninstall = [n for n in name if n in installed_packages] |
| 276 |
if to_uninstall: |
| 277 |
changed = True |
| 278 |
out, err = cargo.uninstall(to_uninstall) |
| 279 |
|
| 280 |
module.exit_json(changed=changed, stdout=out, stderr=err) |
| 281 |
|
| 282 |
|
| 283 |
if __name__ == "__main__": |
| 284 |
main() |