Skip to content

Commit

Permalink
BC-8621 call sync legacy indexes via rest
Browse files Browse the repository at this point in the history
  • Loading branch information
Loki-Afro committed Jan 2, 2025
1 parent cb1f7a6 commit 15dda2c
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 9 deletions.
21 changes: 21 additions & 0 deletions ansible/roles/schulcloud-server-init/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@
tags:
- configmap

- name: Management Api Configmap File
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: management-configmap.yml.j2
when: WITH_SCHULCLOUD_INIT
tags:
- configmap

- name: Remove Management Api Configmap File
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
state: absent
api_version: v1
kind: ConfigMap
name: api-management-configmap
when: not WITH_SCHULCLOUD_INIT
tags:
- configmap

- name: Management Deployment
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: api-management-configmap
namespace: {{ NAMESPACE }}
labels:
app: management-deployment
data:
NEST_LOG_LEVEL: "{{ NEST_LOG_LEVEL }}"
EXIT_ON_ERROR: "true"
ENABLE_SYNC_LEGACY_INDEXES_VIA_FEATHERS_SERVICE: "true"
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ spec:
envFrom:
- configMapRef:
name: api-configmap
- configMapRef:
name: api-management-configmap
- secretRef:
name: api-secret
- secretRef:
Expand Down
21 changes: 16 additions & 5 deletions apps/server/src/apps/management.app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import { NestFactory } from '@nestjs/core';
import { ExpressAdapter } from '@nestjs/platform-express';
import express from 'express';

// register source-map-support for debugging
import { install as sourceMapInstall } from 'source-map-support';

// application imports
import { LegacyLogger } from '@src/core/logger';
import { ManagementServerModule } from '@modules/management';
import { MikroORM } from '@mikro-orm/core';
import legacyAppPromise = require('../../../../src/app');
import { createRequestLoggerMiddleware } from './helpers/request-logger-middleware';
import { enableOpenApiDocs } from './helpers';

Expand All @@ -21,12 +20,24 @@ async function bootstrap() {

const nestExpressAdapter = new ExpressAdapter(nestExpress);
const nestApp = await NestFactory.create(ManagementServerModule, nestExpressAdapter);
const orm = nestApp.get(MikroORM);

nestApp.use(createRequestLoggerMiddleware());

// WinstonLogger
nestApp.useLogger(await nestApp.resolve(LegacyLogger));

// load the legacy feathers/express server
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const feathersExpress = await legacyAppPromise(orm);
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
await feathersExpress.setup();

// set reference to legacy app as an express setting so we can
// access it over the current request within FeathersServiceProvider
// TODO remove if not needed anymore, needed for legacy indexes
nestExpress.set('feathersApp', feathersExpress);

// customize nest app settings
nestApp.enableCors();
enableOpenApiDocs(nestApp, 'docs');
Expand All @@ -45,8 +56,8 @@ async function bootstrap() {

console.log('#################################');
console.log(`### Start Management Server ###`);
console.log(`### Port: ${port} ###`);
console.log(`### Base path: ${basePath} ###`);
console.log(`### Port: ${port} ###`);
console.log(`### Base path: ${basePath} ###`);
console.log('#################################');
}
void bootstrap();
4 changes: 2 additions & 2 deletions apps/server/src/infra/feathers/feathers-service.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ export interface FeathersService {
*
* @param id
* @param params
* @deprecated Access legacy eathers service get method
* @deprecated Access legacy feathers service get method
*/
get(id: string, params?: FeathersServiceParams): Promise<FeathersServiceResponse>;
/**
*
* @param params
* @deprecated Access legacy eathers service find method
* @deprecated Access legacy feathers service find method
*/
find(params?: FeathersServiceParams): Promise<FeathersServiceResponse>;
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Controller, Param, Post, All, Query } from '@nestjs/common';
import { FeathersServiceProvider } from '@infra/feathers';
import { DatabaseManagementUc } from '../uc/database-management.uc';

@Controller('management/database')
export class DatabaseManagementController {
constructor(private databaseManagementUc: DatabaseManagementUc) {}
constructor(
private databaseManagementUc: DatabaseManagementUc,
private feathersServiceProvider: FeathersServiceProvider
) {}

@All('seed')
async importCollections(@Query('with-indexes') withIndexes: boolean): Promise<string[]> {
Expand All @@ -30,7 +34,9 @@ export class DatabaseManagementController {
}

@Post('sync-indexes')
syncIndexes() {
async syncIndexes() {
// it is absolutely crucial to call the legacy stuff first, otherwise it will drop newly created indexes!
await this.feathersServiceProvider.getService('sync-legacy-indexes').create();
return this.databaseManagementUc.syncIndexes();
}
}
2 changes: 2 additions & 0 deletions apps/server/src/modules/management/management.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { createConfigModuleOptions } from '@src/config';
import { LoggerModule } from '@src/core/logger';
import { FeathersModule } from '@infra/feathers';
import { DatabaseManagementConsole } from './console/database-management.console';
import { DatabaseManagementController } from './controller/database-management.controller';
import { BsonConverter } from './converter/bson.converter';
Expand All @@ -20,6 +21,7 @@ const baseImports = [
LoggerModule,
ConfigModule.forRoot(createConfigModuleOptions(serverConfig)),
EncryptionModule,
FeathersModule,
];

const imports = (Configuration.get('FEATURE_IDENTITY_MANAGEMENT_ENABLED') as boolean)
Expand Down
5 changes: 5 additions & 0 deletions config/default.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1675,6 +1675,11 @@
"type": "boolean",
"default": "false",
"description": "Enables the external system logout feature"
},
"ENABLE_SYNC_LEGACY_INDEXES_VIA_FEATHERS_SERVICE": {
"type": "boolean",
"default": "false",
"description": "if calling sync legacy indexes is allowed, only management module should have access to this, this should not exist in the long term when all entites and their indexes have been migrated"
}
},
"required": []
Expand Down
2 changes: 2 additions & 0 deletions src/services/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const etherpad = require('./etherpad');
const storageProvider = require('./storageProvider');
const activation = require('./activation');
const config = require('./config');
const syncLegacyIndexes = require('./sync-legacy-indexes');
const docs = require('./docs');

module.exports = function initializeServices() {
Expand Down Expand Up @@ -99,6 +100,7 @@ module.exports = function initializeServices() {
app.configure(etherpad);
app.configure(storageProvider);
app.configure(activation);
app.configure(syncLegacyIndexes);
app.configure(config);

// initialize events
Expand Down
43 changes: 43 additions & 0 deletions src/services/sync-legacy-indexes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const mongoose = require('mongoose');
const { Configuration } = require('@hpi-schul-cloud/commons');
const logger = require('../../logger');

const getModels = () => Object.entries(mongoose.models);

class Service {
constructor(options) {
this.options = options || {};
this.docs = {};
}

async create(data, params) {
if (Configuration.get('ENABLE_SYNC_LEGACY_INDEXES_VIA_FEATHERS_SERVICE')) {
const models = getModels();
for (const [modelName, model] of models) {
logger.alert(`${modelName}.syncIndexes()`);
try {
// eslint-disable-next-line no-await-in-loop
await model.syncIndexes();
} catch (err) {
logger.alert(err);
}
}
} else {
logger.alert(
'sync-legacy-indexes has been called without enabling ENABLE_SYNC_LEGACY_INDEXES_VIA_FEATHERS_SERVICE'
);
}
}

setup(app) {
this.app = app;
}
}

module.exports = function () {
const app = this;

app.use('/sync-legacy-indexes', new Service());
};

module.exports.Service = Service;

0 comments on commit 15dda2c

Please sign in to comment.