Пример #1
0
    def save_compose_services(self, tenant, user, region, group_compose, data):
        # 开启保存点
        sid = transaction.savepoint()
        service_list = []
        try:
            if data["check_status"] == "success":
                if group_compose.create_status == "checking":
                    logger.debug("checking compose service install,save info into database")
                # 先删除原来创建的组件
                self.__delete_created_compose_info(tenant, group_compose.compose_id)
                # 保存compose检测结果
                if data["check_status"] == "success":
                    service_info_list = data["service_info"]
                    service_dep_map = {}
                    # 组件列表
                    name_service_map = {}
                    for service_info in service_info_list:
                        service_cname = service_info.get("cname", service_info["image_alias"])
                        image = service_info["image"]["name"] + ":" + service_info["image"]["tag"]
                        # 保存信息
                        service = self.__init_compose_service(tenant, user, service_cname, image, region)
                        # 缓存创建的组件
                        service_list.append(service)
                        name_service_map[service_cname] = service

                        group_service.add_service_to_group(tenant, region, group_compose.group_id, service.service_id)

                        code, msg = app_check_service.save_service_info(tenant, service, service_info)
                        if code != 200:
                            return code, msg, None
                        # save service info
                        service.save()
                        # 创建组件构建源信息,存储账号密码
                        envs = service_info.get("envs", [])
                        hub_user = group_compose.hub_user
                        hub_password = group_compose.hub_pass
                        for env in envs:
                            if env.get("name", "") == "HUB_USER":
                                hub_user = env.get("value")
                            if env.get("name", "") == "HUB_PASSWORD":
                                hub_password = env.get("value")
                        app_service.create_service_source_info(tenant, service, hub_user, hub_password)
                        dependencies = service_info.get("depends", None)
                        if dependencies:
                            service_dep_map[service_cname] = dependencies

                    # 保存compose-relation
                    self.__save_compose_relation(service_list, tenant.tenant_id, group_compose.compose_id)
                    # 保存依赖关系
                    self.__save_service_dep_relation(tenant, service_dep_map, name_service_map)
                group_compose.create_status = "checked"
                group_compose.save()
                transaction.savepoint_commit(sid)
        except Exception as e:
            logger.exception(e)
            if sid:
                transaction.savepoint_rollback(sid)
            return 500, "{0}".format(e.message), service_list
        return 200, "success", service_list
    def save_data(self, migrate_tenant, migrate_region, user,
                  changed_service_map, metadata, group_id):

        group = group_repo.get_group_by_id(group_id)
        apps = metadata["apps"]

        old_new_service_id_map = dict()
        service_relations_list = []
        service_mnt_list = []
        for app in apps:
            service_base_info = app["service_base"]

            new_service_id = changed_service_map[
                service_base_info["service_id"]]["ServiceID"]
            new_service_alias = changed_service_map[
                service_base_info["service_id"]]["ServiceAlias"]

            ts = self.__init_app(app["service_base"], new_service_id,
                                 new_service_alias, user, migrate_region,
                                 migrate_tenant)
            old_new_service_id_map[app["service_base"]
                                   ["service_id"]] = ts.service_id
            group_service.add_service_to_group(migrate_tenant, migrate_region,
                                               group.ID, ts.service_id)
            self.__save_env(migrate_tenant, ts, app["service_env_vars"])
            self.__save_volume(ts, app["service_volumes"])
            lb_mapping_port = changed_service_map[
                service_base_info["service_id"]].get("LBPorts", None)
            self.__save_port(migrate_tenant, ts, app["service_ports"],
                             lb_mapping_port)
            self.__save_compile_env(ts, app["service_compile_env"])
            self.__save_service_label(migrate_tenant, ts, migrate_region,
                                      app["service_labels"])
            # self.__save_service_domain(ts, app["service_domains"])
            self.__save_service_event(migrate_tenant, ts,
                                      app["service_events"])
            self.__save_service_perms(ts, app["service_perms"])
            self.__save_service_probes(ts, app["service_probes"])
            self.__save_service_source(migrate_tenant, ts,
                                       app["service_source"])
            self.__save_service_auth(ts, app["service_auths"])
            self.__save_service_image_relation(migrate_tenant, ts,
                                               app["image_service_relation"])

            service_relations = app["service_relation"]
            service_mnts = app["service_mnts"]

            if service_relations:
                service_relations_list[0:0] = list(service_relations)
            if service_mnts:
                service_mnt_list[0:0] = list(service_mnts)
            # 更新状态
            ts.create_status = "complete"
            ts.save()

        self.__save_service_relations(migrate_tenant, service_relations_list,
                                      old_new_service_id_map)
        self.__save_service_mnt_relation(migrate_tenant, service_mnt_list,
                                         old_new_service_id_map)
