fixed zypper.py and zypper_repository.py for SLE10 platform

reviewable/pr18780/r1
Boris Manojlovic 10 years ago
parent 681db4ce2c
commit 4e0b11cbe7

@ -84,6 +84,16 @@ EXAMPLES = '''
- zypper: name=nmap state=absent - zypper: name=nmap state=absent
''' '''
# Function used for getting zypper version
def zypper_version(module):
"""Return (rc, message) tuple"""
cmd = ['/usr/bin/zypper', '-V']
rc, stdout, stderr = module.run_command(cmd, check_rc=False)
if rc == 0:
return rc, stdout
else:
return rc, stderr
# Function used for getting versions of currently installed packages. # Function used for getting versions of currently installed packages.
def get_current_version(m, name): def get_current_version(m, name):
cmd = ['/bin/rpm', '-q', '--qf', '%{NAME} %{VERSION}-%{RELEASE}\n'] cmd = ['/bin/rpm', '-q', '--qf', '%{NAME} %{VERSION}-%{RELEASE}\n']
@ -130,7 +140,7 @@ def get_package_state(m, packages):
return installed_state 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, old_zypper):
packages = [] packages = []
for package in name: for package in name:
if installed_state[package] is False: if installed_state[package] is False:
@ -138,12 +148,14 @@ def package_present(m, name, installed_state, disable_gpg_check, disable_recomme
if len(packages) != 0: 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 and not old_zypper:
cmd.append('--no-gpg-check') cmd.append('--no-gpg-check')
else:
cmd.append('--no-gpg-checks')
cmd.extend(['install', '--auto-agree-with-licenses']) cmd.extend(['install', '--auto-agree-with-licenses'])
# add install parameter # add install parameter
if disable_recommends: if disable_recommends and not old_zypper:
cmd.append('--no-recommends') cmd.append('--no-recommends')
cmd.extend(packages) cmd.extend(packages)
rc, stdout, stderr = m.run_command(cmd, check_rc=False) rc, stdout, stderr = m.run_command(cmd, check_rc=False)
@ -161,16 +173,19 @@ def package_present(m, name, installed_state, disable_gpg_check, disable_recomme
return (rc, stdout, stderr, changed) return (rc, stdout, stderr, changed)
# 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, old_zypper):
# first of all, make sure all the packages are installed # first of all, make sure all the packages are installed
(rc, stdout, stderr, changed) = package_present(m, name, installed_state, disable_gpg_check) (rc, stdout, stderr, changed) = package_present(m, name, installed_state, disable_gpg_check, disable_recommends, old_zypper)
# if we've already made a change, we don't have to check whether a version changed # if we've already made a change, we don't have to check whether a version changed
if not changed: if not changed:
pre_upgrade_versions = get_current_version(m, name) pre_upgrade_versions = get_current_version(m, name)
cmd = ['/usr/bin/zypper', '--non-interactive', 'update', '--auto-agree-with-licenses'] if old_zypper:
cmd = ['/usr/bin/zypper', '--non-interactive', 'install', '--auto-agree-with-licenses']
else:
cmd = ['/usr/bin/zypper', '--non-interactive', 'update', '--auto-agree-with-licenses']
cmd.extend(name) cmd.extend(name)
rc, stdout, stderr = m.run_command(cmd, check_rc=False) rc, stdout, stderr = m.run_command(cmd, check_rc=False)
@ -183,7 +198,7 @@ def package_latest(m, name, installed_state, disable_gpg_check, disable_recommen
return (rc, stdout, stderr, changed) return (rc, stdout, stderr, changed)
# 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, old_zypper):
packages = [] packages = []
for package in name: for package in name:
if installed_state[package] is True: if installed_state[package] is True:
@ -234,16 +249,23 @@ def main():
result['name'] = name result['name'] = name
result['state'] = state result['state'] = state
rc, out = zypper_version(module)
match = re.match(r'zypper\s+(\d+)\.(\d+)\.(\d+)', out)
if not match or int(match.group(1)) > 0:
old_zypper = False
else:
old_zypper = True
# Get package state # Get package state
installed_state = get_package_state(module, name) installed_state = get_package_state(module, name)
# Perform requested action # Perform requested action
if state in ['installed', 'present']: if state in ['installed', 'present']:
(rc, stdout, stderr, changed) = package_present(module, name, installed_state, disable_gpg_check, disable_recommends) (rc, stdout, stderr, changed) = package_present(module, name, installed_state, disable_gpg_check, disable_recommends, old_zypper)
elif state in ['absent', 'removed']: elif state in ['absent', 'removed']:
(rc, stdout, stderr, changed) = package_absent(module, name, installed_state) (rc, stdout, stderr, changed) = package_absent(module, name, installed_state, old_zypper)
elif state == 'latest': elif state == 'latest':
(rc, stdout, stderr, changed) = package_latest(module, name, installed_state, disable_gpg_check, disable_recommends) (rc, stdout, stderr, changed) = package_latest(module, name, installed_state, disable_gpg_check, disable_recommends, old_zypper)
if rc != 0: if rc != 0:
if stderr: if stderr:

@ -72,16 +72,24 @@ EXAMPLES = '''
# Add python development repository # Add python development repository
- zypper_repository: repo=http://download.opensuse.org/repositories/devel:/languages:/python/SLE_11_SP3/devel:languages:python.repo - zypper_repository: repo=http://download.opensuse.org/repositories/devel:/languages:/python/SLE_11_SP3/devel:languages:python.repo
''' '''
from xml.dom.minidom import parseString as parseXML
REPO_OPTS = ['alias', 'name', 'priority', 'enabled', 'autorefresh', 'gpgcheck'] REPO_OPTS = ['alias', 'name', 'priority', 'enabled', 'autorefresh', 'gpgcheck']
def zypper_version(module):
"""Return (rc, message) tuple"""
cmd = ['/usr/bin/zypper', '-V']
rc, stdout, stderr = module.run_command(cmd, check_rc=False)
if rc == 0:
return rc, stdout
else:
return rc, stderr
def _parse_repos(module): def _parse_repos(module):
"""parses the output of zypper -x lr and returns a parse repo dictionary""" """parses the output of zypper -x lr and returns a parse repo dictionary"""
cmd = ['/usr/bin/zypper', '-x', 'lr'] cmd = ['/usr/bin/zypper', '-x', 'lr']
repos = [] repos = []
from xml.dom.minidom import parseString as parseXML
rc, stdout, stderr = module.run_command(cmd, check_rc=True) rc, stdout, stderr = module.run_command(cmd, check_rc=True)
dom = parseXML(stdout) dom = parseXML(stdout)
repo_list = dom.getElementsByTagName('repo') repo_list = dom.getElementsByTagName('repo')
@ -95,8 +103,25 @@ def _parse_repos(module):
return repos return repos
def _parse_repos_old(module):
"""parses the output of zypper sl and returns a parse repo dictionary"""
cmd = ['/usr/bin/zypper', 'sl']
repos = []
rc, stdout, stderr = module.run_command(cmd, check_rc=True)
for line in stdout.split('\n'):
matched = re.search(r'\d+\s+\|\s+(?P<enabled>\w+)\s+\|\s+(?P<autorefresh>\w+)\s+\|\s+(?P<type>\w+)\s+\|\s+(?P<name>\w+)\s+\|\s+(?P<url>.*)', line)
if matched == None:
continue
m = matched.groupdict()
m['alias']= m['name']
m['priority'] = 100
m['gpgcheck'] = 1
repos.append(m)
return repos
def repo_exists(module, **kwargs): def repo_exists(module, old_zypper, **kwargs):
def repo_subset(realrepo, repocmp): def repo_subset(realrepo, repocmp):
for k in repocmp: for k in repocmp:
@ -109,7 +134,10 @@ def repo_exists(module, **kwargs):
return False return False
return True return True
repos = _parse_repos(module) if old_zypper:
repos = _parse_repos_old(module)
else:
repos = _parse_repos(module)
for repo in repos: for repo in repos:
if repo_subset(repo, kwargs): if repo_subset(repo, kwargs):
@ -117,13 +145,21 @@ def repo_exists(module, **kwargs):
return False return False
def add_repo(module, repo, alias, description, disable_gpg_check): def add_repo(module, repo, alias, description, disable_gpg_check, old_zypper):
cmd = ['/usr/bin/zypper', 'ar', '--check', '--refresh'] if old_zypper:
cmd = ['/usr/bin/zypper', 'sa']
else:
cmd = ['/usr/bin/zypper', 'ar', '--check', '--refresh']
if repo.startswith("file:/") and old_zypper:
cmd.extend(['-t', 'Plaindir'])
else:
cmd.extend(['-t', 'plaindir'])
if description: if description:
cmd.extend(['--name', description]) cmd.extend(['--name', description])
if disable_gpg_check: if disable_gpg_check and not old_zypper:
cmd.append('--no-gpgcheck') cmd.append('--no-gpgcheck')
cmd.append(repo) cmd.append(repo)
@ -138,14 +174,21 @@ def add_repo(module, repo, alias, description, disable_gpg_check):
elif 'already exists. Please use another alias' in stderr: elif 'already exists. Please use another alias' in stderr:
changed = False changed = False
else: else:
module.fail_json(msg=stderr if stderr else stdout) #module.fail_json(msg=stderr if stderr else stdout)
if stderr:
module.fail_json(msg=stderr)
else:
module.fail_json(msg=stdout)
return changed return changed
def remove_repo(module, repo, alias): def remove_repo(module, repo, alias, old_zypper):
cmd = ['/usr/bin/zypper', 'rr'] if old_zypper:
cmd = ['/usr/bin/zypper', 'sd']
else:
cmd = ['/usr/bin/zypper', 'rr']
if alias: if alias:
cmd.append(alias) cmd.append(alias)
else: else:
@ -158,7 +201,11 @@ def remove_repo(module, repo, alias):
def fail_if_rc_is_null(module, rc, stdout, stderr): def fail_if_rc_is_null(module, rc, stdout, stderr):
if rc != 0: if rc != 0:
module.fail_json(msg=stderr if stderr else stdout) #module.fail_json(msg=stderr if stderr else stdout)
if stderr:
module.fail_json(msg=stderr)
else:
module.fail_json(msg=stdout)
def main(): def main():
@ -182,6 +229,13 @@ def main():
def exit_unchanged(): def exit_unchanged():
module.exit_json(changed=False, repo=repo, state=state, name=name) module.exit_json(changed=False, repo=repo, state=state, name=name)
rc, out = zypper_version(module)
match = re.match(r'zypper\s+(\d+)\.(\d+)\.(\d+)', out)
if not match or int(match.group(1)) > 0:
old_zypper = False
else:
old_zypper = True
# Check run-time module parameters # Check run-time module parameters
if state == 'present' and not repo: if state == 'present' and not repo:
module.fail_json(msg='Module option state=present requires repo') module.fail_json(msg='Module option state=present requires repo')
@ -196,22 +250,22 @@ def main():
module.fail_json(msg='Name required when adding non-repo files:') module.fail_json(msg='Name required when adding non-repo files:')
if repo and repo.endswith('.repo'): if repo and repo.endswith('.repo'):
exists = repo_exists(module, url=repo, alias=name) exists = repo_exists(module, old_zypper, url=repo, alias=name)
elif repo: elif repo:
exists = repo_exists(module, url=repo) exists = repo_exists(module, old_zypper, url=repo)
else: else:
exists = repo_exists(module, alias=name) exists = repo_exists(module, old_zypper, alias=name)
if state == 'present': if state == 'present':
if exists: if exists:
exit_unchanged() exit_unchanged()
changed = add_repo(module, repo, name, description, disable_gpg_check) changed = add_repo(module, repo, name, description, disable_gpg_check, old_zypper)
elif state == 'absent': elif state == 'absent':
if not exists: if not exists:
exit_unchanged() exit_unchanged()
changed = remove_repo(module, repo, name) changed = remove_repo(module, repo, name, old_zypper)
module.exit_json(changed=changed, repo=repo, state=state) module.exit_json(changed=changed, repo=repo, state=state)

Loading…
Cancel
Save