Ejemplo n.º 1
0
 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
Ejemplo n.º 2
0
 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
Ejemplo n.º 3
0
 def create(self, name, project_id=None, password=None):
     '''
     创建用户
     :param name:
     :param project_id:
     :param passward:
     :return:
     '''
     admin_token = get_admin_token()
     assert admin_token != "", "can not login with admin user"
     self._get_domian()
     path = url_user_common
     method = "POST"
     head = {
         "Content-Type": "application/json",
         "X-Auth-Token": admin_token
     }
     params = {
         "user": {
             "name": name,
             "domain_id": self.domain_id,
             "enabled": True
         }
     }
     if project_id:
         params["user"].update({"default_project_id": project_id})
     if password:
         params["user"].update({"password": password})
     ret = send_request(method, IP_keystone, PORT_keystone, path, params,
                        head)
     return ret
Ejemplo n.º 4
0
 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
Ejemplo n.º 5
0
 def get_project_user(self, project_id):
     '''
     获取指定project中的成员
     :return:返回结果中只有user的id,没有name
     '''
     admin_token = get_admin_token()
     assert admin_token != "", "can not login with admin user"
     path = url_project_member
     self._get_role()
     query_dict = {
         "role.id": self.role_id,
         "scope.project.id": project_id,
         "include_subtree": True
     }
     query_str = urllib.urlencode(query_dict)
     path = "%s?%s" % (path, query_str)
     method = "GET"
     params = ""
     head = {
         "Content-Type": "application/json",
         "X-Auth-Token": admin_token
     }
     ret = send_request(method, IP_keystone, PORT_keystone, path, params,
                        head)
     return ret
Ejemplo n.º 6
0
def get_project(user_name):
    global user_token_dict
    user_token = user_token_dict[user_name]
    assert user_token != "", "not login"
    method = "GET"
    path = url_project_id
    params = ''
    head = {"X-Auth-Token": user_token}
    ret = send_request(method, IP_keystone, PORT_keystone, path, params, head)
    return ret
Ejemplo n.º 7
0
def get_project(user_name):
    global user_token_dict
    user_token = user_token_dict[user_name]
    assert user_token != "", "not login"
    method = "GET"
    path = url_project_id
    params = ''
    head = {"X-Auth-Token": user_token}
    ret = send_request(method, IP_keystone, PORT_keystone, path, params, head)
    return ret
Ejemplo n.º 8
0
 def delete(self, backup_id,username):
     ret = 0
     token = self.token_dict[username]
     project_id = self.project_id_dict[username]
     assert token != "", "not login"
     path = url_volume_backup_action.format(project_id=project_id,backup_id=backup_id)
     method = "DELETE"
     head = {"Content-Type": "application/json", "X-Auth-Token": token}
     params = ""
     ret = send_request(method, IP_cinder, PORT_cinder, path, params, head)
     return ret
Ejemplo n.º 9
0
 def list_detail(self, username):
     ret = 0
     token = self.token_dict[username]
     project_id = self.project_id_dict[username]
     assert token != "", "not login"
     path = url_volume_backup_list_detail.format(project_id=project_id)
     method = "GET"
     head = {"Content-Type": "application/json", "X-Auth-Token": token}
     params = ""
     ret = send_request(method, IP_cinder, PORT_cinder, path, params, head)
     return ret
Ejemplo n.º 10
0
 def show_detail(self, vm_id, attach_id,username):
     ret = 0
     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 = "GET"
     head = {"Content-Type": "application/json", "X-Auth-Token": token}
     params = ""
     ret = send_request(method, IP_nova, PORT_nova, path, params, head)
     return ret
Ejemplo n.º 11
0
 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
Ejemplo n.º 12
0
 def create(self, volume_id, backup_name,username):
     ret = 0
     token = self.token_dict[username]
     project_id = self.project_id_dict[username]
     assert token != "", "not login"
     path = url_volume_backup_create.format(project_id=project_id)
     method = "POST"
     head = {"Content-Type": "application/json", "X-Auth-Token": token}
     params = {"backup": {"container": "", "description": "", "name": backup_name, "volume_id": volume_id,
                          "incremental": ""}}
     ret = send_request(method, IP_cinder, PORT_cinder, path, params, head)
     return ret
