Esempio n. 1
0
 def cloud_app_model_to_db_model(self,
                                 market,
                                 app_id,
                                 version,
                                 for_install=False):
     app = app_store.get_app(market, app_id)
     app_template = app_store.get_app_version(market,
                                              app_id,
                                              version,
                                              for_install=for_install,
                                              get_template=True)
     rainbond_app = RainbondCenterApp(app_id=app.app_key_id,
                                      app_name=app.name,
                                      dev_status=app.dev_status,
                                      source="market",
                                      scope="goodrain",
                                      describe=app.desc,
                                      details=app.introduction,
                                      pic=app.logo,
                                      create_time=app.create_time,
                                      update_time=app.update_time)
     rainbond_app.market_name = market.name
     rainbond_app_version = RainbondCenterAppVersion(
         app_id=app.app_key_id,
         app_template=app_template.template,
         version=app_template.version,
         version_alias=app_template.version_alias,
         template_version=app_template.rainbond_version,
         app_version_info=app_template.description,
         update_time=app_template.update_time,
         is_official=1)
     rainbond_app_version.template_type = app_template.template_type
     return rainbond_app, rainbond_app_version
Esempio n. 2
0
 def down_market_group_list(self, tenant):
     app_group_list = market_api.get_service_group_list(tenant.tenant_id)
     rainbond_apps = []
     for app_group in app_group_list:
         rbc = rainbond_app_repo.get_rainbond_app_by_key_and_version(
             app_group["group_key"], app_group["group_version"])
         if rbc:
             rbc.describe = app_group["info"] if app_group[
                 "info"] else rbc.describe
             rbc.pic = app_group["pic"] if app_group["pic"] else rbc.pic
             rbc.update_time = current_time_str("%Y-%m-%d %H:%M:%S")
             rbc.template_version = app_group.get("template_version",
                                                  rbc.template_version)
             rbc.save()
         else:
             rainbond_app = RainbondCenterApp(
                 group_key=app_group["group_key"],
                 group_name=app_group["group_name"],
                 version=app_group['group_version'],
                 share_user=0,
                 record_id=0,
                 share_team="",
                 source="market",
                 scope="goodrain",
                 describe=app_group["info"],
                 pic=app_group["pic"],
                 app_template="",
                 template_version=app_group.get("template_version", ""))
             rainbond_apps.append(rainbond_app)
     rainbond_app_repo.bulk_create_rainbond_apps(rainbond_apps)
Esempio n. 3
0
    def __save_import_info(self, tenant_name, scope, metadata):
        rainbond_apps = []
        metadata = json.loads(metadata)
        for app_template in metadata:
            app = rainbond_app_repo.get_rainbond_app_by_key_and_version(app_template["group_key"],
                                                                        app_template["group_version"])
            if app:
                continue
            image_base64_string = app_template.pop("image_base64_string", "")
            pic_url = ""
            if image_base64_string:
                pic_url = self.decode_image(image_base64_string, app_template.pop("suffix", "jpg"))

            rainbond_app = RainbondCenterApp(
                group_key=app_template["group_key"],
                group_name=app_template["group_name"],
                version=app_template['group_version'],
                share_user=0,
                record_id=0,
                share_team=tenant_name,
                source="import",
                scope=scope,
                describe=app_template.pop("describe", ""),
                pic=pic_url,
                app_template=json.dumps(app_template),
                is_complete=True,
                template_version=app_template.get("template_version", "")
            )
            rainbond_apps.append(rainbond_app)
        rainbond_app_repo.bulk_create_rainbond_apps(rainbond_apps)
 def __save_enterprise_import_info(self, import_record, metadata):
     rainbond_apps = []
     rainbond_app_versions = []
     metadata = json.loads(metadata)
     key_and_version_list = []
     if not metadata:
         return
     for app_template in metadata:
         app = rainbond_app_repo.get_rainbond_app_by_app_id(
             import_record.enterprise_id, app_template["group_key"])
         # if app exists, update it
         if app:
             app.scope = import_record.scope
             app.describe = app_template.pop("describe", "")
             app.save()
             app_version = rainbond_app_repo.get_rainbond_app_version_by_app_id_and_version(
                 app.app_id, app_template["group_version"])
             if app_version:
                 # update version if exists
                 app_version.scope = import_record.scope
                 app_version.app_template = json.dumps(app_template)
                 app_version.template_version = app_template[
                     "template_version"]
                 app_version.save()
             else:
                 # create a new version
                 rainbond_app_versions.append(
                     self.create_app_version(app, import_record,
                                             app_template))
         else:
             image_base64_string = app_template.pop("image_base64_string",
                                                    "")
             pic_url = ""
             if image_base64_string:
                 pic_url = self.decode_image(
                     image_base64_string, app_template.pop("suffix", "jpg"))
             key_and_version = "{0}:{1}".format(
                 app_template["group_key"], app_template['group_version'])
             if key_and_version in key_and_version_list:
                 continue
             key_and_version_list.append(key_and_version)
             rainbond_app = RainbondCenterApp(
                 enterprise_id=import_record.enterprise_id,
                 app_id=app_template["group_key"],
                 app_name=app_template["group_name"],
                 source="import",
                 create_team=import_record.team_name,
                 scope=import_record.scope,
                 describe=app_template.pop("describe", ""),
                 pic=pic_url,
             )
             rainbond_apps.append(rainbond_app)
             # create a new app version
             rainbond_app_versions.append(
                 self.create_app_version(rainbond_app, import_record,
                                         app_template))
     rainbond_app_repo.bulk_create_rainbond_app_versions(
         rainbond_app_versions)
     rainbond_app_repo.bulk_create_rainbond_apps(rainbond_apps)
