make yum update_only option actually work (#47942)

* make yum update_only option actually work

Fixes #40615

Signed-off-by: Adam Miller <admiller@redhat.com>

* fix changlog fragment for sanity check

Signed-off-by: Adam Miller <admiller@redhat.com>

* only attempt an update when there are packages to update

Signed-off-by: Adam Miller <admiller@redhat.com>

* fix logic to properly handle the precedent of operations

Signed-off-by: Adam Miller <admiller@redhat.com>

* remove q debug statements

Signed-off-by: Adam Miller <admiller@redhat.com>
(cherry picked from commit 34fc66185e)
This commit is contained in:
Adam Miller 2018-11-13 14:04:19 -06:00 committed by Toshio Kuratomi
parent 16b354e835
commit 295174b3fd
2 changed files with 34 additions and 13 deletions

View file

@ -0,0 +1,3 @@
---
minor_changes:
- "yum now properly supports update_only option"

View file

@ -1199,7 +1199,9 @@ class YumModule(YumDnf):
self.module.fail_json(msg="Failed to get nevra information from RPM package: %s" % spec)
# local rpm files can't be updated
if not self.is_installed(repoq, envra):
if self.is_installed(repoq, envra):
pkgs['update'].append(spec)
else:
pkgs['install'].append(spec)
continue
@ -1214,13 +1216,15 @@ class YumModule(YumDnf):
self.module.fail_json(msg="Failed to get nevra information from RPM package: %s" % spec)
# local rpm files can't be updated
if not self.is_installed(repoq, envra):
pkgs['install'].append(package)
if self.is_installed(repoq, envra):
pkgs['update'].append(spec)
else:
pkgs['install'].append(spec)
continue
# dep/pkgname - find it
else:
if self.is_installed(repoq, spec) or self.update_only:
if self.is_installed(repoq, spec):
pkgs['update'].append(spec)
else:
pkgs['install'].append(spec)
@ -1290,6 +1294,9 @@ class YumModule(YumDnf):
else:
to_update.append((w, '%s.%s from %s' % (updates[w]['version'], updates[w]['dist'], updates[w]['repo'])))
if self.update_only:
res['changes'] = dict(installed=[], updated=to_update)
else:
res['changes'] = dict(installed=pkgs['install'], updated=to_update)
if will_update or pkgs['install']:
@ -1304,7 +1311,18 @@ class YumModule(YumDnf):
if cmd: # update all
rc, out, err = self.module.run_command(cmd)
res['changed'] = True
elif pkgs['install'] or will_update:
elif self.update_only:
if pkgs['update']:
cmd = self.yum_basecmd + ['update'] + pkgs['update']
lang_env = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C')
rc, out, err = self.module.run_command(cmd, environ_update=lang_env)
out_lower = out.strip().lower()
if not out_lower.endswith("no packages marked for update") and \
not out_lower.endswith("nothing to do"):
res['changed'] = True
else:
rc, out, err = [0, '', '']
elif pkgs['install'] or will_update and not self.update_only:
cmd = self.yum_basecmd + ['install'] + pkgs['install'] + pkgs['update']
lang_env = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C')
rc, out, err = self.module.run_command(cmd, environ_update=lang_env)
@ -1423,13 +1441,7 @@ class YumModule(YumDnf):
self.module.fail_json(msg="Error setting/accessing repos: %s" % to_native(e))
except yum.Errors.YumBaseError as e:
self.module.fail_json(msg="Error accessing repos: %s" % to_native(e))
if self.state in ('installed', 'present'):
if self.disable_gpg_check:
self.yum_basecmd.append('--nogpgcheck')
res = self.install(pkgs, repoq)
elif self.state in ('removed', 'absent'):
res = self.remove(pkgs, repoq)
elif self.state == 'latest':
if self.state == 'latest' or self.update_only:
if self.disable_gpg_check:
self.yum_basecmd.append('--nogpgcheck')
if self.security:
@ -1437,6 +1449,12 @@ class YumModule(YumDnf):
if self.bugfix:
self.yum_basecmd.append('--bugfix')
res = self.latest(pkgs, repoq)
elif self.state in ('installed', 'present'):
if self.disable_gpg_check:
self.yum_basecmd.append('--nogpgcheck')
res = self.install(pkgs, repoq)
elif self.state in ('removed', 'absent'):
res = self.remove(pkgs, repoq)
else:
# should be caught by AnsibleModule argument_spec
self.module.fail_json(