Exemplo n.º 1
0
    def get_app_upgrade_versions(self, tenant, group_id, group_key):
        """获取云市组件可升级版本列表"""
        from console.services.group_service import group_service
        # 查询某一个云市组件下的所有组件
        services = group_service.get_rainbond_services(group_id, group_key)
        versions = set()

        # 查询可升级的组件
        for service in services:
            pc = PropertiesChanges(service, tenant)
            service_version = pc.get_upgradeable_versions
            versions |= set(service_version or [])

        # 查询新增组件的版本
        service_keys = services.values_list('service_key', flat=True)
        service_keys = set(service_keys) if service_keys else set()
        app_qs = rainbond_app_repo.get_rainbond_app_versions_by_id(
            tenant.enterprise_id, app_id=group_key)
        add_versions = self.query_the_version_of_the_add_service(
            app_qs, service_keys)
        versions |= add_versions
        versions = [str(version) for version in versions]
        versions = sorted(
            versions,
            key=lambda x: map(lambda y: int(filter(str.isdigit, y)),
                              x.split(".")),
            reverse=True)
        return versions
Exemplo n.º 2
0
 def set_changes(self):
     # list properties changes
     pc = PropertiesChanges(self.service)
     changes = pc.get_property_changes(self.tenant.enterprise_id,
                                       self.version)
     logger.debug("service id: {}; dest version: {}; changes: {}".format(
         self.service.service_id, self.version, changes))
     self.changes = changes
Exemplo n.º 3
0
def test_record_not_found(mocker):
    with pytest.raises(RecordNotFound):
        from console.services.app_actions.properties_changes import PropertiesChanges
        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)
        properties_changes = PropertiesChanges(TenantServiceInfo())
        properties_changes.get_property_changes("eid", "version")
Exemplo n.º 4
0
 def set_changes(self):
     pc = PropertiesChanges(self.service, self.tenant,
                            self.install_from_cloud)
     app = get_upgrade_app_version_template_app(self.tenant, self.version,
                                                pc)
     changes = pc.get_property_changes(app)
     logger.debug("service id: {}; dest version: {}; changes: {}".format(
         self.service.service_id, self.version, changes))
     self.changes = changes
     logger.info("upgrade from cloud do not support.")
Exemplo n.º 5
0
 def get_service_changes(service, tenant, version):
     """获取组件更新信息"""
     from console.services.app_actions.properties_changes import PropertiesChanges
     try:
         pc = PropertiesChanges(service, tenant)
         app = get_upgrade_app_version_template_app(tenant, version, pc)
         return pc.get_property_changes(app, level="app")
     except (RecordNotFound, ErrServiceSourceNotFound) as e:
         AbortRequest(msg=str(e))
     except RbdAppNotFound as e:
         AbortRequest(msg=str(e))
    def get_service_changes(service, tenant, version):
        """获取服务更新信息"""
        from console.services.app_actions.properties_changes import PropertiesChanges

        try:
            pc = PropertiesChanges(service)
            return pc.get_property_changes(tenant.enterprise_id, version)
        except RecordNotFound as e:
            AbortRequest(msg=str(e))
        except RbdAppNotFound as e:
            AbortRequest(msg=str(e))
Exemplo n.º 7
0
def test_envs_changes():
    from console.services.app_actions.properties_changes import PropertiesChanges
    from console.repositories.app_config import env_var_repo

    tenant_id = "c1a29fe4d7b0413993dc859430cf743d"
    service_id = "03289ae373e65e4a1a22046d7f76ca5e"

    tenantServiceEnvVar = {}
    tenantServiceEnvVar["tenant_id"] = tenant_id
    tenantServiceEnvVar["service_id"] = service_id
    tenantServiceEnvVar['container_port'] = 0
    tenantServiceEnvVar["name"] = "NGINX_VERSION"
    tenantServiceEnvVar["attr_name"] = "NGINX_VERSION"
    tenantServiceEnvVar["attr_value"] = "1.15.12-1~stretch"
    tenantServiceEnvVar["is_change"] = False
    tenantServiceEnvVar["scope"] = "inner"
    env_var_repo.add_service_env(**tenantServiceEnvVar)

    envs = [
        {
            "is_change": True,
            "name": "\\u65e5\\u5fd7\\u8f93\\u51fa\\u65b9\\u5f0f",
            "attr_value": "file",
            "attr_name": "DESTINATION"
        },
        {
            "is_change": True,
            "name": "\\u8be6\\u7ec6\\u9519\\u8bef\\u65e5\\u5fd7",
            "attr_value": "true",
            "attr_name": "TRACEALLEXCEPTIONS"
        },
        {
            "is_change": True,
            "name": "NGINX_VERSION",
            "attr_value": "1.15.12-1~stretch",
            "attr_name": "NGINX_VERSION"
        },
    ]
    service = TenantServiceInfo()
    service.tenant_id = tenant_id
    service.service_id = service_id
    properties_changes = PropertiesChanges(service, Tenants())
    env_changes = properties_changes.env_changes(envs)
    print(env_changes)
    assert 2 == len(env_changes["add"])
    assert next(
        iter([
            x for x in env_changes["add"] if x["attr_name"] == "DESTINATION"
        ]), None)
    assert next(
        iter([
            x for x in env_changes["add"]
            if x["attr_name"] == "TRACEALLEXCEPTIONS"
        ]), None)
