Ejemplo n.º 1
0
    def post(self, request, *args, **kwargs):
        """
        创建第三方组件

        """

        group_id = request.data.get("group_id", -1)
        service_cname = request.data.get("service_cname", None)
        endpoints = request.data.get("endpoints", None)
        endpoints_type = request.data.get("endpoints_type", None)

        if not service_cname:
            return Response(general_message(400, "service_cname is null",
                                            "组件名未指明"),
                            status=400)
        if not endpoints and endpoints_type != "api":
            return Response(general_message(400, "end_point is null",
                                            "end_point未指明"),
                            status=400)

        code, msg_show, new_service = app_service.create_third_party_app(
            self.response_region, self.tenant, self.user, service_cname,
            endpoints, endpoints_type)
        if code != 200:
            return Response(general_message(code, "service create fail",
                                            msg_show),
                            status=code)

        # 添加组件所在组
        code, msg_show = group_service.add_service_to_group(
            self.tenant, self.response_region, group_id,
            new_service.service_id)
        if code != 200:
            logger.debug("service.create", msg_show)

        if endpoints_type == "discovery":
            # 添加username,password信息
            if "username" in endpoints and "password" in endpoints:
                if endpoints["username"] or endpoints["password"]:
                    app_service.create_service_source_info(
                        self.tenant, new_service, endpoints["username"],
                        endpoints["password"])

        bean = new_service.to_dict()
        if endpoints_type == "api":
            # 生成秘钥
            deploy = deploy_repo.get_deploy_relation_by_service_id(
                service_id=new_service.service_id)
            api_secret_key = pickle.loads(
                base64.b64decode(deploy)).get("secret_key")
            # 从环境变量中获取域名,没有在从请求中获取
            host = os.environ.get('DEFAULT_DOMAIN',
                                  "http://" + request.get_host())
            api_url = host + "/console/" + "third_party/{0}".format(
                new_service.service_id)
            bean["api_service_key"] = api_secret_key
            bean["url"] = api_url

        result = general_message(200, "success", "创建成功", bean=bean)
        return Response(result, status=result["code"])
Ejemplo n.º 2
0
    def get(self, request, *args, **kwargs):
        """
        判断该应用是否有webhooks自动部署功能,有则返回URL
        """
        try:
            tenant_id = self.tenant.tenant_id
            service_alias = self.service.service_alias
            service_obj = TenantServiceInfo.objects.filter(tenant_id=tenant_id, service_alias=service_alias)[0]
            if service_obj.service_source == AppConstants.MARKET:
                result = general_message(200, "failed", "该应用不符合要求", bean={"display":False})
                return Response(result, status=200)
            support_type = 0
            if service_obj.service_source == AppConstants.SOURCE_CODE:
                support_type = 1
            else:
                support_type = 2

            service_id = service_obj.service_id
            # 生成秘钥
            deploy = deploy_repo.get_deploy_relation_by_service_id(service_id=service_id)
            secret_key = pickle.loads(base64.b64decode(deploy)).get("secret_key")
            # 从环境变量中获取域名,没有在从请求中获取
            host = os.environ.get('DEFAULT_DOMAIN', request.get_host())
            url = "http://" + host + "/console/" + "webhooks/" + service_obj.service_id
            custom_url = "http://" + host + "/console/" + "custom/deploy/" + service_obj.service_id
            # deploy_key = deploy.secret_key
            print deploy
            status = self.service.open_webhooks
            result = general_message(200, "success", "获取URl及开启状态成功", bean={"url": url, "custom_url":custom_url, "secret_key":secret_key, "status": status, "display":True, "support_type":support_type})

            return Response(result, status=200)
        except Exception as e:
            logger.exception(e)
            result = error_message(e.message)
        return Response(result, status=500)
