Exemplo n.º 1
0
    def create_third_party_service(self, tenant, service, user_name):
        data = self.__init_third_party_data(tenant, service, user_name)
        # 端口
        ports = port_repo.get_service_ports(tenant.tenant_id, service.service_id)
        ports_info = ports.values(
            'container_port', 'mapping_port', 'protocol', 'port_alias', 'is_inner_service', 'is_outer_service')

        for port_info in ports_info:
            port_info["is_inner_service"] = False
            port_info["is_outer_service"] = False

        if ports_info:
            data["ports_info"] = list(ports_info)

        # endpoints
        endpoints = service_endpoints_repo.get_service_endpoints_by_service_id(service.service_id)
        endpoints_dict = dict()
        if endpoints:
            if endpoints.endpoints_type != "api":
                endpoints_dict[endpoints.endpoints_type] = endpoints.endpoints_info
                data["endpoints"] = endpoints_dict
        data["kind"] = service.service_source

        # 数据中心创建
        logger.debug('-----------data-----------_>{0}'.format(data))
        region_api.create_service(service.service_region, tenant.tenant_name, data)
        # 将服务创建状态变更为创建完成
        service.create_status = "complete"
        self.__handle_service_ports(tenant, service, ports)
        service.save()
        return service
Exemplo n.º 2
0
    def check_service(self, tenant, service, is_again):
        # if service.create_status == "complete":
        #     return 409, "应用完成创建,请勿重复检测", None
        body = dict()
        body["tenant_id"] = tenant.tenant_id
        body["source_type"] = self.__get_service_region_type(
            service.service_source)
        source_body = ""
        service_source = service_source_repo.get_service_source(
            tenant.tenant_id, service.service_id)
        user_name = ""
        password = ""
        service.service_source = self.__get_service_source(service)
        if service_source:
            user_name = service_source.user_name
            password = service_source.password
        if service.service_source == AppConstants.SOURCE_CODE:

            sb = {
                "server_type": service.server_type,
                "repository_url": service.git_url,
                "branch": service.code_version,
                "user": user_name,
                "password": password,
                "tenant_id": tenant.tenant_id
            }
            source_body = json.dumps(sb)
        elif service.service_source == AppConstants.DOCKER_RUN or service.service_source == AppConstants.DOCKER_IMAGE:
            source_body = service.docker_cmd
        elif service.service_source == AppConstants.THIRD_PARTY:
            # endpoints信息
            service_endpoints = service_endpoints_repo.get_service_endpoints_by_service_id(
                service.service_id)
            if service_endpoints:
                if service_endpoints.endpoints_type == "discovery":
                    source_body = service_endpoints.endpoints_info

        body["username"] = user_name
        body["password"] = password
        body["source_body"] = source_body
        logger.debug('======body========>{0}'.format(json.dumps(body)))
        res, body = region_api.service_source_check(service.service_region,
                                                    tenant.tenant_name, body)
        bean = body["bean"]
        service.check_uuid = bean["check_uuid"]
        service.check_event_id = bean["event_id"]
        # 更新创建状态
        if not is_again:
            service.create_status = "checking"
        service.save()
        bean = dict()
        bean.update(service.to_dict())
        bean.update({"user_name": user_name, "password": password})
        bean.update(self.__wrap_check_service(service))
        return 200, u"success", bean
