Fix yum install root (#19925)

* Reenable yum install root tests

No need for sos to test installroot.  Something with less deps works
just as well.

* Fix yum installroot.

Fix module import to use fail_json when the modules aren't installed.

Remove wildcard imports

* Lsat task is supposed to remove sos so make that happen
pull/19891/merge
Toshio Kuratomi 8 years ago committed by GitHub
parent 22c9428776
commit 95df8977b6

@ -22,12 +22,21 @@
# #
import os import os
import yum import re
import rpm
import platform
import tempfile
import shutil import shutil
from distutils.version import LooseVersion import tempfile
try:
import rpm
HAS_RPM_PYTHON = True
except ImportError:
HAS_RPM_PYTHON = False
try:
import yum
HAS_YUM_PYTHON = True
except ImportError:
HAS_YUM_PYTHON = False
try: try:
from yum.misc import find_unfinished_transactions, find_ts_remaining from yum.misc import find_unfinished_transactions, find_ts_remaining
@ -36,6 +45,11 @@ try:
except: except:
transaction_helpers = False transaction_helpers = False
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.pycompat24 import get_exception
from ansible.module_utils.urls import fetch_url
ANSIBLE_METADATA = {'status': ['stableinterface'], ANSIBLE_METADATA = {'status': ['stableinterface'],
'supported_by': 'core', 'supported_by': 'core',
'version': '1.0'} 'version': '1.0'}
@ -234,7 +248,8 @@ def yum_base(conf_file=None, installroot='/'):
# do not setup installroot by default, because of error # do not setup installroot by default, because of error
# CRITICAL:yum.cli:Config Error: Error accessing file for config file:////etc/yum.conf # CRITICAL:yum.cli:Config Error: Error accessing file for config file:////etc/yum.conf
# in old yum version (like in CentOS 6.6) # in old yum version (like in CentOS 6.6)
my.conf.installroot=installroot my.preconf.root = installroot
my.conf.installroot = installroot
if conf_file and os.path.exists(conf_file): if conf_file and os.path.exists(conf_file):
my.preconf.fn = conf_file my.preconf.fn = conf_file
if os.geteuid() != 0: if os.geteuid() != 0:
@ -318,6 +333,8 @@ def is_installed(module, repoq, pkgspec, conf_file, qf=def_qf, en_repos=None, di
rpmbin = module.get_bin_path('rpm', required=True) rpmbin = module.get_bin_path('rpm', required=True)
cmd = [rpmbin, '-q', '--qf', qf, pkgspec] cmd = [rpmbin, '-q', '--qf', qf, pkgspec]
if installroot != '/':
cmd.extend(['--root', installroot])
# rpm localizes messages and we're screen scraping so make sure we use # rpm localizes messages and we're screen scraping so make sure we use
# the C locale # the C locale
lang_env = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C') lang_env = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C')
@ -330,6 +347,8 @@ def is_installed(module, repoq, pkgspec, conf_file, qf=def_qf, en_repos=None, di
pkgs = [p for p in out.replace('(none)', '0').split('\n') if p.strip()] pkgs = [p for p in out.replace('(none)', '0').split('\n') if p.strip()]
if not pkgs and not is_pkg: if not pkgs and not is_pkg:
cmd = [rpmbin, '-q', '--qf', qf, '--whatprovides', pkgspec] cmd = [rpmbin, '-q', '--qf', qf, '--whatprovides', pkgspec]
if installroot != '/':
cmd.extend(['--root', installroot])
rc2, out2, err2 = module.run_command(cmd, environ_update=lang_env) rc2, out2, err2 = module.run_command(cmd, environ_update=lang_env)
else: else:
rc2, out2, err2 = (0, '', '') rc2, out2, err2 = (0, '', '')
@ -593,6 +612,8 @@ def list_stuff(module, repoquerybin, conf_file, stuff, installroot='/'):
# is_installed goes through rpm instead of repoquery so it needs a slightly different format # is_installed goes through rpm instead of repoquery so it needs a slightly different format
is_installed_qf = "%{name}|%{epoch}|%{version}|%{release}|%{arch}|installed\n" is_installed_qf = "%{name}|%{epoch}|%{version}|%{release}|%{arch}|installed\n"
repoq = [repoquerybin, '--show-duplicates', '--plugins', '--quiet'] repoq = [repoquerybin, '--show-duplicates', '--plugins', '--quiet']
if installroot != '/':
repoq.extend(['--installroot', installroot])
if conf_file and os.path.exists(conf_file): if conf_file and os.path.exists(conf_file):
repoq += ['-c', conf_file] repoq += ['-c', conf_file]
@ -1095,6 +1116,15 @@ def main():
supports_check_mode = True supports_check_mode = True
) )
error_msgs = []
if not HAS_RPM_PYTHON:
error_msgs.append('python2 bindings for rpm are needed for this module')
if not HAS_YUM_PYTHON:
error_msgs.append('python2 yum module is needed for this module')
if error_msgs:
module.fail_json(msg='. '.join(error_msgs))
params = module.params params = module.params
if params['list']: if params['list']:
@ -1122,6 +1152,8 @@ def main():
repoquerybin = ensure_yum_utils(module) repoquerybin = ensure_yum_utils(module)
if repoquerybin: if repoquerybin:
repoquery = [repoquerybin, '--show-duplicates', '--plugins', '--quiet'] repoquery = [repoquerybin, '--show-duplicates', '--plugins', '--quiet']
if params['installroot'] != '/':
repoquery.extend(['--installroot', params['installroot']])
pkg = [ p.strip() for p in params['name']] pkg = [ p.strip() for p in params['name']]
exclude = params['exclude'] exclude = params['exclude']
@ -1138,8 +1170,6 @@ def main():
module.exit_json(**results) module.exit_json(**results)
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.urls import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