Ejemplo n.º 3
0
    def post(self, request, *args, **kwargs):
        """
        创建三方服务

        """

        group_id = request.data.get("group_id", -1)
        service_cname = request.data.get("service_cname", None)
        endpoints = request.data.get("endpoints", None)
        endpoints_type = request.data.get("endpoints_type", None)

        try:
            if not service_cname:
                return Response(general_message(400, "service_cname is null", "服务名未指明"), status=400)
            if not endpoints and endpoints_type != "api":
                return Response(general_message(400, "end_point is null", "end_point未指明"), status=400)

            code, msg_show, new_service = app_service.create_third_party_app(self.response_region, self.tenant,
                                                                             self.user, service_cname,
                                                                             endpoints, endpoints_type)
            if code != 200:
                return Response(general_message(code, "service create fail", msg_show), status=code)

            # 添加服务所在组
            code, msg_show = group_service.add_service_to_group(self.tenant, self.response_region, group_id,
                                                                new_service.service_id)
            if code != 200:
                logger.debug("service.create", msg_show)

            if endpoints_type == "discovery":
                # 添加username,password信息
                logger.debug('========dict=========>{0}'.format(type(endpoints)))
                if endpoints.has_key("username") and endpoints.has_key("password"):
                    if endpoints["username"] or endpoints["password"]:
                        app_service.create_service_source_info(self.tenant, new_service, endpoints["username"], endpoints["password"])

            bean = new_service.to_dict()
            if endpoints_type == "api":
                # 生成秘钥
                deploy = deploy_repo.get_deploy_relation_by_service_id(service_id=new_service.service_id)
                api_secret_key = pickle.loads(base64.b64decode(deploy)).get("secret_key")
                # 从环境变量中获取域名,没有在从请求中获取
                host = os.environ.get('DEFAULT_DOMAIN', request.get_host())
                api_url = "http://" + host + "/console/" + "third_party/{0}".format(new_service.service_id)
                bean["api_service_key"] = api_secret_key
                bean["url"] = api_url

            result = general_message(200, "success", "创建成功", bean=bean)
        except ResourceNotEnoughException as re:
            logger.exception(re)
            return Response(general_message(10406, "resource is not enough", re.message), status=412)
        except Exception as e:
            logger.exception(e)
            result = error_message(e.message)
        return Response(result, status=result["code"])
Ejemplo n.º 4
0
 def put(self, request, *args, **kwargs):
     try:
         key_repo = deploy_repo.get_service_key_by_service_id(service_id=self.service.service_id)
         if not key_repo:
             return Response(general_message(412, "service_key is null", "秘钥不存在"), status=412)
         key_repo.delete()
         # 生成秘钥
         deploy = deploy_repo.get_deploy_relation_by_service_id(service_id=self.service.service_id)
         api_secret_key = pickle.loads(base64.b64decode(deploy)).get("secret_key")
         result = general_message(200, "success", "重置成功", bean={"api_service_key": api_secret_key})
         return Response(result)
     except Exception as e:
         logger.exception(e)
         result = error_message(e.message)
         return Response(result, status=500)