Esempio n. 5
0
def test_rbd_app_not_found(mocker):
    from console.services.app_actions.properties_changes import PropertiesChanges

    with open("{}/tests/console/svcupgrade/app_template.json".format(settings.BASE_DIR)) as json_file:
        app_template = json.load(json_file)
        rain_app = RainbondCenterApp()
        rain_app.app_template = json.dumps(app_template)
    mocker.patch("console.repositories.market_app_repo.rainbond_app_repo.get_enterpirse_app_by_key_and_version",
                 return_value=rain_app)

    service_source = ServiceSourceInfo()
    service_source.group_key = "dummy group_key"
    mocker.patch("console.repositories.app.service_source_repo.get_service_source",
                 return_value=service_source)

    with pytest.raises(RbdAppNotFound):
        properties_changes = PropertiesChanges(TenantServiceInfo(), Tenants())
        properties_changes.get_property_changes("eid", "version")
Esempio n. 6
0
    def __save_import_info(self, tenant, scope, metadata):
        rainbond_apps = []
        metadata = json.loads(metadata)
        key_and_version_list = []
        for app_template in metadata:
            app = rainbond_app_repo.get_rainbond_app_by_key_and_version_eid(
                tenant.enterprise_id, app_template["group_key"],
                app_template["group_version"])
            if app:
                # 覆盖原有应用数据
                app.share_team = tenant.tenant_name  # 分享团队名暂时为那个团队将应用导入进来的
                app.scope = scope
                app.describe = app_template.pop("describe", "")
                app.app_template = json.dumps(app_template)
                app.template_version = app_template.get("template_version", "")
                app.is_complete = True
                app.save()
                continue
            image_base64_string = app_template.pop("image_base64_string", "")
            pic_url = ""
            if image_base64_string:
                pic_url = self.decode_image(image_base64_string,
                                            app_template.pop("suffix", "jpg"))

            key_and_version = "{0}:{1}".format(app_template["group_key"],
                                               app_template['group_version'])
            if key_and_version in key_and_version_list:
                continue
            key_and_version_list.append(key_and_version)
            rainbond_app = RainbondCenterApp(
                enterprise_id=tenant.enterprise_id,
                group_key=app_template["group_key"],
                group_name=app_template["group_name"],
                version=app_template['group_version'],
                share_user=0,
                record_id=0,
                share_team=tenant.tenant_name,
                source="import",
                scope=scope,
                describe=app_template.pop("describe", ""),
                pic=pic_url,
                app_template=json.dumps(app_template),
                is_complete=True,
                template_version=app_template.get("template_version", ""))
            rainbond_apps.append(rainbond_app)
        rainbond_app_repo.bulk_create_rainbond_apps(rainbond_apps)
 def down_market_group_list(self, tenant):
     app_group_list = market_api.get_service_group_list(tenant.tenant_id)
     rainbond_apps = []
     for app_group in app_group_list:
         if not self.is_group_exist(app_group["group_key"],
                                    app_group["group_version"]):
             rainbond_app = RainbondCenterApp(
                 group_key=app_group["group_key"],
                 group_name=app_group["group_name"],
                 version=app_group['group_version'],
                 share_user=0,
                 record_id=0,
                 share_team="",
                 source="market",
                 scope="goodrain",
                 describe="",
                 app_template="")
             rainbond_apps.append(rainbond_app)
     rainbond_app_repo.bulk_create_rainbond_apps(rainbond_apps)
