mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
virt: PEP8 compliancy and doc fixes (#30917)
This PR includes: - PEP8 compliancy fixes - Documentation fixes
This commit is contained in:
parent
54d7c384b6
commit
0ef87c849f
2 changed files with 74 additions and 89 deletions
|
@ -1,21 +1,18 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
# Copyright 2007, 2012 Red Hat, Inc
|
# Copyright: (c) 2007, 2012 Red Hat, Inc
|
||||||
# Michael DeHaan <michael.dehaan@gmail.com>
|
# Michael DeHaan <michael.dehaan@gmail.com>
|
||||||
# Seth Vidal <skvidal@fedoraproject.org>
|
# Seth Vidal <skvidal@fedoraproject.org>
|
||||||
#
|
|
||||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
|
||||||
from __future__ import absolute_import, division, print_function
|
from __future__ import absolute_import, division, print_function
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
|
|
||||||
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
||||||
'status': ['preview'],
|
'status': ['preview'],
|
||||||
'supported_by': 'community'}
|
'supported_by': 'community'}
|
||||||
|
|
||||||
|
|
||||||
DOCUMENTATION = '''
|
DOCUMENTATION = '''
|
||||||
---
|
---
|
||||||
module: virt
|
module: virt
|
||||||
|
@ -29,46 +26,35 @@ options:
|
||||||
- name of the guest VM being managed. Note that VM must be previously
|
- name of the guest VM being managed. Note that VM must be previously
|
||||||
defined with xml.
|
defined with xml.
|
||||||
required: true
|
required: true
|
||||||
default: null
|
|
||||||
aliases: []
|
|
||||||
state:
|
state:
|
||||||
description:
|
description:
|
||||||
- Note that there may be some lag for state requests like C(shutdown)
|
- Note that there may be some lag for state requests like C(shutdown)
|
||||||
since these refer only to VM states. After starting a guest, it may not
|
since these refer only to VM states. After starting a guest, it may not
|
||||||
be immediately accessible.
|
be immediately accessible.
|
||||||
required: false
|
choices: [ destroyed, paused, running, shutdown ]
|
||||||
choices: [ "running", "shutdown", "destroyed", "paused" ]
|
|
||||||
default: "no"
|
|
||||||
command:
|
command:
|
||||||
description:
|
description:
|
||||||
- in addition to state management, various non-idempotent commands are available. See examples
|
- In addition to state management, various non-idempotent commands are available.
|
||||||
required: false
|
choices: [ create, define, destroy, freemem, get_xml, info, list_vms, nodeinfo, pause, shutdown, start, status, stop, undefine, unpause, virttype ]
|
||||||
choices: ["create","status", "start", "stop", "pause", "unpause",
|
|
||||||
"shutdown", "undefine", "destroy", "get_xml",
|
|
||||||
"freemem", "list_vms", "info", "nodeinfo", "virttype", "define"]
|
|
||||||
autostart:
|
autostart:
|
||||||
description:
|
description:
|
||||||
- start VM at host startup
|
- start VM at host startup.
|
||||||
choices: [True, False]
|
type: bool
|
||||||
version_added: "2.3"
|
version_added: "2.3"
|
||||||
default: null
|
|
||||||
uri:
|
uri:
|
||||||
description:
|
description:
|
||||||
- libvirt connection uri
|
- libvirt connection uri.
|
||||||
required: false
|
|
||||||
default: qemu:///system
|
default: qemu:///system
|
||||||
xml:
|
xml:
|
||||||
description:
|
description:
|
||||||
- XML document used with the define command
|
- XML document used with the define command.
|
||||||
required: false
|
|
||||||
default: null
|
|
||||||
requirements:
|
requirements:
|
||||||
- "python >= 2.6"
|
- python >= 2.6
|
||||||
- "libvirt-python"
|
- libvirt-python
|
||||||
author:
|
author:
|
||||||
- "Ansible Core Team"
|
- Ansible Core Team
|
||||||
- "Michael DeHaan"
|
- Michael DeHaan
|
||||||
- "Seth Vidal"
|
- Seth Vidal
|
||||||
'''
|
'''
|
||||||
|
|
||||||
EXAMPLES = '''
|
EXAMPLES = '''
|
||||||
|
@ -131,28 +117,29 @@ from ansible.module_utils._text import to_native
|
||||||
|
|
||||||
VIRT_FAILED = 1
|
VIRT_FAILED = 1
|
||||||
VIRT_SUCCESS = 0
|
VIRT_SUCCESS = 0
|
||||||
VIRT_UNAVAILABLE=2
|
VIRT_UNAVAILABLE = 2
|
||||||
|
|
||||||
ALL_COMMANDS = []
|
ALL_COMMANDS = []
|
||||||
VM_COMMANDS = ['create','status', 'start', 'stop', 'pause', 'unpause',
|
VM_COMMANDS = ['create', 'define', 'destroy', 'get_xml', 'pause', 'shutdown', 'status', 'start', 'stop' 'undefine', 'unpause']
|
||||||
'shutdown', 'undefine', 'destroy', 'get_xml', 'define']
|
HOST_COMMANDS = ['freemem', 'info', 'list_vms', 'nodeinfo', 'virttype']
|
||||||
HOST_COMMANDS = ['freemem', 'list_vms', 'info', 'nodeinfo', 'virttype']
|
|
||||||
ALL_COMMANDS.extend(VM_COMMANDS)
|
ALL_COMMANDS.extend(VM_COMMANDS)
|
||||||
ALL_COMMANDS.extend(HOST_COMMANDS)
|
ALL_COMMANDS.extend(HOST_COMMANDS)
|
||||||
|
|
||||||
VIRT_STATE_NAME_MAP = {
|
VIRT_STATE_NAME_MAP = {
|
||||||
0 : "running",
|
0: 'running',
|
||||||
1 : "running",
|
1: 'running',
|
||||||
2 : "running",
|
2: 'running',
|
||||||
3 : "paused",
|
3: 'paused',
|
||||||
4 : "shutdown",
|
4: 'shutdown',
|
||||||
5 : "shutdown",
|
5: 'shutdown',
|
||||||
6 : "crashed"
|
6: 'crashed',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class VMNotFound(Exception):
|
class VMNotFound(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class LibvirtConnection(object):
|
class LibvirtConnection(object):
|
||||||
|
|
||||||
def __init__(self, uri, module):
|
def __init__(self, uri, module):
|
||||||
|
@ -230,11 +217,11 @@ class LibvirtConnection(object):
|
||||||
|
|
||||||
def get_status2(self, vm):
|
def get_status2(self, vm):
|
||||||
state = vm.info()[0]
|
state = vm.info()[0]
|
||||||
return VIRT_STATE_NAME_MAP.get(state,"unknown")
|
return VIRT_STATE_NAME_MAP.get(state, "unknown")
|
||||||
|
|
||||||
def get_status(self, vmid):
|
def get_status(self, vmid):
|
||||||
state = self.find_vm(vmid).info()[0]
|
state = self.find_vm(vmid).info()[0]
|
||||||
return VIRT_STATE_NAME_MAP.get(state,"unknown")
|
return VIRT_STATE_NAME_MAP.get(state, "unknown")
|
||||||
|
|
||||||
def nodeinfo(self):
|
def nodeinfo(self):
|
||||||
return self.conn.getInfo()
|
return self.conn.getInfo()
|
||||||
|
@ -288,7 +275,7 @@ class Virt(object):
|
||||||
state = []
|
state = []
|
||||||
for vm in vms:
|
for vm in vms:
|
||||||
state_blurb = self.conn.get_status(vm)
|
state_blurb = self.conn.get_status(vm)
|
||||||
state.append("%s %s" % (vm,state_blurb))
|
state.append("%s %s" % (vm, state_blurb))
|
||||||
return state
|
return state
|
||||||
|
|
||||||
def info(self):
|
def info(self):
|
||||||
|
@ -301,31 +288,30 @@ class Virt(object):
|
||||||
# This throws exceptions, so convert them to strings here and
|
# This throws exceptions, so convert them to strings here and
|
||||||
# assume the other end of the xmlrpc connection can figure things
|
# assume the other end of the xmlrpc connection can figure things
|
||||||
# out or doesn't care.
|
# out or doesn't care.
|
||||||
info[vm] = {
|
info[vm] = dict(
|
||||||
"state" : VIRT_STATE_NAME_MAP.get(data[0],"unknown"),
|
state=VIRT_STATE_NAME_MAP.get(data[0], "unknown"),
|
||||||
"maxMem" : str(data[1]),
|
maxMem=str(data[1]),
|
||||||
"memory" : str(data[2]),
|
memory=str(data[2]),
|
||||||
"nrVirtCpu" : data[3],
|
nrVirtCpu=data[3],
|
||||||
"cpuTime" : str(data[4]),
|
cpuTime=str(data[4]),
|
||||||
}
|
autostart=self.conn.get_autostart(vm),
|
||||||
info[vm]["autostart"] = self.conn.get_autostart(vm)
|
)
|
||||||
|
|
||||||
return info
|
return info
|
||||||
|
|
||||||
def nodeinfo(self):
|
def nodeinfo(self):
|
||||||
self.__get_conn()
|
self.__get_conn()
|
||||||
info = dict()
|
|
||||||
data = self.conn.nodeinfo()
|
data = self.conn.nodeinfo()
|
||||||
info = {
|
info = dict(
|
||||||
"cpumodel" : str(data[0]),
|
cpumodel=str(data[0]),
|
||||||
"phymemory" : str(data[1]),
|
phymemory=str(data[1]),
|
||||||
"cpus" : str(data[2]),
|
cpus=str(data[2]),
|
||||||
"cpumhz" : str(data[3]),
|
cpumhz=str(data[3]),
|
||||||
"numanodes" : str(data[4]),
|
numanodes=str(data[4]),
|
||||||
"sockets" : str(data[5]),
|
sockets=str(data[5]),
|
||||||
"cpucores" : str(data[6]),
|
cpucores=str(data[6]),
|
||||||
"cputhreads" : str(data[7])
|
cputhreads=str(data[7])
|
||||||
}
|
)
|
||||||
return info
|
return info
|
||||||
|
|
||||||
def list_vms(self, state=None):
|
def list_vms(self, state=None):
|
||||||
|
@ -366,7 +352,6 @@ class Virt(object):
|
||||||
self.conn.shutdown(vmid)
|
self.conn.shutdown(vmid)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def pause(self, vmid):
|
def pause(self, vmid):
|
||||||
""" Pause the machine with the given vmid. """
|
""" Pause the machine with the given vmid. """
|
||||||
|
|
||||||
|
@ -441,27 +426,28 @@ class Virt(object):
|
||||||
self.__get_conn()
|
self.__get_conn()
|
||||||
return self.conn.define_from_xml(xml)
|
return self.conn.define_from_xml(xml)
|
||||||
|
|
||||||
|
|
||||||
def core(module):
|
def core(module):
|
||||||
|
|
||||||
state = module.params.get('state', None)
|
state = module.params.get('state', None)
|
||||||
autostart = module.params.get('autostart', None)
|
autostart = module.params.get('autostart', None)
|
||||||
guest = module.params.get('name', None)
|
guest = module.params.get('name', None)
|
||||||
command = module.params.get('command', None)
|
command = module.params.get('command', None)
|
||||||
uri = module.params.get('uri', None)
|
uri = module.params.get('uri', None)
|
||||||
xml = module.params.get('xml', None)
|
xml = module.params.get('xml', None)
|
||||||
|
|
||||||
v = Virt(uri, module)
|
v = Virt(uri, module)
|
||||||
res = {}
|
res = dict()
|
||||||
|
|
||||||
if state and command=='list_vms':
|
if state and command == 'list_vms':
|
||||||
res = v.list_vms(state=state)
|
res = v.list_vms(state=state)
|
||||||
if not isinstance(res, dict):
|
if not isinstance(res, dict):
|
||||||
res = { command: res }
|
res = {command: res}
|
||||||
return VIRT_SUCCESS, res
|
return VIRT_SUCCESS, res
|
||||||
|
|
||||||
if state:
|
if state:
|
||||||
if not guest:
|
if not guest:
|
||||||
module.fail_json(msg = "state change requires a guest specified")
|
module.fail_json(msg="state change requires a guest specified")
|
||||||
|
|
||||||
if state == 'running':
|
if state == 'running':
|
||||||
if v.status(guest) is 'paused':
|
if v.status(guest) is 'paused':
|
||||||
|
@ -493,10 +479,10 @@ def core(module):
|
||||||
if command:
|
if command:
|
||||||
if command in VM_COMMANDS:
|
if command in VM_COMMANDS:
|
||||||
if not guest:
|
if not guest:
|
||||||
module.fail_json(msg = "%s requires 1 argument: guest" % command)
|
module.fail_json(msg="%s requires 1 argument: guest" % command)
|
||||||
if command == 'define':
|
if command == 'define':
|
||||||
if not xml:
|
if not xml:
|
||||||
module.fail_json(msg = "define requires xml argument")
|
module.fail_json(msg="define requires xml argument")
|
||||||
try:
|
try:
|
||||||
v.get_vm(guest)
|
v.get_vm(guest)
|
||||||
except VMNotFound:
|
except VMNotFound:
|
||||||
|
@ -505,13 +491,13 @@ def core(module):
|
||||||
return VIRT_SUCCESS, res
|
return VIRT_SUCCESS, res
|
||||||
res = getattr(v, command)(guest)
|
res = getattr(v, command)(guest)
|
||||||
if not isinstance(res, dict):
|
if not isinstance(res, dict):
|
||||||
res = { command: res }
|
res = {command: res}
|
||||||
return VIRT_SUCCESS, res
|
return VIRT_SUCCESS, res
|
||||||
|
|
||||||
elif hasattr(v, command):
|
elif hasattr(v, command):
|
||||||
res = getattr(v, command)()
|
res = getattr(v, command)()
|
||||||
if not isinstance(res, dict):
|
if not isinstance(res, dict):
|
||||||
res = { command: res }
|
res = {command: res}
|
||||||
return VIRT_SUCCESS, res
|
return VIRT_SUCCESS, res
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
@ -519,21 +505,21 @@ def core(module):
|
||||||
|
|
||||||
module.fail_json(msg="expected state or command parameter to be specified")
|
module.fail_json(msg="expected state or command parameter to be specified")
|
||||||
|
|
||||||
def main():
|
|
||||||
|
|
||||||
module = AnsibleModule(argument_spec=dict(
|
def main():
|
||||||
name = dict(aliases=['guest']),
|
module = AnsibleModule(
|
||||||
state = dict(choices=['running', 'shutdown', 'destroyed', 'paused']),
|
argument_spec=dict(
|
||||||
autostart = dict(type='bool'),
|
name=dict(type='str', aliases=['guest']),
|
||||||
command = dict(choices=ALL_COMMANDS),
|
state=dict(type='str', choices=['destroyed', 'pause', 'running', 'shutdown']),
|
||||||
uri = dict(default='qemu:///system'),
|
autostart=dict(type='bool'),
|
||||||
xml = dict(),
|
command=dict(type='str', choices=ALL_COMMANDS),
|
||||||
))
|
uri=dict(type='str', default='qemu:///system'),
|
||||||
|
xml=dict(type='str'),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
if not HAS_VIRT:
|
if not HAS_VIRT:
|
||||||
module.fail_json(
|
module.fail_json(msg='The `libvirt` module is not importable. Check the requirements.')
|
||||||
msg='The `libvirt` module is not importable. Check the requirements.'
|
|
||||||
)
|
|
||||||
|
|
||||||
rc = VIRT_SUCCESS
|
rc = VIRT_SUCCESS
|
||||||
try:
|
try:
|
||||||
|
@ -541,7 +527,7 @@ def main():
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
||||||
|
|
||||||
if rc != 0: # something went wrong emit the msg
|
if rc != 0: # something went wrong emit the msg
|
||||||
module.fail_json(rc=rc, msg=result)
|
module.fail_json(rc=rc, msg=result)
|
||||||
else:
|
else:
|
||||||
module.exit_json(**result)
|
module.exit_json(**result)
|
||||||
|
|
|
@ -102,7 +102,6 @@ lib/ansible/modules/cloud/lxd/lxd_profile.py
|
||||||
lib/ansible/modules/cloud/misc/ovirt.py
|
lib/ansible/modules/cloud/misc/ovirt.py
|
||||||
lib/ansible/modules/cloud/misc/rhevm.py
|
lib/ansible/modules/cloud/misc/rhevm.py
|
||||||
lib/ansible/modules/cloud/misc/serverless.py
|
lib/ansible/modules/cloud/misc/serverless.py
|
||||||
lib/ansible/modules/cloud/misc/virt.py
|
|
||||||
lib/ansible/modules/cloud/misc/virt_net.py
|
lib/ansible/modules/cloud/misc/virt_net.py
|
||||||
lib/ansible/modules/cloud/misc/virt_pool.py
|
lib/ansible/modules/cloud/misc/virt_pool.py
|
||||||
lib/ansible/modules/cloud/misc/xenserver_facts.py
|
lib/ansible/modules/cloud/misc/xenserver_facts.py
|
||||||
|
|
Loading…
Add table
Reference in a new issue