Пример #3
0
    def save_compose_services(self, tenant, user, region, group_compose, data):
        service_list = []
        try:
            if data["check_status"] == "success":
                if group_compose.create_status == "checking":
                    logger.debug(
                        "checking compose service install,save info into database"
                    )
                # 先删除原来创建的应用
                self.__delete_created_compose_info(tenant,
                                                   group_compose.compose_id)
                # 保存compose检测结果
                if data["check_status"] == "success":
                    service_info_list = data["service_info"]
                    service_dep_map = {}
                    # 服务列表
                    name_service_map = {}
                    for service_info in service_info_list:
                        service_cname = service_info.get(
                            "image_alias", service_info["image"]["name"])
                        image = service_info["image"][
                            "name"] + ":" + service_info["image"]["tag"]
                        # 保存信息
                        service = self.__init_compose_service(
                            tenant, user, service_cname, image, region)
                        group_service.add_service_to_group(
                            tenant, region, group_compose.group_id,
                            service.service_id)
                        service_list.append(service)
                        name_service_map[service_cname] = service
                        code, msg = app_check_service.save_service_info(
                            tenant, service, service_info)
                        if code != 200:
                            return code, msg, None
                        dependencies = service_info.get("depends", None)
                        if dependencies:
                            service_dep_map[service_cname] = dependencies

                    # 保存compose-relation
                    self.__save_compose_relation(service_list,
                                                 tenant.tenant_id,
                                                 group_compose.compose_id)
                    # 保存依赖关系
                    self.__save_service_dep_relation(tenant, service_dep_map,
                                                     name_service_map)

                    for s in service_list:
                        s.create_status = "checked"
                        s.save()
                group_compose.create_status = "checked"
                group_compose.save()
        except Exception as e:
            logger.exception(e)
            return 500, "{0}".format(e.message), service_list

        return 200, "success", service_list
Пример #4
0
    def save_data(self, migrate_tenant, migrate_region, user, changed_service_map, metadata, group_id):
        group = group_repo.get_group_by_id(group_id)
        apps = metadata["apps"]

        old_new_service_id_map = dict()
        service_relations_list = []
        service_mnt_list = []
        # restore component
        for app in apps:
            service_base_info = app["service_base"]
            new_service_id = changed_service_map[service_base_info["service_id"]]["ServiceID"]
            new_service_alias = changed_service_map[service_base_info["service_id"]]["ServiceAlias"]
            ts = self.__init_app(app["service_base"], new_service_id, new_service_alias, user, migrate_region, migrate_tenant)
            old_new_service_id_map[app["service_base"]["service_id"]] = ts.service_id
            group_service.add_service_to_group(migrate_tenant, migrate_region, group.ID, ts.service_id)
            self.__save_env(migrate_tenant, ts, app["service_env_vars"])
            self.__save_volume(migrate_tenant, ts, app["service_volumes"],
                               app["service_config_file"] if 'service_config_file' in app else None)
            self.__save_port(migrate_tenant, ts, app["service_ports"])
            self.__save_compile_env(ts, app["service_compile_env"])
            self.__save_service_label(migrate_tenant, ts, migrate_region, app["service_labels"])
            self.__save_service_perms(ts, app["service_perms"])
            self.__save_service_probes(ts, app["service_probes"])
            self.__save_service_source(migrate_tenant, ts, app["service_source"])
            self.__save_service_auth(ts, app["service_auths"])
            service_relations = app["service_relation"]
            service_mnts = app["service_mnts"]

            if service_relations:
                service_relations_list[0:0] = list(service_relations)
            if service_mnts:
                service_mnt_list[0:0] = list(service_mnts)
            # 更新状态
            ts.create_status = "complete"
            ts.save()

        # restore plugin info
        self.__save_plugin_config_items(metadata["plugin_info"]["plugin_config_items"])
        self.__save_plugin_config_groups(metadata["plugin_info"]["plugin_config_groups"])
        versions = self.__save_plugin_build_versions(migrate_tenant, metadata["plugin_info"]["plugin_build_versions"])
        self.__save_plugins(migrate_region, migrate_tenant, metadata["plugin_info"]["plugins"])
        for app in apps:
            # plugin
            if app.get("service_plugin_relation", None):
                self.__save_plugin_relations(ts, app["service_plugin_relation"], versions)
            if app.get("service_plugin_config", None):
                self.__save_service_plugin_config(ts.service_id, app["service_plugin_config"])
        self.__save_service_relations(migrate_tenant, service_relations_list, old_new_service_id_map)
        self.__save_service_mnt_relation(migrate_tenant, service_mnt_list, old_new_service_id_map)
