mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 13:52:54 +00:00
Bulk autopep8 (modules)
As agreed in 2017-12-07 Core meeting bulk fix pep8 issues Generated using: autopep8 1.3.3 (pycodestyle: 2.3.1) autopep8 -r --max-line-length 160 --in-place --ignore E305,E402,E722,E741 lib/ansible/modules Manually fix issues that autopep8 has introduced
This commit is contained in:
committed by
John R Barker
parent
d13d7e9404
commit
c57a7f05e1
@@ -347,6 +347,7 @@ failed = False
|
||||
|
||||
class RHEVConn(object):
|
||||
'Connection to RHEV-M'
|
||||
|
||||
def __init__(self, module):
|
||||
self.module = module
|
||||
|
||||
@@ -726,11 +727,11 @@ class RHEVConn(object):
|
||||
bond.append(ifacelist[slave])
|
||||
try:
|
||||
tmpiface = params.Bonding(
|
||||
slaves = params.Slaves(host_nic = bond),
|
||||
options = params.Options(
|
||||
option = [
|
||||
params.Option(name = 'miimon', value = '100'),
|
||||
params.Option(name = 'mode', value = '4')
|
||||
slaves=params.Slaves(host_nic=bond),
|
||||
options=params.Options(
|
||||
option=[
|
||||
params.Option(name='miimon', value='100'),
|
||||
params.Option(name='mode', value='4')
|
||||
]
|
||||
)
|
||||
)
|
||||
@@ -741,16 +742,16 @@ class RHEVConn(object):
|
||||
return False
|
||||
try:
|
||||
tmpnetwork = params.HostNIC(
|
||||
network = params.Network(name = iface['network']),
|
||||
name = iface['name'],
|
||||
boot_protocol = iface['boot_protocol'],
|
||||
ip = params.IP(
|
||||
address = iface['ip'],
|
||||
netmask = iface['netmask'],
|
||||
gateway = iface['gateway']
|
||||
network=params.Network(name=iface['network']),
|
||||
name=iface['name'],
|
||||
boot_protocol=iface['boot_protocol'],
|
||||
ip=params.IP(
|
||||
address=iface['ip'],
|
||||
netmask=iface['netmask'],
|
||||
gateway=iface['gateway']
|
||||
),
|
||||
override_configuration = True,
|
||||
bonding = tmpiface)
|
||||
override_configuration=True,
|
||||
bonding=tmpiface)
|
||||
networklist.append(tmpnetwork)
|
||||
setMsg('Applying network ' + iface['name'])
|
||||
except Exception as e:
|
||||
@@ -760,13 +761,13 @@ class RHEVConn(object):
|
||||
return False
|
||||
else:
|
||||
tmpnetwork = params.HostNIC(
|
||||
network = params.Network(name = iface['network']),
|
||||
name = iface['name'],
|
||||
boot_protocol = iface['boot_protocol'],
|
||||
ip = params.IP(
|
||||
address = iface['ip'],
|
||||
netmask = iface['netmask'],
|
||||
gateway = iface['gateway']
|
||||
network=params.Network(name=iface['network']),
|
||||
name=iface['name'],
|
||||
boot_protocol=iface['boot_protocol'],
|
||||
ip=params.IP(
|
||||
address=iface['ip'],
|
||||
netmask=iface['netmask'],
|
||||
gateway=iface['gateway']
|
||||
))
|
||||
networklist.append(tmpnetwork)
|
||||
setMsg('Applying network ' + iface['name'])
|
||||
@@ -828,8 +829,8 @@ class RHEVConn(object):
|
||||
try:
|
||||
HOST.nics.setupnetworks(params.Action(
|
||||
force=True,
|
||||
check_connectivity = False,
|
||||
host_nics = params.HostNics(host_nic = networklist)
|
||||
check_connectivity=False,
|
||||
host_nics=params.HostNics(host_nic=networklist)
|
||||
))
|
||||
setMsg('nics are set')
|
||||
except Exception as e:
|
||||
@@ -1008,24 +1009,24 @@ class RHEV(object):
|
||||
VM = self.conn.get_VM(name)
|
||||
if VM:
|
||||
vminfo = dict()
|
||||
vminfo['uuid'] = VM.id
|
||||
vminfo['name'] = VM.name
|
||||
vminfo['status'] = VM.status.state
|
||||
vminfo['cpu_cores'] = VM.cpu.topology.cores
|
||||
vminfo['uuid'] = VM.id
|
||||
vminfo['name'] = VM.name
|
||||
vminfo['status'] = VM.status.state
|
||||
vminfo['cpu_cores'] = VM.cpu.topology.cores
|
||||
vminfo['cpu_sockets'] = VM.cpu.topology.sockets
|
||||
vminfo['cpu_shares'] = VM.cpu_shares
|
||||
vminfo['memory'] = (int(VM.memory) // 1024 // 1024 // 1024)
|
||||
vminfo['mem_pol'] = (int(VM.memory_policy.guaranteed) // 1024 // 1024 // 1024)
|
||||
vminfo['os'] = VM.get_os().type_
|
||||
vminfo['del_prot'] = VM.delete_protected
|
||||
vminfo['cpu_shares'] = VM.cpu_shares
|
||||
vminfo['memory'] = (int(VM.memory) // 1024 // 1024 // 1024)
|
||||
vminfo['mem_pol'] = (int(VM.memory_policy.guaranteed) // 1024 // 1024 // 1024)
|
||||
vminfo['os'] = VM.get_os().type_
|
||||
vminfo['del_prot'] = VM.delete_protected
|
||||
try:
|
||||
vminfo['host'] = str(self.conn.get_Host_byid(str(VM.host.id)).name)
|
||||
vminfo['host'] = str(self.conn.get_Host_byid(str(VM.host.id)).name)
|
||||
except Exception:
|
||||
vminfo['host'] = None
|
||||
vminfo['boot_order'] = []
|
||||
vminfo['host'] = None
|
||||
vminfo['boot_order'] = []
|
||||
for boot_dev in VM.os.get_boot():
|
||||
vminfo['boot_order'].append(str(boot_dev.dev))
|
||||
vminfo['disks'] = []
|
||||
vminfo['disks'] = []
|
||||
for DISK in VM.disks.list():
|
||||
disk = dict()
|
||||
disk['name'] = DISK.name
|
||||
@@ -1033,7 +1034,7 @@ class RHEV(object):
|
||||
disk['domain'] = str((self.conn.get_domain_byid(DISK.get_storage_domains().get_storage_domain()[0].id)).name)
|
||||
disk['interface'] = DISK.interface
|
||||
vminfo['disks'].append(disk)
|
||||
vminfo['ifaces'] = []
|
||||
vminfo['ifaces'] = []
|
||||
for NIC in VM.nics.list():
|
||||
iface = dict()
|
||||
iface['name'] = str(NIC.name)
|
||||
@@ -1083,17 +1084,17 @@ class RHEV(object):
|
||||
bootselect = True
|
||||
|
||||
for disk in disks:
|
||||
diskname = name + "_Disk" + str(counter) + "_" + disk.get('name', '').replace('/', '_')
|
||||
disksize = disk.get('size', 1)
|
||||
diskdomain = disk.get('domain', None)
|
||||
diskname = name + "_Disk" + str(counter) + "_" + disk.get('name', '').replace('/', '_')
|
||||
disksize = disk.get('size', 1)
|
||||
diskdomain = disk.get('domain', None)
|
||||
if diskdomain is None:
|
||||
setMsg("`domain` is a required disk key.")
|
||||
setFailed()
|
||||
return False
|
||||
diskinterface = disk.get('interface', 'virtio')
|
||||
diskformat = disk.get('format', 'raw')
|
||||
diskinterface = disk.get('interface', 'virtio')
|
||||
diskformat = disk.get('format', 'raw')
|
||||
diskallocationtype = disk.get('thin', False)
|
||||
diskboot = disk.get('bootable', False)
|
||||
diskboot = disk.get('bootable', False)
|
||||
|
||||
if bootselect is False and counter == 0:
|
||||
diskboot = True
|
||||
@@ -1175,7 +1176,7 @@ class RHEV(object):
|
||||
def setBootOrder(self, vmname, boot_order):
|
||||
self.__get_conn()
|
||||
VM = self.conn.get_VM(vmname)
|
||||
bootorder = []
|
||||
bootorder = []
|
||||
for boot_dev in VM.os.get_boot():
|
||||
bootorder.append(str(boot_dev.dev))
|
||||
|
||||
@@ -1469,31 +1470,31 @@ def core(module):
|
||||
def main():
|
||||
global module
|
||||
module = AnsibleModule(
|
||||
argument_spec = dict(
|
||||
state = dict(default='present', choices=['ping', 'present', 'absent', 'up', 'down', 'restarted', 'cd', 'info']),
|
||||
user = dict(default="admin@internal"),
|
||||
password = dict(required=True, no_log=True),
|
||||
server = dict(default="127.0.0.1"),
|
||||
port = dict(default="443"),
|
||||
insecure_api = dict(default=False, type='bool'),
|
||||
name = dict(),
|
||||
image = dict(default=False),
|
||||
datacenter = dict(default="Default"),
|
||||
type = dict(default="server", choices=['server', 'desktop', 'host']),
|
||||
cluster = dict(default=''),
|
||||
vmhost = dict(default=False),
|
||||
vmcpu = dict(default="2"),
|
||||
vmmem = dict(default="1"),
|
||||
disks = dict(),
|
||||
osver = dict(default="rhel_6x64"),
|
||||
ifaces = dict(aliases=['nics', 'interfaces']),
|
||||
timeout = dict(default=False),
|
||||
mempol = dict(default="1"),
|
||||
vm_ha = dict(default=True),
|
||||
cpu_share = dict(default="0"),
|
||||
boot_order = dict(default=["network", "hd"]),
|
||||
del_prot = dict(default=True, type="bool"),
|
||||
cd_drive = dict(default=False)
|
||||
argument_spec=dict(
|
||||
state=dict(default='present', choices=['ping', 'present', 'absent', 'up', 'down', 'restarted', 'cd', 'info']),
|
||||
user=dict(default="admin@internal"),
|
||||
password=dict(required=True, no_log=True),
|
||||
server=dict(default="127.0.0.1"),
|
||||
port=dict(default="443"),
|
||||
insecure_api=dict(default=False, type='bool'),
|
||||
name=dict(),
|
||||
image=dict(default=False),
|
||||
datacenter=dict(default="Default"),
|
||||
type=dict(default="server", choices=['server', 'desktop', 'host']),
|
||||
cluster=dict(default=''),
|
||||
vmhost=dict(default=False),
|
||||
vmcpu=dict(default="2"),
|
||||
vmmem=dict(default="1"),
|
||||
disks=dict(),
|
||||
osver=dict(default="rhel_6x64"),
|
||||
ifaces=dict(aliases=['nics', 'interfaces']),
|
||||
timeout=dict(default=False),
|
||||
mempol=dict(default="1"),
|
||||
vm_ha=dict(default=True),
|
||||
cpu_share=dict(default="0"),
|
||||
boot_order=dict(default=["network", "hd"]),
|
||||
del_prot=dict(default=True, type="bool"),
|
||||
cd_drive=dict(default=False)
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@@ -154,13 +154,13 @@ def get_service_name(module, stage):
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
service_path = dict(required=True, type='path'),
|
||||
state = dict(default='present', choices=['present', 'absent'], required=False),
|
||||
functions = dict(type='list', required=False),
|
||||
region = dict(default='', required=False),
|
||||
stage = dict(default='', required=False),
|
||||
deploy = dict(default=True, type='bool', required=False),
|
||||
serverless_bin_path = dict(required=False, type='path')
|
||||
service_path=dict(required=True, type='path'),
|
||||
state=dict(default='present', choices=['present', 'absent'], required=False),
|
||||
functions=dict(type='list', required=False),
|
||||
region=dict(default='', required=False),
|
||||
stage=dict(default='', required=False),
|
||||
deploy=dict(default=True, type='bool', required=False),
|
||||
serverless_bin_path=dict(required=False, type='path')
|
||||
),
|
||||
)
|
||||
|
||||
@@ -198,13 +198,13 @@ def main():
|
||||
if rc != 0:
|
||||
if state == 'absent' and "-{}' does not exist".format(stage) in out:
|
||||
module.exit_json(changed=False, state='absent', command=command,
|
||||
out=out, service_name=get_service_name(module, stage))
|
||||
out=out, service_name=get_service_name(module, stage))
|
||||
|
||||
module.fail_json(msg="Failure when executing Serverless command. Exited {}.\nstdout: {}\nstderr: {}".format(rc, out, err))
|
||||
|
||||
# gather some facts about the deployment
|
||||
module.exit_json(changed=True, state='present', out=out, command=command,
|
||||
service_name=get_service_name(module, stage))
|
||||
service_name=get_service_name(module, stage))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -151,31 +151,32 @@ from ansible.module_utils._text import to_native
|
||||
|
||||
VIRT_FAILED = 1
|
||||
VIRT_SUCCESS = 0
|
||||
VIRT_UNAVAILABLE=2
|
||||
VIRT_UNAVAILABLE = 2
|
||||
|
||||
ALL_COMMANDS = []
|
||||
ENTRY_COMMANDS = ['create', 'status', 'start', 'stop',
|
||||
'undefine', 'destroy', 'get_xml', 'define',
|
||||
'modify' ]
|
||||
HOST_COMMANDS = [ 'list_nets', 'facts', 'info' ]
|
||||
'modify']
|
||||
HOST_COMMANDS = ['list_nets', 'facts', 'info']
|
||||
ALL_COMMANDS.extend(ENTRY_COMMANDS)
|
||||
ALL_COMMANDS.extend(HOST_COMMANDS)
|
||||
|
||||
ENTRY_STATE_ACTIVE_MAP = {
|
||||
0 : "inactive",
|
||||
1 : "active"
|
||||
0: "inactive",
|
||||
1: "active"
|
||||
}
|
||||
|
||||
ENTRY_STATE_AUTOSTART_MAP = {
|
||||
0 : "no",
|
||||
1 : "yes"
|
||||
0: "no",
|
||||
1: "yes"
|
||||
}
|
||||
|
||||
ENTRY_STATE_PERSISTENT_MAP = {
|
||||
0 : "no",
|
||||
1 : "yes"
|
||||
0: "no",
|
||||
1: "yes"
|
||||
}
|
||||
|
||||
|
||||
class EntryNotFound(Exception):
|
||||
pass
|
||||
|
||||
@@ -245,9 +246,9 @@ class LibvirtConnection(object):
|
||||
if host is None:
|
||||
# add the host
|
||||
if not self.module.check_mode:
|
||||
res = network.update (libvirt.VIR_NETWORK_UPDATE_COMMAND_ADD_LAST,
|
||||
libvirt.VIR_NETWORK_SECTION_IP_DHCP_HOST,
|
||||
-1, xml, libvirt.VIR_NETWORK_UPDATE_AFFECT_CURRENT)
|
||||
res = network.update(libvirt.VIR_NETWORK_UPDATE_COMMAND_ADD_LAST,
|
||||
libvirt.VIR_NETWORK_SECTION_IP_DHCP_HOST,
|
||||
-1, xml, libvirt.VIR_NETWORK_UPDATE_AFFECT_CURRENT)
|
||||
else:
|
||||
# pretend there was a change
|
||||
res = 0
|
||||
@@ -259,9 +260,9 @@ class LibvirtConnection(object):
|
||||
return False
|
||||
else:
|
||||
if not self.module.check_mode:
|
||||
res = network.update (libvirt.VIR_NETWORK_UPDATE_COMMAND_MODIFY,
|
||||
libvirt.VIR_NETWORK_SECTION_IP_DHCP_HOST,
|
||||
-1, xml, libvirt.VIR_NETWORK_UPDATE_AFFECT_CURRENT)
|
||||
res = network.update(libvirt.VIR_NETWORK_UPDATE_COMMAND_MODIFY,
|
||||
libvirt.VIR_NETWORK_SECTION_IP_DHCP_HOST,
|
||||
-1, xml, libvirt.VIR_NETWORK_UPDATE_AFFECT_CURRENT)
|
||||
else:
|
||||
# pretend there was a change
|
||||
res = 0
|
||||
@@ -286,18 +287,18 @@ class LibvirtConnection(object):
|
||||
|
||||
def get_status2(self, entry):
|
||||
state = entry.isActive()
|
||||
return ENTRY_STATE_ACTIVE_MAP.get(state,"unknown")
|
||||
return ENTRY_STATE_ACTIVE_MAP.get(state, "unknown")
|
||||
|
||||
def get_status(self, entryid):
|
||||
if not self.module.check_mode:
|
||||
state = self.find_entry(entryid).isActive()
|
||||
return ENTRY_STATE_ACTIVE_MAP.get(state,"unknown")
|
||||
return ENTRY_STATE_ACTIVE_MAP.get(state, "unknown")
|
||||
else:
|
||||
try:
|
||||
state = self.find_entry(entryid).isActive()
|
||||
return ENTRY_STATE_ACTIVE_MAP.get(state,"unknown")
|
||||
return ENTRY_STATE_ACTIVE_MAP.get(state, "unknown")
|
||||
except:
|
||||
return ENTRY_STATE_ACTIVE_MAP.get("inactive","unknown")
|
||||
return ENTRY_STATE_ACTIVE_MAP.get("inactive", "unknown")
|
||||
|
||||
def get_uuid(self, entryid):
|
||||
return self.find_entry(entryid).UUIDString()
|
||||
@@ -331,7 +332,7 @@ class LibvirtConnection(object):
|
||||
|
||||
def get_autostart(self, entryid):
|
||||
state = self.find_entry(entryid).autostart()
|
||||
return ENTRY_STATE_AUTOSTART_MAP.get(state,"unknown")
|
||||
return ENTRY_STATE_AUTOSTART_MAP.get(state, "unknown")
|
||||
|
||||
def get_autostart2(self, entryid):
|
||||
if not self.module.check_mode:
|
||||
@@ -358,7 +359,7 @@ class LibvirtConnection(object):
|
||||
|
||||
def get_persistent(self, entryid):
|
||||
state = self.find_entry(entryid).isPersistent()
|
||||
return ENTRY_STATE_PERSISTENT_MAP.get(state,"unknown")
|
||||
return ENTRY_STATE_PERSISTENT_MAP.get(state, "unknown")
|
||||
|
||||
def get_dhcp_leases(self, entryid):
|
||||
network = self.find_entry(entryid)
|
||||
@@ -398,7 +399,7 @@ class VirtNetwork(object):
|
||||
results = []
|
||||
for entry in self.list_nets():
|
||||
state_blurb = self.conn.get_status(entry)
|
||||
results.append("%s %s" % (entry,state_blurb))
|
||||
results.append("%s %s" % (entry, state_blurb))
|
||||
return results
|
||||
|
||||
def autostart(self, entryid):
|
||||
@@ -481,11 +482,11 @@ class VirtNetwork(object):
|
||||
|
||||
def core(module):
|
||||
|
||||
state = module.params.get('state', None)
|
||||
name = module.params.get('name', None)
|
||||
command = module.params.get('command', None)
|
||||
uri = module.params.get('uri', None)
|
||||
xml = module.params.get('xml', None)
|
||||
state = module.params.get('state', None)
|
||||
name = module.params.get('name', None)
|
||||
command = module.params.get('command', None)
|
||||
uri = module.params.get('uri', None)
|
||||
xml = module.params.get('xml', None)
|
||||
autostart = module.params.get('autostart', None)
|
||||
|
||||
v = VirtNetwork(uri, module)
|
||||
@@ -494,33 +495,33 @@ def core(module):
|
||||
if state and command == 'list_nets':
|
||||
res = v.list_nets(state=state)
|
||||
if not isinstance(res, dict):
|
||||
res = { command: res }
|
||||
res = {command: res}
|
||||
return VIRT_SUCCESS, res
|
||||
|
||||
if state:
|
||||
if not name:
|
||||
module.fail_json(msg = "state change requires a specified name")
|
||||
module.fail_json(msg="state change requires a specified name")
|
||||
|
||||
res['changed'] = False
|
||||
if state in [ 'active' ]:
|
||||
if state in ['active']:
|
||||
if v.status(name) is not 'active':
|
||||
res['changed'] = True
|
||||
res['msg'] = v.start(name)
|
||||
elif state in [ 'present' ]:
|
||||
elif state in ['present']:
|
||||
try:
|
||||
v.get_net(name)
|
||||
except EntryNotFound:
|
||||
if not xml:
|
||||
module.fail_json(msg = "network '" + name + "' not present, but xml not specified")
|
||||
module.fail_json(msg="network '" + name + "' not present, but xml not specified")
|
||||
v.define(name, xml)
|
||||
res = {'changed': True, 'created': name}
|
||||
elif state in [ 'inactive' ]:
|
||||
elif state in ['inactive']:
|
||||
entries = v.list_nets()
|
||||
if name in entries:
|
||||
if v.status(name) is not 'inactive':
|
||||
res['changed'] = True
|
||||
res['msg'] = v.destroy(name)
|
||||
elif state in [ 'undefined', 'absent' ]:
|
||||
elif state in ['undefined', 'absent']:
|
||||
entries = v.list_nets()
|
||||
if name in entries:
|
||||
if v.status(name) is not 'inactive':
|
||||
@@ -535,10 +536,10 @@ def core(module):
|
||||
if command:
|
||||
if command in ENTRY_COMMANDS:
|
||||
if not name:
|
||||
module.fail_json(msg = "%s requires 1 argument: name" % command)
|
||||
module.fail_json(msg="%s requires 1 argument: name" % command)
|
||||
if command in ('define', 'modify'):
|
||||
if not xml:
|
||||
module.fail_json(msg = command+" requires xml argument")
|
||||
module.fail_json(msg=command + " requires xml argument")
|
||||
try:
|
||||
v.get_net(name)
|
||||
except EntryNotFound:
|
||||
@@ -551,13 +552,13 @@ def core(module):
|
||||
return VIRT_SUCCESS, res
|
||||
res = getattr(v, command)(name)
|
||||
if not isinstance(res, dict):
|
||||
res = { command: res }
|
||||
res = {command: res}
|
||||
return VIRT_SUCCESS, res
|
||||
|
||||
elif hasattr(v, command):
|
||||
res = getattr(v, command)()
|
||||
if not isinstance(res, dict):
|
||||
res = { command: res }
|
||||
res = {command: res}
|
||||
return VIRT_SUCCESS, res
|
||||
|
||||
else:
|
||||
@@ -565,7 +566,7 @@ def core(module):
|
||||
|
||||
if autostart is not None:
|
||||
if not name:
|
||||
module.fail_json(msg = "state change requires a specified name")
|
||||
module.fail_json(msg="state change requires a specified name")
|
||||
|
||||
res['changed'] = False
|
||||
if autostart:
|
||||
@@ -584,16 +585,16 @@ def core(module):
|
||||
|
||||
def main():
|
||||
|
||||
module = AnsibleModule (
|
||||
argument_spec = dict(
|
||||
name = dict(aliases=['network']),
|
||||
state = dict(choices=['active', 'inactive', 'present', 'absent']),
|
||||
command = dict(choices=ALL_COMMANDS),
|
||||
uri = dict(default='qemu:///system'),
|
||||
xml = dict(),
|
||||
autostart = dict(type='bool')
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
name=dict(aliases=['network']),
|
||||
state=dict(choices=['active', 'inactive', 'present', 'absent']),
|
||||
command=dict(choices=ALL_COMMANDS),
|
||||
uri=dict(default='qemu:///system'),
|
||||
xml=dict(),
|
||||
autostart=dict(type='bool')
|
||||
),
|
||||
supports_check_mode = True
|
||||
supports_check_mode=True
|
||||
)
|
||||
|
||||
if not HAS_VIRT:
|
||||
@@ -612,7 +613,7 @@ def main():
|
||||
except Exception as e:
|
||||
module.fail_json(msg=str(e))
|
||||
|
||||
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)
|
||||
else:
|
||||
module.exit_json(**result)
|
||||
|
||||
@@ -165,49 +165,49 @@ from ansible.module_utils.basic import AnsibleModule
|
||||
|
||||
VIRT_FAILED = 1
|
||||
VIRT_SUCCESS = 0
|
||||
VIRT_UNAVAILABLE=2
|
||||
VIRT_UNAVAILABLE = 2
|
||||
|
||||
ALL_COMMANDS = []
|
||||
ENTRY_COMMANDS = ['create', 'status', 'start', 'stop', 'build', 'delete',
|
||||
'undefine', 'destroy', 'get_xml', 'define', 'refresh']
|
||||
HOST_COMMANDS = [ 'list_pools', 'facts', 'info' ]
|
||||
HOST_COMMANDS = ['list_pools', 'facts', 'info']
|
||||
ALL_COMMANDS.extend(ENTRY_COMMANDS)
|
||||
ALL_COMMANDS.extend(HOST_COMMANDS)
|
||||
|
||||
ENTRY_STATE_ACTIVE_MAP = {
|
||||
0 : "inactive",
|
||||
1 : "active"
|
||||
0: "inactive",
|
||||
1: "active"
|
||||
}
|
||||
|
||||
ENTRY_STATE_AUTOSTART_MAP = {
|
||||
0 : "no",
|
||||
1 : "yes"
|
||||
0: "no",
|
||||
1: "yes"
|
||||
}
|
||||
|
||||
ENTRY_STATE_PERSISTENT_MAP = {
|
||||
0 : "no",
|
||||
1 : "yes"
|
||||
0: "no",
|
||||
1: "yes"
|
||||
}
|
||||
|
||||
ENTRY_STATE_INFO_MAP = {
|
||||
0 : "inactive",
|
||||
1 : "building",
|
||||
2 : "running",
|
||||
3 : "degraded",
|
||||
4 : "inaccessible"
|
||||
0: "inactive",
|
||||
1: "building",
|
||||
2: "running",
|
||||
3: "degraded",
|
||||
4: "inaccessible"
|
||||
}
|
||||
|
||||
ENTRY_BUILD_FLAGS_MAP = {
|
||||
"new" : 0,
|
||||
"repair" : 1,
|
||||
"resize" : 2,
|
||||
"no_overwrite" : 4,
|
||||
"overwrite" : 8
|
||||
"new": 0,
|
||||
"repair": 1,
|
||||
"resize": 2,
|
||||
"no_overwrite": 4,
|
||||
"overwrite": 8
|
||||
}
|
||||
|
||||
ENTRY_DELETE_FLAGS_MAP = {
|
||||
"normal" : 0,
|
||||
"zeroed" : 1
|
||||
"normal": 0,
|
||||
"zeroed": 1
|
||||
}
|
||||
|
||||
ALL_MODES = []
|
||||
@@ -283,18 +283,18 @@ class LibvirtConnection(object):
|
||||
|
||||
def get_status2(self, entry):
|
||||
state = entry.isActive()
|
||||
return ENTRY_STATE_ACTIVE_MAP.get(state,"unknown")
|
||||
return ENTRY_STATE_ACTIVE_MAP.get(state, "unknown")
|
||||
|
||||
def get_status(self, entryid):
|
||||
if not self.module.check_mode:
|
||||
state = self.find_entry(entryid).isActive()
|
||||
return ENTRY_STATE_ACTIVE_MAP.get(state,"unknown")
|
||||
return ENTRY_STATE_ACTIVE_MAP.get(state, "unknown")
|
||||
else:
|
||||
try:
|
||||
state = self.find_entry(entryid).isActive()
|
||||
return ENTRY_STATE_ACTIVE_MAP.get(state,"unknown")
|
||||
return ENTRY_STATE_ACTIVE_MAP.get(state, "unknown")
|
||||
except:
|
||||
return ENTRY_STATE_ACTIVE_MAP.get("inactive","unknown")
|
||||
return ENTRY_STATE_ACTIVE_MAP.get("inactive", "unknown")
|
||||
|
||||
def get_uuid(self, entryid):
|
||||
return self.find_entry(entryid).UUIDString()
|
||||
@@ -378,7 +378,7 @@ class LibvirtConnection(object):
|
||||
|
||||
def get_autostart(self, entryid):
|
||||
state = self.find_entry(entryid).autostart()
|
||||
return ENTRY_STATE_AUTOSTART_MAP.get(state,"unknown")
|
||||
return ENTRY_STATE_AUTOSTART_MAP.get(state, "unknown")
|
||||
|
||||
def get_autostart2(self, entryid):
|
||||
if not self.module.check_mode:
|
||||
@@ -405,7 +405,7 @@ class LibvirtConnection(object):
|
||||
|
||||
def get_persistent(self, entryid):
|
||||
state = self.find_entry(entryid).isPersistent()
|
||||
return ENTRY_STATE_PERSISTENT_MAP.get(state,"unknown")
|
||||
return ENTRY_STATE_PERSISTENT_MAP.get(state, "unknown")
|
||||
|
||||
def define_from_xml(self, entryid, xml):
|
||||
if not self.module.check_mode:
|
||||
@@ -441,7 +441,7 @@ class VirtStoragePool(object):
|
||||
results = []
|
||||
for entry in self.list_pools():
|
||||
state_blurb = self.conn.get_status(entry)
|
||||
results.append("%s %s" % (entry,state_blurb))
|
||||
results.append("%s %s" % (entry, state_blurb))
|
||||
return results
|
||||
|
||||
def autostart(self, entryid):
|
||||
@@ -478,10 +478,10 @@ class VirtStoragePool(object):
|
||||
return self.conn.define_from_xml(entryid, xml)
|
||||
|
||||
def build(self, entryid, flags):
|
||||
return self.conn.build(entryid, ENTRY_BUILD_FLAGS_MAP.get(flags,0))
|
||||
return self.conn.build(entryid, ENTRY_BUILD_FLAGS_MAP.get(flags, 0))
|
||||
|
||||
def delete(self, entryid, flags):
|
||||
return self.conn.delete(entryid, ENTRY_DELETE_FLAGS_MAP.get(flags,0))
|
||||
return self.conn.delete(entryid, ENTRY_DELETE_FLAGS_MAP.get(flags, 0))
|
||||
|
||||
def refresh(self, entryid):
|
||||
return self.conn.refresh(entryid)
|
||||
@@ -501,10 +501,10 @@ class VirtStoragePool(object):
|
||||
# assume the other end of the xmlrpc connection can figure things
|
||||
# out or doesn't care.
|
||||
results[entry] = {
|
||||
"status" : ENTRY_STATE_INFO_MAP.get(data[0],"unknown"),
|
||||
"size_total" : str(data[1]),
|
||||
"size_used" : str(data[2]),
|
||||
"size_available" : str(data[3]),
|
||||
"status": ENTRY_STATE_INFO_MAP.get(data[0], "unknown"),
|
||||
"size_total": str(data[1]),
|
||||
"size_used": str(data[2]),
|
||||
"size_available": str(data[3]),
|
||||
}
|
||||
results[entry]["autostart"] = self.conn.get_autostart(entry)
|
||||
results[entry]["persistent"] = self.conn.get_persistent(entry)
|
||||
@@ -555,13 +555,13 @@ class VirtStoragePool(object):
|
||||
|
||||
def core(module):
|
||||
|
||||
state = module.params.get('state', None)
|
||||
name = module.params.get('name', None)
|
||||
command = module.params.get('command', None)
|
||||
uri = module.params.get('uri', None)
|
||||
xml = module.params.get('xml', None)
|
||||
state = module.params.get('state', None)
|
||||
name = module.params.get('name', None)
|
||||
command = module.params.get('command', None)
|
||||
uri = module.params.get('uri', None)
|
||||
xml = module.params.get('xml', None)
|
||||
autostart = module.params.get('autostart', None)
|
||||
mode = module.params.get('mode', None)
|
||||
mode = module.params.get('mode', None)
|
||||
|
||||
v = VirtStoragePool(uri, module)
|
||||
res = {}
|
||||
@@ -569,40 +569,40 @@ def core(module):
|
||||
if state and command == 'list_pools':
|
||||
res = v.list_pools(state=state)
|
||||
if not isinstance(res, dict):
|
||||
res = { command: res }
|
||||
res = {command: res}
|
||||
return VIRT_SUCCESS, res
|
||||
|
||||
if state:
|
||||
if not name:
|
||||
module.fail_json(msg = "state change requires a specified name")
|
||||
module.fail_json(msg="state change requires a specified name")
|
||||
|
||||
res['changed'] = False
|
||||
if state in [ 'active' ]:
|
||||
if state in ['active']:
|
||||
if v.status(name) is not 'active':
|
||||
res['changed'] = True
|
||||
res['msg'] = v.start(name)
|
||||
elif state in [ 'present' ]:
|
||||
elif state in ['present']:
|
||||
try:
|
||||
v.get_pool(name)
|
||||
except EntryNotFound:
|
||||
if not xml:
|
||||
module.fail_json(msg = "storage pool '" + name + "' not present, but xml not specified")
|
||||
module.fail_json(msg="storage pool '" + name + "' not present, but xml not specified")
|
||||
v.define(name, xml)
|
||||
res = {'changed': True, 'created': name}
|
||||
elif state in [ 'inactive' ]:
|
||||
elif state in ['inactive']:
|
||||
entries = v.list_pools()
|
||||
if name in entries:
|
||||
if v.status(name) is not 'inactive':
|
||||
res['changed'] = True
|
||||
res['msg'] = v.destroy(name)
|
||||
elif state in [ 'undefined', 'absent' ]:
|
||||
elif state in ['undefined', 'absent']:
|
||||
entries = v.list_pools()
|
||||
if name in entries:
|
||||
if v.status(name) is not 'inactive':
|
||||
v.destroy(name)
|
||||
res['changed'] = True
|
||||
res['msg'] = v.undefine(name)
|
||||
elif state in [ 'deleted' ]:
|
||||
elif state in ['deleted']:
|
||||
entries = v.list_pools()
|
||||
if name in entries:
|
||||
if v.status(name) is not 'inactive':
|
||||
@@ -618,10 +618,10 @@ def core(module):
|
||||
if command:
|
||||
if command in ENTRY_COMMANDS:
|
||||
if not name:
|
||||
module.fail_json(msg = "%s requires 1 argument: name" % command)
|
||||
module.fail_json(msg="%s requires 1 argument: name" % command)
|
||||
if command == 'define':
|
||||
if not xml:
|
||||
module.fail_json(msg = "define requires xml argument")
|
||||
module.fail_json(msg="define requires xml argument")
|
||||
try:
|
||||
v.get_pool(name)
|
||||
except EntryNotFound:
|
||||
@@ -631,22 +631,22 @@ def core(module):
|
||||
elif command == 'build':
|
||||
res = v.build(name, mode)
|
||||
if not isinstance(res, dict):
|
||||
res = { 'changed': True, command: res }
|
||||
res = {'changed': True, command: res}
|
||||
return VIRT_SUCCESS, res
|
||||
elif command == 'delete':
|
||||
res = v.delete(name, mode)
|
||||
if not isinstance(res, dict):
|
||||
res = { 'changed': True, command: res }
|
||||
res = {'changed': True, command: res}
|
||||
return VIRT_SUCCESS, res
|
||||
res = getattr(v, command)(name)
|
||||
if not isinstance(res, dict):
|
||||
res = { command: res }
|
||||
res = {command: res}
|
||||
return VIRT_SUCCESS, res
|
||||
|
||||
elif hasattr(v, command):
|
||||
res = getattr(v, command)()
|
||||
if not isinstance(res, dict):
|
||||
res = { command: res }
|
||||
res = {command: res}
|
||||
return VIRT_SUCCESS, res
|
||||
|
||||
else:
|
||||
@@ -654,7 +654,7 @@ def core(module):
|
||||
|
||||
if autostart is not None:
|
||||
if not name:
|
||||
module.fail_json(msg = "state change requires a specified name")
|
||||
module.fail_json(msg="state change requires a specified name")
|
||||
|
||||
res['changed'] = False
|
||||
if autostart:
|
||||
@@ -673,17 +673,17 @@ def core(module):
|
||||
|
||||
def main():
|
||||
|
||||
module = AnsibleModule (
|
||||
argument_spec = dict(
|
||||
name = dict(aliases=['pool']),
|
||||
state = dict(choices=['active', 'inactive', 'present', 'absent', 'undefined', 'deleted']),
|
||||
command = dict(choices=ALL_COMMANDS),
|
||||
uri = dict(default='qemu:///system'),
|
||||
xml = dict(),
|
||||
autostart = dict(type='bool'),
|
||||
mode = dict(choices=ALL_MODES),
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
name=dict(aliases=['pool']),
|
||||
state=dict(choices=['active', 'inactive', 'present', 'absent', 'undefined', 'deleted']),
|
||||
command=dict(choices=ALL_COMMANDS),
|
||||
uri=dict(default='qemu:///system'),
|
||||
xml=dict(),
|
||||
autostart=dict(type='bool'),
|
||||
mode=dict(choices=ALL_MODES),
|
||||
),
|
||||
supports_check_mode = True
|
||||
supports_check_mode=True
|
||||
)
|
||||
|
||||
if not HAS_VIRT:
|
||||
@@ -702,7 +702,7 @@ def main():
|
||||
except Exception as e:
|
||||
module.fail_json(msg=str(e))
|
||||
|
||||
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)
|
||||
else:
|
||||
module.exit_json(**result)
|
||||
|
||||
@@ -137,12 +137,14 @@ def change_keys(recs, key='uuid', filter_func=None):
|
||||
|
||||
return new_recs
|
||||
|
||||
|
||||
def get_host(session):
|
||||
"""Get the host"""
|
||||
host_recs = session.xenapi.host.get_all()
|
||||
# We only have one host, so just return its entry
|
||||
return session.xenapi.host.get_record(host_recs[0])
|
||||
|
||||
|
||||
def get_vms(session):
|
||||
xs_vms = {}
|
||||
recs = session.xenapi.VM.get_all()
|
||||
@@ -165,6 +167,7 @@ def get_srs(session):
|
||||
xs_srs[sr['name_label']] = sr
|
||||
return xs_srs
|
||||
|
||||
|
||||
def main():
|
||||
module = AnsibleModule({})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user