Exemplo n.º 3
0
    def create_third_party_service(self, tenant, service, user_name):
        data = self.__init_third_party_data(tenant, service, user_name)
        # env var
        envs_info = env_var_repo.get_service_env(tenant.tenant_id,
                                                 service.service_id).values(
                                                     'container_port', 'name',
                                                     'attr_name', 'attr_value',
                                                     'is_change', 'scope')
        if envs_info:
            data["envs_info"] = list(envs_info)
        # 端口
        ports = port_repo.get_service_ports(tenant.tenant_id,
                                            service.service_id)
        ports_info = ports.values('container_port', 'mapping_port', 'protocol',
                                  'port_alias', 'is_inner_service',
                                  'is_outer_service', 'k8s_service_name')

        for port_info in ports_info:
            port_info["is_inner_service"] = False
            port_info["is_outer_service"] = False

        if ports_info:
            data["ports_info"] = list(ports_info)

        # endpoints
        endpoints = service_endpoints_repo.get_service_endpoints_by_service_id(
            service.service_id).first()
        if endpoints:
            if endpoints.endpoints_type == "static":
                eps = json.loads(endpoints.endpoints_info)
                validate_endpoints_info(eps)
            endpoints_dict = dict()
            endpoints_dict[endpoints.endpoints_type] = endpoints.endpoints_info
            data["endpoints"] = endpoints_dict
        data["kind"] = service.service_source

        # etcd keys
        data["etcd_key"] = service.check_uuid
        # 数据中心创建
        logger.debug('-----------data-----------_>{0}'.format(data))
        app_id = service_group_relation_repo.get_group_id_by_service(service)
        region_app_id = region_app_repo.get_region_app_id(
            service.service_region, app_id)
        data["app_id"] = region_app_id
        region_api.create_service(service.service_region, tenant.tenant_name,
                                  data)
        # 将组件创建状态变更为创建完成
        service.create_status = "complete"
        self.__handle_service_ports(tenant, service, ports)
        service.save()
        return service
Exemplo n.º 4
0
    def create_third_party_service(self, tenant, service, user_name):
        data = self.__init_third_party_data(tenant, service, user_name)
        # 端口
        ports = port_repo.get_service_ports(tenant.tenant_id,
                                            service.service_id)
        ports_info = ports.values('container_port', 'mapping_port', 'protocol',
                                  'port_alias', 'is_inner_service',
                                  'is_outer_service')

        for port_info in ports_info:
            port_info["is_inner_service"] = False
            port_info["is_outer_service"] = False

        if ports_info:
            data["ports_info"] = list(ports_info)

        # endpoints
        endpoints = service_endpoints_repo.get_service_endpoints_by_service_id(
            service.service_id)
        if endpoints.endpoints_type == "static":
            eps = json.loads(endpoints.endpoints_info)
            for address in eps:
                if "https://" in address:
                    address = address.partition("https://")[2]
                if "http://" in address:
                    address = address.partition("http://")[2]
                if ":" in address:
                    address = address.rpartition(":")[0]
                errs = validate_endpoint_address(address)
                if errs:
                    if len(eps) > 1:
                        raise ErrDoNotSupportMultiDomain(
                            "do not support multi domain address")
        endpoints_dict = dict()
        if endpoints:
            endpoints_dict[endpoints.endpoints_type] = endpoints.endpoints_info
            data["endpoints"] = endpoints_dict
        data["kind"] = service.service_source

        # 数据中心创建
        logger.debug('-----------data-----------_>{0}'.format(data))
        region_api.create_service(service.service_region, tenant.tenant_name,
                                  data)
        # 将组件创建状态变更为创建完成
        service.create_status = "complete"
        self.__handle_service_ports(tenant, service, ports)
        service.save()
        return service