Пример #5
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"])
Пример #6
0
 def post(self, request, app_id, *args, **kwargs):
     ctcs = CreateThirdComponentSerializer(data=request.data)
     ctcs.is_valid(raise_exception=True)
     req_date = ctcs.data
     validate_endpoints_info(req_date["endpoints"])
     new_component = console_app_service.create_third_party_app(self.region_name, self.team, self.user,
                                                                req_date["component_name"], req_date["endpoints"],
                                                                req_date["endpoints_type"])
     # add component to app
     code, msg_show = group_service.add_service_to_group(self.team, self.region_name, app_id, new_component.service_id)
     if code != 200:
         raise ServiceHandleException(
             msg="add component to app failure", msg_show=msg_show, status_code=code, error_code=code)
     endpoints_type = req_date["endpoints_type"]
     bean = new_component.to_dict()
     if endpoints_type == "api":
         # 生成秘钥
         deploy = deploy_repo.get_deploy_relation_by_service_id(service_id=new_component.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_component.service_id)
         bean["api_service_key"] = api_secret_key
         bean["url"] = api_url
     console_app_service.create_third_party_service(self.team, new_component, self.user.nick_name)
     return Response(bean, status=status.HTTP_200_OK)
Пример #7
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"])
    def save_multi_services(self, region_name, tenant, group_id, service, user,
                            service_infos):
        service_source = service_source_repo.get_service_source(
            tenant.tenant_id, service.service_id)
        tx = transaction.savepoint()
        for service_info in service_infos:
            code, msg_show, new_service = app_service \
                .create_source_code_app(region_name, tenant, user,
                                        service.code_from,
                                        service_info["cname"],
                                        service.clone_url,
                                        service.git_project_id,
                                        service.code_version,
                                        service.server_type,
                                        oauth_service_id=service.oauth_service_id,
                                        git_full_name=service.git_full_name)
            if code != 200:
                logger.error(
                    "Multiple services; Service alias: {}; error creating service"
                    .format(service.service_alias))
                transaction.savepoint_rollback(tx)
                return code, msg_show
            # add username and password
            if service_source:
                git_password = service_source.password
                git_user_name = service_source.user_name
                if git_password or git_user_name:
                    app_service.create_service_source_info(
                        tenant, new_service, git_user_name, git_password)
            #  add group
            code, msg_show = group_service.add_service_to_group(
                tenant, region_name, group_id, new_service.service_id)
            if code != 200:
                logger.debug(
                    "Group ID: {0}; Service ID: {1}; error adding service to group"
                    .format(group_id, new_service.service_id))
                transaction.savepoint_rollback(tx)
                return code, msg_show
            # save service info, such as envs, ports, etc
            code, msg = app_check_service.save_service_info(
                tenant, new_service, service_info)
            if code != 200:
                logger.debug(
                    "Group ID: {0}; Service ID: {1}; error saving services".
                    format(group_id, new_service.service_id))
                transaction.savepoint_rollback(tx)
                return code, msg
            new_service = app_service.create_region_service(
                tenant, new_service, user.nick_name)
            new_service.create_status = "complete"
            new_service.save()

        transaction.savepoint_commit(tx)

        return 200, "success"
Пример #9
0
    def save_multi_services(self, region_name, tenant, group_id, service, user,
                            service_infos):
        service_source = service_source_repo.get_service_source(
            tenant.tenant_id, service.service_id)
        service_ids = []
        for service_info in service_infos:
            code, msg_show, new_service = app_service \
                .create_source_code_app(region_name, tenant, user,
                                        service.code_from,
                                        service_info["cname"],
                                        service.clone_url,
                                        service.git_project_id,
                                        service.code_version,
                                        service.server_type,
                                        oauth_service_id=service.oauth_service_id,
                                        git_full_name=service.git_full_name)
            if code != 200:
                raise AbortRequest(
                    "Multiple services; Service alias: {}; error creating service"
                    .format(service.service_alias), "创建多组件应用失败")
            # add username and password
            if service_source:
                git_password = service_source.password
                git_user_name = service_source.user_name
                if git_password or git_user_name:
                    app_service.create_service_source_info(
                        tenant, new_service, git_user_name, git_password)
            #  add group
            code, msg_show = group_service.add_service_to_group(
                tenant, region_name, group_id, new_service.service_id)
            if code != 200:
                logger.debug(
                    "Group ID: {0}; Service ID: {1}; error adding service to group"
                    .format(group_id, new_service.service_id))
                raise AbortRequest("app not found", "创建多组件应用失败", 404, 404)
            # save service info, such as envs, ports, etc
            code, msg = app_check_service.save_service_info(
                tenant, new_service, service_info)
            if code != 200:
                logger.debug(
                    "Group ID: {0}; Service ID: {1}; error saving services".
                    format(group_id, new_service.service_id))
                raise AbortRequest(msg, "创建多组件应用失败")
            new_service = app_service.create_region_service(
                tenant, new_service, user.nick_name)
            new_service.create_status = "complete"
            new_service.save()
            service_ids.append(new_service.service_id)

        return service_ids
