1. Use plain Record instead of Map

2. Call fs.realpath inside the utility function

Signed-off-by: Himanshu Mishra <himanshu@orkohunter.net>
This commit is contained in:
Himanshu Mishra
2021-03-08 12:44:11 +01:00
parent 29b64038e1
commit c0c26244db
6 changed files with 34 additions and 35 deletions
@@ -57,10 +57,10 @@ describe('runDockerContainer', () => {
const imageName = 'dockerOrg/image';
const args = ['bash', '-c', 'echo test'];
const mountDirs = new Map([
[path.join(rootDir, 'input'), '/input'],
[path.join(rootDir, 'output'), '/output'],
]);
const mountDirs = {
[path.join(rootDir, 'input')]: '/input',
[path.join(rootDir, 'output')]: '/output',
};
const workingDir = path.join(rootDir, 'input');
const envVars = ['HOME=/tmp', 'LOG_LEVEL=debug'];
+10 -6
View File
@@ -15,6 +15,7 @@
*/
import Docker from 'dockerode';
import fs from 'fs-extra';
import { PassThrough, Writable } from 'stream';
export type UserOptions = {
@@ -26,7 +27,7 @@ export type RunDockerContainerOptions = {
args: string[];
logStream?: Writable;
dockerClient: Docker;
mountDirs?: Map<string, string>;
mountDirs?: Record<string, string>;
workingDir?: string;
envVars?: string[];
createOptions?: Docker.ContainerCreateOptions;
@@ -40,7 +41,7 @@ export type RunDockerContainerOptions = {
* @param options.logStream the log streamer to capture log messages
* @param options.dockerClient the dockerClient to use
* @param options.mountDirs A map of host directories to mount on the container.
* Map Key: Path on host machine, Value: Path on Docker container
* Object Key: Path on host machine, Value: Path on Docker container
* @param options.workingDir Working dir in the container
* @param options.envVars Environment variables to set in the container. e.g. ['HOME=/tmp']
*/
@@ -49,7 +50,7 @@ export const runDockerContainer = async ({
args,
logStream = new PassThrough(),
dockerClient,
mountDirs = new Map(),
mountDirs = {},
workingDir,
envVars = [],
createOptions = {},
@@ -85,14 +86,17 @@ export const runDockerContainer = async ({
// Initialize volumes to mount based on mountDirs map
const Volumes: { [T: string]: object } = {};
for (const containerDir of mountDirs.values()) {
for (const containerDir of Object.values(mountDirs)) {
Volumes[containerDir] = {};
}
// Create bind volumes
const Binds: string[] = [];
for (const [hostDir, containerDir] of mountDirs.entries()) {
Binds.push(`${hostDir}:${containerDir}`);
for (const [hostDir, containerDir] of Object.entries(mountDirs)) {
// Need to use realpath here as Docker mounting does not like
// symlinks for binding volumes
const realHostDir = await fs.realpath(hostDir);
Binds.push(`${realHostDir}:${containerDir}`);
}
const [{ Error: error, StatusCode: statusCode }] = await dockerClient.run(
@@ -16,7 +16,6 @@
import { runDockerContainer } from '@backstage/backend-common';
import { Config } from '@backstage/config';
import fs from 'fs-extra';
import path from 'path';
import { PassThrough } from 'stream';
import { Logger } from 'winston';
@@ -80,12 +79,10 @@ export class TechdocsGenerator implements GeneratorBase {
}
// Directories to bind on container
const mountDirs = new Map([
// Need to use realpath here as Docker mounting does not like
// symlinks for binding volumes
[await fs.realpath(inputDir), '/input'],
[await fs.realpath(outputDir), '/output'],
]);
const mountDirs = {
[inputDir]: '/input',
[outputDir]: '/output',
};
try {
switch (this.options.runGeneratorIn) {
@@ -160,10 +160,10 @@ describe('CookieCutter Templater', () => {
'--verbose',
],
envVars: ['HOME=/tmp'],
mountDirs: new Map([
[path.join('tempdir', 'template'), '/input'],
[path.join('tempdir', 'intermediate'), '/output'],
]),
mountDirs: {
[path.join('tempdir', 'template')]: '/input',
[path.join('tempdir', 'intermediate')]: '/output',
},
workingDir: '/input',
logStream: undefined,
dockerClient: mockDocker,
@@ -203,10 +203,10 @@ describe('CookieCutter Templater', () => {
'--verbose',
],
envVars: ['HOME=/tmp'],
mountDirs: new Map([
[path.join('tempdir', 'template'), '/input'],
[path.join('tempdir', 'intermediate'), '/output'],
]),
mountDirs: {
[path.join('tempdir', 'template')]: '/input',
[path.join('tempdir', 'intermediate')]: '/output',
},
workingDir: '/input',
logStream: stream,
dockerClient: mockDocker,
@@ -59,12 +59,10 @@ export class CookieCutter implements TemplaterBase {
await fs.writeJSON(path.join(templateDir, 'cookiecutter.json'), cookieInfo);
// Directories to bind on container
const mountDirs = new Map([
// Need to use realpath here as Docker mounting does not like
// symlinks for binding volumes
[await fs.realpath(templateDir), '/input'],
[await fs.realpath(intermediateDir), '/output'],
]);
const mountDirs = {
[templateDir]: '/input',
[intermediateDir]: '/output',
};
const cookieCutterInstalled = await commandExists('cookiecutter');
if (cookieCutterInstalled) {
@@ -40,10 +40,10 @@ export class CreateReactAppTemplater implements TemplaterBase {
const intermediateDir = path.join(workspacePath, 'template');
await fs.ensureDir(intermediateDir);
const mountDirs = new Map([
[await fs.realpath(intermediateDir), '/template'],
[await fs.realpath(intermediateDir), '/result'],
]);
const mountDirs = {
[intermediateDir]: '/template',
[intermediateDir]: '/result',
};
await runDockerContainer({
imageName: 'node:lts-alpine',