def yield_app_info(): for group_key in set(service_group_keys): app_qs = rainbond_app_repo.get_rainbond_app_qs_by_key( group_key=group_key) app = app_qs.first() if not app: continue upgrade_versions = upgrade_service.get_app_upgrade_versions( self.tenant, group_id, group_key) not_upgrade_record = upgrade_service.get_app_not_upgrade_record( self.tenant.tenant_id, group_id, group_key) services = group_service.get_rainbond_services( int(group_id), group_key) yield { 'current_version': upgrade_service.get_old_version( group_key, services.values_list('service_id', flat=True)), 'can_upgrade': bool(upgrade_versions), 'upgrade_versions': list(upgrade_versions), 'not_upgrade_record_id': not_upgrade_record.ID, 'not_upgrade_record_status': not_upgrade_record.status, 'group_key': app.group_key, 'group_name': app.group_name, 'share_user': app.share_user, 'share_team': app.share_team, 'tenant_service_group_id': app.tenant_service_group_id, 'pic': app.pic, 'source': app.source, 'describe': app.describe, 'enterprise_id': app.enterprise_id, 'is_official': app.is_official, 'details': app.details, 'min_memory': group_service.get_service_group_memory(app.app_template), }
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))