OSS lowercase migration added

Signed-off-by: Mert Can Bilgiç <mert.bilgic@trendyol.com>
This commit is contained in:
Mert Can Bilgiç
2021-11-09 12:36:18 +03:00
parent 056c85aa72
commit 41e5f28ab7
3 changed files with 117 additions and 15 deletions
@@ -24,7 +24,7 @@ import { SwiftClient } from '@trendyol-js/openstack-swift-sdk';
import { NotFound } from '@trendyol-js/openstack-swift-sdk/lib/types';
import { Stream, Readable } from 'stream';
import { Logger } from 'winston';
import { getFileTreeRecursively, getHeadersForFileExtension } from './helpers';
import { getFileTreeRecursively, getHeadersForFileExtension, lowerCaseEntityTripletInStoragePath } from './helpers';
import {
PublisherBase,
PublishRequest,
@@ -77,7 +77,7 @@ export class OpenStackSwiftPublish implements PublisherBase {
} catch (error) {
throw new Error(
"Since techdocs.publisher.type is set to 'openStackSwift' in your app config, " +
'techdocs.publisher.openStackSwift.containerName is required.',
'techdocs.publisher.openStackSwift.containerName is required.',
);
}
@@ -115,9 +115,9 @@ export class OpenStackSwiftPublish implements PublisherBase {
}
this.logger.error(
`Could not retrieve metadata about the OpenStack Swift container ${this.containerName}. ` +
'Make sure the container exists. Also make sure that authentication is setup either by ' +
'explicitly defining credentials and region in techdocs.publisher.openStackSwift in app config or ' +
'by using environment variables. Refer to https://backstage.io/docs/features/techdocs/using-cloud-storage',
'Make sure the container exists. Also make sure that authentication is setup either by ' +
'explicitly defining credentials and region in techdocs.publisher.openStackSwift in app config or ' +
'by using environment variables. Refer to https://backstage.io/docs/features/techdocs/using-cloud-storage',
);
return {
isAvailable: false,
@@ -289,4 +289,60 @@ export class OpenStackSwiftPublish implements PublisherBase {
return false;
}
}
async migrateDocsCase({
removeOriginal = false,
concurrency = 25,
}): Promise<void> {
// Iterate through every file in the root of the publisher.
const allObjects = await this.getAllObjectsFromContainer();
const limiter = createLimiter(concurrency);
await Promise.all(
allObjects.map(f =>
limiter(async file => {
let newPath;
try {
newPath = lowerCaseEntityTripletInStoragePath(file);
} catch (e) {
assertError(e);
this.logger.warn(e.message);
return;
}
// If all parts are already lowercase, ignore.
if (file === newPath) {
return;
}
try {
this.logger.verbose(`Migrating ${file} to ${newPath}`);
await this.storageClient.copy(this.containerName, file, this.containerName, newPath);
if (removeOriginal) {
await this.storageClient.delete(this.containerName, file);
}
} catch (e) {
assertError(e);
this.logger.warn(`Unable to migrate ${file}: ${e.message}`);
}
}, f),
),
);
}
/**
* Returns a list of all object keys from the configured container.
*/
protected async getAllObjectsFromContainer(
{ prefix } = { prefix: '' },
): Promise<string[]> {
let objects: string[] = [];
let allObjects: any;
const OSS_MAX_LIMIT = Math.pow(2, 31) - 1;
allObjects = await this.storageClient.list(this.containerName, prefix, OSS_MAX_LIMIT)
objects = allObjects.map((object: any) => object = object.name)
return objects;
}
}