zypper: handle lists of packages efficiently
This commit is contained in:
parent
a0635ebb0d
commit
acd37c87a9
2 changed files with 65 additions and 42 deletions
|
@ -667,7 +667,7 @@ class Runner(object):
|
||||||
if type(items) != list:
|
if type(items) != list:
|
||||||
raise errors.AnsibleError("lookup plugins have to return a list: %r" % items)
|
raise errors.AnsibleError("lookup plugins have to return a list: %r" % items)
|
||||||
|
|
||||||
if len(items) and utils.is_list_of_strings(items) and self.module_name in [ 'apt', 'yum', 'pkgng' ]:
|
if len(items) and utils.is_list_of_strings(items) and self.module_name in [ 'apt', 'yum', 'pkgng', 'zypper' ]:
|
||||||
# hack for apt, yum, and pkgng so that with_items maps back into a single module call
|
# hack for apt, yum, and pkgng so that with_items maps back into a single module call
|
||||||
use_these_items = []
|
use_these_items = []
|
||||||
for x in items:
|
for x in items:
|
||||||
|
|
|
@ -84,36 +84,58 @@ EXAMPLES = '''
|
||||||
- zypper: name=nmap state=absent
|
- zypper: name=nmap state=absent
|
||||||
'''
|
'''
|
||||||
|
|
||||||
# Function used for getting the name of a currently installed package.
|
# Function used for getting versions of currently installed packages.
|
||||||
def get_current_name(m, name):
|
def get_current_version(m, name):
|
||||||
cmd = '/bin/rpm -q --qf \'%{NAME}-%{VERSION}\''
|
cmd = ['/bin/rpm', '-q', '--qf', '%{NAME} %{VERSION}-%{RELEASE}\n']
|
||||||
(rc, stdout, stderr) = m.run_command("%s %s" % (cmd, name))
|
cmd.extend(name)
|
||||||
|
(rc, stdout, stderr) = m.run_command(cmd)
|
||||||
|
|
||||||
if rc != 0:
|
current_version = {}
|
||||||
return (rc, stdout, stderr)
|
rpmoutput_re = re.compile('^(\S+) (\S+)$')
|
||||||
|
for stdoutline, package in zip(stdout.splitlines(), name):
|
||||||
|
m = rpmoutput_re.match(stdoutline)
|
||||||
|
if m == None:
|
||||||
|
return None
|
||||||
|
rpmpackage = m.group(1)
|
||||||
|
rpmversion = m.group(2)
|
||||||
|
if package != rpmpackage:
|
||||||
|
return None
|
||||||
|
current_version[package] = rpmversion
|
||||||
|
|
||||||
syntax = "%s"
|
return current_version
|
||||||
|
|
||||||
for line in stdout.splitlines():
|
|
||||||
if syntax % name in line:
|
|
||||||
current_name = line.split()[0]
|
|
||||||
|
|
||||||
return current_name
|
|
||||||
|
|
||||||
# Function used to find out if a package is currently installed.
|
# Function used to find out if a package is currently installed.
|
||||||
def get_package_state(m, name):
|
def get_package_state(m, packages):
|
||||||
cmd = ['/bin/rpm', '--query', '--info', name]
|
cmd = ['/bin/rpm', '--query', '--qf', 'package %{NAME} is installed\n']
|
||||||
|
cmd.extend(packages)
|
||||||
|
|
||||||
rc, stdout, stderr = m.run_command(cmd, check_rc=False)
|
rc, stdout, stderr = m.run_command(cmd, check_rc=False)
|
||||||
|
|
||||||
if rc == 0:
|
installed_state = {}
|
||||||
return True
|
rpmoutput_re = re.compile('^package (\S+) (.*)$')
|
||||||
else:
|
for stdoutline, name in zip(stdout.splitlines(), packages):
|
||||||
return False
|
m = rpmoutput_re.match(stdoutline)
|
||||||
|
if m == None:
|
||||||
|
return None
|
||||||
|
package = m.group(1)
|
||||||
|
result = m.group(2)
|
||||||
|
if not name.startswith(package):
|
||||||
|
print name + ':' + package + ':' + stdoutline + '\n'
|
||||||
|
return None
|
||||||
|
if result == 'is installed':
|
||||||
|
installed_state[name] = True
|
||||||
|
else:
|
||||||
|
installed_state[name] = False
|
||||||
|
|
||||||
|
return installed_state
|
||||||
|
|
||||||
# Function used to make sure a package is present.
|
# Function used to make sure a package is present.
|
||||||
def package_present(m, name, installed_state, disable_gpg_check, disable_recommends):
|
def package_present(m, name, installed_state, disable_gpg_check, disable_recommends):
|
||||||
if installed_state is False:
|
packages = []
|
||||||
|
for package in name:
|
||||||
|
if installed_state[package] is False:
|
||||||
|
packages.append(package)
|
||||||
|
if len(packages) != 0:
|
||||||
cmd = ['/usr/bin/zypper', '--non-interactive']
|
cmd = ['/usr/bin/zypper', '--non-interactive']
|
||||||
# add global options before zypper command
|
# add global options before zypper command
|
||||||
if disable_gpg_check:
|
if disable_gpg_check:
|
||||||
|
@ -123,7 +145,7 @@ def package_present(m, name, installed_state, disable_gpg_check, disable_recomme
|
||||||
# add install parameter
|
# add install parameter
|
||||||
if disable_recommends:
|
if disable_recommends:
|
||||||
cmd.append('--no-recommends')
|
cmd.append('--no-recommends')
|
||||||
cmd.append(name)
|
cmd.extend(packages)
|
||||||
rc, stdout, stderr = m.run_command(cmd, check_rc=False)
|
rc, stdout, stderr = m.run_command(cmd, check_rc=False)
|
||||||
|
|
||||||
if rc == 0:
|
if rc == 0:
|
||||||
|
@ -141,33 +163,34 @@ def package_present(m, name, installed_state, disable_gpg_check, disable_recomme
|
||||||
# Function used to make sure a package is the latest available version.
|
# Function used to make sure a package is the latest available version.
|
||||||
def package_latest(m, name, installed_state, disable_gpg_check, disable_recommends):
|
def package_latest(m, name, installed_state, disable_gpg_check, disable_recommends):
|
||||||
|
|
||||||
if installed_state is True:
|
# first of all, make sure all the packages are installed
|
||||||
cmd = ['/usr/bin/zypper', '--non-interactive', 'update', '--auto-agree-with-licenses', name]
|
(rc, stdout, stderr, changed) = package_present(m, name, installed_state, disable_gpg_check)
|
||||||
pre_upgrade_name = ''
|
|
||||||
post_upgrade_name = ''
|
|
||||||
|
|
||||||
# Compare the installed package before and after to know if we changed anything.
|
# if we've already made a change, we don't have to check whether a version changed
|
||||||
pre_upgrade_name = get_current_name(m, name)
|
if not changed:
|
||||||
|
pre_upgrade_versions = get_current_version(m, name)
|
||||||
|
|
||||||
rc, stdout, stderr = m.run_command(cmd, check_rc=False)
|
cmd = ['/usr/bin/zypper', '--non-interactive', 'update', '--auto-agree-with-licenses']
|
||||||
|
cmd.extend(name)
|
||||||
|
rc, stdout, stderr = m.run_command(cmd, check_rc=False)
|
||||||
|
|
||||||
post_upgrade_name = get_current_name(m, name)
|
# if we've already made a change, we don't have to check whether a version changed
|
||||||
|
if not changed:
|
||||||
if pre_upgrade_name == post_upgrade_name:
|
post_upgrade_versions = get_current_version(m, name)
|
||||||
changed = False
|
if pre_upgrade_versions != post_upgrade_versions:
|
||||||
else:
|
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
return (rc, stdout, stderr, changed)
|
return (rc, stdout, stderr, changed)
|
||||||
|
|
||||||
else:
|
|
||||||
# If package was not installed at all just make it present.
|
|
||||||
return package_present(m, name, installed_state, disable_gpg_check, disable_recommends)
|
|
||||||
|
|
||||||
# Function used to make sure a package is not installed.
|
# Function used to make sure a package is not installed.
|
||||||
def package_absent(m, name, installed_state):
|
def package_absent(m, name, installed_state):
|
||||||
if installed_state is True:
|
packages = []
|
||||||
cmd = ['/usr/bin/zypper', '--non-interactive', 'remove', name]
|
for package in name:
|
||||||
|
if installed_state[package] is True:
|
||||||
|
packages.append(package)
|
||||||
|
if len(packages) != 0:
|
||||||
|
cmd = ['/usr/bin/zypper', '--non-interactive', 'remove']
|
||||||
|
cmd.extend(packages)
|
||||||
rc, stdout, stderr = m.run_command(cmd)
|
rc, stdout, stderr = m.run_command(cmd)
|
||||||
|
|
||||||
if rc == 0:
|
if rc == 0:
|
||||||
|
@ -188,7 +211,7 @@ def package_absent(m, name, installed_state):
|
||||||
def main():
|
def main():
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
argument_spec = dict(
|
argument_spec = dict(
|
||||||
name = dict(required=True, aliases=['pkg']),
|
name = dict(required=True, aliases=['pkg'], type='list'),
|
||||||
state = dict(required=False, default='present', choices=['absent', 'installed', 'latest', 'present', 'removed']),
|
state = dict(required=False, default='present', choices=['absent', 'installed', 'latest', 'present', 'removed']),
|
||||||
disable_gpg_check = dict(required=False, default='no', type='bool'),
|
disable_gpg_check = dict(required=False, default='no', type='bool'),
|
||||||
disable_recommends = dict(requiered=False, default='yes', type='bool'),
|
disable_recommends = dict(requiered=False, default='yes', type='bool'),
|
||||||
|
|
Loading…
Reference in a new issue