Пример #10
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"])
Пример #11
0
    def post(self, request, *args, **kwargs):
        """
        源码创建组件
        ---
        parameters:
            - name: tenantName
              description: 租户名
              required: true
              type: string
              paramType: path
            - name: group_id
              description: 组id
              required: true
              type: string
              paramType: form
            - name: code_from
              description: 组件代码来源
              required: true
              type: string
              paramType: form
            - name: service_cname
              description: 组件名称
              required: true
              type: string
              paramType: form
            - name: git_url
              description: git地址
              required: false
              type: string
              paramType: form
            - name: git_project_id
              description: 代码ID
              required: false
              type: string
              paramType: form
            - name: code_version
              description: 代码版本
              required: false
              type: string
              paramType: form
            - name: username
              description: 私有云用户名称
              required: false
              type: string
              paramType: form
            - name: password
              description: 私有云账户密码
              required: false
              type: string
              paramType: form
            - name: server_type
              description: 仓库类型git或svn
              required: false
              type: string
              paramType: form

        """

        group_id = request.data.get("group_id", -1)
        service_code_from = request.data.get("code_from", None)
        service_cname = request.data.get("service_cname", None)
        service_code_clone_url = request.data.get("git_url", None)
        git_password = request.data.get("password", None)
        git_user_name = request.data.get("username", None)
        service_code_id = request.data.get("git_project_id", None)
        service_code_version = request.data.get("code_version", "master")
        server_type = request.data.get("server_type", "git")
        result = {}
        try:
            if not service_code_clone_url:
                return Response(general_message(400, "code url is null",
                                                "仓库地址未指明"),
                                status=400)
            if not service_code_from:
                return Response(general_message(400, "params error",
                                                "参数service_code_from未指明"),
                                status=400)
            if not server_type:
                return Response(general_message(400, "params error",
                                                "仓库类型未指明"),
                                status=400)
            # 创建源码组件
            if service_code_clone_url:
                service_code_clone_url = service_code_clone_url.strip()
            code, msg_show, new_service = app_service.create_source_code_app(
                self.response_region, self.tenant, self.user,
                service_code_from, service_cname, service_code_clone_url,
                service_code_id, service_code_version, server_type)
            if code != 200:
                return Response(general_message(code, "service create fail",
                                                msg_show),
                                status=code)
            # 添加username,password信息
            if git_password or git_user_name:
                app_service.create_service_source_info(self.tenant,
                                                       new_service,
                                                       git_user_name,
                                                       git_password)

            # 添加组件所在组
            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)
            result = general_message(200,
                                     "success",
                                     "创建成功",
                                     bean=new_service.to_dict())
        except ResourceNotEnoughException as re:
            raise re
        except AccountOverdueException as re:
            logger.exception(re)
            return Response(general_message(10410, "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"])