Esempio n. 8
0
 def add_basic_app_info(self, **kwargs):
     app = RainbondCenterApp(**kwargs)
     app.save()
     return app
    def create_share_info(self, share_record, share_team, share_user,
                          share_info, use_force):
        # 开启事务
        sid = transaction.savepoint()
        try:
            group_info = share_info["share_group_info"]
            # 删除历史数据
            ServiceShareRecordEvent.objects.filter(
                record_id=share_record.ID).delete()
            RainbondCenterApp.objects.filter(
                Q(record_id=share_record.ID)
                | Q(group_key=group_info["group_key"],
                    version=group_info["version"],
                    enterprise_id=share_team.enterprise_id)).delete()
            app_templete = {}
            # 处理基本信息
            try:
                app_templete["template_version"] = "v2"
                app_templete["group_key"] = group_info["group_key"]
                app_templete["group_name"] = group_info["group_name"]
                app_templete["group_version"] = group_info["version"]
            except Exception as e:
                if sid:
                    transaction.savepoint_rollback(sid)
                logger.exception(e)
                return 500, "基本信息处理错误", None
            try:
                # 确定分享的插件ID
                plugins = share_info.get("share_plugin_list", None)
                shared_plugin_info = None
                if plugins:

                    for plugin_info in plugins:
                        # one account for one plugin
                        share_image_info = app_store.get_image_connection_info(
                            group_info["scope"], share_team.tenant_name)
                        plugin_info["plugin_image"] = share_image_info
                        event = PluginShareRecordEvent(
                            record_id=share_record.ID,
                            team_name=share_team.tenant_name,
                            team_id=share_team.tenant_id,
                            plugin_id=plugin_info['plugin_id'],
                            plugin_name=plugin_info['plugin_alias'],
                            event_status='not_start')
                        event.save()

                    shared_plugin_info = self.get_plugins_group_items(plugins)
                    app_templete["plugins"] = shared_plugin_info
            except ServiceHandleException as e:
                raise e
            except Exception as e:
                if sid:
                    transaction.savepoint_rollback(sid)
                logger.exception(e)
                return 500, "插件处理发生错误", None
            # 处理组件相关
            try:
                services = share_info["share_service_list"]
                if services:
                    new_services = list()
                    service_ids = [s["service_id"] for s in services]
                    version_list = base_service.get_apps_deploy_versions(
                        services[0]["service_region"], share_team.tenant_name,
                        service_ids)
                    delivered_type_map = {
                        v["service_id"]: v["delivered_type"]
                        for v in version_list
                    }

                    dep_service_keys = {
                        service['service_share_uuid']
                        for service in services
                    }

                    for service in services:
                        # slug组件
                        # if image.startswith("goodrain.me/runner") and service["language"] != "dockerfile":
                        if delivered_type_map[service['service_id']] == "slug":
                            service[
                                'service_slug'] = app_store.get_slug_connection_info(
                                    group_info["scope"],
                                    share_team.tenant_name)
                            service["share_type"] = "slug"
                            if not service['service_slug']:
                                if sid:
                                    transaction.savepoint_rollback(sid)
                                return 400, "获取源码包上传地址错误", None
                        else:
                            service[
                                "service_image"] = app_store.get_image_connection_info(
                                    group_info["scope"],
                                    share_team.tenant_name)
                            service["share_type"] = "image"
                            if not service["service_image"]:
                                if sid:
                                    transaction.savepoint_rollback(sid)
                                return 400, "获取镜像上传地址错误", None

                        # 处理依赖关系
                        self._handle_dependencies(service, dep_service_keys,
                                                  use_force)

                        service[
                            "service_related_plugin_config"] = self.wrapper_service_plugin_config(
                                service["service_related_plugin_config"],
                                shared_plugin_info)

                        if service.get("need_share", None):
                            ssre = ServiceShareRecordEvent(
                                team_id=share_team.tenant_id,
                                service_key=service["service_key"],
                                service_id=service["service_id"],
                                service_name=service["service_cname"],
                                service_alias=service["service_alias"],
                                record_id=share_record.ID,
                                team_name=share_team.tenant_name,
                                event_status="not_start")
                            ssre.save()
                        new_services.append(service)
                    app_templete["apps"] = new_services
                else:
                    if sid:
                        transaction.savepoint_rollback(sid)
                    return 400, "分享的组件信息不能为空", None
            except ServiceHandleException as e:
                raise e
            except Exception as e:
                if sid:
                    transaction.savepoint_rollback(sid)
                logger.exception(e)
                return 500, "组件信息处理发生错误", None
            # 删除同个应用分享的相同版本
            RainbondCenterApp.objects.filter(
                version=group_info["version"],
                tenant_service_group_id=share_record.group_id).delete()
            # 新增加
            app = RainbondCenterApp(
                group_key=app_templete["group_key"],
                group_name=app_templete["group_name"],
                share_user=share_user.user_id,
                share_team=share_team.tenant_name,
                tenant_service_group_id=share_record.group_id,
                pic=group_info.get("pic", ""),
                source="local",
                record_id=share_record.ID,
                version=group_info["version"],
                enterprise_id=share_team.enterprise_id,
                scope=group_info["scope"],
                describe=group_info["describe"],
                details=group_info.get("details", ""),
                app_template=json.dumps(app_templete))
            app.save()
            share_record.step = 2
            share_record.update_time = datetime.datetime.now()
            share_record.save()
            # 提交事务
            if sid:
                transaction.savepoint_commit(sid)
            return 200, "分享信息处理成功", share_record.to_dict()
        except ServiceHandleException as e:
            raise e
        except Exception as e:
            logger.exception(e)
            if sid:
                transaction.savepoint_rollback(sid)
            return 500, "应用分享处理发生错误", None