Ejemplo n.º 5
0
    def get(self, request, *args, **kwargs):
        """
        判断该应用是否有webhooks自动部署功能,有则返回URL
        """
        try:
            deployment_way = request.GET.get("deployment_way", None)
            if not deployment_way:
                result = general_message(400, "Parameter cannot be empty",
                                         "缺少参数")
                return Response(result, status=400)
            tenant_id = self.tenant.tenant_id
            service_alias = self.service.service_alias
            service_obj = TenantServiceInfo.objects.filter(
                tenant_id=tenant_id, service_alias=service_alias)[0]
            if service_obj.service_source == AppConstants.MARKET:
                result = general_message(200,
                                         "failed",
                                         "该应用不符合要求",
                                         bean={"display": False})
                return Response(result, status=200)
            if service_obj.service_source == AppConstants.SOURCE_CODE:
                support_type = 1
            else:
                support_type = 2

            service_id = service_obj.service_id
            # 从环境变量中获取域名,没有在从请求中获取
            host = os.environ.get('DEFAULT_DOMAIN', request.get_host())

            service_webhook = service_webhooks_repo.get_service_webhooks_by_service_id_and_type(
                self.service.service_id, deployment_way)
            if not service_webhook:
                service_webhook = service_webhooks_repo.create_service_webhooks(
                    self.service.service_id, deployment_way)

            # api处发自动部署
            if deployment_way == "api_webhooks":
                # 生成秘钥
                deploy = deploy_repo.get_deploy_relation_by_service_id(
                    service_id=service_id)
                secret_key = pickle.loads(
                    base64.b64decode(deploy)).get("secret_key")
                url = "http://" + host + "/console/" + "custom/deploy/" + service_obj.service_id
                status = service_webhook.state
                result = general_message(200,
                                         "success",
                                         "获取URl及开启状态成功",
                                         bean={
                                             "url": url,
                                             "secret_key": secret_key,
                                             "status": status,
                                             "display": True,
                                             "support_type": support_type
                                         })
            # 镜像处发自动部署
            elif deployment_way == "image_webhooks":
                url = "http://" + host + "/console/" + "image/webhooks/" + service_obj.service_id
                status = service_webhook.state

                result = general_message(200,
                                         "success",
                                         "获取URl及开启状态成功",
                                         bean={
                                             "url": url,
                                             "status": status,
                                             "display": True,
                                             "support_type": support_type
                                         })
            # 源码处发自动部署
            else:
                url = "http://" + host + "/console/" + "webhooks/" + service_obj.service_id
                status = service_webhook.state
                deploy_keyword = service_webhook.deploy_keyword
                result = general_message(200,
                                         "success",
                                         "获取URl及开启状态成功",
                                         bean={
                                             "url": url,
                                             "status": status,
                                             "display": True,
                                             "support_type": support_type,
                                             "deploy_keyword": deploy_keyword
                                         })
            return Response(result, status=200)
        except Exception as e:
            logger.exception(e)
            result = error_message(e.message)
        return Response(result, status=500)
Ejemplo n.º 6
0
    def post(self, request, *args, **kwargs):
        """
        创建第三方组件

        """

        group_id = request.data.get("group_id", -1)
        service_cname = request.data.get("service_cname", None)
        static = request.data.get("static", None)
        endpoints_type = request.data.get("endpoints_type", None)
        service_name = request.data.get("serviceName", "")

        if not service_cname:
            return Response(general_message(400, "service_cname is null",
                                            "组件名未指明"),
                            status=400)
        if endpoints_type == "static":
            validate_endpoints_info(static)
        source_config = {}
        if endpoints_type == "kubernetes":
            if not service_name:
                return Response(general_message(
                    400, "kubernetes service name is null",
                    "Kubernetes Service名称必须指定"),
                                status=400)
            source_config = {
                "service_name": service_name,
                "namespace": request.data.get("namespace", "")
            }
        new_service = app_service.create_third_party_app(
            self.response_region, self.tenant, self.user, service_cname,
            static, endpoints_type, source_config)
        # 添加组件所在组
        code, msg_show = group_service.add_service_to_group(
            self.tenant, self.response_region, group_id,
            new_service.service_id)
        if code != 200:
            new_service.delete()
            raise ServiceHandleException(msg="add component to app failure",
                                         msg_show=msg_show,
                                         status_code=code,
                                         error_code=code)
        bean = new_service.to_dict()
        if endpoints_type == "api":
            # 生成秘钥
            deploy = deploy_repo.get_deploy_relation_by_service_id(
                service_id=new_service.service_id)
            api_secret_key = pickle.loads(
                base64.b64decode(deploy)).get("secret_key")
            # 从环境变量中获取域名,没有在从请求中获取
            host = os.environ.get('DEFAULT_DOMAIN',
                                  "http://" + request.get_host())
            api_url = host + "/console/" + "third_party/{0}".format(
                new_service.service_id)
            bean["api_service_key"] = api_secret_key
            bean["url"] = api_url
            service_endpoints_repo.create_api_endpoints(
                self.tenant, new_service)

        result = general_message(200, "success", "创建成功", bean=bean)
        return Response(result, status=result["code"])