Exemplo n.º 5
0
    def get(self, request, *args, **kwargs):
        """
        组件详情信息
        ---
        parameters:
            - name: tenantName
              description: 租户名
              required: true
              type: string
              paramType: path
            - name: serviceAlias
              description: 组件别名
              required: true
              type: string
              paramType: path
        """
        bean = dict()
        try:
            # status_map = app_service.get_service_status(self.tenant, self.service)
            # used_resource = app_service.get_service_resource_with_plugin(self.tenant, self.service,
            #                                                              status_map["status"])
            # service_abled_plugins = app_plugin_service.get_service_abled_plugin(self.service)
            # plugin_list = [p.to_dict() for p in service_abled_plugins]
            # bean.update(status_map)
            # bean.update(used_resource)
            # bean.update({"plugin_list": plugin_list})
            service_model = self.service.to_dict()
            group_map = group_service.get_services_group_name(
                [self.service.service_id])
            group_name = group_map.get(self.service.service_id)["group_name"]
            group_id = group_map.get(self.service.service_id)["group_id"]
            service_model["group_name"] = group_name
            service_model["group_id"] = group_id
            bean.update({"service": service_model})
            tenant_actions = self.user.actions.tenant_actions
            bean.update({"tenant_actions": tenant_actions})
            service_actions = self.user.actions.service_actions
            bean.update({"service_actions": service_actions})
            event_websocket_url = ws_service.get_event_log_ws(
                self.request, self.service.service_region)
            bean.update({"event_websocket_url": event_websocket_url})
            if self.service.service_source == "market":
                service_source = service_source_repo.get_service_source(
                    self.tenant.tenant_id, self.service.service_id)
                if not service_source:
                    result = general_message(200, "success", "查询成功", bean=bean)
                    return Response(result, status=result["code"])
                rain_app = rainbond_app_repo.get_rainbond_app_by_key_and_version(
                    service_source.group_key, service_source.version)
                if not rain_app:
                    result = general_message(200,
                                             "success",
                                             "当前云市组件已删除",
                                             bean=bean)
                    return Response(result, status=result["code"])
                else:
                    bean.update({"rain_app_name": rain_app.group_name})
                    apps_template = json.loads(rain_app.app_template)
                    apps_list = apps_template.get("apps")
                    for app in apps_list:
                        if app["service_key"] == self.service.service_key:
                            if self.service.deploy_version and int(
                                    app["deploy_version"]) > int(
                                        self.service.deploy_version):
                                self.service.is_upgrate = True
                                self.service.save()
                                bean.update({"service": service_model})
                    try:
                        apps_template = json.loads(rain_app.app_template)
                        apps_list = apps_template.get("apps")
                        service_source = service_source_repo.get_service_source(
                            self.service.tenant_id, self.service.service_id)
                        if service_source and service_source.extend_info:
                            extend_info = json.loads(
                                service_source.extend_info)
                            if extend_info:
                                for app in apps_list:
                                    if "service_share_uuid" in app:
                                        if app["service_share_uuid"] == extend_info[
                                                "source_service_share_uuid"]:
                                            new_version = int(
                                                app["deploy_version"])
                                            old_version = int(extend_info[
                                                "source_deploy_version"])
                                            if new_version > old_version:
                                                self.service.is_upgrate = True
                                                self.service.save()
                                                service_model[
                                                    "is_upgrade"] = True
                                                bean.update(
                                                    {"service": service_model})
                                    elif "service_share_uuid" not in app and "service_key" in app:
                                        if app["service_key"] == extend_info[
                                                "source_service_share_uuid"]:
                                            new_version = int(
                                                app["deploy_version"])
                                            old_version = int(extend_info[
                                                "source_deploy_version"])
                                            if new_version > old_version:
                                                self.service.is_upgrate = True
                                                self.service.save()
                                                service_model[
                                                    "is_upgrade"] = True
                                                bean.update(
                                                    {"service": service_model})

                    except Exception as e:
                        logger.exception(e)

            if self.service.service_source == AppConstants.DOCKER_COMPOSE:
                if self.service.create_status != "complete":
                    compose_service_relation = compose_service.get_service_compose_id(
                        self.service)
                    if compose_service_relation:
                        service_model[
                            "compose_id"] = compose_service_relation.compose_id
                        bean.update({"service": service_model})
            bean["is_third"] = False
            if self.service.service_source == "third_party":
                bean["is_third"] = True
                service_endpoints = service_endpoints_repo.get_service_endpoints_by_service_id(
                    self.service.service_id)
                if service_endpoints:
                    bean["register_way"] = service_endpoints.endpoints_type
                    if service_endpoints.endpoints_type == "api":
                        # 从环境变量中获取域名,没有在从请求中获取
                        host = os.environ.get('DEFAULT_DOMAIN',
                                              request.get_host())
                        bean["api_url"] = "http://" + host + "/console/" + \
                            "third_party/{0}".format(self.service.service_id)
                        key_repo = deploy_repo.get_service_key_by_service_id(
                            service_id=self.service.service_id)
                        if key_repo:
                            bean["api_service_key"] = pickle.loads(
                                base64.b64decode(
                                    key_repo.secret_key)).get("secret_key")
                    if service_endpoints.endpoints_type == "discovery":
                        # 返回类型和key
                        endpoints_info_dict = json.loads(
                            service_endpoints.endpoints_info)

                        bean["discovery_type"] = endpoints_info_dict["type"]
                        bean["discovery_key"] = endpoints_info_dict["key"]

            result = general_message(200, "success", "查询成功", bean=bean)
        except Exception as e:
            logger.exception(e)
            result = error_message(e.message)
        return Response(result, status=result["code"])
