fix: handle patch & duplicate versions correctly

This commit is contained in:
Rainer Killinger
2021-09-06 14:36:06 +02:00
parent aeebbf9656
commit 8dffe29146
3 changed files with 42 additions and 24 deletions

View File

@@ -17,6 +17,7 @@ import {Logger} from '@openstapps/logger';
import Dockerode from 'dockerode';
import {render} from 'mustache';
import {join} from 'path';
import * as semver from 'semver';
import {
asyncReadFile,
configFile,
@@ -86,26 +87,36 @@ export async function generateUpstreamMap(
.map(async (activeVersionRegex) => {
const upstreamName = activeVersionRegex.replace(/[\\|.+]/g, '_');
const activeBackends = containers.filter((container) => {
let activeBackends = containers.filter((container) => {
return containerMatchesRegex('backend', new RegExp(activeVersionRegex), container);
});
// .Labels['stapps.version'] is available
if (activeBackends.length > 0) {
foundMatchingContainer = true;
if (activeBackends.length > 1) {
throw new Error('Multiple backends for one version found.');
activeBackends = activeBackends.sort((a, b) => semver.rcompare(a.Labels['stapps.version'],b.Labels['stapps.version']));
const activeBackendsVersions = activeBackends.map((container) => container.Labels['stapps.version'])
// tslint:disable-next-line: strict-boolean-expressions
.reduce((map, e) => map.set(e, (map.get(e) || 0) + 1), new Map<string, number>());
for (const [version, occurrences] of activeBackendsVersions) {
if (occurrences > 1) {
await Logger.error(`Omitting running version ${version} ! Multiple backends with this exact version are running`);
activeBackends = activeBackends.filter((container) => container.Labels['stapps.version'] !== version);
}
}
const gateWayOfContainer = await getGatewayOfStAppsBackend(activeBackends[0]);
if (activeBackends.length !== 0) {
// not only dublicates
foundMatchingContainer = true;
if (gateWayOfContainer.length !== 0) {
upstreams += `\nupstream ${upstreamName} {\n server ${gateWayOfContainer};\n}`;
const gateWayOfContainer = await getGatewayOfStAppsBackend(activeBackends[0]);
return ` \"~${activeVersionRegex}\" ${upstreamName};\n`;
if (gateWayOfContainer.length !== 0) {
upstreams += `\nupstream ${upstreamName} {\n server ${gateWayOfContainer};\n}`;
return ` \"~${activeVersionRegex}\" ${upstreamName};\n`;
}
}
return ` \"~${activeVersionRegex}\" unavailable;\n`;
}
await Logger.error('No backend for version', activeVersionRegex, 'found');