virt: PEP8 compliancy and doc fixes (#30917)

This PR includes:
- PEP8 compliancy fixes
- Documentation fixes
pull/31033/head
Dag Wieers 7 years ago committed by ansibot
parent 54d7c384b6
commit 0ef87c849f

@ -1,21 +1,18 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright 2007, 2012 Red Hat, Inc
# Copyright: (c) 2007, 2012 Red Hat, Inc
# Michael DeHaan <michael.dehaan@gmail.com>
# Seth Vidal <skvidal@fedoraproject.org>
#
# 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
__metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}
DOCUMENTATION = '''
---
module: virt
@ -29,46 +26,35 @@ options:
- name of the guest VM being managed. Note that VM must be previously
defined with xml.
required: true
default: null
aliases: []
state:
description:
- 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
be immediately accessible.
required: false
choices: [ "running", "shutdown", "destroyed", "paused" ]
default: "no"
choices: [ destroyed, paused, running, shutdown ]
command:
description:
- in addition to state management, various non-idempotent commands are available. See examples
required: false
choices: ["create","status", "start", "stop", "pause", "unpause",
"shutdown", "undefine", "destroy", "get_xml",
"freemem", "list_vms", "info", "nodeinfo", "virttype", "define"]
- In addition to state management, various non-idempotent commands are available.
choices: [ create, define, destroy, freemem, get_xml, info, list_vms, nodeinfo, pause, shutdown, start, status, stop, undefine, unpause, virttype ]
autostart:
description:
- start VM at host startup
choices: [True, False]
- start VM at host startup.
type: bool
version_added: "2.3"
default: null
uri:
description:
- libvirt connection uri
required: false
- libvirt connection uri.
default: qemu:///system
xml:
description:
- XML document used with the define command
required: false
default: null
- XML document used with the define command.
requirements:
- "python >= 2.6"
- "libvirt-python"
- python >= 2.6
- libvirt-python
author:
- "Ansible Core Team"
- "Michael DeHaan"
- "Seth Vidal"
- Ansible Core Team
- Michael DeHaan
- Seth Vidal
'''
EXAMPLES = '''
@ -134,25 +120,26 @@ VIRT_SUCCESS = 0
VIRT_UNAVAILABLE = 2
ALL_COMMANDS = []
VM_COMMANDS = ['create','status', 'start', 'stop', 'pause', 'unpause',
'shutdown', 'undefine', 'destroy', 'get_xml', 'define']
HOST_COMMANDS = ['freemem', 'list_vms', 'info', 'nodeinfo', 'virttype']
VM_COMMANDS = ['create', 'define', 'destroy', 'get_xml', 'pause', 'shutdown', 'status', 'start', 'stop' 'undefine', 'unpause']
HOST_COMMANDS = ['freemem', 'info', 'list_vms', 'nodeinfo', 'virttype']
ALL_COMMANDS.extend(VM_COMMANDS)
ALL_COMMANDS.extend(HOST_COMMANDS)
VIRT_STATE_NAME_MAP = {
0 : "running",
1 : "running",
2 : "running",
3 : "paused",
4 : "shutdown",
5 : "shutdown",
6 : "crashed"
0: 'running',
1: 'running',
2: 'running',
3: 'paused',
4: 'shutdown',
5: 'shutdown',
6: 'crashed',
}
class VMNotFound(Exception):
pass
class LibvirtConnection(object):
def __init__(self, uri, module):
@ -301,31 +288,30 @@ class Virt(object):
# This throws exceptions, so convert them to strings here and
# assume the other end of the xmlrpc connection can figure things
# out or doesn't care.
info[vm] = {
"state" : VIRT_STATE_NAME_MAP.get(data[0],"unknown"),
"maxMem" : str(data[1]),
"memory" : str(data[2]),
"nrVirtCpu" : data[3],
"cpuTime" : str(data[4]),
}
info[vm]["autostart"] = self.conn.get_autostart(vm)
info[vm] = dict(
state=VIRT_STATE_NAME_MAP.get(data[0], "unknown"),
maxMem=str(data[1]),
memory=str(data[2]),
nrVirtCpu=data[3],
cpuTime=str(data[4]),
autostart=self.conn.get_autostart(vm),
)
return info
def nodeinfo(self):
self.__get_conn()
info = dict()
data = self.conn.nodeinfo()
info = {
"cpumodel" : str(data[0]),
"phymemory" : str(data[1]),
"cpus" : str(data[2]),
"cpumhz" : str(data[3]),
"numanodes" : str(data[4]),
"sockets" : str(data[5]),
"cpucores" : str(data[6]),
"cputhreads" : str(data[7])
}
info = dict(
cpumodel=str(data[0]),
phymemory=str(data[1]),
cpus=str(data[2]),
cpumhz=str(data[3]),
numanodes=str(data[4]),
sockets=str(data[5]),
cpucores=str(data[6]),
cputhreads=str(data[7])
)
return info
def list_vms(self, state=None):
@ -366,7 +352,6 @@ class Virt(object):
self.conn.shutdown(vmid)
return 0
def pause(self, vmid):
""" Pause the machine with the given vmid. """
@ -441,6 +426,7 @@ class Virt(object):
self.__get_conn()
return self.conn.define_from_xml(xml)
def core(module):
state = module.params.get('state', None)
@ -451,7 +437,7 @@ def core(module):
xml = module.params.get('xml', None)
v = Virt(uri, module)
res = {}
res = dict()
if state and command == 'list_vms':
res = v.list_vms(state=state)
@ -519,21 +505,21 @@ def core(module):
module.fail_json(msg="expected state or command parameter to be specified")
def main():
module = AnsibleModule(argument_spec=dict(
name = dict(aliases=['guest']),
state = dict(choices=['running', 'shutdown', 'destroyed', 'paused']),
def main():
module = AnsibleModule(
argument_spec=dict(
name=dict(type='str', aliases=['guest']),
state=dict(type='str', choices=['destroyed', 'pause', 'running', 'shutdown']),
autostart=dict(type='bool'),
command = dict(choices=ALL_COMMANDS),
uri = dict(default='qemu:///system'),
xml = dict(),
))
command=dict(type='str', choices=ALL_COMMANDS),
uri=dict(type='str', default='qemu:///system'),
xml=dict(type='str'),
),
)
if not HAS_VIRT:
module.fail_json(
msg='The `libvirt` module is not importable. Check the requirements.'
)
module.fail_json(msg='The `libvirt` module is not importable. Check the requirements.')
rc = VIRT_SUCCESS
try:

@ -102,7 +102,6 @@ lib/ansible/modules/cloud/lxd/lxd_profile.py
lib/ansible/modules/cloud/misc/ovirt.py
lib/ansible/modules/cloud/misc/rhevm.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_pool.py
lib/ansible/modules/cloud/misc/xenserver_facts.py

Loading…
Cancel
Save