2022-01-08 15:40:13 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright (c) 2021 Radek Sprta <mail@radeksprta.eu>
|
2022-08-05 12:28:29 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2022-01-08 15:40:13 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = r"""
|
|
|
|
---
|
|
|
|
module: cargo
|
|
|
|
short_description: Manage Rust packages with cargo
|
|
|
|
version_added: 4.3.0
|
|
|
|
description:
|
|
|
|
- Manage Rust packages with cargo.
|
|
|
|
author: "Radek Sprta (@radek-sprta)"
|
2023-02-24 09:25:20 +01:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: full
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2022-01-08 15:40:13 +01:00
|
|
|
options:
|
2023-10-06 07:54:16 +02:00
|
|
|
executable:
|
|
|
|
description:
|
|
|
|
- Path to the C(cargo) installed in the system.
|
|
|
|
- If not specified, the module will look C(cargo) in E(PATH).
|
|
|
|
type: path
|
|
|
|
version_added: 7.5.0
|
2022-01-08 15:40:13 +01:00
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The name of a Rust package to install.
|
|
|
|
type: list
|
|
|
|
elements: str
|
|
|
|
required: true
|
|
|
|
path:
|
|
|
|
description:
|
|
|
|
->
|
|
|
|
The base path where to install the Rust packages. Cargo automatically appends
|
2023-06-15 19:04:07 +02:00
|
|
|
V(/bin). In other words, V(/usr/local) will become V(/usr/local/bin).
|
2022-01-08 15:40:13 +01:00
|
|
|
type: path
|
|
|
|
version:
|
|
|
|
description:
|
|
|
|
->
|
2023-06-15 19:04:07 +02:00
|
|
|
The version to install. If O(name) contains multiple values, the module will
|
2022-01-08 15:40:13 +01:00
|
|
|
try to install all of them in this version.
|
|
|
|
type: str
|
|
|
|
required: false
|
2023-09-25 05:51:31 +02:00
|
|
|
locked:
|
|
|
|
description:
|
|
|
|
- Install with locked dependencies.
|
|
|
|
- This is only used when installing packages.
|
|
|
|
required: false
|
|
|
|
type: bool
|
|
|
|
default: false
|
|
|
|
version_added: 7.5.0
|
2022-01-08 15:40:13 +01:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- The state of the Rust package.
|
|
|
|
required: false
|
|
|
|
type: str
|
|
|
|
default: present
|
|
|
|
choices: [ "present", "absent", "latest" ]
|
|
|
|
requirements:
|
2023-10-06 07:54:16 +02:00
|
|
|
- cargo installed
|
2022-01-08 15:40:13 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = r"""
|
|
|
|
- name: Install "ludusavi" Rust package
|
|
|
|
community.general.cargo:
|
|
|
|
name: ludusavi
|
|
|
|
|
2023-09-25 05:51:31 +02:00
|
|
|
- name: Install "ludusavi" Rust package with locked dependencies
|
|
|
|
community.general.cargo:
|
|
|
|
name: ludusavi
|
|
|
|
locked: true
|
|
|
|
|
2022-01-08 15:40:13 +01:00
|
|
|
- name: Install "ludusavi" Rust package in version 0.10.0
|
|
|
|
community.general.cargo:
|
|
|
|
name: ludusavi
|
|
|
|
version: '0.10.0'
|
|
|
|
|
|
|
|
- name: Install "ludusavi" Rust package to global location
|
|
|
|
community.general.cargo:
|
|
|
|
name: ludusavi
|
|
|
|
path: /usr/local
|
|
|
|
|
|
|
|
- name: Remove "ludusavi" Rust package
|
|
|
|
community.general.cargo:
|
|
|
|
name: ludusavi
|
|
|
|
state: absent
|
|
|
|
|
|
|
|
- name: Update "ludusavi" Rust package its latest version
|
|
|
|
community.general.cargo:
|
|
|
|
name: ludusavi
|
|
|
|
state: latest
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
|
|
|
|
class Cargo(object):
|
|
|
|
def __init__(self, module, **kwargs):
|
|
|
|
self.module = module
|
2023-10-06 07:54:16 +02:00
|
|
|
self.executable = [kwargs["executable"] or module.get_bin_path("cargo", True)]
|
2022-01-08 15:40:13 +01:00
|
|
|
self.name = kwargs["name"]
|
|
|
|
self.path = kwargs["path"]
|
|
|
|
self.state = kwargs["state"]
|
|
|
|
self.version = kwargs["version"]
|
2023-09-25 05:51:31 +02:00
|
|
|
self.locked = kwargs["locked"]
|
2022-01-08 15:40:13 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def path(self):
|
|
|
|
return self._path
|
|
|
|
|
|
|
|
@path.setter
|
|
|
|
def path(self, path):
|
|
|
|
if path is not None and not os.path.isdir(path):
|
|
|
|
self.module.fail_json(msg="Path %s is not a directory" % path)
|
|
|
|
self._path = path
|
|
|
|
|
|
|
|
def _exec(
|
|
|
|
self, args, run_in_check_mode=False, check_rc=True, add_package_name=True
|
|
|
|
):
|
|
|
|
if not self.module.check_mode or (self.module.check_mode and run_in_check_mode):
|
|
|
|
cmd = self.executable + args
|
|
|
|
rc, out, err = self.module.run_command(cmd, check_rc=check_rc)
|
|
|
|
return out, err
|
|
|
|
return "", ""
|
|
|
|
|
|
|
|
def get_installed(self):
|
|
|
|
cmd = ["install", "--list"]
|
2024-02-23 20:59:40 +01:00
|
|
|
if self.path:
|
|
|
|
cmd.append("--root")
|
|
|
|
cmd.append(self.path)
|
|
|
|
|
2022-01-08 15:40:13 +01:00
|
|
|
data, dummy = self._exec(cmd, True, False, False)
|
|
|
|
|
2022-01-18 12:12:48 +01:00
|
|
|
package_regex = re.compile(r"^([\w\-]+) v(.+):$")
|
2022-01-08 15:40:13 +01:00
|
|
|
installed = {}
|
|
|
|
for line in data.splitlines():
|
|
|
|
package_info = package_regex.match(line)
|
|
|
|
if package_info:
|
|
|
|
installed[package_info.group(1)] = package_info.group(2)
|
|
|
|
|
|
|
|
return installed
|
|
|
|
|
|
|
|
def install(self, packages=None):
|
|
|
|
cmd = ["install"]
|
|
|
|
cmd.extend(packages or self.name)
|
2023-09-25 05:51:31 +02:00
|
|
|
if self.locked:
|
|
|
|
cmd.append("--locked")
|
2022-01-08 15:40:13 +01:00
|
|
|
if self.path:
|
|
|
|
cmd.append("--root")
|
|
|
|
cmd.append(self.path)
|
|
|
|
if self.version:
|
|
|
|
cmd.append("--version")
|
|
|
|
cmd.append(self.version)
|
|
|
|
return self._exec(cmd)
|
|
|
|
|
|
|
|
def is_outdated(self, name):
|
|
|
|
installed_version = self.get_installed().get(name)
|
|
|
|
|
|
|
|
cmd = ["search", name, "--limit", "1"]
|
2022-01-18 12:12:48 +01:00
|
|
|
data, dummy = self._exec(cmd, True, False, False)
|
2022-01-08 15:40:13 +01:00
|
|
|
|
|
|
|
match = re.search(r'"(.+)"', data)
|
|
|
|
if match:
|
2022-01-18 12:12:48 +01:00
|
|
|
latest_version = match.group(1)
|
2022-01-08 15:40:13 +01:00
|
|
|
|
|
|
|
return installed_version != latest_version
|
|
|
|
|
|
|
|
def uninstall(self, packages=None):
|
|
|
|
cmd = ["uninstall"]
|
|
|
|
cmd.extend(packages or self.name)
|
|
|
|
return self._exec(cmd)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
arg_spec = dict(
|
2023-10-06 07:54:16 +02:00
|
|
|
executable=dict(default=None, type="path"),
|
2022-01-08 15:40:13 +01:00
|
|
|
name=dict(required=True, type="list", elements="str"),
|
|
|
|
path=dict(default=None, type="path"),
|
|
|
|
state=dict(default="present", choices=["present", "absent", "latest"]),
|
|
|
|
version=dict(default=None, type="str"),
|
2023-09-25 05:51:31 +02:00
|
|
|
locked=dict(default=False, type="bool"),
|
2022-01-08 15:40:13 +01:00
|
|
|
)
|
|
|
|
module = AnsibleModule(argument_spec=arg_spec, supports_check_mode=True)
|
|
|
|
|
|
|
|
name = module.params["name"]
|
|
|
|
state = module.params["state"]
|
|
|
|
version = module.params["version"]
|
|
|
|
|
|
|
|
if not name:
|
|
|
|
module.fail_json(msg="Package name must be specified")
|
|
|
|
|
|
|
|
# Set LANG env since we parse stdout
|
|
|
|
module.run_command_environ_update = dict(
|
|
|
|
LANG="C", LC_ALL="C", LC_MESSAGES="C", LC_CTYPE="C"
|
|
|
|
)
|
|
|
|
|
2023-10-06 07:54:16 +02:00
|
|
|
cargo = Cargo(module, **module.params)
|
2022-01-08 15:40:13 +01:00
|
|
|
changed, out, err = False, None, None
|
|
|
|
installed_packages = cargo.get_installed()
|
|
|
|
if state == "present":
|
|
|
|
to_install = [
|
|
|
|
n
|
|
|
|
for n in name
|
|
|
|
if (n not in installed_packages)
|
|
|
|
or (version and version != installed_packages[n])
|
|
|
|
]
|
|
|
|
if to_install:
|
|
|
|
changed = True
|
|
|
|
out, err = cargo.install(to_install)
|
|
|
|
elif state == "latest":
|
|
|
|
to_update = [
|
|
|
|
n for n in name if n not in installed_packages or cargo.is_outdated(n)
|
|
|
|
]
|
|
|
|
if to_update:
|
|
|
|
changed = True
|
|
|
|
out, err = cargo.install(to_update)
|
|
|
|
else: # absent
|
|
|
|
to_uninstall = [n for n in name if n in installed_packages]
|
|
|
|
if to_uninstall:
|
|
|
|
changed = True
|
|
|
|
out, err = cargo.uninstall(to_uninstall)
|
|
|
|
|
|
|
|
module.exit_json(changed=changed, stdout=out, stderr=err)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|