Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xbps: support --rootdir and --repository #9174

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- xbps - add ``root`` and ``repository`` options to enable bootstrapping new void installations. (https://github.com/ansible-collections/community.general/pull/9174).
43 changes: 43 additions & 0 deletions plugins/modules/xbps.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@
type: bool
default: true
version_added: '0.2.0'
root:
description:
- The full path for the target root directory.
type: str
version_added: '10.1.0'
repository:
description:
- Repository URL(s) to prepend to the repository list for the
package installation.
The URL can be a URL to a repository for
remote repositories or a path for local repositories.
aliases: [repositories]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally aliases should only be used if absolutely necessary. Here I would remove it, and potentially rename the option to repositories.

type: list
elements: str
version_added: '10.1.0'
'''

EXAMPLES = '''
Expand Down Expand Up @@ -107,6 +122,13 @@
name: foo
state: present
upgrade_xbps: false

- name: Install a new void system on a mounted partition
community.general.xbps:
name: base-system
state: present
repository: https://repo-default.voidlinux.org/current
root: /mnt
'''

RETURN = '''
Expand All @@ -133,16 +155,29 @@ def is_installed(xbps_output):
return bool(len(xbps_output))


def append_flags(module, xbps_path, cmd):
"""Appends the repository/root flags when needed"""
if module.params["root"]:
cmd = "%s -r %s" % (cmd, module.params["root"])
if module.params["repository"] and not cmd.startswith(xbps_path["remove"]):
for repo in module.params["repository"]:
cmd = "%s --repository=%s" % (cmd, repo)

return cmd


def query_package(module, xbps_path, name, state="present"):
"""Returns Package info"""
if state == "present":
lcmd = "%s %s" % (xbps_path['query'], name)
lcmd = append_flags(module, xbps_path, lcmd)
lrc, lstdout, lstderr = module.run_command(lcmd, check_rc=False)
if not is_installed(lstdout):
# package is not installed locally
return False, False

rcmd = "%s -Sun" % (xbps_path['install'])
rcmd = append_flags(module, xbps_path, rcmd)
rrc, rstdout, rstderr = module.run_command(rcmd, check_rc=False)
if rrc == 0 or rrc == 17:
"""Return True to indicate that the package is installed locally,
Expand All @@ -156,6 +191,7 @@ def query_package(module, xbps_path, name, state="present"):
def update_package_db(module, xbps_path):
"""Returns True if update_package_db changed"""
cmd = "%s -S" % (xbps_path['install'])
cmd = append_flags(module, xbps_path, cmd)
rc, stdout, stderr = module.run_command(cmd, check_rc=False)

if rc != 0:
Expand All @@ -168,6 +204,7 @@ def update_package_db(module, xbps_path):

def upgrade_xbps(module, xbps_path, exit_on_success=False):
cmdupgradexbps = "%s -uy xbps" % (xbps_path['install'])
cmdupgradexbps = append_flags(module, xbps_path, cmdupgradexbps)
rc, stdout, stderr = module.run_command(cmdupgradexbps, check_rc=False)
if rc != 0:
module.fail_json(msg='Could not upgrade xbps itself')
Expand All @@ -177,6 +214,8 @@ def upgrade(module, xbps_path):
"""Returns true is full upgrade succeeds"""
cmdupgrade = "%s -uy" % (xbps_path['install'])
cmdneedupgrade = "%s -un" % (xbps_path['install'])
cmdupgrade = append_flags(module, xbps_path, cmdupgrade)
cmdneedupgrade = append_flags(module, xbps_path, cmdneedupgrade)

rc, stdout, stderr = module.run_command(cmdneedupgrade, check_rc=False)
if rc == 0:
Expand Down Expand Up @@ -210,6 +249,7 @@ def remove_packages(module, xbps_path, packages):
continue

cmd = "%s -y %s" % (xbps_path['remove'], package)
cmd = append_flags(module, xbps_path, cmd)
rc, stdout, stderr = module.run_command(cmd, check_rc=False)

if rc != 0:
Expand Down Expand Up @@ -242,6 +282,7 @@ def install_packages(module, xbps_path, state, packages):
module.exit_json(changed=False, msg="Nothing to Install")

cmd = "%s -y %s" % (xbps_path['install'], " ".join(toInstall))
cmd = append_flags(module, xbps_path, cmd)
rc, stdout, stderr = module.run_command(cmd, check_rc=False)

if rc == 16 and module.params['upgrade_xbps']:
Expand Down Expand Up @@ -308,6 +349,8 @@ def main():
upgrade=dict(default=False, type='bool'),
update_cache=dict(default=True, type='bool'),
upgrade_xbps=dict(default=True, type='bool'),
root=dict(type='str'),
repository=dict(type='list', aliases=['repositories'], elements='str'),
),
required_one_of=[['name', 'update_cache', 'upgrade']],
supports_check_mode=True)
Expand Down