Пример #12
0
    def post(self, request, *args, **kwargs):
        """
        源码创建组件
        ---
        parameters:
            - name: tenantName
              description: 租户名
              required: true
              type: string
              paramType: path
            - name: group_id
              description: 组id
              required: true
              type: string
              paramType: form
            - name: code_from
              description: 组件代码来源
              required: true
              type: string
              paramType: form
            - name: service_cname
              description: 组件名称
              required: true
              type: string
              paramType: form
            - name: git_url
              description: git地址
              required: false
              type: string
              paramType: form
            - name: git_project_id
              description: 代码ID
              required: false
              type: string
              paramType: form
            - name: code_version
              description: 代码版本
              required: false
              type: string
              paramType: form
            - name: username
              description: 私有云用户名称
              required: false
              type: string
              paramType: form
            - name: password
              description: 私有云账户密码
              required: false
              type: string
              paramType: form
            - name: server_type
              description: 仓库类型git或svn
              required: false
              type: string
              paramType: form

        """

        group_id = request.data.get("group_id", -1)
        service_code_from = request.data.get("code_from", None)
        service_cname = request.data.get("service_cname", None)
        service_code_clone_url = request.data.get("git_url", None)
        git_password = request.data.get("password", None)
        git_user_name = request.data.get("username", None)
        service_code_id = request.data.get("git_project_id", None)
        service_code_version = request.data.get("code_version", "master")
        is_oauth = request.data.get("is_oauth", False)
        check_uuid = request.data.get("check_uuid")
        event_id = request.data.get("event_id")
        server_type = request.data.get("server_type", "git")
        user_id = request.user.user_id
        oauth_service_id = request.data.get("service_id")
        git_full_name = request.data.get("full_name")
        git_service = None
        open_webhook = False
        host = os.environ.get('DEFAULT_DOMAIN', "http://" + request.get_host())

        result = {}
        if is_oauth:
            open_webhook = request.data.get("open_webhook", False)
            try:
                oauth_service = oauth_repo.get_oauth_services_by_service_id(service_id=oauth_service_id)
                oauth_user = oauth_user_repo.get_user_oauth_by_user_id(service_id=oauth_service_id, user_id=user_id)
            except Exception as e:
                logger.debug(e)
                rst = {"data": {"bean": None}, "status": 400, "msg_show": "未找到OAuth服务, 请检查该服务是否存在且属于开启状态"}
                return Response(rst, status=200)
            try:
                git_service = get_oauth_instance(oauth_service.oauth_type, oauth_service, oauth_user)
            except Exception as e:
                logger.debug(e)
                rst = {"data": {"bean": None}, "status": 400, "msg_show": "未找到OAuth服务"}
                return Response(rst, status=200)
            if not git_service.is_git_oauth():
                rst = {"data": {"bean": None}, "status": 400, "msg_show": "该OAuth服务不是代码仓库类型"}
                return Response(rst, status=200)

            service_code_from = "oauth_" + oauth_service.oauth_type
        try:
            if not service_code_clone_url:
                return Response(general_message(400, "code url is null", "仓库地址未指明"), status=400)
            if not service_code_from:
                return Response(general_message(400, "params error", "参数service_code_from未指明"), status=400)
            if not server_type:
                return Response(general_message(400, "params error", "仓库类型未指明"), status=400)
            # 创建源码组件
            if service_code_clone_url:
                service_code_clone_url = service_code_clone_url.strip()
            code, msg_show, new_service = app_service.create_source_code_app(
                self.response_region, self.tenant, self.user, service_code_from, service_cname, service_code_clone_url,
                service_code_id, service_code_version, server_type, check_uuid, event_id, oauth_service_id, git_full_name)
            if code != 200:
                return Response(general_message(code, "service create fail", msg_show), status=code)
            # 添加username,password信息
            if git_password or git_user_name:
                app_service.create_service_source_info(self.tenant, new_service, git_user_name, git_password)

            # 自动添加hook
            if open_webhook and is_oauth and not new_service.open_webhooks:
                service_webhook = service_webhooks_repo.create_service_webhooks(new_service.service_id, "code_webhooks")
                service_webhook.state = True
                service_webhook.deploy_keyword = "deploy"
                service_webhook.save()
                try:
                    git_service.create_hook(host, git_full_name, endpoint='console/webhooks/' + new_service.service_id)
                    new_service.open_webhooks = True
                except Exception as e:
                    logger.exception(e)
                    new_service.open_webhooks = False
                new_service.save()
            # 添加组件所在组

            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)
            bean = new_service.to_dict()
            result = general_message(200, "success", "创建成功", bean=bean)
        except ResourceNotEnoughException as re:
            raise re
        except AccountOverdueException as re:
            logger.exception(re)
            return Response(general_message(10410, "resource is not enough", re.message), status=412)
        return Response(result, status=result["code"])
Пример #13
0
    def install_service(self, tenant, region, user, group_id, market_app):
        service_list = []
        service_key_dep_key_map = {}
        key_service_map = {}
        tenant_service_group = None
        service_probe_map = {}
        try:
            app_templates = json.loads(market_app.app_template)
            apps = app_templates["apps"]
            tenant_service_group = self.__create_tenant_service_group(
                region, tenant.tenant_id, group_id, market_app.group_key,
                market_app.version, market_app.group_name)
            for app in apps:
                ts = self.__init_market_app(tenant.tenant_id, region, user,
                                            app, tenant_service_group.ID)
                group_service.add_service_to_group(tenant, region, group_id,
                                                   ts.service_id)
                service_list.append(ts)

                # 先保存env,再保存端口,因为端口需要处理env
                code, msg = self.__save_env(
                    tenant, ts, app["service_env_map_list"],
                    app["service_connect_info_map_list"])
                if code != 200:
                    raise Exception(msg)
                code, msg = self.__save_port(tenant, ts, app["port_map_list"])
                if code != 200:
                    raise Exception(msg)
                code, msg = self.__save_volume(tenant, ts,
                                               app["service_volume_map_list"])
                if code != 200:
                    raise Exception(msg)

                # 保存应用探针信息
                probe_infos = app.get("probes", None)
                if probe_infos:
                    service_probe_map[ts.service_id] = probe_infos

                self.__save_extend_info(ts, app["extend_method_map"])

                dep_apps_key = app.get("dep_service_map_list", None)
                if dep_apps_key:
                    service_key_dep_key_map[ts.service_key] = dep_apps_key
                key_service_map[ts.service_key] = ts
            # 保存依赖关系
            self.__save_service_deps(tenant, service_key_dep_key_map,
                                     key_service_map)
            # 构建应用
            self.__build_services(tenant, user, service_list,
                                  service_probe_map)
        except Exception as e:
            logger.exception(e)
            if tenant_service_group:
                tenant_service_group_repo.delete_tenant_service_group_by_pk(
                    tenant_service_group.ID)
            for service in service_list:
                try:
                    app_manage_service.truncate_service(tenant, service)
                except Exception as le:
                    logger.exception(le)
            raise e
