1. Shinya Okano
  2. xswizard

Commits

Shinya Okano  committed f8a4901

clone/start/shutdown/reboot/suspend/resume

  • Participants
  • Parent commits 5d0b737
  • Branches default

Comments (0)

Files changed (4)

File examples/sample.py

View file
 print vms
 vm = vms[1].snapshot('test_snapshot_%s' % int(time.time()))
 print vm
-#vm.export_as_file('test.xva')
+#vm.export_as_file('test.xva')

File tests.py

View file
+import time
+
+from xswizard.api import API
+
+def main():
+    api = API('https://192.168.11.100/', 'root', 'test')
+    print api
+    vm_template = api.get_instant_vm_template_by_name('ubuntu10.04-server')
+    print vm_template
+    new_vm = vm_template.clone('sandbox')
+    print new_vm
+    new_vm.provision()
+    print new_vm.ref
+    print 'start...'
+    new_vm.start()
+    time.sleep(10)
+    print 'suspend...'
+    new_vm.suspend()
+    time.sleep(10)
+    print 'resume...'
+    new_vm.resume()
+    time.sleep(10)
+    print 'reboot...'
+    new_vm.reboot()
+    time.sleep(20)
+    print 'shutdown...'
+    new_vm.shutdown()
+
+if __name__ == '__main__':
+    main()

File xswizard/api.py

View file
         """
         return self._api.VM.get_snapshots(ref)
 
+    def _vm_clone(self, ref, name):
+        """
+        xenapi.VM.clone
+        """
+        return self._api.VM.clone(ref, name)
+
+    def _vm_provision(self, ref):
+        """
+        xenapi.VM.provision
+        """
+        return self._api.VM.provision(ref)
+
+    def _vm_start(self, ref):
+        """
+        xenapi.VM.start
+        """
+        return self._api.VM.start(ref, False, False)
+
+    def _vm_resume(self, ref):
+        """
+        xenapi.VM.resume
+        """
+        return self._api.VM.resume(ref, False, False)
+
+    def _vm_clean_shutdown(self, ref):
+        """
+        xenapi.VM.clean_shutdown
+        """
+        return self._api.VM.clean_shutdown(ref)
+
+    def _vm_clean_reboot(self, ref):
+        """
+        xenapi.VM.clean_reboot
+        """
+        return self._api.VM.clean_reboot(ref)
+
     def _export(self, uuid):
         """
         export as backup (return stream handler)
         """
         self._vm_suspend(vm.ref)
 
+    def clone_vm(self, vm, name):
+        """
+        clone vm
+        """
+        clone_ref = self._vm_clone(vm.ref, name)
+        return VM(clone_ref, self)
+
+    def provision_vm(self, vm):
+        """
+        provision vm
+        """
+        return self._vm_provision(vm.ref)
+
+    def start_vm(self, vm):
+        """
+        start vm
+        """
+        return self._vm_start(vm.ref)
+
+    def clean_shutdown_vm(self, vm):
+        """
+        clean shutdown vm
+        """
+        return self._vm_clean_shutdown(vm.ref)
+
+    def shutdown_vm(self, vm):
+        """
+        shutdown vm alias
+        """
+        return self.clean_shutdown_vm(vm)
+
+    def clean_reboot_vm(self, vm):
+        """
+        clean reboot vm
+        """
+        return self._vm_clean_reboot(vm.ref)
+
+    def reboot_vm(self, vm):
+        """
+        reboot vm alias
+        """
+        return self.clean_reboot_vm(vm)
+
+    def resume_vm(self, vm):
+        """
+        resume vm
+        """
+        return self._vm_resume(vm.ref)
+
     def get_snapshots(self, vm):
         """
         get_snapshots vm
         data = self._vm_get_all_records()
         return [VM(record, self) for record in data]
 
+    def get_vm_by_name(self, name):
+        """
+        vm by name
+        """
+        vms = self.get_all_vms()
+        filtered = filter(lambda vm: vm.name_label == name, vms)
+        if filtered:
+            return filtered[0]
+
     def get_all_vm_templates(self):
         """
         all vm templates
         """
         vms = self.get_all_vm_templates()
         return filter(lambda vm: vm.is_instant, vms)
+
+    def get_instant_vm_template_by_name(self, name):
+        """
+        vm template by name
+        """
+        vms = self.get_instant_vm_templates()
+        filtered = filter(lambda vm: vm.name_label == name, vms)
+        if filtered:
+            return filtered[0]
+
+    def get_all_vm_not_templates(self):
+        """
+        all vm templates
+        """
+        vms = self.get_all_vms()
+        return filter(lambda vm: not vm.is_a_template, vms)

File xswizard/models.py

View file
         return value == 'true'
     is_instant = property(get_is_instant)
 
+    def get_name_label(self):
+        return self.record['name_label']
+    name_label = property(get_name_label)
+
     def snapshot(self, name):
         return self.api.snapshot_vm(self, name)
 
     def suspend(self):
         return self.api.suspend_vm(self)
 
+    def clone(self, name):
+        return self.api.clone_vm(self, name)
+
+    def provision(self):
+        return self.api.provision_vm(self)
+
+    def start(self):
+        return self.api.start_vm(self)
+
+    def resume(self):
+        return self.api.resume_vm(self)
+
+    def clean_shutdown(self):
+        return self.api.clean_shutdown_vm(self)
+
+    def shutdown(self):
+        return self.api.shutdown_vm(self)
+
+    def clean_reboot(self):
+        return self.api.clean_reboot_vm(self)
+
+    def reboot(self):
+        return self.api.reboot_vm(self)
+
     def get_snapshots(self):
         return self.api.get_snapshots(self)
 
-    def _export(self):
+    def export(self):
         return self.api._export(self.record['uuid'])
 
     def export_as_file(self, filepath):
-        input = self._export()
+        input = self.export()
         output = open(filepath, 'wb')
         while True:
             data = input.read(constants.EXPORT_BLOCK_SIZE)