Ejemplo n.º 13
0
 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
Ejemplo n.º 14
0
 def delete(self, image_id,username):
     ret = 0
     token = self.token_dict[username]
     project_id = self.project_id_dict[username]
     assert token != "", "not login"
     path = url_image_action.format(project_id=project_id,image_id=image_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
Ejemplo n.º 15
0
 def show_detail(self, vm_id, attach_id, username):
     ret = 0
     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 = "GET"
     head = {"Content-Type": "application/json", "X-Auth-Token": token}
     params = ""
     ret = send_request(method, IP_nova, PORT_nova, path, params, head)
     return ret
Ejemplo n.º 16
0
 def restore(self, backup_id, volume_id, volume_name,username):
     ret = 0
     token = self.token_dict[username]
     project_id = self.project_id_dict[username]
     assert token != "", "not login"
     path = url_volume_backup_restore.format(project_id=project_id,backup_id=backup_id)
     method = "POST"
     head = {"Content-Type": "application/json", "X-Auth-Token": token}
     params = {"restore": {"name": volume_name}}
     if volume_id:
         params["restore"].update({"volume_id": volume_id})
     ret = send_request(method, IP_cinder, PORT_cinder, path, params, head)
     return ret
Ejemplo n.º 17
0
 def delete(self, image_id, username):
     ret = 0
     token = self.token_dict[username]
     project_id = self.project_id_dict[username]
     assert token != "", "not login"
     path = url_image_action.format(project_id=project_id,
                                    image_id=image_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
Ejemplo n.º 18
0
 def restore(self, backup_id, volume_id, volume_name, username):
     ret = 0
     token = self.token_dict[username]
     project_id = self.project_id_dict[username]
     assert token != "", "not login"
     path = url_volume_backup_restore.format(project_id=project_id,
                                             backup_id=backup_id)
     method = "POST"
     head = {"Content-Type": "application/json", "X-Auth-Token": token}
     params = {"restore": {"name": volume_name}}
     if volume_id:
         params["restore"].update({"volume_id": volume_id})
     ret = send_request(method, IP_cinder, PORT_cinder, path, params, head)
     return ret
Ejemplo n.º 19
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
Ejemplo n.º 20
0
 def list(self, vm_id,username):
     ret = 0
     try:
         token = self.token_dict[username]
         project_id = self.project_id_dict[username]
         assert token != "", "not login"
         path = url_volume_attach_list.format(project_id=project_id,vm_id=vm_id)
         method = "GET"
         head = {"Content-Type": "application/json", "X-Auth-Token": token}
         params = ""
         ret = send_request(method, IP_nova, PORT_nova, path, params, head)
     except Exception,err:
         ret = 1
         dlog("Volume_attach.list err:%s"%err,lever="ERROR")
Ejemplo n.º 21
0
 def project_user_del(self,project_id,user_id):
     '''
     将user从project中移除
     :return:
     '''
     admin_token = get_admin_token()
     assert admin_token != "","can not login with admin user"
     self._get_role()
     path = url_project_user_del.format(project_id=project_id,user_id=user_id,role_id=self.role_id)
     method = "DELETE"
     params = ""
     head = {"Content-Type": "application/json", "X-Auth-Token": admin_token}
     ret = send_request(method, IP_keystone, PORT_keystone, path, params, head)
     return ret
Ejemplo n.º 22
0
 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
Ejemplo n.º 23
0
 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
Ejemplo n.º 24
0
 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
Ejemplo n.º 25
0
 def list(self, vm_id, username):
     ret = 0
     try:
         token = self.token_dict[username]
         project_id = self.project_id_dict[username]
         assert token != "", "not login"
         path = url_volume_attach_list.format(project_id=project_id,
                                              vm_id=vm_id)
         method = "GET"
         head = {"Content-Type": "application/json", "X-Auth-Token": token}
         params = ""
         ret = send_request(method, IP_nova, PORT_nova, path, params, head)
     except Exception, err:
         ret = 1
         dlog("Volume_attach.list err:%s" % err, lever="ERROR")
Ejemplo n.º 26
0
 def list_detail(self, username):
     ret = 0
     try:
         token = self.token_dict[username]
         project_id = self.project_id_dict[username]
         assert token != "", "not login"
         path = url_image_list_detail.format(project_id=project_id)
         method = "GET"
         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"
     except Exception, err:
         ret = 1
         dlog("Image.list_detail err:%s" % err, lever="ERROR")
Ejemplo n.º 27
0
 def show_detail(self, image_id,username):
     ret = 0
     try:
         token = self.token_dict[username]
         project_id = self.project_id_dict[username]
         assert token != "", "not login"
         path = url_image_action.format(project_id=project_id,image_id=image_id)
         method = "GET"
         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"
     except Exception,err:
         ret = 1
         dlog("Image.show_detail err:%s"%err,lever="ERROR")
Ejemplo n.º 28
0
 def show_detail(self, vm_id, username):
     '''
     列出指定虚拟机详细信息,这个函数不能使用cache,因为创建虚拟机时需要大量调用
     :return:
     '''
     ret = 0
     token = self.token_dict[username]
     project_id = self.project_id_dict[username]
     assert token != "", "not login"
     path = url_vm_action.format(project_id=project_id, vm_id=vm_id)
     method = "GET"
     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
Ejemplo n.º 29
0
 def get_user_project(self,name):
     '''
     获取指定user所在的project
     :param name:
     :return:
     '''
     admin_token = get_admin_token()
     assert admin_token != "","can not login with admin user"
     user_id = self.get_id_by_name(name)
     assert user_id != 1,"get user id faild"
     path = url_user_project.format(user_id=user_id)
     method = "GET"
     params = ""
     head = {"Content-Type": "application/json", "X-Auth-Token": admin_token}
     ret = send_request(method, IP_keystone, PORT_keystone, path, params, head)
     return ret
Ejemplo n.º 30
0
 def show_detail(self, vm_id,username):
     '''
     列出指定虚拟机详细信息,这个函数不能使用cache,因为创建虚拟机时需要大量调用
     :return:
     '''
     ret = 0
     token = self.token_dict[username]
     project_id = self.project_id_dict[username]
     assert token != "", "not login"
     path = url_vm_action.format(project_id=project_id,vm_id=vm_id)
     method = "GET"
     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
Ejemplo n.º 31
0
 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
Ejemplo n.º 32
0
 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
Ejemplo n.º 33
0
 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
Ejemplo n.º 34
0
 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
Ejemplo n.º 35
0
 def list_detail(self,username):
     ret = 0
     try:
         token = self.token_dict[username]
         project_id = self.project_id_dict[username]
         assert token != "", "not login"
         # path = "/v2.1/%s/flavors/detail" % self.project_id
         path = url_flavor_list_detail.format(project_id=project_id)
         method = "GET"
         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"
     except Exception,err:
         ret = 1
         dlog("Flavor.list_detail err:%s"%err,lever="ERROR")
Ejemplo n.º 36
0
 def show_detail(self, volume_id,username):
     '''
     显示指定虚拟卷详细信息
     :param volume_id:
     :return:
     '''
     token = self.token_dict[username]
     project_id = self.project_id_dict[username]
     assert token != "", "not login"
     path = url_volume_action.format(project_id=project_id,volume_id=volume_id)
     method = "GET"
     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
Ejemplo n.º 37
0
 def get_project_user(self,project_id):
     '''
     获取指定project中的成员
     :return:返回结果中只有user的id,没有name
     '''
     admin_token = get_admin_token()
     assert admin_token != "","can not login with admin user"
     path = url_project_member
     self._get_role()
     query_dict = {"role.id":self.role_id,"scope.project.id":project_id,"include_subtree":True}
     query_str = urllib.urlencode(query_dict)
     path = "%s?%s" % (path, query_str)
     method = "GET"
     params = ""
     head = {"Content-Type": "application/json", "X-Auth-Token": admin_token}
     ret = send_request(method, IP_keystone, PORT_keystone, path, params, head)
     return ret
Ejemplo n.º 38
0
 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
Ejemplo n.º 39
0
 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
Ejemplo n.º 40
0
 def show_detail(self, volume_id, username):
     '''
     显示指定虚拟卷详细信息
     :param volume_id:
     :return:
     '''
     token = self.token_dict[username]
     project_id = self.project_id_dict[username]
     assert token != "", "not login"
     path = url_volume_action.format(project_id=project_id,
                                     volume_id=volume_id)
     method = "GET"
     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
Ejemplo n.º 41
0
 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
Ejemplo n.º 42
0
 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
Ejemplo n.º 43
0
 def list_pro(self,query_dict=None):
     '''
     列出所有project,由于都是keystone的接口,所以和user的放在一起
     :param query_dict:
     :return:
     '''
     admin_token = get_admin_token()
     assert admin_token != "","can not login with admin user"
     path = url_project_list
     method = "GET"
     head = {"Content-Type": "application/json", "X-Auth-Token": admin_token}
     params = ""
     if query_dict:
         query_str = urllib.urlencode(query_dict)
         path = "%s?%s" % (path, query_str)
     ret = send_request(method, IP_keystone, PORT_keystone, path, params, head)
     return ret
Ejemplo n.º 44
0
 def show_detail(self, flavor_id, username):
     ret = 0
     try:
         token = self.token_dict[username]
         project_id = self.project_id_dict[username]
         assert token != "", "not login"
         # path = "/v2.1/%s/flavors/%s" % (self.project_id, flavor_id)
         path = url_flavor_action.format(project_id=project_id,
                                         flavor_id=flavor_id)
         method = "GET"
         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"
     except Exception, err:
         ret = 1
         dlog("Flavor")
Ejemplo n.º 45
0
 def list(self, query_dict={},username=""):
     ret = 0
     try:
         token = self.token_dict[username]
         project_id = self.project_id_dict[username]
         assert token != "", "not login"
         path = url_image_list.format(project_id=project_id)
         if query_dict:
             query_str = urllib.urlencode(query_dict)
             path = "%s?%s" % (path, query_str)
         method = "GET"
         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"
     except Exception,err:
         ret = 1
         dlog("Image.list err:%s"%err,lever="ERROR")
Ejemplo n.º 46
0
 def list(self, query_dict={}, username=""):
     ret = 0
     try:
         token = self.token_dict[username]
         project_id = self.project_id_dict[username]
         assert token != "", "not login"
         path = url_image_list.format(project_id=project_id)
         if query_dict:
             query_str = urllib.urlencode(query_dict)
             path = "%s?%s" % (path, query_str)
         method = "GET"
         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"
     except Exception, err:
         ret = 1
         dlog("Image.list err:%s" % err, lever="ERROR")
Ejemplo n.º 47
0
 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
Ejemplo n.º 48
0
 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
Ejemplo n.º 49
0
 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
Ejemplo n.º 50
0
 def list_detail(self,username):
     '''
     虚拟卷详细信息
     :return:
     '''
     try:
         token = self.token_dict[username]
         project_id = self.project_id_dict[username]
         assert token != "", "not login"
         path = url_volume_list_detail.format(project_id=project_id)
         method = "GET"
         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"
     except Exception,err:
         ret = 1
         dlog("Volume.list_detail err:%s"%err,lever="ERROR")
Ejemplo n.º 51
0
 def token_login(self):
     '''
     得到能对项目操作的token
     :return:
     '''
     global token_dict
     ret = 0
     assert self.project_id != "", "proejct_id is none"
     method = "POST"
     path = url_get_token
     params = {
         "auth":
             {
                 "identity":
                     {
                         "methods":
                             [
                                 "password"
                             ],
                         "password":
                             {
                                 "user":
                                     {
                                         "name": self.name,
                                         "domain": {
                                             "name": "default"
                                         },
                                         "password": self.password
                                     }
                             }
                     },
                 "scope":
                     {
                         "project": {
                             "id": self.project_id
                         }
                     }
             }
     }
     head = {"Content-Type": "application/json"}
     ret = send_request(method, IP_keystone, PORT_keystone, path, params, head, flag=1)
     self.token = ret["token_id"]
     token_dict[self.name] = self.token
     return 0
Ejemplo n.º 52
0
 def create(self, volume_id, backup_name, username):
     ret = 0
     token = self.token_dict[username]
     project_id = self.project_id_dict[username]
     assert token != "", "not login"
     path = url_volume_backup_create.format(project_id=project_id)
     method = "POST"
     head = {"Content-Type": "application/json", "X-Auth-Token": token}
     params = {
         "backup": {
             "container": "",
             "description": "",
             "name": backup_name,
             "volume_id": volume_id,
             "incremental": ""
         }
     }
     ret = send_request(method, IP_cinder, PORT_cinder, path, params, head)
     return ret
Ejemplo n.º 53
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
Ejemplo n.º 54
0
 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
Ejemplo n.º 55
0
 def token_login(self):
     '''
     得到能对项目操作的token
     :return:
     '''
     global token_dict
     ret = 0
     assert self.project_id != "", "proejct_id is none"
     method = "POST"
     path = url_get_token
     params = {
         "auth": {
             "identity": {
                 "methods": ["password"],
                 "password": {
                     "user": {
                         "name": self.name,
                         "domain": {
                             "name": "default"
                         },
                         "password": self.password
                     }
                 }
             },
             "scope": {
                 "project": {
                     "id": self.project_id
                 }
             }
         }
     }
     head = {"Content-Type": "application/json"}
     ret = send_request(method,
                        IP_keystone,
                        PORT_keystone,
                        path,
                        params,
                        head,
                        flag=1)
     self.token = ret["token_id"]
     token_dict[self.name] = self.token
     return 0
Ejemplo n.º 56
0
 def get_user_project(self, name):
     '''
     获取指定user所在的project
     :param name:
     :return:
     '''
     admin_token = get_admin_token()
     assert admin_token != "", "can not login with admin user"
     user_id = self.get_id_by_name(name)
     assert user_id != 1, "get user id faild"
     path = url_user_project.format(user_id=user_id)
     method = "GET"
     params = ""
     head = {
         "Content-Type": "application/json",
         "X-Auth-Token": admin_token
     }
     ret = send_request(method, IP_keystone, PORT_keystone, path, params,
                        head)
     return ret
Ejemplo n.º 57
0
 def project_user_del(self, project_id, user_id):
     '''
     将user从project中移除
     :return:
     '''
     admin_token = get_admin_token()
     assert admin_token != "", "can not login with admin user"
     self._get_role()
     path = url_project_user_del.format(project_id=project_id,
                                        user_id=user_id,
                                        role_id=self.role_id)
     method = "DELETE"
     params = ""
     head = {
         "Content-Type": "application/json",
         "X-Auth-Token": admin_token
     }
     ret = send_request(method, IP_keystone, PORT_keystone, path, params,
                        head)
     return ret
Ejemplo n.º 58
0
 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
Ejemplo n.º 59
0
 def list_pro(self, query_dict=None):
     '''
     列出所有project,由于都是keystone的接口,所以和user的放在一起
     :param query_dict:
     :return:
     '''
     admin_token = get_admin_token()
     assert admin_token != "", "can not login with admin user"
     path = url_project_list
     method = "GET"
     head = {
         "Content-Type": "application/json",
         "X-Auth-Token": admin_token
     }
     params = ""
     if query_dict:
         query_str = urllib.urlencode(query_dict)
         path = "%s?%s" % (path, query_str)
     ret = send_request(method, IP_keystone, PORT_keystone, path, params,
                        head)
     return ret
Ejemplo n.º 60
0
 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