Exemplo n.º 6
0
    def check_service(self, tenant, service, is_again, user=None):
        body = dict()
        body["tenant_id"] = tenant.tenant_id
        body["source_type"] = self.__get_service_region_type(
            service.service_source)
        source_body = ""
        service_source = service_source_repo.get_service_source(
            tenant.tenant_id, service.service_id)
        user_name = ""
        password = ""
        service.service_source = self.__get_service_source(service)
        if service_source:
            user_name = service_source.user_name
            password = service_source.password
        if service.service_source == AppConstants.SOURCE_CODE:
            if service.oauth_service_id:
                try:
                    oauth_service = oauth_repo.get_oauth_services_by_service_id(
                        service.oauth_service_id)
                    oauth_user = oauth_user_repo.get_user_oauth_by_user_id(
                        service_id=service.oauth_service_id,
                        user_id=user.user_id)
                except Exception as e:
                    logger.debug(e)
                    return 400, "未找到oauth服务, 请检查该服务是否存在且属于开启状态", None
                if oauth_user is None:
                    return 400, "未成功获取第三方用户信息", None

                try:
                    instance = get_oauth_instance(oauth_service.oauth_type,
                                                  oauth_service, oauth_user)
                except Exception as e:
                    logger.debug(e)
                    return 400, "未找到OAuth服务", None
                if not instance.is_git_oauth():
                    return 400, "该OAuth服务不是代码仓库类型", None
                tenant = Tenants.objects.get(tenant_name=tenant.tenant_name)
                try:
                    service_code_clone_url = instance.get_clone_url(
                        service.git_url)
                except Exception as e:
                    logger.debug(e)
                    return 400, "Access Token 已过期", None
            else:
                service_code_clone_url = service.git_url

            sb = {
                "server_type": service.server_type,
                "repository_url": service_code_clone_url,
                "branch": service.code_version,
                "user": user_name,
                "password": password,
                "tenant_id": tenant.tenant_id
            }
            source_body = json.dumps(sb)
        elif service.service_source == AppConstants.DOCKER_RUN or service.service_source == AppConstants.DOCKER_IMAGE:
            source_body = service.docker_cmd
        elif service.service_source == AppConstants.THIRD_PARTY:
            # endpoints信息
            service_endpoints = service_endpoints_repo.get_service_endpoints_by_service_id(
                service.service_id).first()
            if service_endpoints and service_endpoints.endpoints_type == "discovery":
                source_body = service_endpoints.endpoints_info

        body["username"] = user_name
        body["password"] = password
        body["source_body"] = source_body
        res, body = region_api.service_source_check(service.service_region,
                                                    tenant.tenant_name, body)
        bean = body["bean"]
        service.check_uuid = bean["check_uuid"]
        service.check_event_id = bean["event_id"]
        # 更新创建状态
        if not is_again:
            service.create_status = "checking"
        service.save()
        bean = dict()
        bean.update(service.to_dict())
        bean.update({"user_name": user_name, "password": password})
        bean.update(self.__wrap_check_service(service))
        return 200, "success", bean
