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
|
|
|
|
import urllib.request
|
|
|
|
import tarfile
|
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
use_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-31 20:12:28 +03:00
|
|
|
'internal': ['env', 'LC_ALL=C', '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
|
|
|
|
|
|
|
|
rc, out, err = module.run_command(use_cmd[use] + ['-u'], 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-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:
|
|
|
|
rc, out, err = module.run_command(use_cmd[use] + [package], check_rc=True)
|
|
|
|
changed_iter = changed_iter or not (out == '' or '-- skipping' in out or 'there is 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',
|
2017-12-31 20:12:28 +03:00
|
|
|
'choices': ['auto', 'pacaur', 'trizen', 'yaourt', 'yay', 'internal'],
|
|
|
|
},
|
|
|
|
'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()
|