1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Apk: add support for a custom world file (#4976)

* Apk: add support for a custom world file

* Apk: Add changelog fragment for custom world file
This commit is contained in:
CactiChameleon9 2022-07-26 09:14:17 +00:00 committed by GitHub
parent 037c75db4f
commit be0e47bfdc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 6 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- apk - add ``world`` parameter for supporting a custom world file (https://github.com/ansible-collections/community.general/pull/4976).

View file

@ -61,6 +61,12 @@ options:
- Upgrade all installed packages to their latest version. - Upgrade all installed packages to their latest version.
type: bool type: bool
default: no default: no
world:
description:
- Use a custom world file when checking for explicitly installed packages.
type: str
default: /etc/apk/world
version_added: 5.4.0
notes: notes:
- 'I(name) and I(upgrade) are mutually exclusive.' - 'I(name) and I(upgrade) are mutually exclusive.'
- When used with a C(loop:) each package will be processed individually, it is much more efficient to pass the list directly to the I(name) option. - When used with a C(loop:) each package will be processed individually, it is much more efficient to pass the list directly to the I(name) option.
@ -134,6 +140,12 @@ EXAMPLES = '''
name: foo name: foo
state: latest state: latest
no_cache: yes no_cache: yes
- name: Install package checking a custom world
community.general.apk:
name: foo
state: latest
world: /etc/apk/world.custom
''' '''
RETURN = ''' RETURN = '''
@ -171,11 +183,11 @@ def update_package_db(module, exit):
return True return True
def query_toplevel(module, name): def query_toplevel(module, name, world):
# /etc/apk/world contains a list of top-level packages separated by ' ' or \n # world contains a list of top-level packages separated by ' ' or \n
# packages may contain repository (@) or version (=<>~) separator characters or start with negation ! # packages may contain repository (@) or version (=<>~) separator characters or start with negation !
regex = re.compile(r'^' + re.escape(name) + r'([@=<>~].+)?$') regex = re.compile(r'^' + re.escape(name) + r'([@=<>~].+)?$')
with open('/etc/apk/world') as f: with open(world) as f:
content = f.read().split() content = f.read().split()
for p in content: for p in content:
if regex.search(p): if regex.search(p):
@ -237,7 +249,7 @@ def upgrade_packages(module, available):
module.exit_json(changed=True, msg="upgraded packages", stdout=stdout, stderr=stderr, packages=packagelist) module.exit_json(changed=True, msg="upgraded packages", stdout=stdout, stderr=stderr, packages=packagelist)
def install_packages(module, names, state): def install_packages(module, names, state, world):
upgrade = False upgrade = False
to_install = [] to_install = []
to_upgrade = [] to_upgrade = []
@ -250,7 +262,7 @@ def install_packages(module, names, state):
if state == 'latest' and not query_latest(module, dependency): if state == 'latest' and not query_latest(module, dependency):
to_upgrade.append(dependency) to_upgrade.append(dependency)
else: else:
if not query_toplevel(module, name): if not query_toplevel(module, name, world):
to_install.append(name) to_install.append(name)
elif state == 'latest' and not query_latest(module, name): elif state == 'latest' and not query_latest(module, name):
to_upgrade.append(name) to_upgrade.append(name)
@ -313,6 +325,7 @@ def main():
update_cache=dict(default=False, type='bool'), update_cache=dict(default=False, type='bool'),
upgrade=dict(default=False, type='bool'), upgrade=dict(default=False, type='bool'),
available=dict(default=False, type='bool'), available=dict(default=False, type='bool'),
world=dict(default='/etc/apk/world', type='str'),
), ),
required_one_of=[['name', 'update_cache', 'upgrade']], required_one_of=[['name', 'update_cache', 'upgrade']],
mutually_exclusive=[['name', 'upgrade']], mutually_exclusive=[['name', 'upgrade']],
@ -348,7 +361,7 @@ def main():
upgrade_packages(module, p['available']) upgrade_packages(module, p['available'])
if p['state'] in ['present', 'latest']: if p['state'] in ['present', 'latest']:
install_packages(module, p['name'], p['state']) install_packages(module, p['name'], p['state'], p['world'])
elif p['state'] == 'absent': elif p['state'] == 'absent':
remove_packages(module, p['name']) remove_packages(module, p['name'])