Esempio n. 10
0
 def create_app(self, data):
     app_model = RainbondCenterApp(**data)
     app_model.save()
     return app_model
Esempio n. 11
0
 def init(self):
     self.sources = [
         Tenants(),
         TenantRegionInfo(),
         TenantRegionResource(),
         ServiceInfo(),
         TenantServiceInfo(),
         TenantServiceInfoDelete(),
         TenantServiceLog(),
         TenantServiceRelation(),
         TenantServiceEnv(),
         TenantServiceAuth(),
         TenantServiceExtendMethod(),
         ServiceDomain(),
         ServiceDomainCertificate(),
         PermRelService(),
         PermRelTenant(),
         PhoneCode(),
         TenantServiceL7Info(),
         TenantServiceEnvVar(),
         TenantServicesPort(),
         TenantServiceMountRelation(),
         TenantServiceVolume(),
         TenantServiceConfigurationFile(),
         ServiceGroup(),
         ServiceGroupRelation(),
         ImageServiceRelation(),
         ComposeServiceRelation(),
         ServiceRule(),
         ServiceRuleHistory(),
         ServiceCreateStep(),
         ServiceProbe(),
         ConsoleConfig(),
         TenantEnterprise(),
         TenantEnterpriseToken(),
         TenantServiceGroup(),
         ServiceTcpDomain(),
         ThirdPartyServiceEndpoints(),
         ServiceWebhooks(),
         GatewayCustomConfiguration(),
         ConsoleSysConfig(),
         RainbondCenterApp(),
         RainbondCenterAppInherit(),
         RainbondCenterPlugin(),
         ServiceShareRecord(),
         EnterpriseUserPerm(),
         TenantUserRole(),
         TenantUserPermission(),
         TenantUserRolePermission(),
         PermGroup(),
         ServiceRelPerms(),
         AppExportRecord(),
         UserMessage(),
         AppImportRecord(),
         GroupAppBackupRecord(),
         GroupAppMigrateRecord(),
         GroupAppBackupImportRecord(),
         Applicants(),
         DeployRelation(),
         ServiceBuildSource(),
         TenantServiceBackup(),
         AppUpgradeRecord(),
         ServiceUpgradeRecord(),
         RegionConfig(),
         CloundBangImages(),
         Announcement(),
     ]