@ -227,5 +227,8 @@
- "'results' in yum_result" - "'results' in yum_result"
- name: uninstall sos - name: uninstall sos
yum: name=sos installroot='/' yum:
name: sos
installroot: '/'
state: removed
register: yum_result register: yum_result

@ -17,44 +17,44 @@
- name: Make a necessary directory - name: Make a necessary directory
file: file:
path: "/{{ yumroot.stdout }}/etc/yum/vars/" path: "{{ yumroot.stdout }}/etc/yum/vars/"
state: directory state: directory
mode: 0755 mode: 0755
#- name: Populate directory - name: Populate directory
# copy: copy:
# content: "{{ ansible_lsb.major_release }}\n" content: "{{ ansible_distribution_major_version }}\n"
# dest: "/{{ yumroot.stdout }}/etc/yum/vars/releasever" dest: "/{{ yumroot.stdout }}/etc/yum/vars/releasever"
#
## This will drag in > 200 MB. # This will drag in > 200 MB.
#- name: attempt installroot - name: attempt installroot
# yum: name=sos installroot="/{{ yumroot.stdout }}/" disable_gpg_check=yes yum: name=zlib installroot="{{ yumroot.stdout }}/" disable_gpg_check=yes
# register: yum_result register: yum_result
#
#- name: check sos with rpm in installroot - name: check sos with rpm in installroot
# shell: rpm -q sos --root="/{{ yumroot.stdout }}/" shell: rpm -q zlib --root="{{ yumroot.stdout }}/"
# failed_when: False failed_when: False
# register: rpm_result register: rpm_result
#
#- debug: var=yum_result - debug: var=yum_result
#- debug: var=rpm_result - debug: var=rpm_result
#
#- name: verify installation of sos - name: verify installation of sos
# assert: assert:
# that: that:
# - "yum_result.rc == 0" - "yum_result.rc == 0"
# - "yum_result.changed" - "yum_result.changed"
# - "rpm_result.rc == 0" - "rpm_result.rc == 0"
#
#- name: verify yum module outputs - name: verify yum module outputs
# assert: assert:
# that: that:
# - "'changed' in yum_result" - "'changed' in yum_result"
# - "'msg' in yum_result" - "'msg' in yum_result"
# - "'rc' in yum_result" - "'rc' in yum_result"
# - "'results' in yum_result" - "'results' in yum_result"
#
- name: cleanup installroot - name: cleanup installroot
file: file:
path: "/{{ yumroot.stdout }}/" path: "{{ yumroot.stdout }}/"
state: absent state: absent

Loading…
Cancel
Save