Allow using the yum module without repoquery

It is still required to use list=..., but the typical install and remove
won't need it.
This commit is contained in:
Daniel Hokka Zakrisson 2012-08-09 16:12:22 +02:00
parent 093bbd1c82
commit 5a7d271759

View file

@ -23,13 +23,15 @@
import traceback import traceback
import os import os
import subprocess
def_qf = "%{name}-%{version}-%{release}.%{arch}" def_qf = "%{name}-%{version}-%{release}.%{arch}"
repoquery='/usr/bin/repoquery' repoquery='/usr/bin/repoquery'
yumbin='/usr/bin/yum' yumbin='/usr/bin/yum'
rpmbin = '/bin/rpm'
def is_installed(repoq, pkgspec, qf=def_qf): def is_installed(repoq, pkgspec, qf=def_qf):
cmd = repoq + "--disablerepo=\* --pkgnarrow=installed --qf '%s' %s " % (qf, pkgspec) cmd = repoq + ["--disablerepo=*", "--pkgnarrow=installed", "--qf", qf, pkgspec]
rc,out,err = run(cmd) rc,out,err = run(cmd)
if rc == 0: if rc == 0:
return [ p for p in out.split('\n') if p.strip() ] return [ p for p in out.split('\n') if p.strip() ]
@ -37,7 +39,7 @@ def is_installed(repoq, pkgspec, qf=def_qf):
return [] return []
def is_available(repoq, pkgspec, qf=def_qf): def is_available(repoq, pkgspec, qf=def_qf):
cmd = repoq + "--qf '%s' %s " % (qf, pkgspec) cmd = repoq + ["--qf", qf, pkgspec]
rc,out,err = run(cmd) rc,out,err = run(cmd)
if rc == 0: if rc == 0:
return [ p for p in out.split('\n') if p.strip() ] return [ p for p in out.split('\n') if p.strip() ]
@ -46,7 +48,7 @@ def is_available(repoq, pkgspec, qf=def_qf):
def is_update(repoq, pkgspec, qf=def_qf): def is_update(repoq, pkgspec, qf=def_qf):
cmd = repoq + "--pkgnarrow=updates --qf '%s' %s " % (qf, pkgspec) cmd = repoq + ["--pkgnarrow=updates", "--qf", qf, pkgspec]
rc,out,err = run(cmd) rc,out,err = run(cmd)
if rc == 0: if rc == 0:
return set([ p for p in out.split('\n') if p.strip() ]) return set([ p for p in out.split('\n') if p.strip() ])
@ -55,7 +57,7 @@ def is_update(repoq, pkgspec, qf=def_qf):
def what_provides(repoq, req_spec, qf=def_qf): def what_provides(repoq, req_spec, qf=def_qf):
cmd = repoq + "--qf '%s' --whatprovides %s" % (qf, req_spec) cmd = repoq + ["--qf", qf, "--whatprovides", req_spec]
rc,out,err = run(cmd) rc,out,err = run(cmd)
ret = [] ret = []
if rc == 0: if rc == 0:
@ -63,16 +65,6 @@ def what_provides(repoq, req_spec, qf=def_qf):
return ret return ret
def local_nvra(path):
"""return nvra of a local rpm passed in"""
cmd = "/bin/rpm -qp --qf='%%{name}-%%{version}-%%{release}.%%{arch}\n' %s'" % path
rc, out, err = run(cmd)
if rc != 0:
return None
nvra = out.split('\n')[0]
return nvra
def pkg_to_dict(pkgstr): def pkg_to_dict(pkgstr):
if pkgstr.strip(): if pkgstr.strip():
@ -98,7 +90,7 @@ def pkg_to_dict(pkgstr):
return d return d
def repolist(repoq, qf="%{repoid}"): def repolist(repoq, qf="%{repoid}"):
cmd = repoq + "--qf '%s' -a" % (qf) cmd = repoq + ["--qf", qf, "-a"]
rc,out,err = run(cmd) rc,out,err = run(cmd)
ret = [] ret = []
if rc == 0: if rc == 0:
@ -108,9 +100,9 @@ def repolist(repoq, qf="%{repoid}"):
def list_stuff(conf_file, stuff): def list_stuff(conf_file, stuff):
qf = "%{name}|%{epoch}|%{version}|%{release}|%{arch}|%{repoid}" qf = "%{name}|%{epoch}|%{version}|%{release}|%{arch}|%{repoid}"
repoq = '%s --plugins --quiet -q ' % repoquery repoq = [repoquery, '--plugins', '--quiet', '-q']
if conf_file and os.path.exists(conf_file): if conf_file and os.path.exists(conf_file):
repoq = '%s -c %s --plugins --quiet -q ' % (repoquery,conf_file) repoq += ['-c', conf_file]
if stuff == 'installed': if stuff == 'installed':
return [ pkg_to_dict(p) for p in is_installed(repoq, '-a', qf=qf) if p.strip() ] return [ pkg_to_dict(p) for p in is_installed(repoq, '-a', qf=qf) if p.strip() ]
@ -125,7 +117,7 @@ def list_stuff(conf_file, stuff):
def run(command): def run(command):
try: try:
cmd = subprocess.Popen(command, shell=True, cmd = subprocess.Popen(command,
stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = cmd.communicate() out, err = cmd.communicate()
except (OSError, IOError), e: except (OSError, IOError), e:
@ -146,6 +138,58 @@ def run(command):
return rc, out, err return rc, out, err
def install_no_repoq(module, items, yum_basecmd, latest=False):
res = {'changed': False}
if not latest:
to_install = []
for item in items:
rc, out, err = run([rpmbin, "-q", "--whatprovides", item])
if rc != 0:
to_install.append(item)
if len(to_install) > 0:
res['changed'] = True
else:
rc, out, err = run(yum_basecmd + ["check-update"] + items)
if rc == 100:
res['changed'] = True
to_install = items
elif rc != 0:
module.fail_json(msg=err)
if len(to_install) > 0:
rc, out, err = run(yum_basecmd + ["--obsoletes", "install"] + to_install)
if rc != 0:
module.fail_json(msg=err)
for item in to_install:
rc, out, err = run([rpmbin, "-q", "--whatprovides", item])
if rc != 0:
module.fail_json(msg="%s could not be installed" % item)
module.exit_json(**res)
def remove_no_repoq(module, items, yum_basecmd):
res = {'changed': False}
to_remove = []
for item in items:
rc, out, err = run([rpmbin, "-q", "--whatprovides", "--qf", "%{NAME}\n", item])
if rc == 0:
to_remove.append(out.strip())
if len(to_remove) > 0:
res['changed'] = True
rc, out, err = run(yum_basecmd + ["remove"] + to_remove)
if rc != 0:
module.fail_json(msg=err)
res['out'] = out
res['err'] = err
for item in to_remove:
rc, out, err = run([rpmbin, "-q", item])
if rc == 0:
module.fail_json(msg="%s was not removed" % item)
module.exit_json(**res)
def install(module, items, repoq, yum_basecmd): def install(module, items, repoq, yum_basecmd):
res = {} res = {}
@ -197,7 +241,7 @@ def install(module, items, repoq, yum_basecmd):
# the error we're catching here # the error we're catching here
pkg = spec pkg = spec
cmd = "%s install '%s'" % (yum_basecmd, pkg) cmd = yum_basecmd + ['install', pkg]
rc, out, err = run(cmd) rc, out, err = run(cmd)
# FIXME - if we did an install - go and check the rpmdb to see if it actually installed # FIXME - if we did an install - go and check the rpmdb to see if it actually installed
# look for the pkg in rpmdb # look for the pkg in rpmdb
@ -247,7 +291,7 @@ def remove(module, items, repoq, yum_basecmd):
continue continue
pkg = spec pkg = spec
cmd = "%s remove '%s'" % (yum_basecmd, pkg) cmd = yum_basecmd + ["remove", pkg]
rc, out, err = run(cmd) rc, out, err = run(cmd)
# FIXME if we ran the remove - check to make sure it actually removed :( # FIXME if we ran the remove - check to make sure it actually removed :(
@ -308,7 +352,7 @@ def latest(module, items, repoq, yum_basecmd):
pkg = spec pkg = spec
cmd = "%s %s '%s'" % (yum_basecmd, basecmd, pkg) cmd = yum_basecmd + [basecmd, pkg]
rc, out, err = run(cmd) rc, out, err = run(cmd)
# FIXME if it is - update it and check to see if it applied # FIXME if it is - update it and check to see if it applied
@ -335,39 +379,36 @@ def latest(module, items, repoq, yum_basecmd):
module.exit_json(**res) module.exit_json(**res)
def ensure(module, state, pkgspec, conf_file): def ensure(module, state, pkgspec, conf_file):
res = {}
stdout = ""
stderr = ""
# take multiple args comma separated # take multiple args comma separated
items = [pkgspec] items = pkgspec.split(',')
if pkgspec.find(',') != -1:
items = pkgspec.split(',')
yum_basecmd = '%s -d 1 -y ' % yumbin yum_basecmd = [yumbin, '-d', '1', '-y']
repoq = '%s --show-duplicates --plugins --quiet -q ' % repoquery repoq = [repoquery, '--show-duplicates', '--plugins', '--quiet', '-q']
if conf_file and os.path.exists(conf_file): if conf_file and os.path.exists(conf_file):
yum_basecmd = '%s -c %s -d 1 -y' % (yumbin, conf_file) yum_basecmd += ['-c', conf_file]
repoq = '%s --show-duplicates -c %s --plugins --quiet -q ' % (repoquery,conf_file) repoq += ['-c', conf_file]
if state in ['installed', 'present']: if os.path.exists(repoquery):
install(module, items, repoq, yum_basecmd) if state in ['installed', 'present']:
elif state in ['removed', 'absent']: install(module, items, repoq, yum_basecmd)
remove(module, items, repoq, yum_basecmd) elif state in ['removed', 'absent']:
elif state == 'latest': remove(module, items, repoq, yum_basecmd)
latest(module, items, repoq, yum_basecmd) elif state == 'latest':
latest(module, items, repoq, yum_basecmd)
else:
if len(filter(lambda x: x.find('>') != -1 or x.find('<') != -1 or x.find('=') != -1, items)) > 0:
module.fail_json(msg="%s is required to use yum equality comparisons. Please install the yum-utils package." % repoquery)
if state in ['installed', 'present']:
install_no_repoq(module, items, yum_basecmd)
elif state in ['removed', 'absent']:
remove_no_repoq(module, items, yum_basecmd)
elif state == 'latest':
install_no_repoq(module, items, yum_basecmd, latest=True)
# should be caught by AnsibleModule argument_spec # should be caught by AnsibleModule argument_spec
return dict(changed=False, failed=True, results='', errors='unexpected state') return dict(changed=False, failed=True, results='', errors='unexpected state')
def remove_only(pkgspec):
# remove this pkg and only this pkg - fail if it will require more to remove
pass
def main(): def main():
# state=installed pkg=pkgspec # state=installed pkg=pkgspec
# state=removed pkg=pkgspec # state=removed pkg=pkgspec
@ -396,17 +437,15 @@ def main():
if params['list'] and params['pkg']: if params['list'] and params['pkg']:
module.fail_json(msg="expected 'list=' or 'name=', but not both") module.fail_json(msg="expected 'list=' or 'name=', but not both")
if not os.path.exists(repoquery):
module.fail_json(msg="%s is required to run this module. Please install the yum-utils package." % repoquery)
if params['list']: if params['list']:
if not os.path.exists(repoquery):
module.fail_json(msg="%s is required to use list= with this module. Please install the yum-utils package." % repoquery)
results = dict(results=list_stuff(params['conf_file'], params['list'])) results = dict(results=list_stuff(params['conf_file'], params['list']))
module.exit_json(**results) module.exit_json(**results)
else: else:
pkg = params['pkg'] pkg = params['pkg']
if 'pkg' is None: if pkg is None:
module.fail_json(msg="expected 'list=' or 'name='") module.fail_json(msg="expected 'list=' or 'name='")
else: else:
state = params['state'] state = params['state']