Merge pull request #3 from ckotte/master

Check mode support, skip pgp check, etc. pp.
pull/4/head
kewl 2018-05-17 16:47:12 +09:00 committed by GitHub
commit b9dfba15f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 18 deletions

View File

@ -1,25 +1,30 @@
# Ansible AUR helper # 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) - [aurman](https://github.com/polygamma/aurman)
- [pacaur](https://github.com/rmarquis/pacaur) - [pacaur](https://github.com/rmarquis/pacaur)
- [trizen](https://github.com/trizen/trizen) - [trizen](https://github.com/trizen/trizen)
- [pikaur](https://github.com/actionless/pikaur) - [pikaur](https://github.com/actionless/pikaur)
- [yaourt](https://github.com/archlinuxfr/yaourt) - [yaourt](https://github.com/archlinuxfr/yaourt)
- [yay](https://github.com/Jguer/yay) - [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 ## Options
|parameter |required |default |choices |comments| |parameter |required |default |choices |comments|
|--- |--- |--- |--- |---| |--- |--- |--- |--- |---|
|name |no | | |Name or list of names of the package(s) to install or upgrade.| |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.| |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.| |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 ### Note
* Either *name* or *upgrade* is required, both cannot be used together. * Either *name* or *upgrade* is required, both cannot be used together.
* *skip_installed* cannot be used with *upgrade*. * *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 ## Installing
1. Clone the *ansibe-aur* repository in your playbook custom-module directory: 1. Clone the *ansibe-aur* repository in your playbook custom-module directory:
@ -42,8 +47,8 @@ ln --symbolic ansible-aur/aur.py aur
### Examples ### Examples
Use it in a task, as in the following examples: Use it in a task, as in the following examples:
``` ```
# Install trizen using the internal helper, skip if trizen is already installed # Install trizen using makepkg, skip if trizen is already installed
- aur: name=trizen use=internal skip_installed=true - aur: name=trizen use=makepkg skip_installed=true
become: yes become: yes
become_user: aur_builder become_user: aur_builder
@ -76,4 +81,4 @@ This can be done in Ansible with the following actions:
line: 'aur_builder ALL=(ALL) NOPASSWD: /usr/bin/pacman' line: 'aur_builder ALL=(ALL) NOPASSWD: /usr/bin/pacman'
create: yes create: yes
validate: 'visudo -cf %s' validate: 'visudo -cf %s'
``` ```

76
aur.py
View File

@ -19,17 +19,49 @@ use_cmd = {
'pikaur': ['pikaur', '-S', '--noconfirm', '--noedit', '--needed'], 'pikaur': ['pikaur', '-S', '--noconfirm', '--noedit', '--needed'],
'yaourt': ['yaourt', '-S', '--noconfirm', '--needed'], 'yaourt': ['yaourt', '-S', '--noconfirm', '--needed'],
'yay': ['yay', '-S', '--noconfirm'], '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 # optional: aurman, pacaur, trizen have a --aur option, do things only for aur
def package_installed(module, package): def package_installed(module, package):
"""
Determine if the package is already installed
"""
rc, _, _ = module.run_command(['pacman', '-Q', package], check_rc=False) rc, _, _ = module.run_command(['pacman', '-Q', package], check_rc=False)
return rc == 0 return rc == 0
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)
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)) f = urllib.request.urlopen('https://aur.archlinux.org/rpc/?v=5&type=info&arg={}'.format(package))
result = json.loads(f.read().decode('utf8')) result = json.loads(f.read().decode('utf8'))
if result['resultcount'] != 1: if result['resultcount'] != 1:
@ -46,12 +78,17 @@ def install_internal(module, package):
tar.extractall() tar.extractall()
tar.close() tar.close()
os.chdir(format(result['Name'])) os.chdir(format(result['Name']))
rc, out, err = module.run_command(use_cmd['internal'], check_rc=True) 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) os.chdir(current_path)
return (rc, out, err) return (rc, out, err)
def upgrade(module, use): def upgrade(module, use):
"""
Upgrade the whole system
"""
assert use in use_cmd assert use in use_cmd
rc, out, err = module.run_command(def_lang + use_cmd[use] + ['-u'], check_rc=True) rc, out, err = module.run_command(def_lang + use_cmd[use] + ['-u'], check_rc=True)
@ -59,11 +96,14 @@ def upgrade(module, use):
module.exit_json( module.exit_json(
changed=not (out == '' or 'nothing to do' in out or 'No AUR updates found' in out), changed=not (out == '' or 'nothing to do' in out or 'No AUR updates found' in out),
msg='upgraded system', msg='upgraded system',
helper_used=use, helper=use,
) )
def install_packages(module, packages, use, skip_installed): def install_packages(module, packages, use, skip_installed):
"""
Install the specified packages
"""
assert use in use_cmd assert use in use_cmd
changed_iter = False changed_iter = False
@ -73,16 +113,22 @@ def install_packages(module, packages, use, skip_installed):
if package_installed(module, package): if package_installed(module, package):
rc = 0 rc = 0
continue continue
if use == 'internal': if use == 'makepkg':
rc, out, err = install_internal(module, package) rc, out, err = install_with_makepkg(module, package)
else: else:
rc, out, err = module.run_command(def_lang + use_cmd[use] + [package], check_rc=True) 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) 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( module.exit_json(
changed=changed_iter, changed=changed_iter,
msg='installed package' if not rc else err, msg=message if not rc else err,
helper_used=use, helper=use,
rc=rc, rc=rc,
) )
@ -99,20 +145,28 @@ def main():
}, },
'use': { 'use': {
'default': 'auto', 'default': 'auto',
'choices': ['auto', 'aurman', 'pacaur', 'trizen', 'pikaur', 'yaourt', 'yay', 'internal'], 'choices': ['auto', 'aurman', 'pacaur', 'trizen', 'pikaur', 'yaourt', 'yay', 'makepkg'],
}, },
'skip_installed': { 'skip_installed': {
'default': False, 'default': False,
'type': 'bool', 'type': 'bool',
}, },
'skip_pgp_check': {
'default': False,
'type': 'bool',
},
}, },
required_one_of=[['name', 'upgrade']], required_one_of=[['name', 'upgrade']],
supports_check_mode=True
) )
params = module.params params = module.params
if module.check_mode:
check_packages(module, params['name'])
if params['use'] == 'auto': if params['use'] == 'auto':
use = 'internal' use = 'makepkg'
# auto: select the first helper for which the bin is found # auto: select the first helper for which the bin is found
for k in use_cmd: for k in use_cmd:
if module.get_bin_path(k, False): if module.get_bin_path(k, False):
@ -121,7 +175,7 @@ def main():
else: else:
use = params['use'] 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.") module.fail_json(msg="Upgrade cannot be used with this option.")
else: else:
if params['upgrade']: if params['upgrade']: