def rebootVirtualMachine(request): logger.info("重启虚拟机") try: if request.method == 'POST': vmId = request.POST['vmId'] print 'vmid is %s ' % vmId vcenterVirtualMachineModel = VcenterVirtualMachine.objects.get(id=vmId) accountModel = vcenterVirtualMachineModel.account #print accountModel if accountModel is None: res = { 'code': '10021', 'msg': u"重启失败,资源账号错误", } return render_json(res) else: vmManager = VmManage(host=accountModel.vcenter_host,user=accountModel.account_name,password=accountModel.account_password,port=accountModel.vcenter_port,ssl=None) task = vmManager.reboot(vcenterVirtualMachineModel.name) result = vmManager.handleTask(task) #同步信息 if result == False: res = { 'code': '10022', 'msg': u"重启失败", } else: vm = vmManager.find_by_uuid(vcenterVirtualMachineModel.instance_uuid) vcenterVirtualMachineModel.power_state = vm.summary.runtime.powerState vcenterVirtualMachineModel.save() res = { 'code': '0', 'msg': u"重启成功", } except Exception as e: traceback.print_exc() res = { 'code': '50000', 'msg': e.message, } return render_json(res)
def destroyVirtualMachine(request): logger.info("销毁虚拟机") try: if request.method == 'POST': vmId = request.POST['vmId'] vcenterVirtualMachineModel = VcenterVirtualMachine.objects.get(id=vmId) accountModel = vcenterVirtualMachineModel.account if accountModel is None: res = { 'code': '10031', 'msg': u"销毁失败,资源账号错误", } return render_json(res) else: vmManager = VmManage(host=accountModel.vcenter_host,user=accountModel.account_name,password=accountModel.account_password,port=accountModel.vcenter_port,ssl=None) task = vmManager.destroy(vcenterVirtualMachineModel.name) result = vmManager.handleTask(task) #同步信息 if result == False: res = { 'code': '10032', 'msg': u"销毁失败", } else: vcenterVirtualMachineModel.delete() res = { 'code': '0', 'msg': u"销毁成功", } except Exception as e: res = { 'code': '50000', 'msg': e.message, } return render_json(res)