Merge pull request #1946 from ansible/pr/1657
Update check-mode results to be a dictionary (finishes pr 1657)
This commit is contained in:
commit
c228739f2d
1 changed files with 108 additions and 53 deletions
|
@ -224,7 +224,7 @@ def is_installed(module, repoq, pkgspec, conf_file, qf=def_qf, en_repos=None, di
|
||||||
for rid in en_repos:
|
for rid in en_repos:
|
||||||
my.repos.enableRepo(rid)
|
my.repos.enableRepo(rid)
|
||||||
|
|
||||||
e,m,u = my.rpmdb.matchPackageNames([pkgspec])
|
e, m, u = my.rpmdb.matchPackageNames([pkgspec])
|
||||||
pkgs = e + m
|
pkgs = e + m
|
||||||
if not pkgs:
|
if not pkgs:
|
||||||
pkgs.extend(my.returnInstalledPackagesByDep(pkgspec))
|
pkgs.extend(my.returnInstalledPackagesByDep(pkgspec))
|
||||||
|
@ -236,16 +236,16 @@ def is_installed(module, repoq, pkgspec, conf_file, qf=def_qf, en_repos=None, di
|
||||||
else:
|
else:
|
||||||
|
|
||||||
cmd = repoq + ["--disablerepo=*", "--pkgnarrow=installed", "--qf", qf, pkgspec]
|
cmd = repoq + ["--disablerepo=*", "--pkgnarrow=installed", "--qf", qf, pkgspec]
|
||||||
rc,out,err = module.run_command(cmd)
|
rc, out, err = module.run_command(cmd)
|
||||||
if not is_pkg:
|
if not is_pkg:
|
||||||
cmd = repoq + ["--disablerepo=*", "--pkgnarrow=installed", "--qf", qf, "--whatprovides", pkgspec]
|
cmd = repoq + ["--disablerepo=*", "--pkgnarrow=installed", "--qf", qf, "--whatprovides", pkgspec]
|
||||||
rc2,out2,err2 = module.run_command(cmd)
|
rc2, out2, err2 = module.run_command(cmd)
|
||||||
else:
|
else:
|
||||||
rc2,out2,err2 = (0, '', '')
|
rc2, out2, err2 = (0, '', '')
|
||||||
|
|
||||||
if rc == 0 and rc2 == 0:
|
if rc == 0 and rc2 == 0:
|
||||||
out += out2
|
out += out2
|
||||||
return [ p for p in out.split('\n') if p.strip() ]
|
return [p for p in out.split('\n') if p.strip()]
|
||||||
else:
|
else:
|
||||||
module.fail_json(msg='Error from repoquery: %s: %s' % (cmd, err + err2))
|
module.fail_json(msg='Error from repoquery: %s: %s' % (cmd, err + err2))
|
||||||
|
|
||||||
|
@ -553,7 +553,7 @@ def install(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos):
|
||||||
module.fail_json(msg="Failure downloading %s, %s" % (spec, e))
|
module.fail_json(msg="Failure downloading %s, %s" % (spec, e))
|
||||||
|
|
||||||
#groups :(
|
#groups :(
|
||||||
elif spec.startswith('@'):
|
elif spec.startswith('@'):
|
||||||
# complete wild ass guess b/c it's a group
|
# complete wild ass guess b/c it's a group
|
||||||
pkg = spec
|
pkg = spec
|
||||||
|
|
||||||
|
@ -620,7 +620,8 @@ def install(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos):
|
||||||
shutil.rmtree(tempdir)
|
shutil.rmtree(tempdir)
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
module.fail_json(msg="Failure deleting temp directory %s, %s" % (tempdir, e))
|
module.fail_json(msg="Failure deleting temp directory %s, %s" % (tempdir, e))
|
||||||
module.exit_json(changed=True)
|
|
||||||
|
module.exit_json(changed=True, results=res['results'], changes=dict(installed=pkgs))
|
||||||
|
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
|
@ -688,7 +689,7 @@ def remove(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos):
|
||||||
cmd = yum_basecmd + ["remove"] + pkgs
|
cmd = yum_basecmd + ["remove"] + pkgs
|
||||||
|
|
||||||
if module.check_mode:
|
if module.check_mode:
|
||||||
module.exit_json(changed=True)
|
module.exit_json(changed=True, results=res['results'], changes=dict(removed=pkgs))
|
||||||
|
|
||||||
rc, out, err = module.run_command(cmd)
|
rc, out, err = module.run_command(cmd)
|
||||||
|
|
||||||
|
@ -723,47 +724,69 @@ def latest(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos):
|
||||||
res['msg'] = ''
|
res['msg'] = ''
|
||||||
res['changed'] = False
|
res['changed'] = False
|
||||||
res['rc'] = 0
|
res['rc'] = 0
|
||||||
|
pkgs = {}
|
||||||
|
pkgs['update'] = []
|
||||||
|
pkgs['install'] = []
|
||||||
|
updates = {}
|
||||||
|
update_all = False
|
||||||
|
cmd = None
|
||||||
|
|
||||||
for spec in items:
|
# determine if we're doing an update all
|
||||||
|
if '*' in items:
|
||||||
|
update_all = True
|
||||||
|
|
||||||
pkg = None
|
# run check-update to see if we have packages pending
|
||||||
basecmd = 'update'
|
rc, out, err = module.run_command(yum_basecmd + ['check-update'])
|
||||||
cmd = ''
|
if rc == 0 and update_all:
|
||||||
# groups, again
|
res['results'].append('Nothing to do here, all packages are up to date')
|
||||||
if spec.startswith('@'):
|
return res
|
||||||
pkg = spec
|
elif rc == 100:
|
||||||
|
available_updates = out.split('\n')
|
||||||
elif spec == '*': #update all
|
# build update dictionary
|
||||||
# use check-update to see if there is any need
|
for line in available_updates:
|
||||||
rc,out,err = module.run_command(yum_basecmd + ['check-update'])
|
line = line.split()
|
||||||
if rc == 100:
|
# ignore irrelevant lines
|
||||||
cmd = yum_basecmd + [basecmd]
|
# FIXME... revisit for something less kludgy
|
||||||
else:
|
if '*' in line or len(line) != 3 or '.' not in line[0]:
|
||||||
res['results'].append('All packages up to date')
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# dep/pkgname - find it
|
|
||||||
else:
|
|
||||||
if is_installed(module, repoq, spec, conf_file, en_repos=en_repos, dis_repos=dis_repos):
|
|
||||||
basecmd = 'update'
|
|
||||||
else:
|
else:
|
||||||
basecmd = 'install'
|
pkg, version, repo = line
|
||||||
|
name, dist = pkg.rsplit('.', 1)
|
||||||
|
updates.update({name: {'version': version, 'dist': dist, 'repo': repo}})
|
||||||
|
elif rc == 1:
|
||||||
|
res['msg'] = err
|
||||||
|
res['rc'] = rc
|
||||||
|
module.fail_json(**res)
|
||||||
|
|
||||||
|
if update_all:
|
||||||
|
cmd = yum_basecmd + ['update']
|
||||||
|
else:
|
||||||
|
for spec in items:
|
||||||
|
# some guess work involved with groups. update @<group> will install the group if missing
|
||||||
|
if spec.startswith('@'):
|
||||||
|
pkgs['update'].append(spec)
|
||||||
|
continue
|
||||||
|
# dep/pkgname - find it
|
||||||
|
else:
|
||||||
|
if is_installed(module, repoq, spec, conf_file, en_repos=en_repos, dis_repos=dis_repos):
|
||||||
|
pkgs['update'].append(spec)
|
||||||
|
else:
|
||||||
|
pkgs['install'].append(spec)
|
||||||
pkglist = what_provides(module, repoq, spec, conf_file, en_repos=en_repos, dis_repos=dis_repos)
|
pkglist = what_provides(module, repoq, spec, conf_file, en_repos=en_repos, dis_repos=dis_repos)
|
||||||
|
# FIXME..? may not be desirable to throw an exception here if a single package is missing
|
||||||
if not pkglist:
|
if not pkglist:
|
||||||
res['msg'] += "No Package matching '%s' found available, installed or updated" % spec
|
res['msg'] += "No Package matching '%s' found available, installed or updated" % spec
|
||||||
module.fail_json(**res)
|
module.fail_json(**res)
|
||||||
|
|
||||||
nothing_to_do = True
|
nothing_to_do = True
|
||||||
for this in pkglist:
|
for this in pkglist:
|
||||||
if basecmd == 'install' and is_available(module, repoq, this, conf_file, en_repos=en_repos, dis_repos=dis_repos):
|
if spec in pkgs['install'] and is_available(module, repoq, this, conf_file, en_repos=en_repos, dis_repos=dis_repos):
|
||||||
nothing_to_do = False
|
nothing_to_do = False
|
||||||
break
|
break
|
||||||
|
|
||||||
if basecmd == 'update' and is_update(module, repoq, this, conf_file, en_repos=en_repos, dis_repos=dis_repos):
|
if spec in pkgs['update'] and spec in updates.keys():
|
||||||
nothing_to_do = False
|
nothing_to_do = False
|
||||||
break
|
|
||||||
|
|
||||||
if nothing_to_do:
|
if nothing_to_do:
|
||||||
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
|
||||||
|
@ -775,27 +798,60 @@ def latest(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos):
|
||||||
res['msg'] += "The following packages have pending transactions: %s" % ", ".join(conflicts)
|
res['msg'] += "The following packages have pending transactions: %s" % ", ".join(conflicts)
|
||||||
module.fail_json(**res)
|
module.fail_json(**res)
|
||||||
|
|
||||||
pkg = spec
|
# list of package updates
|
||||||
if not cmd:
|
if update_all:
|
||||||
cmd = yum_basecmd + [basecmd, pkg]
|
will_update = updates.keys()
|
||||||
|
else:
|
||||||
|
will_update = [u for u in pkgs['update'] if u in updates.keys() or u.startswith('@')]
|
||||||
|
|
||||||
if module.check_mode:
|
# check_mode output
|
||||||
return module.exit_json(changed=True)
|
if module.check_mode:
|
||||||
|
to_update = []
|
||||||
|
for w in will_update:
|
||||||
|
if w.startswith('@'):
|
||||||
|
to_update.append((w, None))
|
||||||
|
msg = '%s will be updated' % w
|
||||||
|
else:
|
||||||
|
to_update.append((w, '%s.%s from %s' % (updates[w]['version'], updates[w]['dist'], updates[w]['repo'])))
|
||||||
|
|
||||||
rc, out, err = module.run_command(cmd)
|
res['changes'] = dict(installed=pkgs['install'], updated=to_update)
|
||||||
|
|
||||||
res['rc'] += rc
|
if len(will_update) > 0 or len(pkgs['install']) > 0:
|
||||||
res['results'].append(out)
|
|
||||||
res['msg'] += err
|
|
||||||
|
|
||||||
# FIXME if it is - update it and check to see if it applied
|
|
||||||
# check to see if there is no longer an update available for the pkgspec
|
|
||||||
|
|
||||||
if rc:
|
|
||||||
res['failed'] = True
|
|
||||||
else:
|
|
||||||
res['changed'] = True
|
res['changed'] = True
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
|
# run commands
|
||||||
|
if cmd: # update all
|
||||||
|
rc, out, err = module.run_command(cmd)
|
||||||
|
res['changed'] = True
|
||||||
|
else:
|
||||||
|
if len(pkgs['install']) > 0: # install missing
|
||||||
|
cmd = yum_basecmd + ['install'] + pkgs['install']
|
||||||
|
rc, out, err = module.run_command(cmd)
|
||||||
|
res['changed'] = True
|
||||||
|
else:
|
||||||
|
rc, out, err = [0, '', '']
|
||||||
|
|
||||||
|
if len(will_update) > 0: # update present
|
||||||
|
cmd = yum_basecmd + ['update'] + pkgs['update']
|
||||||
|
rc2, out2, err2 = module.run_command(cmd)
|
||||||
|
res['changed'] = True
|
||||||
|
else:
|
||||||
|
rc2, out2, err2 = [0, '', '']
|
||||||
|
|
||||||
|
if not update_all:
|
||||||
|
rc += rc2
|
||||||
|
out += out2
|
||||||
|
err += err2
|
||||||
|
|
||||||
|
res['rc'] += rc
|
||||||
|
res['msg'] += err
|
||||||
|
res['results'].append(out)
|
||||||
|
|
||||||
|
if rc:
|
||||||
|
res['failed'] = True
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def ensure(module, state, pkgs, conf_file, enablerepo, disablerepo,
|
def ensure(module, state, pkgs, conf_file, enablerepo, disablerepo,
|
||||||
|
@ -939,4 +995,3 @@ from ansible.module_utils.basic import *
|
||||||
from ansible.module_utils.urls import *
|
from ansible.module_utils.urls import *
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue