Misc fixes and Pep8 fixes for proxmox_* modules (#24162)

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
pull/24144/head
Abhijeet Kasurde 8 years ago committed by Matt Martz
parent cdca648f15
commit d8f76bed97

@ -309,6 +309,9 @@ EXAMPLES = '''
import os
import time
# import module snippets
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.pycompat24 import get_exception
try:
from proxmoxer import ProxmoxAPI
@ -318,25 +321,32 @@ except ImportError:
VZ_TYPE = None
def get_nextvmid(proxmox):
def get_nextvmid(module, proxmox):
try:
vmid = proxmox.cluster.nextid.get()
return vmid
except Exception as e:
module.fail_json(msg="Unable to get next vmid. Failed with exception: %s")
except Exception:
exc = get_exception()
module.fail_json(msg="Unable to get next vmid. Failed with exception: %s" % exc)
def get_vmid(proxmox, hostname):
return [vm['vmid'] for vm in proxmox.cluster.resources.get(type='vm') if vm['name'] == hostname]
def get_instance(proxmox, vmid):
return [vm for vm in proxmox.cluster.resources.get(type='vm') if vm['vmid'] == int(vmid)]
def content_check(proxmox, node, ostemplate, template_store):
return [True for cnt in proxmox.nodes(node).storage(template_store).content.get() if cnt['volid'] == ostemplate]
def node_check(proxmox, node):
return [True for nd in proxmox.nodes.get() if nd['node'] == node]
def create_instance(module, proxmox, vmid, node, disk, storage, cpus, memory, swap, timeout, **kwargs):
proxmox_node = proxmox.nodes(node)
kwargs = dict((k, v) for k, v in kwargs.items() if v is not None)
@ -364,7 +374,7 @@ def create_instance(module, proxmox, vmid, node, disk, storage, cpus, memory, sw
if (proxmox_node.tasks(taskid).status.get()['status'] == 'stopped' and
proxmox_node.tasks(taskid).status.get()['exitstatus'] == 'OK'):
return True
timeout = timeout - 1
timeout -= 1
if timeout == 0:
module.fail_json(msg='Reached timeout while waiting for creating VM. Last line in task before timeout: %s' %
proxmox_node.tasks(taskid).log.get()[:1])
@ -372,13 +382,14 @@ def create_instance(module, proxmox, vmid, node, disk, storage, cpus, memory, sw
time.sleep(1)
return False
def start_instance(module, proxmox, vm, vmid, timeout):
taskid = getattr(proxmox.nodes(vm[0]['node']), VZ_TYPE)(vmid).status.start.post()
while timeout:
if (proxmox.nodes(vm[0]['node']).tasks(taskid).status.get()['status'] == 'stopped' and
proxmox.nodes(vm[0]['node']).tasks(taskid).status.get()['exitstatus'] == 'OK'):
return True
timeout = timeout - 1
timeout -= 1
if timeout == 0:
module.fail_json(msg='Reached timeout while waiting for starting VM. Last line in task before timeout: %s' %
proxmox.nodes(vm[0]['node']).tasks(taskid).log.get()[:1])
@ -386,6 +397,7 @@ def start_instance(module, proxmox, vm, vmid, timeout):
time.sleep(1)
return False
def stop_instance(module, proxmox, vm, vmid, timeout, force):
if force:
taskid = getattr(proxmox.nodes(vm[0]['node']), VZ_TYPE)(vmid).status.shutdown.post(forceStop=1)
@ -395,28 +407,30 @@ def stop_instance(module, proxmox, vm, vmid, timeout, force):
if (proxmox.nodes(vm[0]['node']).tasks(taskid).status.get()['status'] == 'stopped' and
proxmox.nodes(vm[0]['node']).tasks(taskid).status.get()['exitstatus'] == 'OK'):
return True
timeout = timeout - 1
timeout -= 1
if timeout == 0:
module.fail_json(msg='Reached timeout while waiting for stopping VM. Last line in task before timeout: %s' %
proxmox_node.tasks(taskid).log.get()[:1])
proxmox.nodes(vm[0]['node']).tasks(taskid).log.get()[:1])
time.sleep(1)
return False
def umount_instance(module, proxmox, vm, vmid, timeout):
taskid = getattr(proxmox.nodes(vm[0]['node']), VZ_TYPE)(vmid).status.umount.post()
while timeout:
if (proxmox.nodes(vm[0]['node']).tasks(taskid).status.get()['status'] == 'stopped' and
proxmox.nodes(vm[0]['node']).tasks(taskid).status.get()['exitstatus'] == 'OK'):
return True
timeout = timeout - 1
timeout -= 1
if timeout == 0:
module.fail_json(msg='Reached timeout while waiting for unmounting VM. Last line in task before timeout: %s' %
proxmox_node.tasks(taskid).log.get()[:1])
proxmox.nodes(vm[0]['node']).tasks(taskid).log.get()[:1])
time.sleep(1)
return False
def main():
module = AnsibleModule(
argument_spec=dict(
@ -488,7 +502,7 @@ def main():
# If vmid not set get the Next VM id from ProxmoxAPI
# If hostname is set get the VM id from ProxmoxAPI
if not vmid and state == 'present':
vmid = get_nextvmid(proxmox)
vmid = get_nextvmid(module, proxmox)
elif not vmid and hostname:
vmid = get_vmid(proxmox, hostname)[0]
elif not vmid:
@ -596,17 +610,15 @@ def main():
if (proxmox.nodes(vm[0]['node']).tasks(taskid).status.get()['status'] == 'stopped' and
proxmox.nodes(vm[0]['node']).tasks(taskid).status.get()['exitstatus'] == 'OK'):
module.exit_json(changed=True, msg="VM %s removed" % vmid)
timeout = timeout - 1
timeout -= 1
if timeout == 0:
module.fail_json(msg='Reached timeout while waiting for removing VM. Last line in task before timeout: %s'
% proxmox_node.tasks(taskid).log.get()[:1])
% proxmox.nodes(vm[0]['node']).tasks(taskid).log.get()[:1])
time.sleep(1)
except Exception as e:
module.fail_json(msg="deletion of VM %s failed with exception: %s" % (vmid, e))
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__':
main()

@ -704,8 +704,11 @@ status:
'''
import os
import re
import time
# import module snippets
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.pycompat24 import get_exception
try:
from proxmoxer import ProxmoxAPI
@ -716,12 +719,13 @@ except ImportError:
VZ_TYPE = 'qemu'
def get_nextvmid(proxmox):
def get_nextvmid(module, proxmox):
try:
vmid = proxmox.cluster.nextid.get()
return vmid
except Exception as e:
module.fail_json(msg="Unable to get next vmid. Failed with exception: %s")
except Exception:
exc = get_exception()
module.fail_json(msg="Unable to get next vmid. Failed with exception: %s" % exc)
def get_vmid(proxmox, name):
@ -878,7 +882,7 @@ def start_vm(module, proxmox, vm, vmid, timeout):
if (proxmox.nodes(vm[0]['node']).tasks(taskid).status.get()['status'] == 'stopped' and
proxmox.nodes(vm[0]['node']).tasks(taskid).status.get()['exitstatus'] == 'OK'):
return True
timeout = timeout - 1
timeout -= 1
if timeout == 0:
module.fail_json(msg='Reached timeout while waiting for starting VM. Last line in task before timeout: %s'
% proxmox.nodes(vm[0]['node']).tasks(taskid).log.get()[:1])
@ -896,7 +900,7 @@ def stop_vm(module, proxmox, vm, vmid, timeout, force):
if (proxmox.nodes(vm[0]['node']).tasks(taskid).status.get()['status'] == 'stopped' and
proxmox.nodes(vm[0]['node']).tasks(taskid).status.get()['exitstatus'] == 'OK'):
return True
timeout = timeout - 1
timeout -= 1
if timeout == 0:
module.fail_json(msg='Reached timeout while waiting for stopping VM. Last line in task before timeout: %s'
% proxmox.nodes(vm[0]['node']).tasks(taskid).log.get()[:1])
@ -1028,7 +1032,7 @@ def main():
if not vmid:
if state == 'present' and (not update and not clone) and (not delete and not revert):
try:
vmid = get_nextvmid(proxmox)
vmid = get_nextvmid(module, proxmox)
except Exception as e:
module.fail_json(msg="Can't get the next vimd for VM {} automatically. Ensure your cluster state is good".format(name))
else:
@ -1052,7 +1056,7 @@ def main():
module.fail_json(msg='VM with vmid = %s does not exist in cluster' % vmid)
if not newid:
try:
newid = get_nextvmid(proxmox)
newid = get_nextvmid(module, proxmox)
except Exception as e:
module.fail_json(msg="Can't get the next vimd for VM {} automatically. Ensure your cluster state is good".format(name))
else:
@ -1193,8 +1197,7 @@ def main():
if getattr(proxmox.nodes(vm[0]['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'stopped':
module.exit_json(changed=False, msg="VM %s is not running" % vmid)
if (stop_vm(module, proxmox, vm, vmid, timeout, force = module.params['force'])
and start_vm(module, proxmox, vm, vmid, timeout)):
if stop_vm(module, proxmox, vm, vmid, timeout, force=module.params['force']) and start_vm(module, proxmox, vm, vmid, timeout):
module.exit_json(changed=True, msg="VM %s is restarted" % vmid)
except Exception as e:
module.fail_json(msg="restarting of VM %s failed with exception: %s" % (vmid, e))
@ -1213,10 +1216,10 @@ def main():
if (proxmox.nodes(vm[0]['node']).tasks(taskid).status.get()['status'] == 'stopped' and
proxmox.nodes(vm[0]['node']).tasks(taskid).status.get()['exitstatus'] == 'OK'):
module.exit_json(changed=True, msg="VM %s removed" % vmid)
timeout = timeout - 1
timeout -= 1
if timeout == 0:
module.fail_json(msg='Reached timeout while waiting for removing VM. Last line in task before timeout: %s'
% proxmox_node.tasks(taskid).log.get()[:1])
% proxmox.nodes(vm[0]['node']).tasks(taskid).log.get()[:1])
time.sleep(1)
except Exception as e:
@ -1236,7 +1239,5 @@ def main():
module.fail_json(msg="Unable to get vm {} with vmid = {} status: ".format(name, vmid) + str(e))
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__':
main()

@ -138,6 +138,8 @@ EXAMPLES = '''
import os
import time
# import module snippets
from ansible.module_utils.basic import AnsibleModule
try:
from proxmoxer import ProxmoxAPI
@ -145,10 +147,12 @@ try:
except ImportError:
HAS_PROXMOXER = False
def get_template(proxmox, node, storage, content_type, template):
return [True for tmpl in proxmox.nodes(node).storage(storage).content.get()
if tmpl['volid'] == '%s:%s/%s' % (storage, content_type, template)]
def upload_template(module, proxmox, api_host, node, storage, content_type, realpath, timeout):
taskid = proxmox.nodes(node).storage(storage).upload.post(content=content_type, filename=open(realpath))
while timeout:
@ -163,6 +167,7 @@ def upload_template(module, proxmox, api_host, node, storage, content_type, real
time.sleep(1)
return False
def delete_template(module, proxmox, node, storage, content_type, template, timeout):
volid = '%s:%s/%s' % (storage, content_type, template)
proxmox.nodes(node).storage(storage).content.delete(volid)
@ -176,6 +181,7 @@ def delete_template(module, proxmox, node, storage, content_type, template, time
time.sleep(1)
return False
def main():
module = AnsibleModule(
argument_spec=dict(
@ -253,8 +259,6 @@ def main():
except Exception as e:
module.fail_json(msg="deleting of template %s failed with exception: %s" % (template, e))
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__':
main()

@ -284,9 +284,6 @@ lib/ansible/modules/cloud/lxc/lxc_container.py
lib/ansible/modules/cloud/lxd/lxd_container.py
lib/ansible/modules/cloud/lxd/lxd_profile.py
lib/ansible/modules/cloud/misc/ovirt.py
lib/ansible/modules/cloud/misc/proxmox.py
lib/ansible/modules/cloud/misc/proxmox_kvm.py
lib/ansible/modules/cloud/misc/proxmox_template.py
lib/ansible/modules/cloud/misc/rhevm.py
lib/ansible/modules/cloud/misc/serverless.py
lib/ansible/modules/cloud/misc/virt.py

Loading…
Cancel
Save