Ejemplo n.º 1
0
    def post(self, request, *args, **kwargs):
        """
        构建插件
        ---
        parameters:
            - name: tenantName
              description: 租户名
              required: true
              type: string
              paramType: path
            - name: plugin_id
              description: 插件ID
              required: true
              type: string
              paramType: path
            - name: build_version
              description: 版本
              required: true
              type: string
              paramType: path
            - name: update_info
              description: 更新说明
              required: false
              type: string
              paramType: form
        """
        try:

            update_info = request.data.get("update_info", None)

            if self.plugin_version.plugin_version_status == "fixed":
                return Response(general_message(409, "current version is fixed", "该版本已固定,不能构建"), status=409)

            if self.plugin_version.build_status == "building":
                return Response(general_message(409, "too offen", "构建中,请稍后再试"), status=409)

            if update_info:
                self.plugin_version.update_info = update_info
                self.plugin_version.save()
            event_id = make_uuid()

            self.plugin_version.build_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            self.plugin_version.save()

            try:
                plugin_service.build_plugin(self.response_region, self.plugin, self.plugin_version, self.user,
                                            self.tenant, event_id)
                self.plugin_version.build_status = "building"
                self.plugin_version.event_id = event_id
                self.plugin_version.save()
                bean = {"event_id": event_id}
                result = general_message(200, "success", "操作成功", bean=bean)
            except Exception as e:
                logger.exception(e)
                result = general_message(500, "region invoke error", "构建失败,请查看镜像或源代码是否正确")

        except Exception as e:
            logger.exception(e)
            result = error_message(e.message)
        return Response(result, status=result["code"])
Ejemplo n.º 2
0
    def __install_plugin(self, region_name, user, tenant, plugin_template):
        image = None
        image_tag = None
        if plugin_template["share_image"]:
            image_and_tag = plugin_template["share_image"].rsplit(":", 1)
            if len(image_and_tag) > 1:
                image = image_and_tag[0]
                image_tag = image_and_tag[1]
            else:
                image = image_and_tag[0]
                image_tag = "latest"

        status, msg, plugin_base_info = plugin_service.create_tenant_plugin(
            tenant, user.user_id, region_name, plugin_template["desc"],
            plugin_template["plugin_alias"], plugin_template["category"],
            "image", image, plugin_template["code_repo"])
        if status != 200:
            return status, msg

        plugin_base_info.origin = 'local_market'
        plugin_base_info.origin_share_id = plugin_template.get("plugin_key")
        plugin_base_info.save()

        build_version = plugin_template.get('build_version')
        min_memory = plugin_template.get('min_memory', 128)

        plugin_build_version = plugin_version_service.create_build_version(
            region_name,
            plugin_base_info.plugin_id,
            tenant.tenant_id,
            user.user_id,
            "",
            "unbuild",
            min_memory,
            image_tag=image_tag,
            code_version="",
            build_version=build_version)

        share_config_groups = plugin_template.get('config_groups', [])

        plugin_config_service.create_config_groups(plugin_base_info.plugin_id,
                                                   build_version,
                                                   share_config_groups)

        event_id = make_uuid()
        plugin_build_version.event_id = event_id
        plugin_build_version.plugin_version_status = "fixed"

        plugin_service.create_region_plugin(region_name,
                                            tenant,
                                            plugin_base_info,
                                            image_tag=image_tag)

        ret = plugin_service.build_plugin(
            region_name, plugin_base_info, plugin_build_version, user, tenant,
            event_id, plugin_template.get("plugin_image", None))
        plugin_build_version.build_status = ret.get('bean').get('status')
        plugin_build_version.save()
        return 200, "success"
    def install_plugin(self, user, tenant, region_name, market_plugin):
        plugin_template = json.loads(market_plugin.plugin_template)
        share_plugin_info = plugin_template.get("share_plugin_info")

        sid = transaction.savepoint()

        try:
            image = None
            image_tag = None
            if plugin_template["share_image"]:
                image_and_tag = plugin_template["share_image"].rsplit(":", 1)
                if len(image_and_tag) > 1:
                    image = image_and_tag[0]
                    image_tag = image_and_tag[1]
                else:
                    image = image_and_tag[0]
                    image_tag = "latest"

            status, msg, plugin_base_info = plugin_service.create_tenant_plugin(
                tenant, user.user_id, region_name,
                share_plugin_info.get("desc"), plugin_template["plugin_name"],
                plugin_template["category"], "image", image,
                plugin_template["code_repo"])

            if status != 200:
                return status, msg

            plugin_base_info.origin = 'local_market' if market_plugin.source == 'local' else market_plugin.source
            plugin_base_info.origin_share_id = share_plugin_info.get(
                "plugin_key")
            plugin_base_info.save()

            build_version = plugin_template.get('build_version')
            min_memory = build_version.get('min_memory')

            plugin_build_version = plugin_version_service.create_build_version(
                region_name,
                plugin_base_info.plugin_id,
                tenant.tenant_id,
                user.user_id,
                "",
                "unbuild",
                min_memory,
                image_tag=image_tag,
                code_version="")

            config_groups, config_items = [], []
            share_config_groups = share_plugin_info.get('config_groups', [])

            for group in share_config_groups:
                plugin_config_group = PluginConfigGroup(
                    plugin_id=plugin_base_info.plugin_id,
                    build_version=plugin_build_version.build_version,
                    config_name=group.get("config_name"),
                    service_meta_type=group.get("service_meta_type"),
                    injection=group.get("injection"))
                config_groups.append(plugin_config_group)

                share_config_items = group.get('config_items', [])
                for item in share_config_items:
                    plugin_config_item = PluginConfigItems(
                        plugin_id=plugin_base_info.plugin_id,
                        build_version=plugin_build_version.build_version,
                        service_meta_type=item.get("service_meta_type"),
                        attr_name=item.get("attr_name"),
                        attr_alt_value=item.get("attr_alt_value"),
                        attr_type=item.get("attr_type", "string"),
                        attr_default_value=item.get("attr_default_value",
                                                    None),
                        is_change=item.get("is_change", False),
                        attr_info=item.get("attr_info", ""),
                        protocol=item.get("protocol", ""))
                    config_items.append(plugin_config_item)

            PluginConfigGroup.objects.bulk_create(config_groups)
            PluginConfigItems.objects.bulk_create(config_items)

            event_id = make_uuid()
            plugin_build_version.event_id = event_id
            plugin_build_version.plugin_version_status = "fixed"

            plugin_service.create_region_plugin(region_name,
                                                tenant,
                                                plugin_base_info,
                                                image_tag=image_tag)

            ret = plugin_service.build_plugin(
                region_name, plugin_base_info, plugin_build_version, user,
                tenant, event_id, share_plugin_info.get("plugin_image", None))
            plugin_build_version.build_status = ret.get('bean').get('status')
            plugin_build_version.save()

            return 200, '安装成功'
        except Exception as e:
            logger.exception(e)
            if sid:
                transaction.savepoint_rollback(sid)
            return 500, '插件安装失败'
