def moveVMItem(): data = [] duration = 0 result = True error = "" startTime = datetime.now() try: if request.method == 'POST': uuid = request.json['uuid'] owner = request.json['owner'] user = Users.get_by_username(owner) if (user != None): if (VMList.update_vm_byname(uuid, owner)): endTime = datetime.now() duration = endTime - startTime response = responseDataFormat(result, data, error, str(duration)) return RecursiveDumpObject(response) else: result = False error = "Update Failed!" else: result = False error = "User not exist!" except Exception as error: result = False print error.message finally: endTime = datetime.now() response = responseDataFormat(result, None, error, str(endTime - startTime)) return RecursiveDumpObject(response)
def gettaskstatus(): startTime = datetime.now() task_id = request.json['task_id'] #query task_id task = task_list[task_id] if (task is None): endTime = datetime.now() response = responseDataFormat(False, 'Invalid task id', None, str(endTime - startTime)) return RecursiveDumpObject(response) endTime = datetime.now() response = responseDataFormat(True, task, None, str(endTime - startTime)) return RecursiveDumpObject(response)
def GetVMList(): #data = [] duration = 0 result = True error = "" startTime = datetime.now() if 'username' in session: owner = escape(session['username']) VMs = VMList.get_by_owner(owner) data = [r.as_dict() for r in VMs] #data = jsonify(result=result) #for r in VMs: #jsonVM = dict(r.items()) #jsonVM = {} #jsonVM['name'] = r.name #jsonVM['status'] = r.status #jsonVM['owner'] = r.owner #jsonVM['uuid'] = r.uuid #jsonVM['hostname'] = r.hostname #jsonVM['ip'] = r.ip #jsonVM['account'] = r.account #data.append(jsonVM) endTime = datetime.now() duration = endTime - startTime response = responseDataFormat(result, data, error, str(duration)) return RecursiveDumpObject(response) else: result = False error = "User not exist!" return RecursiveDumpObject(response)
def forgetPW(): if request.method == 'POST': startTime = time.time() error = "" data = "" result = False emailAddress = request.json['email'] user = Users.get_by_email(emailAddress) if user is not None: newPassword = GenPassword(10) user.passwordset = newPassword db.session.commit() data = newPassword result = True #if SendEmail(user.name, newPassword): # result = True #else: # error = "It is failed to send email!" else: error = "The email is not registered!" endTime = time.time() duration = endTime - startTime response = responseDataFormat(result, data, error, duration) return RecursiveDumpObject(response) else: return render_template("forgetpassword.html")
def changePW(): if request.method == 'POST': startTime = time.time() error = "" data = "" result = False if 'username' in session: oldPassword = request.json['oldpassword'] newPassword = request.json['newpassword'] curUserName = session['username'] user = Users.get_by_username(curUserName) if user is not None: if user.check_password(oldPassword): user.passwordset = newPassword db.session.commit() result = True else: error = "The password is not valid, please try another one!" else: error = "The current user doesn't exist!" else: error = "There is no valid login user currentlly!" endTime = time.time() duration = endTime - startTime response = responseDataFormat(result, data, error, duration) return RecursiveDumpObject(response)
def delVMItem(): data = [] duration = 0 result = True error = "" startTime = datetime.now() if request.method == 'POST': uuid = request.json['uuid'] if (VMList.delete_vm_byname(uuid)): endTime = datetime.now() duration = endTime - startTime response = responseDataFormat(result, data, error, str(duration)) return RecursiveDumpObject(response) else: endTime = datetime.now() duration = endTime - startTime result = False response = responseDataFormat(result, data, error, str(duration)) return RecursiveDumpObject(response)
def sigup(): if request.method == 'POST': startTime = time.time() error = "" data = "" new_username = request.json['username'] new_email = request.json['email'] new_password = request.json['password'] error = None if Users.get_by_username(new_username): error = "The user name already exists, please try another one!" else: if Users.validate_email(new_email) == 0: error = "The email address is not valid, please try another one!" else: if Users.get_by_useremail(new_email): error = "The email address already exists, please try another one!" if error is not None: result = False endTime = time.time() duration = endTime - startTime response = responseDataFormat(result, data, error, duration) return RecursiveDumpObject(response) else: new_user = Users(name=new_username, email=new_email, passwordset=new_password) db.session.add(new_user) db.session.flush() db.session.commit() result = True endTime = time.time() duration = endTime - startTime response = responseDataFormat(result, data, error, duration) return RecursiveDumpObject(response) else: return render_template("signup.html")
def getCurrentUser(): data = {} duration = 0 result = True error = "" if 'username' in session: data['name'] = escape(session['username']) response = responseDataFormat(result, data, error, duration) return RecursiveDumpObject(response) else: result = False error = "User not exist!" return RecursiveDumpObject(response)
def login(): if request.method == 'POST': startTime = time.time() error = "" data = "" username = request.json['username'] password = request.json['password'] user = Users.get_by_username(username) if user is not None: if user.check_password(password): session['username'] = username result = True endTime = time.time() duration = endTime - startTime response = responseDataFormat(result, data, error, duration) return RecursiveDumpObject(response) else: error = "The password is not valid, please try another one!" else: error = "The user name doesn't exist, please try another one!" result = False endTime = time.time() duration = endTime - startTime response = responseDataFormat(result, data, error, duration) return RecursiveDumpObject(response) else: if 'username' in session: return redirect('../static/Home.html') else: return render_template("signin.html")
def new_vm(): proj_name = request.json['proj_name'] template_name = request.json['img_name'] dns_name = request.json['dns_name'] user_name= request.json['user_name'] password = request.json['password'] #response = responseDataFormat() startTime = datetime.now() template_name = 'Windows_8.1_Professional_x64_Enu' try: # Search the new VM DNS name in database old_vm = VMList.get_by_name(dns_name) if old_vm is not None: result = True error = "The DNS name of new VM has been existing, please try another one." endTime = datetime.now() response = responseDataFormat(result, None, error, str(endTime - startTime)) return RecursiveDumpObject(response) id = GetTaskId(); threading.Thread(target = do_new_vm, args = (session['username'], proj_name, template_name, dns_name, user_name, password, id)).start() result = True except Exception as error: result = False print error.message finally: endTime = datetime.now() response = responseDataFormat(result, id, None, str(endTime - startTime)) return RecursiveDumpObject(response)
def power_off(): startTime = datetime.now() uuid = request.json['uuid'] result = False message = "" try: vm = VCenter.get_vm_by_uuid(uuid=uuid) vm.power_on_vm() result = True message = "The virtual machine (" + uuid + ") is powered off." except Exception as e: print(e.message) finally: endTime = datetime.now() response = responseDataFormat(result, message, None, str(endTime - startTime)) return RecursiveDumpObject(response)
def get_all_templates(): template_info_list = [] startTime = datetime.now() try: vc = GetFreeVCenterConnection() template_list = vc.get_all_templates() for t in template_list: r = dict(t.__dict__) del r['_VirtualMachine__snapshot_list'] del r['_VirtualMachine__vm'] template_info_list.append(r) result = True except: result = False finally: FreeVCenterConnection(vc) endTime = datetime.now() response = responseDataFormat(result, template_info_list, None, str(endTime - startTime)) return RecursiveDumpObject(response)
def newVMItem(): data = [] duration = 0 result = True error = "" startTime = datetime.now() if request.method == 'POST': name = "VM2" status = "power off" owner = "111" uuid = "12345678" hostname = "Vmhost" ip = "10.224.102.13" account = "user1" dnsname = "test" description = "description" vmitem = VMList(name, status, owner, uuid, hostname, ip, account, dnsname, description) VMList.add_vm_item(vmitem) endTime = datetime.now() duration = endTime - startTime response = responseDataFormat(result, data, error, str(duration)) return RecursiveDumpObject(response)
def create_new_virtual_machine(): startTime = datetime.now() project_name = request.json['project_name'] guest_os = request.json['guest_os'] guest_os_version = request.json['guest_os_version'] language = request.json['language'] dns_name = request.json['dns_name'] domain_name = request.json['domain_name'] vcpu = request.json['vcpu'] memory = request.json['memory'] storage = request.json['storage'] user_name = request.json['user_name'] password = request.json['password'] template_name = __get_template_name(guest_os=guest_os, language=language, guest_os_version=guest_os_version) try: # Search the new VM DNS name in database is_existed = VMList.get_by_name(dns_name) if is_existed is not None: result = False error = "The DNS name of new VM has been existing, please try another one." else: result = True error = "" data = [] name = dns_name status = "power off" owner = "111" uuid = random.randint(100, 1000) hostname = "Vmhost" ip = "10.224.102.13" account = user_name dnsname = dns_name description = "description" item = VMList(name, status, owner, uuid, hostname, ip, account, dnsname, description) VMList.add_vm_item(item) result = True response = responseDataFormat(result, data, error, str(endTime - startTime)) #id = get_task_id() #Thread(target=do_new_vm, args=(session['username'], # project_name, # template_name, # dns_name, # user_name, # password, # id)).start() #result = True except Exception as error: result = False print error.message finally: endTime = datetime.now() response = responseDataFormat(result, None, error, str(endTime - startTime)) return RecursiveDumpObject(response)
def get_os_version_list(): startTime = datetime.now() endTime = datetime.now() response = responseDataFormat(True, os_version_list, None, str(endTime - startTime)) return RecursiveDumpObject(response)