Exemplo n.º 8
0
 def set_changes(self):
     pc = PropertiesChanges(
         self.service,
         self.tenant,
         all_component_one_model=self.all_component_one_model,
         install_from_cloud=self.install_from_cloud)
     template = get_upgrade_app_template(self.tenant, self.version, pc)
     self.template = template
     self.pc = pc
     changes = pc.get_property_changes(template=template)
     logger.debug("service id: {}; dest version: {}; changes: {}".format(self.service.service_id, self.version, changes))
     self.changes = changes
Exemplo n.º 9
0
 def get_service_changes(service, tenant, version, services):
     """获取组件更新信息"""
     from console.services.app_actions.properties_changes import \
         PropertiesChanges
     try:
         pc = PropertiesChanges(service, tenant, all_component_one_model=services)
         upgrade_template = get_upgrade_app_template(tenant, version, pc)
         model_component, changes = pc.get_property_changes(template=upgrade_template, level="app")
         return pc.current_version, model_component, changes
     except (RecordNotFound, ErrServiceSourceNotFound) as e:
         AbortRequest(msg=str(e))
     except RbdAppNotFound as e:
         AbortRequest(msg=str(e))
Exemplo n.º 10
0
 def set_changes(self):
     # list properties changes
     if not self.install_from_cloud:
         pc = PropertiesChanges(self.service, self.tenant,
                                self.install_from_cloud)
         changes = pc.get_property_changes(self.tenant.enterprise_id,
                                           self.version)
         logger.debug(
             "service id: {}; dest version: {}; changes: {}".format(
                 self.service.service_id, self.version, changes))
         self.changes = changes
     else:
         # TODO:impl upgrade from cloud
         logger.info("upgrade from cloud do not support.")
Exemplo n.º 11
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")
Exemplo n.º 12
0
    def openapi_upgrade_app_models(self, user, team, region_name,
                                   oauth_instance, app_id, data):
        from console.services.market_app_service import market_app_service
        update_versions = data["update_versions"]
        for update_version in update_versions:
            app_model_id = update_version["app_model_id"]
            app_model_version = update_version["app_model_version"]
            market_name = update_version["market_name"]
            # TODO: get upgrade component will set upgrade_group_id.
            # Otherwise, there is a problem with multiple installs and upgrades of an application.
            services = group_service.get_rainbond_services(
                int(app_id), app_model_id)
            if not services:
                continue
            exist_component = services.first()
            pc = PropertiesChanges(exist_component,
                                   team,
                                   all_component_one_model=services)
            recode_kwargs = {
                "tenant_id": team.tenant_id,
                "group_id": int(app_id),
                "group_key": app_model_id,
                "is_from_cloud": bool(market_name),
                "market_name": market_name,
            }

            # 获取升级信息
            upgrade_info, add_info = self.get_upgrade_info(
                team, services, app_model_id, app_model_version, market_name)

            # 生成升级记录
            app_record = self.get_or_create_upgrade_record(**recode_kwargs)
            self.synchronous_upgrade_status(team, region_name, app_record)
            app_record = AppUpgradeRecord.objects.get(ID=app_record.ID)

            # 处理新增的组件
            install_info = {}
            if add_info:
                old_app = app_market_service.get_market_app_model_version(
                    pc.market,
                    app_model_id,
                    app_model_version,
                    get_template=True)
                new_app = deepcopy(old_app)
                # mock app信息
                template = json.loads(new_app.template)
                template['apps'] = list(add_info.values())
                new_app.template = json.dumps(template)

                # 查询某一个云市应用下的所有组件
                try:
                    install_info = market_app_service.install_service_when_upgrade_app(
                        team, region_name, user, app_id, new_app, old_app,
                        services, True,
                        exist_component.tenant_service_group_id,
                        pc.install_from_cloud, pc.market_name)
                except ResourceNotEnoughException as re:
                    raise re
                except AccountOverdueException as re:
                    logger.exception(re)
                    raise ServiceHandleException(msg="resource is not enough",
                                                 msg_show=re.message,
                                                 status_code=412,
                                                 error_code=10406)
                upgrade_service.create_add_service_record(
                    app_record, install_info['events'], add_info)

            app_record.version = app_model_version
            app_record.old_version = pc.current_version
            app_record.save()
            # 处理升级组件
            upgrade_services = service_repo.get_services_by_service_ids_and_group_key(
                app_model_id, list(upgrade_info.keys()))
            market_services = [
                self.market_service_and_create_backup(team, service,
                                                      app_record.version,
                                                      upgrade_services)
                for service in upgrade_services
            ]
            # 处理依赖关系
            if add_info:
                market_app_service.save_service_deps_when_upgrade_app(
                    team,
                    install_info['service_key_dep_key_map'],
                    install_info['key_service_map'],
                    install_info['apps'],
                    install_info['app_map'],
                )

            upgrade_service.upgrade_database(market_services)
            upgrade_service.send_upgrade_request(market_services, team, user,
                                                 app_record, upgrade_info,
                                                 oauth_instance)
            upgrade_repo.change_app_record_status(
                app_record, UpgradeStatus.UPGRADING.value)