Exemplo n.º 7
0
    def get_service_details(self, tenant, service):
        service_base = service.to_dict()
        service_labels = service_label_repo.get_service_labels(
            service.service_id)
        service_domains = domain_repo.get_service_domains(service.service_id)
        http_rule_configs = configuration_repo.list_by_rule_ids(
            [sd.http_rule_id for sd in service_domains])
        service_tcpdomains = tcp_domain.get_service_tcpdomains(
            service.service_id)
        service_probes = probe_repo.get_service_probe(service.service_id)
        service_source = service_source_repo.get_service_source(
            tenant.tenant_id, service.service_id)
        service_auths = auth_repo.get_service_auth(service.service_id)
        service_env_vars = env_var_repo.get_service_env(
            tenant.tenant_id, service.service_id)
        service_compile_env = compile_env_repo.get_service_compile_env(
            service.service_id)
        service_extend_method = extend_repo.get_extend_method_by_service(
            service)
        service_mnts = mnt_repo.get_service_mnts(tenant.tenant_id,
                                                 service.service_id)
        service_volumes = volume_repo.get_service_volumes_with_config_file(
            service.service_id)
        service_config_file = volume_repo.get_service_config_files(
            service.service_id)
        service_ports = port_repo.get_service_ports(tenant.tenant_id,
                                                    service.service_id)
        service_relation = dep_relation_repo.get_service_dependencies(
            tenant.tenant_id, service.service_id)
        service_monitors = service_monitor_repo.get_component_service_monitors(
            tenant.tenant_id, service.service_id)
        component_graphs = component_graph_repo.list(service.service_id)
        # plugin
        service_plugin_relation = app_plugin_relation_repo.get_service_plugin_relation_by_service_id(
            service.service_id)
        service_plugin_config = service_plugin_config_repo.get_service_plugin_all_config(
            service.service_id)
        # third_party_service
        third_party_service_endpoints = service_endpoints_repo.get_service_endpoints_by_service_id(
            service.service_id)
        if service.service_source == "third_party":
            if not third_party_service_endpoints:
                raise ServiceHandleException(
                    msg="third party service endpoints can't be null",
                    msg_show="第三方组件实例不可为空")
        app_info = {
            "component_id":
            service.component_id,
            "service_base":
            service_base,
            "service_labels": [label.to_dict() for label in service_labels],
            "service_domains":
            [domain.to_dict() for domain in service_domains],
            "http_rule_configs":
            [config.to_dict() for config in http_rule_configs],
            "service_tcpdomains":
            [tcpdomain.to_dict() for tcpdomain in service_tcpdomains],
            "service_probes": [probe.to_dict() for probe in service_probes],
            "service_source":
            service_source.to_dict() if service_source else None,
            "service_auths": [auth.to_dict() for auth in service_auths],
            "service_env_vars":
            [env_var.to_dict() for env_var in service_env_vars],
            "service_compile_env":
            service_compile_env.to_dict() if service_compile_env else None,
            "service_extend_method":
            service_extend_method.to_dict() if service_extend_method else None,
            "service_mnts": [mnt.to_dict() for mnt in service_mnts],
            "service_plugin_relation": [
                plugin_relation.to_dict()
                for plugin_relation in service_plugin_relation
            ],
            "service_plugin_config":
            [config.to_dict() for config in service_plugin_config],
            "service_relation":
            [relation.to_dict() for relation in service_relation],
            "service_volumes":
            [volume.to_dict() for volume in service_volumes],
            "service_config_file":
            [config_file.to_dict() for config_file in service_config_file],
            "service_ports": [port.to_dict() for port in service_ports],
            "third_party_service_endpoints":
            [endpoint.to_dict() for endpoint in third_party_service_endpoints],
            "service_monitors":
            [monitor.to_dict() for monitor in service_monitors],
            "component_graphs":
            [graph.to_dict() for graph in component_graphs]
        }
        plugin_ids = [pr.plugin_id for pr in service_plugin_relation]

        return app_info, plugin_ids