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) 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=data['group_key'], status=UpgradeStatus.NOT.value, pk=data['upgrade_record_id'], ) app_record.version = data['version'] app_record.save() # 处理新增的服务 add_service_infos = [ service['upgrade_info'] for service in data['services'] if service['service']['type'] == UpgradeType.ADD.value ] if add_service_infos: app = rainbond_app_repo.get_rainbond_app_by_key_version( group_key=data['group_key'], version=app_record.version) # mock app信息 template = json.loads(app.app_template) template['apps'] = add_service_infos app.app_template = json.dumps(template) market_app_service.check_package_app_resource( self.tenant, self.response_region, app) market_app_service.install_service(self.tenant, self.response_region, self.user, group_id, app, True) # 处理需要升级的服务 upgrade_service_infos = { service['service']['service_id']: service['upgrade_info'] for service in data['services'] if service['service']['type'] == UpgradeType.UPGRADE.value } 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 ] upgrade_service.upgrade_database(market_services) upgrade_service.send_upgrade_request(market_services, self.tenant, self.user, app_record, upgrade_service_infos) upgrade_repo.change_app_record_status(app_record, UpgradeStatus.UPGRADING.value) return MessageResponse( msg="success", bean=upgrade_service.serialized_upgrade_record(app_record))
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'], ) # 处理新增的服务 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: old_app = rainbond_app_repo.get_rainbond_app_by_key_version( group_key=group_key, version=version) new_app = deepcopy(old_app) # mock app信息 template = json.loads(new_app.app_template) template['apps'] = add_service_infos.values() new_app.app_template = json.dumps(template) # 查询某一个云市应用下的所有服务 services = group_service.get_rainbond_services( int(group_id), group_key) try: market_app_service.check_package_app_resource( self.tenant, self.response_region, new_app) 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) except (ResourceNotEnoughException, 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 = upgrade_service.get_old_version( group_key, upgrade_service_infos.keys()) 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 ] upgrade_service.upgrade_database(market_services) upgrade_service.send_upgrade_request(market_services, self.tenant, self.user, app_record, upgrade_service_infos) upgrade_repo.change_app_record_status(app_record, UpgradeStatus.UPGRADING.value) # 处理依赖关系 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'], ) return MessageResponse( msg="success", bean=upgrade_service.serialized_upgrade_record(app_record))
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_services = group_service.get_rainbond_services(group_id, group_key) pc = PropertiesChanges(old_services.first(), self.tenant, all_component_one_model=old_services) 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) if not old_app_model: raise ServiceHandleException(msg="app is not exist", msg_show="应用市场应用不存在,无法进行升级") if not old_app: raise ServiceHandleException(msg="app version is not exist", msg_show="应用市场应用版本不存在,无法进行升级") old_app.template = old_app.app_template old_app.app_name = old_app_model.app_name new_app = deepcopy(old_app) template = json.loads(new_app.template) # 处理新增的组件 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'] } # 安装插件 plugins = template.get("plugins", None) if plugins: market_app_service.create_plugin_for_tenant( self.response_region, self.user, self.tenant, plugins) else: logger.debug(plugins) install_info = {} if add_service_infos: # mock app信息 template['apps'] = list(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'] } # 升级应用配置组 market_app_service.save_app_config_groups_when_upgrade_app( self.region_name, self.tenant, group_id, upgrade_service_infos) app_record.version = version app_record.old_version = pc.current_version app_record.save() services = service_repo.get_services_by_service_ids_and_group_key( data['group_key'], list(upgrade_service_infos.keys())) market_services = [ upgrade_service.market_service_and_create_backup( self.tenant, service, app_record.version, all_component_one_model=services) 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))