def change(self, volume_id, description, name="", username=""): ''' 修改磁盘信息 :return: ''' token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" cache(del_cache=get_origin_addr(self.list)) cache(del_cache=get_origin_addr(self.list_detail)) path = url_volume_change.format(project_id=project_id, volume_id=volume_id) method = "PUT" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"volume": {"description": description}} if name: params["volume"].update({"name": name}) ret = send_request(method, IP_cinder, PORT_cinder, path, params, head) assert ret != 1, "send_request error" t = run_in_thread(self.wait_complete, volume_id, ["available"], username, timeout=TIMEOUT) assert t == 0 return ret
def delete(self, vm_id,username): ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] cache(del_cache=get_origin_addr(self.list)) assert token != "", "not login" path = url_vm_action.format(project_id=project_id,vm_id=vm_id) method = "DELETE" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = "" ret = send_request(method, IP_nova, PORT_nova, path, params, head) return ret
def delete(self, vm_id, username): ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] cache(del_cache=get_origin_addr(self.list)) assert token != "", "not login" path = url_vm_action.format(project_id=project_id, vm_id=vm_id) method = "DELETE" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = "" ret = send_request(method, IP_nova, PORT_nova, path, params, head) return ret
def create(self, size, availability_zone="", name="", des="", metadata="", volume_type="ceph", snapshot_id="", flag=0, username=""): ''' 创建虚拟卷 :return: :flag:阻塞标志,0表示非阻塞,1表示阻塞 ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" cache(del_cache=get_origin_addr(self.list)) cache(del_cache=get_origin_addr(self.list_detail)) path = url_volume_create.format(project_id=project_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"volume": {"size": size}} if availability_zone: params["volume"].update({"availability_zone": availability_zone}) if name: params["volume"].update({"display_name": name}) if des: params["volume"].update({"display_description": des}) if metadata: params["volume"].update({"metadata": metadata}) if volume_type: params["volume"].update({"volume_type": volume_type}) if snapshot_id: params["volume"].update({"snapshot_id": snapshot_id}) ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" if flag: volume_id = ret["volume"].get("id", "") t = run_in_thread(self.wait_complete, volume_id, ["available"], username, timeout=TIMEOUT) if t != 0: self.result.update({name: 2}) else: self.result.update({name: 1}) return ret
def delete(self, vm_id, attach_id,username): ret = 0 cache(func_str="list") cache(func_str="list_detail") token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" path = url_volume_attach_action.format(project_id=project_id,vm_id=vm_id,attach_id=attach_id) method = "DELETE" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = "" ret = send_request(method, IP_nova, PORT_nova, path, params, head) t = run_in_thread(self.wait_complete,attach_id,"detaching",username,timeout=TIMEOUT) assert t == 0 return ret
def delete(self, volume_id,username): ''' 删除虚拟卷 :param volume_id: :return: ''' token = self.token_dict[username] project_id = self.project_id_dict[username] cache(del_cache=get_origin_addr(self.list)) cache(del_cache=get_origin_addr(self.list_detail)) assert token != "", "not login" path = url_volume_action.format(project_id=project_id,volume_id=volume_id) method = "DELETE" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = '' ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" return ret
def extend(self, volume_id, size,username): ''' 扩展虚拟卷容量 :param volume_id: ''' token = self.token_dict[username] project_id = self.project_id_dict[username] cache(func_str="list") cache(func_str="list_detail") assert token != "", "not login" path = url_volume_extend.format(project_id=project_id,volume_id=volume_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"os-extend": {"new_size": size}} ret = send_request(method, IP_cinder, PORT_cinder, path, params, head) assert ret != 1, "send_request error" t = run_in_thread(self.wait_complete, volume_id, ["available"],username,timeout=TIMEOUT) assert t == 0 return ret
def delete(self, volume_id, username): ''' 删除虚拟卷 :param volume_id: :return: ''' token = self.token_dict[username] project_id = self.project_id_dict[username] cache(del_cache=get_origin_addr(self.list)) cache(del_cache=get_origin_addr(self.list_detail)) assert token != "", "not login" path = url_volume_action.format(project_id=project_id, volume_id=volume_id) method = "DELETE" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = '' ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" return ret
def delete(self, vm_id, attach_id, username): ret = 0 cache(func_str="list") cache(func_str="list_detail") token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" path = url_volume_attach_action.format(project_id=project_id, vm_id=vm_id, attach_id=attach_id) method = "DELETE" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = "" ret = send_request(method, IP_nova, PORT_nova, path, params, head) t = run_in_thread(self.wait_complete, attach_id, "detaching", username, timeout=TIMEOUT) assert t == 0 return ret
def change(self, volume_id,description,name="",username=""): ''' 修改磁盘信息 :return: ''' token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" cache(del_cache=get_origin_addr(self.list)) cache(del_cache=get_origin_addr(self.list_detail)) path = url_volume_change.format(project_id=project_id,volume_id=volume_id) method = "PUT" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"volume": {"description":description}} if name: params["volume"].update({"name": name}) ret = send_request(method, IP_cinder, PORT_cinder, path, params, head) assert ret != 1, "send_request error" t = run_in_thread(self.wait_complete, volume_id, ["available"],username,timeout=TIMEOUT) assert t == 0 return ret
def change(self,snapshot_id,des,name="",username=""): ''' 修改快照名称和描述信息 :param name: :param des: :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "","not login" cache(del_cache=get_origin_addr(self.list)) cache(del_cache=get_origin_addr(self.list_detail)) path = url_volume_snap_change.format(project_id=project_id,snapshot_id=snapshot_id) method = "PUT" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"snapshot":{"description":des}} if name: params["snapshot"].update({"name":name}) ret = send_request(method, IP_cinder, PORT_cinder, path, params, head) return ret
def change(self, snapshot_id, des, name="", username=""): ''' 修改快照名称和描述信息 :param name: :param des: :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" cache(del_cache=get_origin_addr(self.list)) cache(del_cache=get_origin_addr(self.list_detail)) path = url_volume_snap_change.format(project_id=project_id, snapshot_id=snapshot_id) method = "PUT" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"snapshot": {"description": des}} if name: params["snapshot"].update({"name": name}) ret = send_request(method, IP_cinder, PORT_cinder, path, params, head) return ret
def user_token_login(self): ''' 得到一个用户的token,但是没有对项目相关操作的权限 :return: ''' global user_token_dict ret = 0 cache(username=self.name) method = "POST" path = url_get_token params = { "auth": { "identity": { "methods": [ "password" ], "password": { "user": { "name": self.name, "domain": { "name": "default" }, "password": self.password } } } } } cache(del_cache="*") #切换用户时清除缓存 head = {"Content-Type": "application/json"} ret = send_request(method, IP_keystone, PORT_keystone, path, params, head, flag=1) self.user_token = ret["token_id"] user_token_dict[self.name] = self.user_token return 0
def create(self, size, availability_zone="", name="", des="", metadata="", volume_type="ceph", snapshot_id="", flag=0,username=""): ''' 创建虚拟卷 :return: :flag:阻塞标志,0表示非阻塞,1表示阻塞 ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" cache(del_cache=get_origin_addr(self.list)) cache(del_cache=get_origin_addr(self.list_detail)) path = url_volume_create.format(project_id=project_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"volume": {"size": size}} if availability_zone: params["volume"].update({"availability_zone": availability_zone}) if name: params["volume"].update({"display_name": name}) if des: params["volume"].update({"display_description": des}) if metadata: params["volume"].update({"metadata": metadata}) if volume_type: params["volume"].update({"volume_type": volume_type}) if snapshot_id: params["volume"].update({"snapshot_id": snapshot_id}) ret = send_request(method, IP_nova, PORT_nova, path, params, head) assert ret != 1, "send_request error" if flag: volume_id = ret["volume"].get("id", "") t = run_in_thread(self.wait_complete, volume_id, ["available"],username,timeout=TIMEOUT) if t != 0: self.result.update({name: 2}) else: self.result.update({name: 1}) return ret
def user_token_login(self): ''' 得到一个用户的token,但是没有对项目相关操作的权限 :return: ''' global user_token_dict ret = 0 cache(username=self.name) method = "POST" path = url_get_token params = { "auth": { "identity": { "methods": ["password"], "password": { "user": { "name": self.name, "domain": { "name": "default" }, "password": self.password } } } } } cache(del_cache="*") #切换用户时清除缓存 head = {"Content-Type": "application/json"} ret = send_request(method, IP_keystone, PORT_keystone, path, params, head, flag=1) self.user_token = ret["token_id"] user_token_dict[self.name] = self.user_token return 0
def extend(self, volume_id, size, username): ''' 扩展虚拟卷容量 :param volume_id: ''' token = self.token_dict[username] project_id = self.project_id_dict[username] cache(func_str="list") cache(func_str="list_detail") assert token != "", "not login" path = url_volume_extend.format(project_id=project_id, volume_id=volume_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"os-extend": {"new_size": size}} ret = send_request(method, IP_cinder, PORT_cinder, path, params, head) assert ret != 1, "send_request error" t = run_in_thread(self.wait_complete, volume_id, ["available"], username, timeout=TIMEOUT) assert t == 0 return ret
def create(self, volume_id, snap_name="", des="",username=""): ''' 创建快照 :param volume_id: :param snap_name: :param des: :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" cache(del_cache=get_origin_addr(self.list)) cache(del_cache=get_origin_addr(self.list_detail)) path = url_volume_snap_create.format(project_id=project_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token":token} params = {"snapshot": {"volume_id": volume_id}} if snap_name: params["snapshot"].update({"display_name": snap_name}) if des: params["snapshot"].update({"display_description": des}) ret = send_request(method, IP_nova, PORT_nova, path, params, head) return ret
def create(self, volume_id, snap_name="", des="", username=""): ''' 创建快照 :param volume_id: :param snap_name: :param des: :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" cache(del_cache=get_origin_addr(self.list)) cache(del_cache=get_origin_addr(self.list_detail)) path = url_volume_snap_create.format(project_id=project_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"snapshot": {"volume_id": volume_id}} if snap_name: params["snapshot"].update({"display_name": snap_name}) if des: params["snapshot"].update({"display_description": des}) ret = send_request(method, IP_nova, PORT_nova, path, params, head) return ret
def update(self, vm_id, attach_id, volume_id,username): ret = 0 cache(func_str="list") cache(func_str="list_detail") token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" cache(del_cache=get_origin_addr(self.list)) path = url_volume_attach_action.format(project_id=project_id,vm_id=vm_id,attach_id=attach_id) method = "PUT" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"volumeAttachment": {"volumeId": volume_id}} ret = send_request(method, IP_nova, PORT_nova, path, params, head) return ret
def update(self, vm_id, attach_id, volume_id, username): ret = 0 cache(func_str="list") cache(func_str="list_detail") token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" cache(del_cache=get_origin_addr(self.list)) path = url_volume_attach_action.format(project_id=project_id, vm_id=vm_id, attach_id=attach_id) method = "PUT" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"volumeAttachment": {"volumeId": volume_id}} ret = send_request(method, IP_nova, PORT_nova, path, params, head) return ret
def attach(self, vm_id, volum_id, device_name="",username=""): ''' 虚拟磁盘连接虚拟机 :param vm_id: :param volum_id: :param device_name:虚拟机上的盘符名,如/dev/sdb :return: ''' ret = 0 cache(func_str="list") cache(func_str="list_detail") token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" cache(del_cache=get_origin_addr(self.list)) path = url_volume_attach_create.format(project_id=project_id,vm_id=vm_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"volumeAttachment": {"volumeId": volum_id}} if device_name: params["volumeAttachment"].update({"device": device_name}) ret = send_request(method, IP_nova, PORT_nova, path, params, head) return ret
def attach(self, vm_id, volum_id, device_name="", username=""): ''' 虚拟磁盘连接虚拟机 :param vm_id: :param volum_id: :param device_name:虚拟机上的盘符名,如/dev/sdb :return: ''' ret = 0 cache(func_str="list") cache(func_str="list_detail") token = self.token_dict[username] project_id = self.project_id_dict[username] assert token != "", "not login" cache(del_cache=get_origin_addr(self.list)) path = url_volume_attach_create.format(project_id=project_id, vm_id=vm_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} params = {"volumeAttachment": {"volumeId": volum_id}} if device_name: params["volumeAttachment"].update({"device": device_name}) ret = send_request(method, IP_nova, PORT_nova, path, params, head) return ret
def create(self, name, flavor, image, password, userdata, key_name="",disk=None,username=""): ''' 创建虚拟机,创建的接口在后台应该是异步执行的,当创建的请求发送过去后很快会有结果返回,但是虚拟机实际可能还没有创建成功 所以需要先判断虚拟机的创建状态,如果是完成的再绑定磁盘 :param name: :param flavor: :param image: :param password: :param userdata: :param disk:如果创建时需要选择磁盘则传,格式为: [ { "name":"",可选 "size":"", "availability_zone":"",#可选 "des":"",#可选 "metadata":{},#可选 "volume_type":""#可选, "snapshot_id":""#可选, "dev_name":"连接虚拟机后的盘符名"#可选 } ] :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] cache(del_cache=get_origin_addr(self.list)) if disk is None: disk = [] self.result.update({name: {"name": name, "id": "", "status_vm": 0, "status_disk": {}}}) # 虚拟机创建状态,0表示创建中,1表示成功,2表示失败 assert token != "", "not login" path = url_vm_create.format(project_id=project_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} avzone = self.get_avzone() params = {"server": {"name": name, "flavorRef": flavor, "imageRef": image, "adminPass": password, "availability_zone":avzone}} if userdata: params["server"].update({"user_data":userdata}) if key_name: params["server"].update({"key_name": key_name}) ret = send_request(method, IP_nova, PORT_nova, path, params, head) vm_id = ret["server"]["id"] vm_snap = Vm_snap() vm_snap.set_vm(vm_id) vm_snap.create_root_snap(image) self.result[name]["id"] = vm_id if disk: volume = Volume() volume_attach = Volume_attach() vm_compele_flag = 0 # 判断虚拟机是否创建完成的标志,如果置1则下面不再判断创建的状态 for tmp_dict in disk: time_int = int(time.time()) name_disk = tmp_dict.get("name", "%s_%s" % (name, time_int)) self.result[name]["status_disk"].update({name_disk: 0}) size = tmp_dict["size"] availability_zone = tmp_dict.get("availability_zone", "") des = tmp_dict.get("des", "") metadata = tmp_dict.get("metadata", "") volume_type = tmp_dict.get("volume_type", "") snapshot_id = tmp_dict.get("snapshot_id", "") tmpret = volume.create(size, availability_zone, name_disk, des, metadata, volume_type, snapshot_id,username=username) dev_name = tmp_dict.get("dev_name", "") volume_id = tmpret["volume"]["id"] if not vm_compele_flag: t1 = run_in_thread(self.wait_complete, vm_id,username,timeout=TIMEOUT) if t1 == 0: vm_compele_flag = 1 t2 = run_in_thread(volume.wait_complete, volume_id, ["available"],username, timeout=TIMEOUT) # assert vm_compele_flag == 1, "vm status is not activate" if not vm_compele_flag: self.result[name]["status_vm"] = 2 ret = 1 break # assert t2 == 0, "volume status is not available" if t2 != 0: self.result[name]["status_disk"][name_disk] = 2 continue self.result[name]["status_disk"][name_disk] = 1 volume_attach.attach(vm_id, volume_id, dev_name,username) self.result[name]["status_vm"] = 1 else: t = run_in_thread(self.wait_complete, vm_id,username,timeout=TIMEOUT) self.result[name]["status_vm"] = 1 if t == 0 else 2 return ret
def create(self, name, flavor, image, password, userdata, key_name="", disk=None, username=""): ''' 创建虚拟机,创建的接口在后台应该是异步执行的,当创建的请求发送过去后很快会有结果返回,但是虚拟机实际可能还没有创建成功 所以需要先判断虚拟机的创建状态,如果是完成的再绑定磁盘 :param name: :param flavor: :param image: :param password: :param userdata: :param disk:如果创建时需要选择磁盘则传,格式为: [ { "name":"",可选 "size":"", "availability_zone":"",#可选 "des":"",#可选 "metadata":{},#可选 "volume_type":""#可选, "snapshot_id":""#可选, "dev_name":"连接虚拟机后的盘符名"#可选 } ] :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] cache(del_cache=get_origin_addr(self.list)) if disk is None: disk = [] self.result.update({ name: { "name": name, "id": "", "status_vm": 0, "status_disk": {} } }) # 虚拟机创建状态,0表示创建中,1表示成功,2表示失败 assert token != "", "not login" path = url_vm_create.format(project_id=project_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} avzone = self.get_avzone() params = { "server": { "name": name, "flavorRef": flavor, "imageRef": image, "adminPass": password, "availability_zone": avzone } } if userdata: params["server"].update({"user_data": userdata}) if key_name: params["server"].update({"key_name": key_name}) ret = send_request(method, IP_nova, PORT_nova, path, params, head) vm_id = ret["server"]["id"] vm_snap = Vm_snap() vm_snap.set_vm(vm_id) vm_snap.create_root_snap(image) self.result[name]["id"] = vm_id if disk: volume = Volume() volume_attach = Volume_attach() vm_compele_flag = 0 # 判断虚拟机是否创建完成的标志,如果置1则下面不再判断创建的状态 for tmp_dict in disk: time_int = int(time.time()) name_disk = tmp_dict.get("name", "%s_%s" % (name, time_int)) self.result[name]["status_disk"].update({name_disk: 0}) size = tmp_dict["size"] availability_zone = tmp_dict.get("availability_zone", "") des = tmp_dict.get("des", "") metadata = tmp_dict.get("metadata", "") volume_type = tmp_dict.get("volume_type", "") snapshot_id = tmp_dict.get("snapshot_id", "") tmpret = volume.create(size, availability_zone, name_disk, des, metadata, volume_type, snapshot_id, username=username) dev_name = tmp_dict.get("dev_name", "") volume_id = tmpret["volume"]["id"] if not vm_compele_flag: t1 = run_in_thread(self.wait_complete, vm_id, username, timeout=TIMEOUT) if t1 == 0: vm_compele_flag = 1 t2 = run_in_thread( volume.wait_complete, volume_id, ["available"], username, timeout=TIMEOUT ) # assert vm_compele_flag == 1, "vm status is not activate" if not vm_compele_flag: self.result[name]["status_vm"] = 2 ret = 1 break # assert t2 == 0, "volume status is not available" if t2 != 0: self.result[name]["status_disk"][name_disk] = 2 continue self.result[name]["status_disk"][name_disk] = 1 volume_attach.attach(vm_id, volume_id, dev_name, username) self.result[name]["status_vm"] = 1 else: t = run_in_thread(self.wait_complete, vm_id, username, timeout=TIMEOUT) self.result[name]["status_vm"] = 1 if t == 0 else 2 return ret