def resize(self, vm_id, flavor_id, username): ''' 更改虚拟机flavor类型 :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" path = url_vm_control_action.format(project_id=project_id, vm_id=vm_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = { "resize": { "flavorRef": flavor_id, "OS-DCF:diskConfig": "AUTO" } } ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" t1 = run_in_thread(self.wait_complete, vm_id, ["VERIFY_RESIZE"], username, timeout=TIMEOUT) assert t1 == 0 params = {"confirmResize": ""} ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" t2 = run_in_thread(self.wait_complete, vm_id, ["ACTIVE", "SHUTOFF"], username, timeout=TIMEOUT) assert t2 == 0 return ret
def rebuild(self, vm_id, image_id, name, adminPass="", metadata="", personality="", preserve_ephemeral=False, username=""): ''' 主机从镜像(快照)还原 :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" path = url_vm_control_action.format(project_id=project_id, vm_id=vm_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"rebuild": {"imageRef": image_id, "name": name}} if adminPass: params["rebuild"].update({"adminPass": adminPass}) if metadata: params["rebuild"].update({"metadata": metadata}) if personality: params["rebuild"].update({"personality": personality}) if preserve_ephemeral: params["rebuild"].update({"preserve_ephemeral": True}) ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" return ret
def get_console(self, vm_id,username): ''' 主机备份 :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" path = url_vm_control_action.format(project_id=project_id,vm_id=vm_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"os-getVNCConsole": {"type": "novnc"}} ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" return ret
def create_backup(self, vm_id, name, rotation, type="daily",username=""): ''' 主机备份 :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" path = url_vm_control_action.format(project_id=project_id,vm_id=vm_id) method = "POST" head = {"Content-Type": "application/json", "X-uth-Token": token} params = {"createBackup": {"name": name, "backup_type": type, "rotation": rotation}} ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" return ret
def get_console(self, vm_id, username): ''' 主机备份 :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" path = url_vm_control_action.format(project_id=project_id, vm_id=vm_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"os-getVNCConsole": {"type": "novnc"}} ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" return ret
def reboot(self, vm_id, type="HARD", username=""): ''' 重启虚拟机 :param vm_id: :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" path = url_vm_control_action.format(project_id=project_id, vm_id=vm_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"reboot": {"type": type}} ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" return ret
def reboot(self, vm_id, type="HARD",username=""): ''' 重启虚拟机 :param vm_id: :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" path = url_vm_control_action.format(project_id=project_id,vm_id=vm_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"reboot": {"type": type}} ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" return ret
def create_image(self, vm_id, image_name,username): ''' 创建镜像 只有在ACTIVE, SHUTOFF, PAUSED, 或 SUSPENDED的状态下才能制作镜像 :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" path = url_vm_control_action.format(project_id=project_id,vm_id=vm_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"createImage": {"name": image_name, "metadata": {"meta_var": "meta_val"}}} ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" return ret
def live_migrate(self, vm_id, host, block_migration, disk_over_commit,username): ''' 虚拟机热迁移 :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" path = url_vm_control_action.format(project_id=project_id,vm_id=vm_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = { "os-migrateLive": {"host": host, "block_migration": block_migration, "disk_over_commit": disk_over_commit}} ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" return ret
def get_console_log(self, vm_id, length=0,username=""): ''' 获取虚拟机日志 :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" path = url_vm_control_action.format(project_id=project_id,vm_id=vm_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"os-getConsoleOutput": {}} if length: params["os-getConsoleOutput"].update({"length":length}) ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" return ret
def get_console_log(self, vm_id, length=0, username=""): ''' 获取虚拟机日志 :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" path = url_vm_control_action.format(project_id=project_id, vm_id=vm_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"os-getConsoleOutput": {}} if length: params["os-getConsoleOutput"].update({"length": length}) ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" return ret
def stop(self, vm_id,username): ''' 停止虚拟机 :param vm_id: :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" path = url_vm_control_action.format(project_id=project_id,vm_id=vm_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"os-stop": ""} ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" t1 = run_in_thread(self.wait_complete, vm_id, ["SHUTOFF"],username,timeout=TIMEOUT) assert t1 == 0 return ret
def stop(self, vm_id, username): ''' 停止虚拟机 :param vm_id: :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" path = url_vm_control_action.format(project_id=project_id, vm_id=vm_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"os-stop": ""} ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" t1 = run_in_thread(self.wait_complete, vm_id, ["SHUTOFF"], username, timeout=TIMEOUT) assert t1 == 0 return ret
def create_backup(self, vm_id, name, rotation, type="daily", username=""): ''' 主机备份 :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" path = url_vm_control_action.format(project_id=project_id, vm_id=vm_id) method = "POST" head = {"Content-Type": "application/json", "X-uth-Token": token} params = { "createBackup": { "name": name, "backup_type": type, "rotation": rotation } } ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" return ret
def resize(self, vm_id, flavor_id,username): ''' 更改虚拟机flavor类型 :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" path = url_vm_control_action.format(project_id=project_id,vm_id=vm_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"resize": {"flavorRef": flavor_id, "OS-DCF:diskConfig": "AUTO"}} ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" t1 = run_in_thread(self.wait_complete, vm_id, ["VERIFY_RESIZE"],username,timeout=TIMEOUT) assert t1 == 0 params = {"confirmResize": ""} ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" t2 = run_in_thread(self.wait_complete, vm_id, ["ACTIVE", "SHUTOFF"],username,timeout=TIMEOUT) assert t2 == 0 return ret
def live_migrate(self, vm_id, host, block_migration, disk_over_commit, username): ''' 虚拟机热迁移 :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" path = url_vm_control_action.format(project_id=project_id, vm_id=vm_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = { "os-migrateLive": { "host": host, "block_migration": block_migration, "disk_over_commit": disk_over_commit } } ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" return ret
def rebuild(self, vm_id, image_id, name, adminPass="", metadata="", personality="", preserve_ephemeral=False,username=""): ''' 主机从镜像(快照)还原 :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" path = url_vm_control_action.format(project_id=project_id,vm_id=vm_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"rebuild": {"imageRef": image_id, "name": name}} if adminPass: params["rebuild"].update({"adminPass": adminPass}) if metadata: params["rebuild"].update({"metadata": metadata}) if personality: params["rebuild"].update({"personality": personality}) if preserve_ephemeral: params["rebuild"].update({"preserve_ephemeral": True}) ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" return ret
def create_image(self, vm_id, image_name, username): ''' 创建镜像 只有在ACTIVE, SHUTOFF, PAUSED, 或 SUSPENDED的状态下才能制作镜像 :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" path = url_vm_control_action.format(project_id=project_id, vm_id=vm_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = { "createImage": { "name": image_name, "metadata": { "meta_var": "meta_val" } } } ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" return ret