feat: add a new system metadata service
Signed-off-by: aramissennyeydd <aramis.sennyey@doordash.com>
This commit is contained in:
+17
-7
@@ -17,23 +17,33 @@ import {
|
||||
coreServices,
|
||||
createBackendPlugin,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
import { instanceMetadataServiceRef } from '@backstage/backend-plugin-api/alpha';
|
||||
import Router from 'express-promise-router';
|
||||
|
||||
// Example usage of the instance metadata service to log the installed plugins.
|
||||
// Example usage of the instance metadata service to log the installed features.
|
||||
export default createBackendPlugin({
|
||||
pluginId: 'instance-metadata-logging',
|
||||
register(env) {
|
||||
env.registerInit({
|
||||
deps: {
|
||||
instanceMetadata: coreServices.rootInstanceMetadata,
|
||||
instanceMetadata: instanceMetadataServiceRef,
|
||||
logger: coreServices.logger,
|
||||
httpRouter: coreServices.rootHttpRouter,
|
||||
},
|
||||
async init({ instanceMetadata, logger }) {
|
||||
const plugins = await instanceMetadata.getInstalledPlugins();
|
||||
async init({ instanceMetadata, logger, httpRouter }) {
|
||||
logger.info(
|
||||
`Installed plugins on this instance: ${plugins
|
||||
.map(e => e.pluginId)
|
||||
.join(', ')}`,
|
||||
`Installed features on this instance: ${JSON.stringify(
|
||||
instanceMetadata.getInstalledFeatures(),
|
||||
)}`,
|
||||
);
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get('/features/installed', (_, res) => {
|
||||
res.json({ items: instanceMetadata.getInstalledFeatures() });
|
||||
});
|
||||
|
||||
httpRouter.use('/.backstage/instanceInfo', router);
|
||||
},
|
||||
});
|
||||
},
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright 2024 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import {
|
||||
coreServices,
|
||||
createBackendPlugin,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
import {
|
||||
BackendFeatureMeta,
|
||||
systemMetadataServiceRef,
|
||||
} from '@backstage/backend-plugin-api/alpha';
|
||||
import Router from 'express-promise-router';
|
||||
|
||||
// Example usage of the instance metadata service to log the installed features.
|
||||
export default createBackendPlugin({
|
||||
pluginId: 'system-metadata-logging',
|
||||
register(env) {
|
||||
env.registerInit({
|
||||
deps: {
|
||||
systemMetadata: systemMetadataServiceRef,
|
||||
logger: coreServices.logger,
|
||||
httpRouter: coreServices.rootHttpRouter,
|
||||
},
|
||||
async init({ systemMetadata, logger, httpRouter }) {
|
||||
logger.info(
|
||||
`Instances in this system: ${JSON.stringify(
|
||||
await systemMetadata.listInstances(),
|
||||
)}`,
|
||||
);
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get('/instances', async (_, res) => {
|
||||
res.json(await systemMetadata.listInstances());
|
||||
});
|
||||
|
||||
router.get('/features/installed', async (_, res) => {
|
||||
const instances = await systemMetadata.listInstances();
|
||||
const featurePromises = await Promise.allSettled(
|
||||
instances.map(async instance => {
|
||||
const response = await fetch(
|
||||
`${instance.url}/.backstage/instanceInfo/features/installed`,
|
||||
);
|
||||
if (response.ok) {
|
||||
return { instance, response: await response.json() };
|
||||
}
|
||||
throw new Error(
|
||||
`Failed to fetch installed features from ${instance.url}`,
|
||||
);
|
||||
}),
|
||||
);
|
||||
const pluginByInstance: Record<string, string[]> = {};
|
||||
for (const result of featurePromises) {
|
||||
if (result.status !== 'fulfilled') {
|
||||
logger.error(
|
||||
`Failed to fetch installed features: ${result.reason}`,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
const instance = result.value.instance;
|
||||
const installedFeatures = result.value.response
|
||||
.items as BackendFeatureMeta[];
|
||||
for (const feature of installedFeatures) {
|
||||
if (feature.type === 'plugin') {
|
||||
if (!pluginByInstance[feature.pluginId]) {
|
||||
pluginByInstance[feature.pluginId] = [];
|
||||
}
|
||||
pluginByInstance[feature.pluginId].push(
|
||||
`${instance.url}/api/${feature.pluginId}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
res.json(pluginByInstance);
|
||||
});
|
||||
|
||||
httpRouter.use('/.backstage/systemInfo', router);
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
coreServices,
|
||||
createBackendFeatureLoader,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
import { systemMetadataServiceFactory } from '@backstage/backend-defaults/alpha/systemMetadata';
|
||||
|
||||
const backend = createBackend();
|
||||
|
||||
@@ -69,7 +70,9 @@ backend.add(searchLoader);
|
||||
backend.add(import('@backstage/plugin-techdocs-backend'));
|
||||
backend.add(import('@backstage/plugin-signals-backend'));
|
||||
backend.add(import('@backstage/plugin-notifications-backend'));
|
||||
backend.add(import('./instanceMetadata'));
|
||||
backend.add(import('./experimental/instanceMetadata'));
|
||||
backend.add(import('./experimental/systemMetadata'));
|
||||
backend.add(systemMetadataServiceFactory);
|
||||
|
||||
backend.add(import('@backstage/plugin-events-backend-module-google-pubsub'));
|
||||
backend.add(import('@backstage/plugin-mcp-actions-backend'));
|
||||
|
||||
Reference in New Issue
Block a user