예제 #1
0
def resource_ready_destroy():
    logger.logger.info(
        "========================进入用例前置步骤========================")
    time1 = 0
    time2 = 0
    ecs = EcsApi()
    ecs.run_ecs(EcsApi().param_config["ecs_param"]["run_ecs"],
                EcsApi().json_config["run_ecs"])
    ecs_name = ecs.get_jsonpath(expr="$.instanceIds[0]")
    logger.logger.debug(f"==========jsonpath获取到的实例id为{ecs_name}==========")
    logger.logger.info("==========等待查询主机列表接口可以返回主机信息==========")
    time.sleep(10)
    while True:
        ecs.describe_ecs(EcsApi().param_config["ecs_param"]["describe_ecs"])
        status = ecs.get_jsonpath(expr="$.list[0].status")
        logger.logger.info(f"检查主机实例当前的状态为{status}")
        if isinstance(status, list):
            if status[0] == 'RUNNING':
                break
            elif time1 == 18:
                logger.logger.error("创建主机3分钟仍然没有变为运行中状态!")
                break
            else:
                time.sleep(10)
                time1 += 1
                continue
        else:
            logger.logger.error("订单失败,没有下发到A层!")
            exit()
            break
    SetEnv.replace_param(ecs_name[0])  # 修改pram.yaml文件中的${ecs_name}
    yield ecs_name[0], ecs
    logger.logger.info(
        "========================进入用例后置步骤========================")
    result = ecs.stop_ecs(
        EcsApi().param_config["ecs_param"]["stop_ecs"]).status_code
    if result == 200:
        while True:
            ecs.describe_ecs(
                EcsApi().param_config["ecs_param"]["describe_ecs"])
            status = ecs.get_jsonpath(expr="$.list[0].status")
            logger.logger.info(f"检查主机实例当前的状态为{status}")
            if status[0] == 'DOWN':
                break
            elif time2 == 12:
                logger.logger.error("关闭主机1分钟仍然没有变为关停状态!")
                break
            else:
                time.sleep(5)
                time2 += 1
                continue
    else:
        logger.logger.error("关闭主机实例失败!")
        exit()
    ecs.delete_ecs(EcsApi().param_config["ecs_param"]["delete_ecs"])
    SetEnv.recovery_param()  # 将param.yaml恢复回带${ecs_name}的形式
예제 #2
0
    def delete_ecs(self, account_name, region, ecs_id):
        self.account_name = account_name
        self.region = region
        self.ecs_id = ecs_id

        try:
            ilist = []
            for i in self.region_list:
                if self.region == i:
                    __pid_str = "pid" + self.region.replace("-", "_")
                    __itoken = self.ecs_mysql.get_data(
                        "host_token",
                        "token",
                        account_name=self.account_name,
                        region=self.region)
                    __ipid = self.ecs_mysql.get_data(
                        "hwaccount_account",
                        __pid_str,
                        account_name=self.account_name)
                    __opecs_api = EcsApi(__itoken[0][0], self.region,
                                         __ipid[0][0])
                    for i in self.ecs_id:
                        n = __opecs_api.delete_ecs(i)
                        ilist.append(n)
                    return ilist
        except Exception as e:
            logging.error(e)