Esempio n. 12
0
 def create_share_info(self, share_record, share_team, share_user,
                       share_info):
     # 开启事务
     sid = transaction.savepoint()
     try:
         # 删除历史数据
         ServiceShareRecordEvent.objects.filter(
             record_id=share_record.ID).delete()
         RainbondCenterApp.objects.filter(
             record_id=share_record.ID).delete()
         app_templete = {}
         # 处理基本信息
         try:
             app_templete["template_version"] = "v2"
             group_info = share_info["share_group_info"]
             app_templete["group_key"] = group_info["group_key"]
             app_templete["group_name"] = group_info["group_name"]
             app_templete["group_version"] = group_info["version"]
         except Exception as e:
             if sid:
                 transaction.savepoint_rollback(sid)
             logger.exception(e)
             return 500, "基本信息处理错误", None
         # 处理插件相关,v3.5暂时不实现
         try:
             # 确定分享的插件ID
             share_plugin_ids = {}
             plugins = share_info.get("share_plugin_list", None)
             if plugins:
                 share_plugins = list()
                 for plugin in plugins:
                     version = plugin.get("build_version", "")
                     plugin_id = plugin.get("plugin_id", "")
                     if version and plugin_id:
                         # 只有自己创建的插件需要分享出去
                         if plugin["origin"] == "source_code" and plugin[
                                 "is_share"]:
                             plugin_version = plugin_repo.get_plugin_buildversion(
                                 plugin_id, version)
                             if plugin_version and plugin_version.plugin_version_status != "fixed":
                                 plugin_version.plugin_version_status = "fixed"
                                 plugin_version.save()
                             plugin["config_groups"] = [
                                 group.to_dict() for group in
                                 plugin_repo.get_plugin_config_groups(
                                     plugin_id, version)
                             ]
                             plugin["config_items"] = [
                                 item.to_dict() for item in
                                 plugin_repo.get_plugin_config_items(
                                     plugin_id, version)
                             ]
                             plugin["version"] = plugin_version.to_dict()
                             # TODO: 插件应该单独分享,此处暂时不处理,分享到应用内部
                             share_plugin_ids[plugin_id] = True
                             share_plugins.append(plugin)
                 app_templete["plugins"] = share_plugins
         except Exception as e:
             if sid:
                 transaction.savepoint_rollback(sid)
             logger.exception(e)
             return 500, "插件处理发生错误", None
         # 处理应用相关
         try:
             services = share_info["share_service_list"]
             if services:
                 new_services = list()
                 for service in services:
                     image = service["image"]
                     # 源码应用
                     if image.startswith("goodrain.me/runner") and service[
                             "language"] != "dockerfile":
                         service[
                             'service_slug'] = app_store.get_slug_connection_info(
                                 group_info["scope"],
                                 share_team.tenant_name)
                         if not service['service_slug']:
                             if sid:
                                 transaction.savepoint_rollback(sid)
                             return 400, "获取源码包上传地址错误", None
                     else:
                         service[
                             "service_image"] = app_store.get_image_connection_info(
                                 group_info["scope"],
                                 share_team.tenant_name)
                         if not service["service_image"]:
                             if sid:
                                 transaction.savepoint_rollback(sid)
                             return 400, "获取镜像上传地址错误", None
                     if service.get("need_share", None):
                         ssre = ServiceShareRecordEvent(
                             team_id=share_team.tenant_id,
                             service_key=service["service_key"],
                             service_id=service["service_id"],
                             service_name=service["service_cname"],
                             service_alias=service["service_alias"],
                             record_id=share_record.ID,
                             team_name=share_team.tenant_name,
                             event_status="not_start")
                         ssre.save()
                     new_services.append(service)
                     # TODO:处理应用继承关系RainbondCenterAppInherit
                 app_templete["apps"] = new_services
             else:
                 if sid:
                     transaction.savepoint_rollback(sid)
                 return 400, "分享的应用信息不能为空", None
         except Exception as e:
             if sid:
                 transaction.savepoint_rollback(sid)
             logger.exception(e)
             return 500, "应用信息处理发生错误", None
         # 删除同个应用组分享的相同版本
         RainbondCenterApp.objects.filter(
             version=group_info["version"],
             tenant_service_group_id=share_record.group_id).delete()
         # 新增加
         app = RainbondCenterApp(
             group_key=app_templete["group_key"],
             group_name=app_templete["group_name"],
             share_user=share_user.user_id,
             share_team=share_team.tenant_name,
             tenant_service_group_id=share_record.group_id,
             pic=group_info.get("pic", ""),
             source="local",
             record_id=share_record.ID,
             version=group_info["version"],
             enterprise_id=share_team.enterprise_id,
             scope=group_info["scope"],
             describe=group_info["describe"],
             app_template=json.dumps(app_templete))
         app.save()
         share_record.step = 2
         share_record.update_time = datetime.datetime.now()
         share_record.save()
         # 提交事务
         if sid:
             transaction.savepoint_commit(sid)
         return 200, "分享信息处理成功", share_record.to_dict()
     except Exception as e:
         logger.exception(e)
         if sid:
             transaction.savepoint_rollback(sid)
         return 500, "应用分享处理发生错误", None
