Skip to content

Commit

Permalink
Now we can safely create TemplateVM or StandaloneVM
Browse files Browse the repository at this point in the history
  • Loading branch information
kushaldas committed Oct 23, 2018
1 parent 37ecf40 commit 67983dd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
23 changes: 14 additions & 9 deletions ansible_module/qubesos.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,21 @@ def unpause(self, vmname):
def create(self, vmname, vmtype="AppVM", label="red", template=None, netvm="default"):
""" Start the machine via the given vmid """
network_vm = None
template_vm = ""
if template:
template_vm = template
if netvm == "default":
network_vm = self.app.default_netvm
elif not netvm:
network_vm = None
else:
network_vm = self.get_vm(netvm)

vm = self.app.add_new_vm(vmtype, vmname, label, template=template)
vm.netvm = network_vm
if vmtype == "AppVM":
vm = self.app.add_new_vm(vmtype, vmname, label, template=template_vm)
vm.netvm = network_vm
elif vmtype in ["StandaloneVM", "TemplateVM"] and template_vm:
vm = self.app.clone_vm(template_vm, vmname, vmtype)
vm.label = label
return 0

def start(self, vmname):
Expand All @@ -291,7 +297,7 @@ def destroy(self, vmname):
vm.force_shutdown()
return 0

def properties(self, vmname, prefs, vmtype):
def properties(self, vmname, prefs, vmtype, label, vmtemplate):
"Sets the given properties to the VM"
changed = False
vm = None
Expand All @@ -300,7 +306,7 @@ def properties(self, vmname, prefs, vmtype):
vm = self.get_vm(vmname)
except KeyError:
# Means first we have to create the vm
self.create(vmname, vmtype)
self.create(vmname, vmtype, label, vmtemplate)
vm = self.get_vm(vmname)
if "autostart" in prefs and vm.autostart != prefs["autostart"]:
vm.autostart = prefs["autostart"]
Expand Down Expand Up @@ -404,7 +410,6 @@ def core(module):
vmtype = module.params.get('vmtype', 'AppVM')
label = module.params.get('label', 'red')
template = module.params.get('template', None)
netvm = module.params.get('netvm', "default")
properties = module.params.get('properties', {})
tags = module.params.get('tags', [])

Expand Down Expand Up @@ -439,18 +444,19 @@ def core(module):
if not vm.template_for_dispvms:
return VIRT_FAILED, {"Missing dispvm capability": val}
if state == "present" and guest and vmtype:
changed, changed_values = v.properties(guest, properties, vmtype)
changed, changed_values = v.properties(guest, properties, vmtype, label, template)
if tags:
# Apply the tags
v.tags(guest, tags)
return VIRT_SUCCESS, {"Properties updated": changed_values, "changed": changed}

# This is without any properties
if state == "present" and guest and vmtype:
try:
v.get_vm(guest)
res = {"changed": False, "status": "VM is present."}
except KeyError:
v.create(guest, vmtype, label, template, netvm)
v.create(guest, vmtype, label, template)
if tags:
# Apply the tags
v.tags(guest, tags)
Expand Down Expand Up @@ -557,7 +563,6 @@ def main():
label=dict(type='str', default='red'),
vmtype=dict(type='str', default='AppVM'),
template=dict(type='str', default='default'),
netvm=dict(type='str', default='default'),
properties=dict(type='dict', default={}),
tags=dict(type='list', default=[]),
),
Expand Down
6 changes: 0 additions & 6 deletions examples/create_vm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,5 @@
guest: supansible
state: running

- name: Create our xchat
qubesos:
guest: xchat
label: blue
template: "fedora-28"
command: create


6 changes: 6 additions & 0 deletions examples/update_all_templates.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- hosts: templatevms
tasks:
- name: Update the TemplateVM
package: name=* state=latest

0 comments on commit 67983dd

Please sign in to comment.