mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-26 04:11:25 -07: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 = '''
|
||||||
|
@ -134,25 +120,26 @@ 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):
|
||||||
|
@ -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,6 +426,7 @@ 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)
|
||||||
|
@ -451,7 +437,7 @@ def core(module):
|
||||||
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)
|
||||||
|
@ -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(
|
||||||
|
name=dict(type='str', aliases=['guest']),
|
||||||
|
state=dict(type='str', choices=['destroyed', 'pause', 'running', 'shutdown']),
|
||||||
autostart=dict(type='bool'),
|
autostart=dict(type='bool'),
|
||||||
command = dict(choices=ALL_COMMANDS),
|
command=dict(type='str', choices=ALL_COMMANDS),
|
||||||
uri = dict(default='qemu:///system'),
|
uri=dict(type='str', default='qemu:///system'),
|
||||||
xml = dict(),
|
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:
|
||||||
|
|
|
@ -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
Add a link
Reference in a new issue