Ejemplo n.º 4
0
    def build_services(self, user, tenant, region_name, group_id,
                       change_services_map):
        group_services = base_service.get_group_services_list(
            tenant.tenant_id, region_name, group_id)
        change_service_ids = [
            change_service["ServiceID"]
            for change_service in list(change_services_map.values())
        ]
        if not group_services:
            return []
        service_ids = [
            group_service.get("service_id") for group_service in group_services
        ]
        services = service_repo.get_service_by_service_ids(
            service_ids=service_ids)
        result = []
        for service in services:
            if service.service_id in change_service_ids:
                if service.service_source == "third_party":
                    # 数据中心连接创建第三方组件
                    new_service = app_service.create_third_party_service(
                        tenant, service, user.nick_name)
                else:
                    # 数据中心创建组件
                    new_service = app_service.create_region_service(
                        tenant, service, user.nick_name)

                service = new_service
                # 为组件添加默认探针
                if self.is_need_to_add_default_probe(service):
                    code, msg, probe = app_service.add_service_default_porbe(
                        tenant, service)
                    logger.debug("add default probe; code: {}; msg: {}".format(
                        code, msg))
                else:
                    probes = probe_repo.get_service_probe(service.service_id)
                    if probes:
                        for probe in probes:
                            prob_data = {
                                "service_id": service.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(
                                    service.service_region, tenant.tenant_name,
                                    service.service_alias, prob_data)
                                if res.get("status") != 200:
                                    logger.debug(body)
                                    probe.delete()
                            except Exception as e:
                                logger.debug("error", e)
                                probe.delete()
                # 添加组件有无状态标签
                label_service.update_service_state_label(tenant, service)
                # 部署组件
                app_manage_service.deploy(tenant,
                                          service,
                                          user,
                                          group_version=None)

                # 添加组件部署关系
                deploy_repo.create_deploy_relation_by_service_id(
                    service_id=service.service_id)
                result.append(service)
                # 为组件创建插件
                build_error_plugin_ids = []
                service_plugins = app_plugin_relation_repo.get_service_plugin_relation_by_service_id(
                    service.service_id)
                for service_plugin in service_plugins:
                    plugin = plugin_repo.get_by_plugin_id(
                        tenant.tenant_id, service_plugin.plugin_id)
                    plugin_version = plugin_repo.get_plugin_buildversion(
                        service_plugin.plugin_id, service_plugin.build_version)
                    # 在数据中心创建插件
                    try:
                        event_id = make_uuid()
                        plugin_version.event_id = event_id
                        image_tag = (plugin_version.image_tag
                                     if plugin_version.image_tag else "latest")
                        plugin_service.create_region_plugin(
                            region_name, tenant, plugin, image_tag=image_tag)
                        ret = plugin_service.build_plugin(
                            region_name, plugin, plugin_version, user, tenant,
                            event_id)
                        plugin_version.build_status = ret.get('bean').get(
                            'status')
                        plugin_version.save()
                    except Exception as e:
                        logger.debug(e)
                    # 为组件开通插件
                    try:
                        region_config = app_plugin_service.get_region_config_from_db(
                            service, service_plugin.plugin_id,
                            service_plugin.build_version)
                        data = dict()
                        data["plugin_id"] = service_plugin.plugin_id
                        data["switch"] = True
                        data["version_id"] = service_plugin.build_version
                        data.update(region_config)
                        region_api.install_service_plugin(
                            region_name, tenant.tenant_name,
                            service.service_alias, data)
                    except region_api.CallApiError as e:
                        logger.debug(e)
                        build_error_plugin_ids.append(service_plugin.plugin_id)
                if build_error_plugin_ids:
                    app_plugin_relation_repo.get_service_plugin_relation_by_service_id(
                        service.service_id).filter(
                            plugin_id__in=build_error_plugin_ids).delete()
                # create service_monitor in region
                service_monitors = service_monitor_repo.get_component_service_monitors(
                    tenant.tenant_id, service.service_id)
                for monitor in service_monitors:
                    req = {
                        "name": monitor.name,
                        "path": monitor.path,
                        "port": monitor.port,
                        "service_show_name": monitor.service_show_name,
                        "interval": monitor.interval
                    }
                    try:
                        region_api.create_service_monitor(
                            tenant.enterprise_id, service.service_region,
                            tenant.tenant_name, service.service_alias, req)
                    except region_api.CallApiError as e:
                        ServiceMonitor.objects.filter(
                            tenant_id=tenant.tenant_id,
                            service_id=service.service_id,
                            name=monitor.name).delete()
                        logger.debug(e)
        return result
Ejemplo n.º 5
0
    def build_services(self, user, tenant, region_name, group_id,
                       change_services_map):
        group_services = base_service.get_group_services_list(
            tenant.tenant_id, region_name, group_id)
        change_service_ids = [
            change_service["ServiceID"]
            for change_service in list(change_services_map.values())
        ]
        if not group_services:
            return []
        service_ids = [
            group_service.get("service_id") for group_service in group_services
        ]
        services = service_repo.list_by_component_ids(service_ids=service_ids)
        result = []
        for service in services:
            if service.service_id in change_service_ids:
                if service.service_source == "third_party":
                    # 数据中心连接创建第三方组件
                    new_service = app_service.create_third_party_service(
                        tenant, service, user.nick_name)
                else:
                    # 数据中心创建组件
                    new_service = app_service.create_region_service(
                        tenant, service, user.nick_name)
                service = new_service
                # 部署组件
                app_manage_service.deploy(tenant,
                                          service,
                                          user,
                                          group_version=None)

                # 添加组件部署关系
                deploy_repo.create_deploy_relation_by_service_id(
                    service_id=service.service_id)
                result.append(service)
                # 为组件创建插件
                build_error_plugin_ids = []
                service_plugins = app_plugin_relation_repo.get_service_plugin_relation_by_service_id(
                    service.service_id)
                for service_plugin in service_plugins:
                    plugin = plugin_repo.get_by_plugin_id(
                        tenant.tenant_id, service_plugin.plugin_id)
                    plugin_version = plugin_repo.get_plugin_buildversion(
                        service_plugin.plugin_id, service_plugin.build_version)
                    # 在数据中心创建插件
                    try:
                        event_id = make_uuid()
                        plugin_version.event_id = event_id
                        image_tag = (plugin_version.image_tag
                                     if plugin_version.image_tag else "latest")
                        plugin_service.create_region_plugin(
                            region_name, tenant, plugin, image_tag=image_tag)
                        ret = plugin_service.build_plugin(
                            region_name, plugin, plugin_version, user, tenant,
                            event_id)
                        plugin_version.build_status = ret.get('bean').get(
                            'status')
                        plugin_version.save()
                    except Exception as e:
                        logger.debug(e)
                    # 为组件开通插件
                    try:
                        region_config = app_plugin_service.get_region_config_from_db(
                            service, service_plugin.plugin_id,
                            service_plugin.build_version)
                        data = dict()
                        data["plugin_id"] = service_plugin.plugin_id
                        data["switch"] = True
                        data["version_id"] = service_plugin.build_version
                        data["plugin_cpu"] = service_plugin.min_cpu
                        data["plugin_memory"] = service_plugin.min_memory
                        data.update(region_config)
                        region_api.install_service_plugin(
                            region_name, tenant.tenant_name,
                            service.service_alias, data)
                    except region_api.CallApiError as e:
                        logger.debug(e)
                        build_error_plugin_ids.append(service_plugin.plugin_id)
                if build_error_plugin_ids:
                    app_plugin_relation_repo.get_service_plugin_relation_by_service_id(
                        service.service_id).filter(
                            plugin_id__in=build_error_plugin_ids).delete()
        return result