ansible-aur/aur.py

83 lines
2.1 KiB
Python
Raw Normal View History

2017-12-23 20:27:42 +03:00
#!/usr/bin/env python
from ansible.module_utils.basic import *
helper_cmd = {
2017-12-24 10:21:11 +03:00
'pacaur': ['env', 'LC_ALL=C', 'pacaur', '-S', '--noconfirm', '--noedit', '--needed', '--aur'],
2017-12-24 10:26:57 +03:00
'trizen': ['env', 'LC_ALL=C', 'trizen', '-S', '--noconfirm', '--noedit', '--needed', '--aur'],
2017-12-23 20:27:42 +03:00
'yaourt': ['env', 'LC_ALL=C', 'yaourt', '-S', '--noconfirm', '--needed'],
'yay': ['env', 'LC_ALL=C', 'yay', '-S', '--noconfirm'],
}
2017-12-24 10:21:11 +03:00
def upgrade(module, use):
assert use in helper_cmd
2017-12-23 20:27:42 +03:00
2017-12-24 10:21:11 +03:00
cmd = helper_cmd[use] + ['-u']
2017-12-23 20:27:42 +03:00
2017-12-23 22:04:09 +03:00
rc, out, err = module.run_command(cmd, check_rc=True)
2017-12-23 20:27:42 +03:00
module.exit_json(
2017-12-24 10:21:11 +03:00
changed=not (out == '' or 'there is nothing to do' in out or 'No AUR updates found' in out),
2017-12-23 20:27:42 +03:00
msg='upgraded system',
2017-12-24 11:22:13 +03:00
helper_used=use,
2017-12-23 20:27:42 +03:00
)
2017-12-24 10:21:11 +03:00
def install_packages(module, packages, use):
assert use in helper_cmd
2017-12-23 20:27:42 +03:00
2017-12-23 23:05:07 +03:00
changed_iter = False
for package in packages:
2017-12-24 10:21:11 +03:00
cmd = helper_cmd[use] + [package]
2017-12-23 20:27:42 +03:00
2017-12-23 23:05:07 +03:00
rc, out, err = module.run_command(cmd, check_rc=True)
changed_iter = changed_iter or not (out == '' or '-- skipping' in out)
2017-12-23 20:27:42 +03:00
module.exit_json(
2017-12-23 23:05:07 +03:00
changed=changed_iter,
2017-12-23 20:27:42 +03:00
msg='installed package',
2017-12-24 11:22:13 +03:00
helper_used=use,
2017-12-23 20:27:42 +03:00
)
def main():
module = AnsibleModule(
argument_spec={
'name': {
2017-12-23 23:05:07 +03:00
'type': 'list',
2017-12-23 20:27:42 +03:00
},
'upgrade': {
'default': False,
'type': 'bool',
},
2017-12-24 10:21:11 +03:00
'use': {
'default': 'auto',
'choices': ['auto', 'pacaur', 'trizen', 'yaourt', 'yay'],
2017-12-23 20:27:42 +03:00
},
},
required_one_of=[['name', 'upgrade']],
)
params = module.params
2017-12-24 10:21:11 +03:00
if params['use'] == 'auto':
for k in helper_cmd:
if module.get_bin_path(k, False):
helper = k
break
else:
helper = params['use']
2017-12-23 20:27:42 +03:00
if params['upgrade'] and params['name']:
module.fail_json(msg="Upgrade and install must be requested separately")
if params['upgrade']:
2017-12-24 10:21:11 +03:00
upgrade(module, helper)
2017-12-23 20:27:42 +03:00
else:
2017-12-24 10:21:11 +03:00
install_packages(module, params['name'], helper)
2017-12-23 20:27:42 +03:00
if __name__ == '__main__':
main()