add idempotent and ='s separated parameters to virt module to make people happy

This commit is contained in:
Seth Vidal 2012-04-04 17:17:29 -04:00
commit 8a61794234

View file

@ -353,16 +353,11 @@ def main():
'shutdown', 'undefine', 'destroy', 'get_xml', 'autostart'] 'shutdown', 'undefine', 'destroy', 'get_xml', 'autostart']
host_commands = ['freemem', 'list_vms', 'info', 'nodeinfo', 'virttype'] host_commands = ['freemem', 'list_vms', 'info', 'nodeinfo', 'virttype']
msg = """ msg = """
virtmodule arguments: virtmodule arguments:
- queryvm guest <queryable item from libvirt domain api> <more of those> state=[running|shutdown] guest=guestname
- guest commands: (all require a single guest name an argument) command=some_virt_command [guest=guestname]
%s """
- host commands:
%s
""" % (', '.join(sorted(vm_commands)), ', '.join(sorted(host_commands)))
if len(sys.argv) == 1: if len(sys.argv) == 1:
return VIRT_FAILED, msg return VIRT_FAILED, msg
@ -378,43 +373,60 @@ def main():
if not len(items): if not len(items):
return VIRT_FAILED, msg return VIRT_FAILED, msg
basecmd = items[0]
v = Virt()
if basecmd in ('queryvm'): # vm=name state=[running|shutdown|destroyed|undefined]
if len(items) < 3: # command=[some command] [vm=name]
msg = "queryvm requires at least 2 arguments: guest itemname [itemname]"
params = {}
for x in items:
(k, v) = x.split("=")
params[k] = v
state = params.get('state', None)
guest = params.get('guest', None)
command = params.get('command', None)
options = params.get('options', [])
v = Virt()
res = {}
if state:
if not guest:
msg = "state change requires a guest specified"
return VIRT_FAILED, msg return VIRT_FAILED, msg
guest = items[1] res['changed'] = False
reqs = items[2:] if state == 'running':
res = {} if v.status(guest) is not 'running':
for req in reqs: res['changed'] = True
vm = v.get_vm(guest) res['msg'] = v.start(guest)
if hasattr(vm, req): elif state == 'shutdown':
data = getattr(vm,req)() if v.status(guest) is not 'shutdown':
res[req] = data res['changed'] = True
return rc, res res['msg'] = v.shutdown(guest)
# any methods that require the guest/vmid as the argument must be listed here return VIRT_SUCCESS, res
elif basecmd in vm_commands:
if len(items) < 2:
msg = "%s requires 1 argument: guest" % basecmd
return VIRT_FAILED, msg
guest = items[1] if command:
res = getattr(v, basecmd)(guest) if command in vm_commands:
if type(res) != dict: if not guest:
res = { basecmd: res } msg = "%s requires 1 argument: guest" % command
return rc, res return VIRT_FAILED, msg
elif hasattr(v, basecmd): res = getattr(v, command)(guest)
res = getattr(v, basecmd)() if type(res) != dict:
return rc, res res = { command: res }
return rc, res
else: elif hasattr(v, command):
msg = "Command %s not recognized" % basecmd res = getattr(v, command)()
rc = VIRT_FAILED if type(res) != dict:
res = { command: res }
return rc, res
else:
msg = "Command %s not recognized" % basecmd
rc = VIRT_FAILED
return rc, msg return rc, msg