Пример #14
0
    def install_service(self, tenant, region, user, group_id, market_app,
                        is_deploy):
        service_list = []
        service_key_dep_key_map = {}
        key_service_map = {}
        tenant_service_group = None
        service_probe_map = {}
        app_plugin_map = {}  # 新装服务对应的安装的插件映射
        old_new_id_map = {}  # 新旧服务映射关系
        try:
            app_templates = json.loads(market_app.app_template)
            apps = app_templates["apps"]
            tenant_service_group = self.__create_tenant_service_group(
                region, tenant.tenant_id, group_id, market_app.group_key,
                market_app.version, market_app.group_name)

            status, msg = self.__create_plugin_for_tenant(
                region, user, tenant, app_templates.get("plugins", []))
            if status != 200:
                raise Exception(msg)

            for app in apps:
                ts = self.__init_market_app(tenant, region, user, app,
                                            tenant_service_group.ID)
                group_service.add_service_to_group(tenant, region, group_id,
                                                   ts.service_id)
                service_list.append(ts)
                old_new_id_map[app["service_id"]] = ts

                # 先保存env,再保存端口,因为端口需要处理env
                code, msg = self.__save_env(
                    tenant, ts, app["service_env_map_list"],
                    app["service_connect_info_map_list"])
                if code != 200:
                    raise Exception(msg)
                code, msg = self.__save_port(tenant, ts, app["port_map_list"])
                if code != 200:
                    raise Exception(msg)
                code, msg = self.__save_volume(tenant, ts,
                                               app["service_volume_map_list"])
                if code != 200:
                    raise Exception(msg)

                # 保存应用探针信息
                probe_infos = app.get("probes", None)
                if probe_infos:
                    service_probe_map[ts.service_id] = probe_infos

                self.__save_extend_info(ts, app["extend_method_map"])
                if app.get("service_share_uuid", None):
                    dep_apps_key = app.get("dep_service_map_list", None)
                    if dep_apps_key:
                        service_key_dep_key_map[app.get(
                            "service_share_uuid")] = dep_apps_key
                    key_service_map[app.get("service_share_uuid")] = ts
                else:
                    dep_apps_key = app.get("dep_service_map_list", None)
                    if dep_apps_key:
                        service_key_dep_key_map[ts.service_key] = dep_apps_key
                    key_service_map[ts.service_key] = ts
                app_plugin_map[ts.service_id] = app.get(
                    "service_related_plugin_config")

            # 保存依赖关系
            self.__save_service_deps(tenant, service_key_dep_key_map,
                                     key_service_map)

            # 数据中心创建应用
            new_service_list = self.__create_region_services(
                tenant, user, service_list, service_probe_map)
            # 创建应用插件
            self.__create_service_plugins(region, tenant, service_list,
                                          app_plugin_map, old_new_id_map)
            if is_deploy:
                # 部署所有应用
                self.__deploy_services(tenant, user, new_service_list)
        except Exception as e:
            logger.exception(e)
            if tenant_service_group:
                tenant_service_group_repo.delete_tenant_service_group_by_pk(
                    tenant_service_group.ID)
            for service in service_list:
                try:
                    app_manage_service.truncate_service(tenant, service)
                except Exception as le:
                    logger.exception(le)
            raise e
