@ -25,15 +25,16 @@ import traceback
import os
import os
import yum
import yum
def_qf = "%{name}-%{version}-%{release}.%{arch}"
def_qf = "%{name}-%{version}-%{release}.%{arch}"
repoquery='/usr/bin/repoquery'
repoquery='/usr/bin/repoquery'
if not os.path.exists(repoquery):
if not os.path.exists(repoquery):
repoquery = None
repoquery = None
yumbin='/usr/bin/yum'
yumbin='/usr/bin/yum'
def yum_base(conf_file=None, cachedir=False):
def yum_base(conf_file=None, cachedir=False):
my = yum.YumBase()
my = yum.YumBase()
my.preconf.debuglevel=0
my.preconf.debuglevel=0
my.preconf.errorlevel=0
my.preconf.errorlevel=0
@ -50,15 +51,16 @@ def yum_base(conf_file=None, cachedir=False):
return my
return my
def po_to_nevra(po):
def po_to_nevra(po):
if hasattr(po, 'ui_nevra'):
if hasattr(po, 'ui_nevra'):
return po.ui_nevra
return po.ui_nevra
else:
else:
return '%s-%s-%s.%s' % (po.name, po.version, po.release, po.arch)
return '%s-%s-%s.%s' % (po.name, po.version, po.release, po.arch)
def is_installed(module, repoq, pkgspec, conf_file, qf=def_qf):
def is_installed(module, repoq, pkgspec, conf_file, qf=def_qf):
if not repoq:
if not repoq:
pkgs = []
pkgs = []
try:
try:
my = yum_base(conf_file)
my = yum_base(conf_file)
@ -70,7 +72,9 @@ def is_installed(module, repoq, pkgspec, conf_file, qf=def_qf):
module.fail_json(msg="Failure talking to yum: %s" % e)
module.fail_json(msg="Failure talking to yum: %s" % e)
return [ po_to_nevra(p) for p in pkgs ]
return [ po_to_nevra(p) for p in pkgs ]
else:
else:
cmd = repoq + ["--disablerepo=*", "--pkgnarrow=installed", "--qf", qf, pkgspec]
cmd = repoq + ["--disablerepo=*", "--pkgnarrow=installed", "--qf", qf, pkgspec]
rc,out,err = run(cmd)
rc,out,err = run(cmd)
cmd = repoq + ["--disablerepo=*", "--pkgnarrow=installed", "--qf", qf, "--whatprovides", pkgspec]
cmd = repoq + ["--disablerepo=*", "--pkgnarrow=installed", "--qf", qf, "--whatprovides", pkgspec]
@ -84,7 +88,9 @@ def is_installed(module, repoq, pkgspec, conf_file, qf=def_qf):
return []
return []
def is_available(module, repoq, pkgspec, conf_file, qf=def_qf):
def is_available(module, repoq, pkgspec, conf_file, qf=def_qf):
if not repoq:
if not repoq:
pkgs = []
pkgs = []
try:
try:
my = yum_base(conf_file)
my = yum_base(conf_file)
@ -96,7 +102,9 @@ def is_available(module, repoq, pkgspec, conf_file, qf=def_qf):
module.fail_json(msg="Failure talking to yum: %s" % e)
module.fail_json(msg="Failure talking to yum: %s" % e)
return [ po_to_nevra(p) for p in pkgs ]
return [ po_to_nevra(p) for p in pkgs ]
else:
else:
cmd = repoq + ["--qf", qf, pkgspec]
cmd = repoq + ["--qf", qf, pkgspec]
rc,out,err = run(cmd)
rc,out,err = run(cmd)
if rc == 0:
if rc == 0:
@ -107,10 +115,13 @@ def is_available(module, repoq, pkgspec, conf_file, qf=def_qf):
return []
return []
def is_update(module, repoq, pkgspec, conf_file, qf=def_qf):
def is_update(module, repoq, pkgspec, conf_file, qf=def_qf):
if not repoq:
if not repoq:
retpkgs = []
retpkgs = []
pkgs = []
pkgs = []
updates = []
updates = []
try:
try:
my = yum_base(conf_file)
my = yum_base(conf_file)
pkgs = my.returnPackagesByDep(pkgspec) + my.returnInstalledPackagesByDep(pkgspec)
pkgs = my.returnPackagesByDep(pkgspec) + my.returnInstalledPackagesByDep(pkgspec)
@ -128,6 +139,7 @@ def is_update(module, repoq, pkgspec, conf_file, qf=def_qf):
return set([ po_to_nevra(p) for p in retpkgs ])
return set([ po_to_nevra(p) for p in retpkgs ])
else:
else:
cmd = repoq + ["--pkgnarrow=updates", "--qf", qf, pkgspec]
cmd = repoq + ["--pkgnarrow=updates", "--qf", qf, pkgspec]
rc,out,err = run(cmd)
rc,out,err = run(cmd)
@ -139,7 +151,9 @@ def is_update(module, repoq, pkgspec, conf_file, qf=def_qf):
return []
return []
def what_provides(module, repoq, req_spec, conf_file, qf=def_qf):
def what_provides(module, repoq, req_spec, conf_file, qf=def_qf):
if not repoq:
if not repoq:
pkgs = []
pkgs = []
try:
try:
my = yum_base(conf_file)
my = yum_base(conf_file)
@ -155,7 +169,9 @@ def what_provides(module, repoq, req_spec, conf_file, qf=def_qf):
module.fail_json(msg="Failure talking to yum: %s" % e)
module.fail_json(msg="Failure talking to yum: %s" % e)
return set([ po_to_nevra(p) for p in pkgs ])
return set([ po_to_nevra(p) for p in pkgs ])
else:
else:
cmd = repoq + ["--qf", qf, "--whatprovides", req_spec]
cmd = repoq + ["--qf", qf, "--whatprovides", req_spec]
rc,out,err = run(cmd)
rc,out,err = run(cmd)
cmd = repoq + ["--qf", qf, req_spec]
cmd = repoq + ["--qf", qf, req_spec]
@ -180,6 +196,7 @@ def local_nvra(path):
return nvra
return nvra
def pkg_to_dict(pkgstr):
def pkg_to_dict(pkgstr):
if pkgstr.strip():
if pkgstr.strip():
n,e,v,r,a,repo = pkgstr.split('|')
n,e,v,r,a,repo = pkgstr.split('|')
else:
else:
@ -203,6 +220,7 @@ def pkg_to_dict(pkgstr):
return d
return d
def repolist(repoq, qf="%{repoid}"):
def repolist(repoq, qf="%{repoid}"):
cmd = repoq + ["--qf", qf, "-a"]
cmd = repoq + ["--qf", qf, "-a"]
rc,out,err = run(cmd)
rc,out,err = run(cmd)
ret = []
ret = []
@ -211,6 +229,7 @@ def repolist(repoq, qf="%{repoid}"):
return ret
return ret
def list_stuff(module, conf_file, stuff):
def list_stuff(module, conf_file, stuff):
qf = "%{name}|%{epoch}|%{version}|%{release}|%{arch}|%{repoid}"
qf = "%{name}|%{epoch}|%{version}|%{release}|%{arch}|%{repoid}"
repoq = [repoquery, '--show-duplicates', '--plugins', '--quiet', '-q']
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):
@ -228,6 +247,7 @@ def list_stuff(module, conf_file, stuff):
return [ pkg_to_dict(p) for p in is_installed(module, repoq, stuff, conf_file, qf=qf) + is_available(module, repoq, stuff, conf_file, qf=qf) if p.strip() ]
return [ pkg_to_dict(p) for p in is_installed(module, repoq, stuff, conf_file, qf=qf) + is_available(module, repoq, stuff, conf_file, qf=qf) if p.strip() ]
def run(command):
def run(command):
try:
try:
cmd = subprocess.Popen(command,
cmd = subprocess.Popen(command,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
@ -251,6 +271,7 @@ def run(command):
def install(module, items, repoq, yum_basecmd, conf_file):
def install(module, items, repoq, yum_basecmd, conf_file):
res = {}
res = {}
res['results'] = []
res['results'] = []
res['msg'] = ''
res['msg'] = ''
@ -369,6 +390,7 @@ def remove(module, items, repoq, yum_basecmd, conf_file):
module.exit_json(**res)
module.exit_json(**res)
def latest(module, items, repoq, yum_basecmd, conf_file):
def latest(module, items, repoq, yum_basecmd, conf_file):
res = {}
res = {}
res['results'] = []
res['results'] = []
res['msg'] = ''
res['msg'] = ''
@ -376,6 +398,7 @@ def latest(module, items, repoq, yum_basecmd, conf_file):
res['rc'] = 0
res['rc'] = 0
for spec in items:
for spec in items:
pkg = None
pkg = None
basecmd = 'update'
basecmd = 'update'
# groups, again
# groups, again
@ -408,7 +431,6 @@ def latest(module, items, repoq, yum_basecmd, conf_file):
res['results'].append("All packages providing %s are up to date" % spec)
res['results'].append("All packages providing %s are up to date" % spec)
continue
continue
pkg = spec
pkg = spec
cmd = yum_basecmd + [basecmd, pkg]
cmd = yum_basecmd + [basecmd, pkg]
@ -423,7 +445,6 @@ def latest(module, items, repoq, yum_basecmd, conf_file):
changed = True
changed = True
failed = False
failed = False
if rc:
if rc:
res['changed'] = False
res['changed'] = False
res['failed'] = True
res['failed'] = True
@ -439,20 +460,22 @@ def latest(module, items, repoq, yum_basecmd, conf_file):
module.exit_json(**res)
module.exit_json(**res)
def ensure(module, state, pkgspec, conf_file):
def ensure(module, state, pkgspec, conf_file):
# take multiple args comma separated
# take multiple args comma separated
items = pkgspec.split(',')
items = pkgspec.split(',')
yum_basecmd = [yumbin, '-d', '1', '-y']
yum_basecmd = [yumbin, '-d', '1', '-y']
if not repoquery:
if not repoquery:
repoq = None
repoq = None
else:
else:
repoq = [repoquery, '--show-duplicates', '--plugins', '--quiet', '-q']
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 += ['-c', conf_file]
yum_basecmd += ['-c', conf_file]
if repoq:
if repoq:
repoq += ['-c', conf_file]
repoq += ['-c', conf_file]
if state in ['installed', 'present']:
if state in ['installed', 'present']:
install(module, items, repoq, yum_basecmd, conf_file)
install(module, items, repoq, yum_basecmd, conf_file)
elif state in ['removed', 'absent']:
elif state in ['removed', 'absent']:
@ -460,11 +483,11 @@ def ensure(module, state, pkgspec, conf_file):
elif state == 'latest':
elif state == 'latest':
latest(module, items, repoq, yum_basecmd, conf_file)
latest(module, items, repoq, yum_basecmd, conf_file)
# 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 main():
def main():
# state=installed name=pkgspec
# state=installed name=pkgspec
# state=removed name=pkgspec
# state=removed name=pkgspec
# state=latest name=pkgspec
# state=latest name=pkgspec
@ -504,6 +527,5 @@ def main():
# this is magic, see lib/ansible/module_common.py
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()
main()