Exemplo n.º 13
0
    def post(self, request, group_id, *args, **kwargs):
        """提交升级任务"""
        rq_args = (
            {
                'key': 'upgrade_record_id',
                'required': True,
                'error': 'upgrade_record_id is a required parameter'
            },
            {
                'key': 'group_key',
                'required': True,
                'error': 'group_key is a required parameter'
            },
            {
                'key': 'version',
                'required': True,
                'error': 'version is a required parameter'
            },
            {
                'key': 'services',
                'required': True,
                'error': 'services is a required parameter'
            },
        )
        data = parse_date(request, rq_args)
        group_key = data['group_key']
        version = data['version']
        app_record = get_object_or_404(
            AppUpgradeRecord,
            msg="Upgrade record not found",
            tenant_id=self.tenant.tenant_id,
            group_id=int(group_id),
            group_key=group_key,
            status=UpgradeStatus.NOT.value,
            pk=data['upgrade_record_id'],
        )
        old_service = group_service.get_rainbond_services(group_id, group_key).first()
        pc = PropertiesChanges(old_service, self.tenant)

        # 处理新增的组件
        add_service_infos = {
            service['service']['service_key']: service['upgrade_info']
            for service in data['services'] if service['service']['type'] == UpgradeType.ADD.value and service['upgrade_info']
        }
        install_info = {}
        if add_service_infos:
            if pc.install_from_cloud:
                old_app_model, old_app = app_market_service.cloud_app_model_to_db_model(pc.market, group_key, version)
            else:
                old_app_model, old_app = rainbond_app_repo.get_rainbond_app_and_version(self.tenant.enterprise_id, group_key,
                                                                                        version)
            old_app.template = old_app.app_template
            old_app.app_name = old_app_model.app_name
            new_app = deepcopy(old_app)
            # mock app信息
            template = json.loads(new_app.template)
            template['apps'] = add_service_infos.values()
            new_app.template = json.dumps(template)

            # 查询某一个云市应用下的所有组件
            services = group_service.get_rainbond_services(int(group_id), group_key)
            try:
                install_info = market_app_service.install_service_when_upgrade_app(self.tenant, self.response_region, self.user,
                                                                                   group_id, new_app, old_app, services, True,
                                                                                   pc.install_from_cloud, pc.market_name)

            except ResourceNotEnoughException as re:
                raise re
            except AccountOverdueException as re:
                logger.exception(re)
                return MessageResponse(msg="resource is not enough", msg_show=re.message, status_code=412, error_code=10406)
            upgrade_service.create_add_service_record(app_record, install_info['events'], add_service_infos)

        # 处理需要升级的组件
        upgrade_service_infos = {
            service['service']['service_id']: service['upgrade_info']
            for service in data['services']
            if service['service']['type'] == UpgradeType.UPGRADE.value and service['upgrade_info']
        }

        app_record.version = version
        app_record.old_version = pc.current_version.version
        app_record.save()

        services = service_repo.get_services_by_service_ids_and_group_key(data['group_key'], upgrade_service_infos.keys())

        market_services = [
            upgrade_service.market_service_and_create_backup(self.tenant, service, app_record.version) for service in services
        ]

        # 处理依赖关系
        if add_service_infos:
            market_app_service.save_service_deps_when_upgrade_app(
                self.tenant,
                install_info['service_key_dep_key_map'],
                install_info['key_service_map'],
                install_info['apps'],
                install_info['app_map'],
            )

        upgrade_service.upgrade_database(market_services)
        upgrade_service.send_upgrade_request(market_services, self.tenant, self.user, app_record, upgrade_service_infos,
                                             self.oauth_instance)
        upgrade_repo.change_app_record_status(app_record, UpgradeStatus.UPGRADING.value)

        return MessageResponse(msg="success", bean=upgrade_service.serialized_upgrade_record(app_record))