Пример #15
0
    def post(self, request, *args, **kwargs):
        """
        image和docker-run创建应用
        ---
        parameters:
            - name: tenantName
              description: 租户名
              required: true
              type: string
              paramType: path
            - name: group_id
              description: 组id
              required: true
              type: string
              paramType: form
            - name: service_cname
              description: 应用名称
              required: true
              type: string
              paramType: form
            - name: docker_cmd
              description: docker运行命令
              required: true
              type: string
              paramType: form
            - name: image_type
              description: 创建方式 docker_run或docker_image
              required: true
              type: string
              paramType: form

        """
        image_type = request.data.get("image_type", None)
        group_id = request.data.get("group_id", -1)
        service_cname = request.data.get("service_cname", None)
        docker_cmd = request.data.get("docker_cmd", "")

        try:
            if not image_type:
                return Response(general_message(400,
                                                "image_type cannot be null",
                                                "参数错误"),
                                status=400)
            if not docker_cmd:
                return Response(general_message(400,
                                                "docker_cmd cannot be null",
                                                "参数错误"),
                                status=400)

            code, msg_show, new_service = app_service.create_docker_run_app(
                self.response_region, self.tenant, self.user, service_cname,
                docker_cmd, image_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)
            result = general_message(200,
                                     "success",
                                     "创建成功",
                                     bean=new_service.to_dict())
        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()
        return Response(result, status=result["code"])
    def save_data(
        self,
        migrate_tenant,
        migrate_region,
        user,
        changed_service_map,
        metadata,
        group_id,
        same_team,
        same_region,
        sync_flag=False,
    ):
        from console.services.groupcopy_service import groupapp_copy_service
        group = group_repo.get_group_by_id(group_id)
        apps = metadata["apps"]

        old_new_service_id_map = dict()
        service_relations_list = []
        service_mnt_list = []
        # restore component
        for app in apps:
            service_base_info = app["service_base"]
            new_service_id = changed_service_map[
                service_base_info["service_id"]]["ServiceID"]
            new_service_alias = changed_service_map[
                service_base_info["service_id"]]["ServiceAlias"]
            ts = self.__init_app(app["service_base"], new_service_id,
                                 new_service_alias, user, migrate_region,
                                 migrate_tenant)
            old_new_service_id_map[app["service_base"]
                                   ["service_id"]] = ts.service_id
            group_service.add_service_to_group(migrate_tenant, migrate_region,
                                               group.ID, ts.service_id)
            self.__save_port(migrate_region, migrate_tenant, ts,
                             app["service_ports"], group.governance_mode,
                             app["service_env_vars"], sync_flag)
            self.__save_env(migrate_tenant, ts, app["service_env_vars"])
            self.__save_volume(
                migrate_tenant, ts, app["service_volumes"],
                app["service_config_file"]
                if 'service_config_file' in app else None)
            self.__save_compile_env(ts, app["service_compile_env"])
            self.__save_service_label(migrate_tenant, ts, migrate_region,
                                      app["service_labels"])
            if sync_flag:
                self.__save_service_probes(ts, app["service_probes"])
            self.__save_service_source(migrate_tenant, ts,
                                       app["service_source"])
            self.__save_service_auth(ts, app["service_auths"])
            self.__save_third_party_service_endpoints(
                ts, app.get("third_party_service_endpoints", []))
            self.__save_service_monitors(migrate_tenant, ts,
                                         app.get("service_monitors"))
            self.__save_component_graphs(ts, app.get("component_graphs"))

            if ts.service_source == "third_party":
                app_service.create_third_party_service(migrate_tenant, ts,
                                                       user.nick_name)
                probes = probe_repo.get_service_probe(ts.service_id)
                # 为组件添加默认探针
                if not probes:
                    if groupapp_copy_service.is_need_to_add_default_probe(ts):
                        code, msg, probe = app_service.add_service_default_porbe(
                            migrate_tenant, ts)
                        logger.debug(
                            "add default probe; code: {}; msg: {}".format(
                                code, msg))
                else:
                    for probe in probes:
                        prob_data = {
                            "service_id": ts.service_id,
                            "scheme": probe.scheme,
                            "path": probe.path,
                            "port": probe.port,
                            "cmd": probe.cmd,
                            "http_header": probe.http_header,
                            "initial_delay_second": probe.initial_delay_second,
                            "period_second": probe.period_second,
                            "timeout_second": probe.timeout_second,
                            "failure_threshold": probe.failure_threshold,
                            "success_threshold": probe.success_threshold,
                            "is_used": (1 if probe.is_used else 0),
                            "probe_id": probe.probe_id,
                            "mode": probe.mode,
                        }
                        try:
                            res, body = region_api.add_service_probe(
                                ts.service_region, migrate_tenant.tenant_name,
                                ts.service_alias, prob_data)
                            if res.get("status") != 200:
                                logger.debug(body)
                                probe.delete()
                        except Exception as e:
                            logger.debug("error", e)
                            probe.delete()
            service_relations = app["service_relation"]
            service_mnts = app["service_mnts"]

            if service_relations:
                service_relations_list[0:0] = list(service_relations)
            if service_mnts:
                service_mnt_list[0:0] = list(service_mnts)
            # 更新状态
            ts.create_status = "complete"
            ts.save()

        # restore plugin info
        self.__save_plugins(migrate_region, migrate_tenant,
                            metadata["plugin_info"]["plugins"])
        self.__save_plugin_config_items(
            metadata["plugin_info"]["plugin_config_items"])
        self.__save_plugin_config_groups(
            metadata["plugin_info"]["plugin_config_groups"])
        versions = self.__save_plugin_build_versions(
            migrate_tenant, metadata["plugin_info"]["plugin_build_versions"])
        for app in apps:
            new_service_id = old_new_service_id_map[app["service_base"]
                                                    ["service_id"]]
            # plugin
            if app.get("service_plugin_relation", None):
                self.__save_plugin_relations(new_service_id,
                                             app["service_plugin_relation"],
                                             versions)
            if app.get("service_plugin_config", None):
                self.__save_service_plugin_config(new_service_id,
                                                  app["service_plugin_config"])
        self.__save_service_relations(migrate_tenant, service_relations_list,
                                      old_new_service_id_map, same_team,
                                      same_region)
        self.__save_service_mnt_relation(migrate_tenant, service_mnt_list,
                                         old_new_service_id_map, same_team,
                                         same_region)
        # restore application config group
        self.__save_app_config_groups(metadata.get("app_config_group_info"),
                                      migrate_tenant, migrate_region, group_id,
                                      changed_service_map)