Esempio n. 13
0
    def save_market_app_template(self, user, tenant, app_templates):
        template_version = app_templates["template_version"]
        is_v1 = bool(template_version == "v1")
        if is_v1:
            v2_template = template_transform_service.v1_to_v2(app_templates)
        else:
            v2_template = app_templates
        rainbond_app = rainbond_app_repo.get_enterpirse_app_by_key_and_version(
            tenant.enterprise_id, v2_template["group_key"],
            v2_template["group_version"])

        if not rainbond_app:
            if common_services.is_public() and user.is_sys_admin:
                enterprise_id = "public"
            else:
                enterprise_id = tenant.enterprise_id
            rainbond_app = RainbondCenterApp(
                group_key=app_templates["group_key"],
                group_name=app_templates["group_name"],
                version=app_templates['group_version'],
                share_user=0,
                record_id=0,
                share_team="",
                source="market",
                scope="goodrain",
                describe=app_templates["info"],
                pic=app_templates["pic"],
                app_template="",
                enterprise_id=enterprise_id,
                template_version=app_templates.get("template_version", ""),
                is_official=app_templates["is_official"],
                details=app_templates["desc"],
                upgrade_time=app_templates["update_version"])
        if is_v1:
            rainbond_app.share_user = v2_template["share_user"]
            rainbond_app.share_team = v2_template["share_team"]
            rainbond_app.pic = v2_template["pic"]
            rainbond_app.describe = v2_template["describe"]
            rainbond_app.app_template = json.dumps(v2_template)
            rainbond_app.is_complete = True
            rainbond_app.update_time = current_time_str("%Y-%m-%d %H:%M:%S")
            rainbond_app.is_official = v2_template["is_official"]
            rainbond_app.details = v2_template["desc"]
            rainbond_app.upgrade_time = v2_template.get("update_version", "0")
            rainbond_app.save()
        else:
            user_name = v2_template.get("publish_user", None)
            user_id = 0
            if user_name:
                try:
                    user = user_repo.get_user_by_username(user_name)
                    user_id = user.user_id
                except Exception as e:
                    logger.exception(e)
            rainbond_app.share_user = user_id
            rainbond_app.share_team = v2_template.get("publish_team", "")
            rainbond_app.pic = v2_template.get("pic", rainbond_app.pic)
            rainbond_app.describe = v2_template.get("update_note",
                                                    rainbond_app.describe)
            rainbond_app.app_template = v2_template["template_content"]
            rainbond_app.is_complete = True
            rainbond_app.update_time = current_time_str("%Y-%m-%d %H:%M:%S")
            rainbond_app.is_official = v2_template.get("is_official", 0)
            rainbond_app.details = v2_template.get("desc", "")
            rainbond_app.upgrade_time = v2_template.get("update_version", "")
            rainbond_app.save()