From c402e575182ec081938454d85b9c426f577cf9e4 Mon Sep 17 00:00:00 2001 From: Christian Kotte Date: Wed, 16 May 2018 20:39:57 +0200 Subject: [PATCH 1/6] added check mode support --- aur.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/aur.py b/aur.py index b8ba463..57875ae 100644 --- a/aur.py +++ b/aur.py @@ -30,6 +30,31 @@ def package_installed(module, package): def install_internal(module, package): +def check_packages(module, packages): + """ + Inform the user what would change if the module were run + """ + would_be_changed = [] + + for package in packages: + installed = package_installed(module, package) + if not installed: + would_be_changed.append(package) + + if would_be_changed: + status = True + if (len(packages) > 1): + message = '%s package(s) would be installed' % str(len(would_be_changed)) + else: + message = 'package would be installed' + else: + status = False + if (len(packages) > 1): + message = 'all packages are already installed' + else: + message = 'package is already installed' + module.exit_json(changed=status, msg=message) + f = urllib.request.urlopen('https://aur.archlinux.org/rpc/?v=5&type=info&arg={}'.format(package)) result = json.loads(f.read().decode('utf8')) if result['resultcount'] != 1: @@ -107,10 +132,14 @@ def main(): }, }, required_one_of=[['name', 'upgrade']], + supports_check_mode=True ) params = module.params + if module.check_mode: + check_packages(module, params['name']) + if params['use'] == 'auto': use = 'internal' # auto: select the first helper for which the bin is found From 47769a51957560edf9b2e2d31071d9f144661217 Mon Sep 17 00:00:00 2001 From: Christian Kotte Date: Wed, 16 May 2018 21:06:06 +0200 Subject: [PATCH 2/6] changed internal helper to makepgk AUR packages can be build via the makepkg script and via AUR helpers. AUR helpers further automate certain tasks for the AUR. makepkg is no helper. Therefore, internal helper should be renamed to just makepkg. --- README.md | 16 ++++++++++------ aur.py | 17 +++++++++-------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 4d3db89..b0a5722 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,29 @@ # Ansible AUR helper -Ansible module to use some Arch User Repository (AUR) helpers as well as a simple internal implementation as a fallback. The following helpers are supported and automatically selected in the order they are listed: +Ansible module to use some Arch User Repository (AUR) helpers as well as makepkg. + +The following helpers are supported and automatically selected in the order they are listed: - [aurman](https://github.com/polygamma/aurman) - [pacaur](https://github.com/rmarquis/pacaur) - [trizen](https://github.com/trizen/trizen) - [pikaur](https://github.com/actionless/pikaur) - [yaourt](https://github.com/archlinuxfr/yaourt) - [yay](https://github.com/Jguer/yay) -- internal helper + +makepkg will be used if no helper was found or if it's specified explicitly. +- [makepkg](https://wiki.archlinux.org/index.php/makepkg) ## Options |parameter |required |default |choices |comments| |--- |--- |--- |--- |---| |name |no | | |Name or list of names of the package(s) to install or upgrade.| |upgrade |no |no |yes, no |Whether or not to upgrade whole system.| -|use |no |auto |auto, aurman, pacaur, trizen, pikaur, yaourt, yay, internal |The helper to use, 'auto' uses the first known helper found, 'internal' uses the internal helper.| +|use |no |auto |auto, aurman, pacaur, trizen, pikaur, yaourt, yay, makepkg |The helper to use, 'auto' uses the first known helper found and makepkg as a fallback.| |skip_installed |no |no |yes, no |Skip operations if the package is present.| ### Note * Either *name* or *upgrade* is required, both cannot be used together. * *skip_installed* cannot be used with *upgrade*. -* In the *use*=*auto* mode, the internal helper is used as a fallback if no known helper is found. +* In the *use*=*auto* mode, makepkg is used as a fallback if no known helper is found. ## Installing 1. Clone the *ansibe-aur* repository in your playbook custom-module directory: @@ -42,8 +46,8 @@ ln --symbolic ansible-aur/aur.py aur ### Examples Use it in a task, as in the following examples: ``` -# Install trizen using the internal helper, skip if trizen is already installed -- aur: name=trizen use=internal skip_installed=true +# Install trizen using makepkg, skip if trizen is already installed +- aur: name=trizen use=makepkg skip_installed=true become: yes become_user: aur_builder diff --git a/aur.py b/aur.py index 57875ae..ae7ee33 100644 --- a/aur.py +++ b/aur.py @@ -19,7 +19,7 @@ use_cmd = { 'pikaur': ['pikaur', '-S', '--noconfirm', '--noedit', '--needed'], 'yaourt': ['yaourt', '-S', '--noconfirm', '--needed'], 'yay': ['yay', '-S', '--noconfirm'], - 'internal': ['makepkg', '--syncdeps', '--install', '--noconfirm', '--needed'] + 'makepkg': ['makepkg', '--syncdeps', '--install', '--noconfirm', '--needed'] } # optional: aurman, pacaur, trizen have a --aur option, do things only for aur @@ -29,7 +29,6 @@ def package_installed(module, package): return rc == 0 -def install_internal(module, package): def check_packages(module, packages): """ Inform the user what would change if the module were run @@ -55,6 +54,8 @@ def check_packages(module, packages): message = 'package is already installed' module.exit_json(changed=status, msg=message) + +def install_with_makepkg(module, package): f = urllib.request.urlopen('https://aur.archlinux.org/rpc/?v=5&type=info&arg={}'.format(package)) result = json.loads(f.read().decode('utf8')) if result['resultcount'] != 1: @@ -71,7 +72,7 @@ def check_packages(module, packages): tar.extractall() tar.close() os.chdir(format(result['Name'])) - rc, out, err = module.run_command(use_cmd['internal'], check_rc=True) + rc, out, err = module.run_command(use_cmd['makepkg'], check_rc=True) os.chdir(current_path) return (rc, out, err) @@ -98,8 +99,8 @@ def install_packages(module, packages, use, skip_installed): if package_installed(module, package): rc = 0 continue - if use == 'internal': - rc, out, err = install_internal(module, package) + if use == 'makepkg': + rc, out, err = install_with_makepkg(module, package) else: rc, out, err = module.run_command(def_lang + use_cmd[use] + [package], check_rc=True) changed_iter = changed_iter or not (out == '' or '-- skipping' in out or 'nothing to do' in out) @@ -124,7 +125,7 @@ def main(): }, 'use': { 'default': 'auto', - 'choices': ['auto', 'aurman', 'pacaur', 'trizen', 'pikaur', 'yaourt', 'yay', 'internal'], + 'choices': ['auto', 'aurman', 'pacaur', 'trizen', 'pikaur', 'yaourt', 'yay', 'makepkg'], }, 'skip_installed': { 'default': False, @@ -141,7 +142,7 @@ def main(): check_packages(module, params['name']) if params['use'] == 'auto': - use = 'internal' + use = 'makepkg' # auto: select the first helper for which the bin is found for k in use_cmd: if module.get_bin_path(k, False): @@ -150,7 +151,7 @@ def main(): else: use = params['use'] - if params['upgrade'] and (params['name'] or params['skip_installed'] or use == 'internal'): + if params['upgrade'] and (params['name'] or params['skip_installed'] or use == 'makepkg'): module.fail_json(msg="Upgrade cannot be used with this option.") else: if params['upgrade']: From 034d05887451430d2d8d7bc5a619ccccc0716c61 Mon Sep 17 00:00:00 2001 From: Christian Kotte Date: Wed, 16 May 2018 21:20:30 +0200 Subject: [PATCH 3/6] added skip pgp check for makepkg This is useful when installing a new system and AUR packages need to be installed before GnuPG is installed and configured (to automatically retrieve public keys). E.g. cower requires PGP signature check and it needs to be installed before pacaur can be installed and used for further AUR package installation --- README.md | 1 + aur.py | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/README.md b/README.md index b0a5722..d8fc6c2 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ makepkg will be used if no helper was found or if it's specified explicitly. |upgrade |no |no |yes, no |Whether or not to upgrade whole system.| |use |no |auto |auto, aurman, pacaur, trizen, pikaur, yaourt, yay, makepkg |The helper to use, 'auto' uses the first known helper found and makepkg as a fallback.| |skip_installed |no |no |yes, no |Skip operations if the package is present.| +|skip_pgp_check |no |no |yes, no |Skip verification of PGP signatures. This is useful when installing packages on a host without GnuPG (properly) configured. Only valid with makepkg.| ### Note * Either *name* or *upgrade* is required, both cannot be used together. diff --git a/aur.py b/aur.py index ae7ee33..cac3a7a 100644 --- a/aur.py +++ b/aur.py @@ -72,6 +72,8 @@ def install_with_makepkg(module, package): tar.extractall() tar.close() os.chdir(format(result['Name'])) + if module.params['skip_pgp_check']: + use_cmd['makepkg'].append('--skippgpcheck') rc, out, err = module.run_command(use_cmd['makepkg'], check_rc=True) os.chdir(current_path) return (rc, out, err) @@ -131,6 +133,10 @@ def main(): 'default': False, 'type': 'bool', }, + 'skip_pgp_check': { + 'default': False, + 'type': 'bool', + }, }, required_one_of=[['name', 'upgrade']], supports_check_mode=True From acb416d0af0d840289f339813bfb43456596cb4e Mon Sep 17 00:00:00 2001 From: Christian Kotte Date: Wed, 16 May 2018 21:29:17 +0200 Subject: [PATCH 4/6] improved verbose messages verbose messages are visible only if -v is used --- aur.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/aur.py b/aur.py index cac3a7a..9c59884 100644 --- a/aur.py +++ b/aur.py @@ -87,7 +87,7 @@ def upgrade(module, use): module.exit_json( changed=not (out == '' or 'nothing to do' in out or 'No AUR updates found' in out), msg='upgraded system', - helper_used=use, + helper=use, ) @@ -105,12 +105,18 @@ def install_packages(module, packages, use, skip_installed): rc, out, err = install_with_makepkg(module, package) else: rc, out, err = module.run_command(def_lang + use_cmd[use] + [package], check_rc=True) + changed_iter = changed_iter or not (out == '' or '-- skipping' in out or 'nothing to do' in out) + if changed_iter: + message = 'installed package(s)' + else: + message = 'package(s) already installed' + module.exit_json( changed=changed_iter, - msg='installed package' if not rc else err, - helper_used=use, + msg=message if not rc else err, + helper=use, rc=rc, ) From 5c77814f65de75e23b981d53c69e380cae3b4a04 Mon Sep 17 00:00:00 2001 From: Christian Kotte Date: Wed, 16 May 2018 21:30:58 +0200 Subject: [PATCH 5/6] added docstrings to functions --- aur.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/aur.py b/aur.py index 9c59884..c4040a1 100644 --- a/aur.py +++ b/aur.py @@ -25,6 +25,9 @@ use_cmd = { def package_installed(module, package): + """ + Determine if the package is already installed + """ rc, _, _ = module.run_command(['pacman', '-Q', package], check_rc=False) return rc == 0 @@ -56,6 +59,9 @@ def check_packages(module, packages): def install_with_makepkg(module, package): + """ + Install the specified package with makepkg + """ f = urllib.request.urlopen('https://aur.archlinux.org/rpc/?v=5&type=info&arg={}'.format(package)) result = json.loads(f.read().decode('utf8')) if result['resultcount'] != 1: @@ -80,6 +86,9 @@ def install_with_makepkg(module, package): def upgrade(module, use): + """ + Upgrade the whole system + """ assert use in use_cmd rc, out, err = module.run_command(def_lang + use_cmd[use] + ['-u'], check_rc=True) @@ -92,6 +101,9 @@ def upgrade(module, use): def install_packages(module, packages, use, skip_installed): + """ + Install the specified packages + """ assert use in use_cmd changed_iter = False From 0b7fc34fed4f6d6277b5d34faa9e6c6db16d404b Mon Sep 17 00:00:00 2001 From: Christian Kotte Date: Wed, 16 May 2018 21:31:32 +0200 Subject: [PATCH 6/6] atom automatically adds a newline to every file --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d8fc6c2..244230f 100644 --- a/README.md +++ b/README.md @@ -81,4 +81,4 @@ This can be done in Ansible with the following actions: line: 'aur_builder ALL=(ALL) NOPASSWD: /usr/bin/pacman' create: yes validate: 'visudo -cf %s' -``` \ No newline at end of file +```