Пример #17
0
    def install_market_apps_directly(self, user, tenant, region_name,
                                     app_service_json_str, service_origin):
        app_templates = json.loads(app_service_json_str)
        apps = app_templates["apps"]

        service_list = []
        service_key_dep_key_map = {}
        key_service_map = {}
        tenant_service_group = None
        new_group = None
        try:
            # 生成分类
            group_name = self.__generator_group_name(
                app_templates["group_name"])
            new_group = group_repo.add_group(tenant.tenant_id, region_name,
                                             group_name)
            group_id = new_group.ID
            tenant_service_group = self.__generate_tenant_service_group(
                region_name, tenant.tenant_id, new_group.ID,
                app_templates["group_key"], app_templates["group_version"],
                app_templates["group_name"])
            for app in apps:
                ts = self.__init_market_app(tenant, region_name, user, app,
                                            tenant_service_group.ID,
                                            service_origin)
                group_service.add_service_to_group(tenant, region_name,
                                                   group_id, ts.service_id)

                # 先保存env,再保存端口,因为端口需要处理env
                code, msg = self.__save_env(
                    tenant, ts, app["service_env_map_list"],
                    app["service_connect_info_map_list"])
                if code != 200:
                    raise Exception(msg)
                code, msg = self.__save_port(tenant, ts, app["port_map_list"])
                if code != 200:
                    raise Exception(msg)
                code, msg = self.__save_volume(tenant, ts,
                                               app["service_volume_map_list"])
                if code != 200:
                    raise Exception(msg)

                # 保存应用探针信息
                probe_infos = app.get("probes", None)
                if probe_infos:
                    for prob_data in probe_infos:
                        code, msg, probe = probe_service.add_service_probe(
                            tenant, ts, prob_data)
                        if code != 200:
                            logger.exception(msg)

                self.__save_extend_info(ts, app["extend_method_map"])

                dep_apps_key = app.get("dep_service_map_list", None)
                if dep_apps_key:
                    service_key_dep_key_map[ts.service_key] = dep_apps_key
                key_service_map[ts.service_key] = ts
                service_list.append(ts)
            # 保存依赖关系,需要等应用都创建完成才能使用
            self.__save_service_deps(tenant, service_key_dep_key_map,
                                     key_service_map)
            # 数据中心创建应用
            for service in service_list:
                new_service = app_service.create_region_service(
                    tenant, service, user.nick_name)
                logger.debug("build service ===> {0}  success".format(
                    service.service_cname))
                # 为服务添加探针
                self.__create_service_probe(tenant, new_service)

                # 添加服务有无状态标签
                label_service.update_service_state_label(tenant, new_service)

            return True, "success", tenant_service_group, service_list
        except Exception as e:
            logger.exception(e)
            # 回滚数据
            if tenant_service_group:
                tenant_service_group_repo.delete_tenant_service_group_by_pk(
                    tenant_service_group.ID)
            if new_group:
                group_repo.delete_group_by_pk(new_group.ID)
            for service in service_list:
                try:
                    app_manage_service.truncate_service(tenant, service)
                except Exception as delete_error:
                    logger.exception(delete_error)
            return False, "create tenant_services from market directly failed !", None, service_list