ansible-aur/aur.py

133 lines
3.9 KiB
Python
Raw Normal View History

2017-12-23 20:27:42 +03:00
#!/usr/bin/env python
from ansible.module_utils.basic import *
2017-12-31 20:12:28 +03:00
import json
from ansible.module_utils import six
from six.moves import urllib
2017-12-31 20:12:28 +03:00
import tarfile
import os
import os.path
import tempfile
2018-04-14 13:53:14 +03:00
2018-03-17 11:15:06 +03:00
def_lang = ['env', 'LC_ALL=C']
2017-12-31 20:12:28 +03:00
use_cmd = {
2018-04-14 13:37:11 +03:00
'aurman': ['aurman', '-S', '--noconfirm', '--noedit', '--needed', '--aur'],
2018-03-17 11:15:06 +03:00
'pacaur': ['pacaur', '-S', '--noconfirm', '--noedit', '--needed', '--aur'],
'trizen': ['trizen', '-S', '--noconfirm', '--noedit', '--needed', '--aur'],
'pikaur': ['pikaur', '-S', '--noconfirm', '--noedit', '--needed'],
'yaourt': ['yaourt', '-S', '--noconfirm', '--needed'],
'yay': ['yay', '-S', '--noconfirm'],
'internal': ['makepkg', '--syncdeps', '--install', '--noconfirm', '--needed']
2017-12-23 20:27:42 +03:00
}
2017-12-31 20:12:28 +03:00
def package_installed(module, package):
rc, _, _ = module.run_command(['pacman', '-Q', package], check_rc=False)
return rc == 0
def install_internal(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:
return (1, '', 'package not found')
result = result['results'][0]
f = urllib.request.urlopen('https://aur.archlinux.org/{}'.format(result['URLPath']))
current_path = os.getcwd()
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
tar_file = '{}.tar.gz'.format(result['Name'])
with open(tar_file, 'wb') as out:
out.write(f.read())
tar = tarfile.open(tar_file)
tar.extractall()
tar.close()
os.chdir(format(result['Name']))
rc, out, err = module.run_command(use_cmd['internal'], check_rc=True)
os.chdir(current_path)
return (rc, out, err)
2017-12-23 20:27:42 +03:00
2017-12-31 20:12:28 +03:00
def upgrade(module, use):
assert use in use_cmd
2018-03-17 11:15:06 +03:00
rc, out, err = module.run_command(def_lang + use_cmd[use] + ['-u'], check_rc=True)
2017-12-23 20:27:42 +03:00
module.exit_json(
2018-04-14 13:37:11 +03:00
changed=not (out == '' or '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-31 20:12:28 +03:00
def install_packages(module, packages, use, skip_installed):
assert use in use_cmd
2017-12-23 20:27:42 +03:00
2017-12-23 23:05:07 +03:00
changed_iter = False
2017-12-23 20:27:42 +03:00
2017-12-31 20:12:28 +03:00
for package in packages:
if skip_installed:
if package_installed(module, package):
rc = 0
continue
if use == 'internal':
rc, out, err = install_internal(module, package)
else:
2018-03-17 11:15:06 +03:00
rc, out, err = module.run_command(def_lang + use_cmd[use] + [package], check_rc=True)
2018-04-14 13:37:11 +03:00
changed_iter = changed_iter or not (out == '' or '-- skipping' in out or 'nothing to do' 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-31 20:12:28 +03:00
msg='installed package' if not rc else err,
2017-12-24 11:22:13 +03:00
helper_used=use,
2017-12-31 20:12:28 +03:00
rc=rc,
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',
2018-04-14 13:37:11 +03:00
'choices': ['auto', 'aurman', 'pacaur', 'trizen', 'pikaur', 'yaourt', 'yay', 'internal'],
2017-12-31 20:12:28 +03:00
},
'skip_installed': {
'default': 'no',
'type': 'bool',
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':
2017-12-31 21:58:29 +03:00
use = 'internal'
2017-12-31 20:12:28 +03:00
for k in use_cmd:
2017-12-24 10:21:11 +03:00
if module.get_bin_path(k, False):
2017-12-31 20:12:28 +03:00
use = k
2017-12-24 10:21:11 +03:00
break
else:
2017-12-31 20:12:28 +03:00
use = params['use']
2017-12-24 10:21:11 +03:00
2017-12-31 20:12:28 +03:00
if params['upgrade'] and (params['name'] or params['skip_installed'] or use == 'internal'):
module.fail_json(msg="Upgrade cannot be used with this option.")
2017-12-23 20:27:42 +03:00
else:
2017-12-31 20:12:28 +03:00
if params['upgrade']:
upgrade(module, use)
else:
install_packages(module, params['name'], use, params['skip_installed'])
2017-12-23 20:27:42 +03:00
if __name__ == '__main__':
main()