示例#1
0
 def post(self, request, service_id, *args, **kwargs):
     """自定义回调接口处发自动部署"""
     logger.debug(request.data)
     import pickle, base64
     secret_key = request.data.get("secret_key")
     # 加密
     deploy_key = deploy_repo.get_secret_key_by_service_id(
         service_id=service_id)
     deploy_key_decode = pickle.loads(
         base64.b64decode(deploy_key)).get("secret_key")
     if secret_key != deploy_key_decode:
         result = general_message(400, "failed", "密钥错误")
         return Response(result, status=400)
     service_obj = TenantServiceInfo.objects.get(service_id=service_id)
     tenant_obj = Tenants.objects.get(tenant_id=service_obj.tenant_id)
     status_map = app_service.get_service_status(tenant_obj, service_obj)
     user_obj = Users.objects.get(user_id=service_obj.creater)
     user_name = user_obj.nick_name
     status = status_map.get("status", None)
     logger.debug(status)
     if status == "running" or status == "abnormal":
         return user_services.deploy_service(tenant_obj=tenant_obj,
                                             service_obj=service_obj,
                                             user=user_obj,
                                             committer_name=user_name)
     else:
         logger.debug("应用状态异常")
         result = general_message(400, "failed", "应用状态不支持")
         return Response(result, status=400)
示例#2
0
 def post(self, request, service_id, *args, **kwargs):
     """自定义回调接口处发自动部署"""
     logger.debug(request.data)
     import pickle
     import base64
     secret_key = request.data.get("secret_key")
     # 加密
     deploy_key = deploy_repo.get_secret_key_by_service_id(
         service_id=service_id)
     deploy_key_decode = pickle.loads(
         base64.b64decode(deploy_key)).get("secret_key")
     if secret_key != deploy_key_decode:
         result = general_message(400, "failed", "密钥错误")
         return Response(result, status=400)
     service_obj = TenantServiceInfo.objects.get(service_id=service_id)
     tenant_obj = Tenants.objects.get(tenant_id=service_obj.tenant_id)
     status_map = app_service.get_service_status(tenant_obj, service_obj)
     user_obj = user_services.init_webhook_user(service_obj, "WebAPI")
     user_name = user_obj.nick_name
     status = status_map.get("status", None)
     logger.debug(status)
     if status != "closed":
         return user_services.deploy_service(tenant_obj=tenant_obj,
                                             service_obj=service_obj,
                                             user=user_obj,
                                             committer_name=user_name)
     else:
         result = general_message(200, "component is closed, not support",
                                  "组件状态不支持")
         return Response(result, status=400)
    def get(self, request, service_id, *args, **kwargs):
        secret_key = request.GET.get("secret_key")
        # 加密
        deploy_key = deploy_repo.get_secret_key_by_service_id(service_id=service_id)
        deploy_key_decode = pickle.loads(base64.b64decode(deploy_key)).get("secret_key")
        logger.debug('---------===========>{0}'.format(deploy_key_decode))
        logger.debug('---------===========>{0}'.format(secret_key))

        if secret_key != deploy_key_decode:
            result = general_message(400, "failed", "密钥错误")
            return Response(result, status=400)

        try:
            service_obj = TenantServiceInfo.objects.get(service_id=service_id)
            tenant_obj = Tenants.objects.get(tenant_id=service_obj.tenant_id)

            res, body = region_api.get_third_party_service_pods(
                service_obj.service_region, tenant_obj.tenant_name, service_obj.service_alias)

            if res.status != 200:
                return Response(general_message(412, "region error", "数据中心查询失败"), status=412)

            endpoint_list = body["list"]
            bean = {"endpoint_num": len(endpoint_list)}

            result = general_message(200, "success", "查询成功", list=endpoint_list, bean=bean)
        except Exception as e:
            logger.exception(e)
            result = error_message(e.message)
        return Response(result, status=result["code"])
示例#4
0
    def delete(self, request, service_id, *args, **kwargs):
        secret_key = request.data.get("secret_key")
        # 加密
        deploy_key = deploy_repo.get_secret_key_by_service_id(
            service_id=service_id)
        deploy_key_decode = pickle.loads(
            base64.b64decode(deploy_key)).get("secret_key")
        if secret_key != deploy_key_decode:
            result = general_message(400, "failed", "密钥错误")
            return Response(result, status=400)
        address = request.data.get("ip", None)
        if not address:
            return Response(general_message(400, "end_point is null",
                                            "end_point未指明"),
                            status=400)
        service_obj = TenantServiceInfo.objects.get(service_id=service_id)
        tenant_obj = Tenants.objects.get(tenant_id=service_obj.tenant_id)
        # 查询
        res, body = region_api.get_third_party_service_pods(
            service_obj.service_region, tenant_obj.tenant_name,
            service_obj.service_alias)

        if res.status != 200:
            return Response(general_message(412, "region error", "数据中心查询失败"),
                            status=412)

        endpoint_list = body["list"]
        if not endpoint_list:
            return Response(general_message(412, "ip is null", "ip不存在"),
                            status=412)
        addresses = []
        for endpoint in endpoint_list:
            addresses.append(endpoint["address"])
        if address not in addresses:
            return Response(general_message(412, "ip is null", "ip不存在"),
                            status=412)
        for endpoint in endpoint_list:
            if endpoint["address"] == address:
                endpoint_dict = dict()
                endpoint_dict["ep_id"] = endpoint["ep_id"]
                res, body = region_api.delete_third_party_service_endpoints(
                    service_obj.service_region, tenant_obj.tenant_name,
                    service_obj.service_alias, endpoint_dict)
                if res.status != 200:
                    return Response(general_message(412, "region error",
                                                    "数据中心删除失败"),
                                    status=412)

        result = general_message(200, "success", "删除成功")
        return Response(result, status=result["code"])
