Made some code cleanups and use of module.run_command

This commit is contained in:
Raul Melo 2013-08-09 16:04:13 +02:00
commit 71e52b38f5

View file

@ -1,8 +1,8 @@
#!/usr/bin/python -tt #!/usr/bin/python -tt
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# (c) 2012, Flowroute LLC # (c) 2013, Raul Melo
# Written by Matthew Williams <matthew@flowroute.com> # Written by Raul Melo <raulmelo@fgmail.com>
# Based on yum module written by Seth Vidal <skvidal at fedoraproject.org> # Based on yum module written by Seth Vidal <skvidal at fedoraproject.org>
# #
# This module is free software: you can redistribute it and/or modify # This module is free software: you can redistribute it and/or modify
@ -62,12 +62,6 @@ EXAMPLES = '''
- swdepot: name=unzip state=absent - swdepot: name=unzip state=absent
''' '''
def _run(cmd):
# returns (rc, stdout, stderr) from shell command
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
stdout, stderr = process.communicate()
return (process.returncode, stdout, stderr)
def compare_package(version1, version2): def compare_package(version1, version2):
""" Compare version packages. """ Compare version packages.
Return values: Return values:
@ -79,14 +73,14 @@ def compare_package(version1, version2):
return [int(x) for x in re.sub(r'(\.0+)*$', '', v).split(".")] return [int(x) for x in re.sub(r'(\.0+)*$', '', v).split(".")]
return cmp(normalize(version1), normalize(version2)) return cmp(normalize(version1), normalize(version2))
def query_package(name,depot=None): def query_package(module, name, depot=None):
""" Returns whether a package is installed or not and version. """ """ Returns whether a package is installed or not and version. """
cmd_list = '/usr/sbin/swlist -a revision -l product' cmd_list = '/usr/sbin/swlist -a revision -l product'
if depot: if depot:
rc, stdout, stderr = _run("%s -s %s %s | grep %s" % (cmd_list, depot, name, name)) rc, stdout, stderr = module.run_command("%s -s %s %s | grep %s" % (cmd_list, depot, name, name))
else: else:
rc, stdout, stderr = _run("%s %s | grep %s" % (cmd_list, name, name)) rc, stdout, stderr = module.run_command("%s %s | grep %s" % (cmd_list, name, name))
if rc == 0: if rc == 0:
version = re.sub("\s\s+|\t" , " ", stdout).strip().split()[1] version = re.sub("\s\s+|\t" , " ", stdout).strip().split()[1]
else: else:
@ -94,22 +88,22 @@ def query_package(name,depot=None):
return rc, version return rc, version
def remove_package(name): def remove_package(module, name):
""" Uninstall package if installed. """ """ Uninstall package if installed. """
cmd_remove = '/usr/sbin/swremove' cmd_remove = '/usr/sbin/swremove'
rc, stdout, stderr = _run("%s %s" % (cmd_remove, name)) rc, stdout, stderr = module.run_command("%s %s" % (cmd_remove, name))
if rc == 0: if rc == 0:
return rc, stdout return rc, stdout
else: else:
return rc, stderr return rc, stderr
def install_package(depot, name): def install_package(module, depot, name):
""" Install package if not already installed """ """ Install package if not already installed """
cmd_install = '/usr/sbin/swinstall -x mount_all_filesystems=false' cmd_install = '/usr/sbin/swinstall -x mount_all_filesystems=false'
rc, stdout, stderr = _run("%s -s %s %s" % (cmd_install, depot, name)) rc, stdout, stderr = module.run_command("%s -s %s %s" % (cmd_install, depot, name))
if rc == 0: if rc == 0:
return rc, stdout return rc, stdout
else: else:
@ -129,15 +123,15 @@ def main():
depot = module.params['depot'] depot = module.params['depot']
changed = False changed = False
msg = 'No changed' msg = "No changed"
rc = 0 rc = 0
if ( state == "present" or state == "latest" ) and depot == None: if ( state == 'present' or state == 'latest' ) and depot == None:
output = "depot parameter is mandatory with present or latest task" output = "depot parameter is mandatory in present or latest task"
module.fail_json(name=name, msg=output, rc=rc) module.fail_json(name=name, msg=output, rc=rc)
#Check local version #Check local version
rc, version_installed = query_package(name) rc, version_installed = query_package(module, name)
if not rc: if not rc:
installed = True installed = True
msg = "Already installed" msg = "Already installed"
@ -148,7 +142,7 @@ def main():
if ( state == 'present' or state == 'latest' ) and installed == False: if ( state == 'present' or state == 'latest' ) and installed == False:
if module.check_mode: if module.check_mode:
module.exit_json(changed=True) module.exit_json(changed=True)
rc, output = install_package(depot, name) rc, output = install_package(module, depot, name)
if not rc: if not rc:
changed = True changed = True
@ -159,17 +153,17 @@ def main():
elif state == 'latest' and installed == True: elif state == 'latest' and installed == True:
#Check depot version #Check depot version
rc, version_depot = query_package(name,depot) rc, version_depot = query_package(module, name, depot)
if not rc: if not rc:
if compare_package(version_installed,version_depot) == -1: if compare_package(version_installed,version_depot) == -1:
if module.check_mode: if module.check_mode:
module.exit_json(changed=True) module.exit_json(changed=True)
#Install new version #Install new version
rc, output = install_package(depot, name) rc, output = install_package(module, depot, name)
if not rc: if not rc:
msg = 'Packge upgraded, Before ' + version_installed + " Now " + version_depot msg = "Packge upgraded, Before " + version_installed + " Now " + version_depot
changed = True changed = True
else: else:
@ -179,10 +173,10 @@ def main():
output = "Software package not in repository " + depot output = "Software package not in repository " + depot
module.fail_json(name=name, msg=output, rc=rc) module.fail_json(name=name, msg=output, rc=rc)
elif state == "absent" and installed == True: elif state == 'absent' and installed == True:
if module.check_mode: if module.check_mode:
module.exit_json(changed=True) module.exit_json(changed=True)
rc, output = remove_package(name) rc, output = remove_package(module, name)
if not rc: if not rc:
changed = True changed = True
msg = "Package removed" msg = "Package removed"