collection 교체

This commit is contained in:
정훈 변
2024-02-23 16:37:40 +09:00
parent b494779b5b
commit 3fd554eee9
38862 changed files with 220204 additions and 6600073 deletions

View File

@@ -1,10 +1,11 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2013, Patrick Pelletier <pp.pelletier@gmail.com>
# Copyright (c) 2013, Patrick Pelletier <pp.pelletier@gmail.com>
# Based on pacman (Afterburn) and pkgin (Shaun Zinck) modules
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# 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
from __future__ import absolute_import, division, print_function
__metaclass__ = type
@@ -14,13 +15,24 @@ DOCUMENTATION = '''
---
module: opkg
author: "Patrick Pelletier (@skinp)"
short_description: Package manager for OpenWrt
short_description: Package manager for OpenWrt and Openembedded/Yocto based Linux distributions
description:
- Manages OpenWrt packages
- Manages ipk packages for OpenWrt and Openembedded/Yocto based Linux distributions
extends_documentation_fragment:
- community.general.attributes
attributes:
check_mode:
support: none
diff_mode:
support: none
options:
name:
description:
- Name of package(s) to install/remove.
- C(NAME=VERSION) syntax is also supported to install a package
in a certain version. See the examples. This only works on Yocto based
Linux distributions (opkg>=0.3.2) and not for OpenWrt. This is
supported since community.general 6.2.0.
aliases: [pkg]
required: true
type: list
@@ -34,6 +46,8 @@ options:
force:
description:
- The C(opkg --force) parameter used.
- Passing V("") as value and not passing any value at all have both
the same effect of B(not) using any C(--force-) parameter.
choices:
- ""
- "depends"
@@ -50,10 +64,13 @@ options:
update_cache:
description:
- Update the package DB first.
- Alias C(update-cache) has been deprecated and will be removed in community.general 5.0.0.
aliases: ['update-cache']
default: false
type: bool
executable:
description:
- The executable location for C(opkg).
type: path
version_added: 7.2.0
requirements:
- opkg
- python
@@ -64,11 +81,16 @@ EXAMPLES = '''
name: foo
state: present
- name: Install foo in version 1.2 (opkg>=0.3.2 on Yocto based Linux distributions)
community.general.opkg:
name: foo=1.2
state: present
- name: Update cache and install foo
community.general.opkg:
name: foo
state: present
update_cache: yes
update_cache: true
- name: Remove foo
community.general.opkg:
@@ -89,114 +111,112 @@ EXAMPLES = '''
force: overwrite
'''
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six.moves import shlex_quote
import os
from ansible_collections.community.general.plugins.module_utils.cmd_runner import CmdRunner, cmd_runner_fmt
from ansible_collections.community.general.plugins.module_utils.module_helper import StateModuleHelper
def update_package_db(module, opkg_path):
""" Updates packages list. """
rc, out, err = module.run_command("%s update" % opkg_path)
if rc != 0:
module.fail_json(msg="could not update package db")
def query_package(module, opkg_path, name, state="present"):
""" Returns whether a package is installed or not. """
if state == "present":
rc, out, err = module.run_command("%s list-installed | grep -q \"^%s \"" % (shlex_quote(opkg_path), shlex_quote(name)), use_unsafe_shell=True)
if rc == 0:
return True
return False
def remove_packages(module, opkg_path, packages):
""" Uninstalls one or more packages if installed. """
p = module.params
force = p["force"]
if force:
force = "--force-%s" % force
remove_c = 0
# Using a for loop in case of error, we can report the package that failed
for package in packages:
# Query the package first, to see if we even need to remove
if not query_package(module, opkg_path, package):
continue
rc, out, err = module.run_command("%s remove %s %s" % (opkg_path, force, package))
if query_package(module, opkg_path, package):
module.fail_json(msg="failed to remove %s: %s" % (package, out))
remove_c += 1
if remove_c > 0:
module.exit_json(changed=True, msg="removed %s package(s)" % remove_c)
module.exit_json(changed=False, msg="package(s) already absent")
def install_packages(module, opkg_path, packages):
""" Installs one or more packages if not already installed. """
p = module.params
force = p["force"]
if force:
force = "--force-%s" % force
install_c = 0
for package in packages:
if query_package(module, opkg_path, package):
continue
rc, out, err = module.run_command("%s install %s %s" % (opkg_path, force, package))
if not query_package(module, opkg_path, package):
module.fail_json(msg="failed to install %s: %s" % (package, out))
install_c += 1
if install_c > 0:
module.exit_json(changed=True, msg="installed %s package(s)" % (install_c))
module.exit_json(changed=False, msg="package(s) already present")
def main():
module = AnsibleModule(
class Opkg(StateModuleHelper):
module = dict(
argument_spec=dict(
name=dict(aliases=["pkg"], required=True, type="list", elements="str"),
state=dict(default="present", choices=["present", "installed", "absent", "removed"]),
force=dict(default="", choices=["", "depends", "maintainer", "reinstall", "overwrite", "downgrade", "space", "postinstall", "remove",
"checksum", "removal-of-dependent-packages"]),
update_cache=dict(
default="no", aliases=["update-cache"], type='bool',
deprecated_aliases=[dict(name='update-cache', version='5.0.0', collection_name='community.general')]),
)
force=dict(choices=["", "depends", "maintainer", "reinstall", "overwrite", "downgrade", "space",
"postinstall", "remove", "checksum", "removal-of-dependent-packages"]),
update_cache=dict(default=False, type='bool'),
executable=dict(type="path"),
),
)
opkg_path = module.get_bin_path('opkg', True, ['/bin'])
def __init_module__(self):
self.vars.set("install_c", 0, output=False, change=True)
self.vars.set("remove_c", 0, output=False, change=True)
p = module.params
state_map = dict(
query="list-installed",
present="install",
installed="install",
absent="remove",
removed="remove",
)
if p["update_cache"]:
update_package_db(module, opkg_path)
def _force(value):
if value == "":
value = None
return cmd_runner_fmt.as_optval("--force-")(value, ctx_ignore_none=True)
pkgs = p["name"]
dir, cmd = os.path.split(self.vars.executable) if self.vars.executable else (None, "opkg")
if p["state"] in ["present", "installed"]:
install_packages(module, opkg_path, pkgs)
self.runner = CmdRunner(
self.module,
command=cmd,
arg_formats=dict(
package=cmd_runner_fmt.as_list(),
state=cmd_runner_fmt.as_map(state_map),
force=cmd_runner_fmt.as_func(_force),
update_cache=cmd_runner_fmt.as_bool("update"),
),
path_prefix=dir,
)
elif p["state"] in ["absent", "removed"]:
remove_packages(module, opkg_path, pkgs)
if self.vars.update_cache:
rc, dummy, dummy = self.runner("update_cache").run()
if rc != 0:
self.do_raise("could not update package db")
@staticmethod
def split_name_and_version(package):
""" Split the name and the version when using the NAME=VERSION syntax """
splitted = package.split('=', 1)
if len(splitted) == 1:
return splitted[0], None
else:
return splitted[0], splitted[1]
def _package_in_desired_state(self, name, want_installed, version=None):
dummy, out, dummy = self.runner("state package").run(state="query", package=name)
has_package = out.startswith(name + " - %s" % ("" if not version else (version + " ")))
return want_installed == has_package
def state_present(self):
with self.runner("state force package") as ctx:
for package in self.vars.name:
pkg_name, pkg_version = self.split_name_and_version(package)
if not self._package_in_desired_state(pkg_name, want_installed=True, version=pkg_version) or self.vars.force == "reinstall":
ctx.run(package=package)
if not self._package_in_desired_state(pkg_name, want_installed=True, version=pkg_version):
self.do_raise("failed to install %s" % package)
self.vars.install_c += 1
if self.verbosity >= 4:
self.vars.run_info = ctx.run_info
if self.vars.install_c > 0:
self.vars.msg = "installed %s package(s)" % (self.vars.install_c)
else:
self.vars.msg = "package(s) already present"
def state_absent(self):
with self.runner("state force package") as ctx:
for package in self.vars.name:
package, dummy = self.split_name_and_version(package)
if not self._package_in_desired_state(package, want_installed=False):
ctx.run(package=package)
if not self._package_in_desired_state(package, want_installed=False):
self.do_raise("failed to remove %s" % package)
self.vars.remove_c += 1
if self.verbosity >= 4:
self.vars.run_info = ctx.run_info
if self.vars.remove_c > 0:
self.vars.msg = "removed %s package(s)" % (self.vars.remove_c)
else:
self.vars.msg = "package(s) already absent"
state_installed = state_present
state_removed = state_absent
def main():
Opkg.execute()
if __name__ == '__main__':