示例#5
0
    def get(self, request, service_id, *args, **kwargs):
        secret_key = request.GET.get("secret_key")
        # 加密
        deploy_key = deploy_repo.get_secret_key_by_service_id(
            service_id=service_id)
        deploy_key_decode = pickle.loads(
            base64.b64decode(deploy_key)).get("secret_key")

        if secret_key != deploy_key_decode:
            result = general_message(400, "failed", "密钥错误")
            return Response(result, status=400)

        service_obj = TenantServiceInfo.objects.get(service_id=service_id)
        tenant_obj = Tenants.objects.get(tenant_id=service_obj.tenant_id)

        res, body = region_api.get_third_party_service_pods(
            service_obj.service_region, tenant_obj.tenant_name,
            service_obj.service_alias)

        if res.status != 200:
            return Response(general_message(412, "region error", "数据中心查询失败"),
                            status=412)

        endpoint_list = []
        for item in body["list"]:
            endpoint = item
            endpoint["ip"] = item["address"]
            endpoint_list.append(endpoint)
        bean = {"endpoint_num": len(endpoint_list)}

        result = general_message(200,
                                 "success",
                                 "查询成功",
                                 list=endpoint_list,
                                 bean=bean)
        return Response(result, status=result["code"])
示例#6
0
    def put(self, request, service_id, *args, **kwargs):
        secret_key = request.data.get("secret_key")
        # 加密
        deploy_key = deploy_repo.get_secret_key_by_service_id(
            service_id=service_id)
        deploy_key_decode = pickle.loads(
            base64.b64decode(deploy_key)).get("secret_key")
        if secret_key != deploy_key_decode:
            result = general_message(400, "failed", "密钥错误")
            return Response(result, status=400)
        address = request.data.get("ip", None)
        # is_online true为上线,false为下线
        is_online = request.data.get("is_online", True)
        if type(is_online) != bool:
            return Response(general_message(400, "is_online type error",
                                            "参数类型错误"),
                            status=400)
        if not address:
            return Response(general_message(400, "end_point is null",
                                            "end_point未指明"),
                            status=400)
        try:
            service_obj = TenantServiceInfo.objects.get(service_id=service_id)
            tenant_obj = Tenants.objects.get(tenant_id=service_obj.tenant_id)
            endpoint_dict = dict()
            endpoint_dict["address"] = address
            endpoint_dict["is_online"] = is_online
            # 根据ip从数据中心查询, 有就put,没有就post
            res, body = region_api.get_third_party_service_pods(
                service_obj.service_region, tenant_obj.tenant_name,
                service_obj.service_alias)

            if res.status != 200:
                return Response(general_message(412, "region error",
                                                "数据中心查询失败"),
                                status=412)

            endpoint_list = body["list"]
            # 添加
            if not endpoint_list:
                res, body = region_api.post_third_party_service_endpoints(
                    service_obj.service_region, tenant_obj.tenant_name,
                    service_obj.service_alias, endpoint_dict)
                if res.status != 200:
                    return Response(general_message(412, "region error",
                                                    "数据中心添加失败"),
                                    status=412)
                return Response(general_message(200, "success", "修改成功"))
            addresses = []
            for endpoint in endpoint_list:
                addresses.append(endpoint["address"])
            if address not in addresses:
                # 添加
                res, body = region_api.post_third_party_service_endpoints(
                    service_obj.service_region, tenant_obj.tenant_name,
                    service_obj.service_alias, endpoint_dict)
                if res.status != 200:
                    return Response(general_message(412, "region error",
                                                    "数据中心添加失败"),
                                    status=412)
                return Response(general_message(200, "success", "修改成功"))
            # 修改
            for endpoint in endpoint_list:
                if endpoint["address"] == address:
                    bean = dict()
                    bean["ep_id"] = endpoint["ep_id"]
                    bean["is_online"] = is_online
                    res, body = region_api.put_third_party_service_endpoints(
                        service_obj.service_region, tenant_obj.tenant_name,
                        service_obj.service_alias, bean)
                    if res.status != 200:
                        return Response(general_message(
                            412, "region error", "数据中心修改失败"),
                                        status=412)

                    result = general_message(200, "success", "修改成功")
                    return Response(result, status=200)
        except region_api.CallApiFrequentError as e:
            logger.exception(e)
            return 409, u"操作过于频繁,请稍后再试"