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
parent e635a1ee5a
commit b8cba01800

View file

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