mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Made some code cleanups and use of module.run_command
This commit is contained in:
parent
a1cdb3110e
commit
71e52b38f5
1 changed files with 33 additions and 39 deletions
|
@ -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:
|
||||||
|
@ -76,17 +70,17 @@ def compare_package(version1, version2):
|
||||||
1 fisrt greater """
|
1 fisrt greater """
|
||||||
|
|
||||||
def normalize(v):
|
def normalize(v):
|
||||||
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:
|
||||||
|
@ -117,27 +111,27 @@ def install_package(depot, name):
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
argument_spec = dict(
|
argument_spec = dict(
|
||||||
name = dict(aliases=['pkg'],required=True),
|
name = dict(aliases=['pkg'], required=True),
|
||||||
state = dict(choices=['present','absent','latest'],required=True),
|
state = dict(choices=['present', 'absent', 'latest'], required=True),
|
||||||
depot = dict(default=None,requied=False)
|
depot = dict(default=None, requied=False)
|
||||||
),
|
),
|
||||||
supports_check_mode=True
|
supports_check_mode=True
|
||||||
)
|
)
|
||||||
name = module.params['name']
|
name = module.params['name']
|
||||||
state = module.params['state']
|
state = module.params['state']
|
||||||
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,51 +142,51 @@ 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
|
||||||
msg = "Packaged installed"
|
msg = "Packaged installed"
|
||||||
|
|
||||||
else:
|
else:
|
||||||
module.fail_json(name=name,msg=output,rc=rc)
|
module.fail_json(name=name, msg=output, rc=rc)
|
||||||
|
|
||||||
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:
|
||||||
module.fail_json(name=name,msg=output,rc=rc)
|
module.fail_json(name=name, msg=output, rc=rc)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
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"
|
||||||
else:
|
else:
|
||||||
module.fail_json(name=name,msg=output,rc=rc)
|
module.fail_json(name=name, msg=output, rc=rc)
|
||||||
|
|
||||||
if module.check_mode:
|
if module.check_mode:
|
||||||
module.exit_json(changed=False)
|
module.exit_json(changed=False)
|
||||||
|
|
||||||
module.exit_json(changed=changed,name=name,state=state,msg=msg)
|
module.exit_json(changed=changed, name=name, state=state, msg=msg)
|
||||||
|
|
||||||
# this is magic, see lib/ansible/module_common.py
|
# this is magic, see lib/ansible/module_common.py
|
||||||
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
||||||
|
|
Loading…
Reference in a new issue