diff --git a/plugins/scaffolder-backend-module-cookiecutter/package.json b/plugins/scaffolder-backend-module-cookiecutter/package.json index 373b22c5c2..9a8afa8324 100644 --- a/plugins/scaffolder-backend-module-cookiecutter/package.json +++ b/plugins/scaffolder-backend-module-cookiecutter/package.json @@ -1,5 +1,5 @@ { - "name": "plugin-scaffolder-backend-module-cookiecutter", + "name": "@backstage/plugin-scaffolder-backend-module-cookiecutter", "version": "0.1.0", "main": "src/index.ts", "types": "src/index.ts", diff --git a/plugins/scaffolder-backend-module-cookiecutter/src/index.ts b/plugins/scaffolder-backend-module-cookiecutter/src/index.ts new file mode 100644 index 0000000000..e8de675502 --- /dev/null +++ b/plugins/scaffolder-backend-module-cookiecutter/src/index.ts @@ -0,0 +1,16 @@ +/* + * Copyright 2021 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. + */ +export { createFetchCookiecutterAction } from './actions'; diff --git a/plugins/scaffolder-backend/package.json b/plugins/scaffolder-backend/package.json index 8f10c9fa53..e44a22403c 100644 --- a/plugins/scaffolder-backend/package.json +++ b/plugins/scaffolder-backend/package.json @@ -35,6 +35,7 @@ "@backstage/config": "^0.1.5", "@backstage/errors": "^0.1.1", "@backstage/integration": "^0.5.8", + "@backstage/plugin-scaffolder-backend-module-cookiecutter": "^0.1.0", "@gitbeaker/core": "^30.2.0", "@gitbeaker/node": "^30.2.0", "@octokit/rest": "^18.5.3", diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/createBuiltinActions.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/createBuiltinActions.ts index 6541625035..c367f0abb7 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/createBuiltinActions.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/createBuiltinActions.ts @@ -25,10 +25,10 @@ import { import { createDebugLogAction } from './debug'; import { - createFetchCookiecutterAction, createFetchPlainAction, createFetchTemplateAction, } from './fetch'; +import { createFetchCookiecutterAction } from '@backstage/plugin-scaffolder-backend-module-cookiecutter'; import { createFilesystemDeleteAction, createFilesystemRenameAction, diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/cookiecutter.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/cookiecutter.test.ts deleted file mode 100644 index 86f953a6a5..0000000000 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/cookiecutter.test.ts +++ /dev/null @@ -1,212 +0,0 @@ -/* - * Copyright 2021 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. - */ -const runCommand = jest.fn(); -const commandExists = jest.fn(); -const fetchContents = jest.fn(); - -jest.mock('./helpers', () => ({ fetchContents })); -jest.mock('command-exists', () => commandExists); -jest.mock('../helpers', () => ({ runCommand })); - -import { - getVoidLogger, - UrlReader, - ContainerRunner, -} from '@backstage/backend-common'; -import { ConfigReader, JsonObject } from '@backstage/config'; -import { ScmIntegrations } from '@backstage/integration'; -import mockFs from 'mock-fs'; -import os from 'os'; -import { PassThrough } from 'stream'; -import { createFetchCookiecutterAction } from './cookiecutter'; -import { join } from 'path'; -import { ActionContext } from '../../types'; - -describe('fetch:cookiecutter', () => { - const integrations = ScmIntegrations.fromConfig( - new ConfigReader({ - integrations: { - azure: [ - { host: 'dev.azure.com', token: 'tokenlols' }, - { host: 'myazurehostnotoken.com' }, - ], - }, - }), - ); - - const mockTmpDir = os.tmpdir(); - - let mockContext: ActionContext<{ - url: string; - targetPath?: string; - values: JsonObject; - copyWithoutRender?: string[]; - extensions?: string[]; - imageName?: string; - }>; - - const containerRunner: jest.Mocked = { - runContainer: jest.fn(), - }; - - const mockReader: UrlReader = { - read: jest.fn(), - readTree: jest.fn(), - search: jest.fn(), - }; - - const action = createFetchCookiecutterAction({ - integrations, - containerRunner, - reader: mockReader, - }); - - beforeEach(() => { - jest.resetAllMocks(); - - mockContext = { - input: { - url: 'https://google.com/cookie/cutter', - targetPath: 'something', - values: { - help: 'me', - }, - }, - baseUrl: 'somebase', - workspacePath: mockTmpDir, - logger: getVoidLogger(), - logStream: new PassThrough(), - output: jest.fn(), - createTemporaryDirectory: jest.fn().mockResolvedValue(mockTmpDir), - }; - - // mock the temp directory - mockFs({ [mockTmpDir]: {} }); - mockFs({ [`${join(mockTmpDir, 'template')}`]: {} }); - - commandExists.mockResolvedValue(null); - - // Mock when run container is called it creates some new files in the mock filesystem - containerRunner.runContainer.mockImplementation(async () => { - mockFs({ - [`${join(mockTmpDir, 'intermediate')}`]: { - 'testfile.json': '{}', - }, - }); - }); - - // Mock when runCommand is called it creats some new files in the mock filesystem - runCommand.mockImplementation(async () => { - mockFs({ - [`${join(mockTmpDir, 'intermediate')}`]: { - 'testfile.json': '{}', - }, - }); - }); - }); - - afterEach(() => { - mockFs.restore(); - }); - - it('should throw an error when copyWithoutRender is not an array', async () => { - (mockContext.input as any).copyWithoutRender = 'not an array'; - - await expect(action.handler(mockContext)).rejects.toThrowError( - /Fetch action input copyWithoutRender must be an Array/, - ); - }); - - it('should throw an error when extensions is not an array', async () => { - (mockContext.input as any).extensions = 'not an array'; - - await expect(action.handler(mockContext)).rejects.toThrowError( - /Fetch action input extensions must be an Array/, - ); - }); - - it('should call fetchContents with the correct variables', async () => { - fetchContents.mockImplementation(() => Promise.resolve()); - await action.handler(mockContext); - expect(fetchContents).toHaveBeenCalledWith( - expect.objectContaining({ - reader: mockReader, - integrations, - baseUrl: mockContext.baseUrl, - fetchUrl: mockContext.input.url, - outputPath: join( - mockTmpDir, - 'template', - "{{cookiecutter and 'contents'}}", - ), - }), - ); - }); - - it('should call out to cookiecutter using runCommand when cookiecutter is installed', async () => { - commandExists.mockResolvedValue(true); - - await action.handler(mockContext); - - expect(runCommand).toHaveBeenCalledWith( - expect.objectContaining({ - command: 'cookiecutter', - args: [ - '--no-input', - '-o', - join(mockTmpDir, 'intermediate'), - join(mockTmpDir, 'template'), - '--verbose', - ], - logStream: mockContext.logStream, - }), - ); - }); - - it('should call out to the containerRunner when there is no cookiecutter installed', async () => { - commandExists.mockResolvedValue(false); - - await action.handler(mockContext); - - expect(containerRunner.runContainer).toHaveBeenCalledWith( - expect.objectContaining({ - imageName: 'spotify/backstage-cookiecutter', - command: 'cookiecutter', - args: ['--no-input', '-o', '/output', '/input', '--verbose'], - mountDirs: { - [join(mockTmpDir, 'intermediate')]: '/output', - [join(mockTmpDir, 'template')]: '/input', - }, - workingDir: '/input', - envVars: { HOME: '/tmp' }, - logStream: mockContext.logStream, - }), - ); - }); - - it('should use a custom imageName when there is an image supplied to the context', async () => { - const imageName = 'test-image'; - mockContext.input.imageName = imageName; - - await action.handler(mockContext); - - expect(containerRunner.runContainer).toHaveBeenCalledWith( - expect.objectContaining({ - imageName, - }), - ); - }); -}); diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/cookiecutter.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/cookiecutter.ts deleted file mode 100644 index 048b2bba64..0000000000 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/cookiecutter.ts +++ /dev/null @@ -1,240 +0,0 @@ -/* - * Copyright 2021 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 { - ContainerRunner, - UrlReader, - resolveSafeChildPath, -} from '@backstage/backend-common'; -import { JsonObject, JsonValue } from '@backstage/config'; -import { InputError } from '@backstage/errors'; -import { ScmIntegrations } from '@backstage/integration'; -import commandExists from 'command-exists'; -import fs from 'fs-extra'; -import path, { resolve as resolvePath } from 'path'; -import { Writable } from 'stream'; -import { runCommand } from '../helpers'; -import { createTemplateAction } from '../../createTemplateAction'; -import { fetchContents } from './helpers'; - -export class CookiecutterRunner { - private readonly containerRunner: ContainerRunner; - - constructor({ containerRunner }: { containerRunner: ContainerRunner }) { - this.containerRunner = containerRunner; - } - - private async fetchTemplateCookieCutter( - directory: string, - ): Promise> { - try { - return await fs.readJSON(path.join(directory, 'cookiecutter.json')); - } catch (ex) { - if (ex.code !== 'ENOENT') { - throw ex; - } - - return {}; - } - } - - public async run({ - workspacePath, - values, - logStream, - }: { - workspacePath: string; - values: JsonObject; - logStream: Writable; - }): Promise { - const templateDir = path.join(workspacePath, 'template'); - const intermediateDir = path.join(workspacePath, 'intermediate'); - await fs.ensureDir(intermediateDir); - const resultDir = path.join(workspacePath, 'result'); - - // First lets grab the default cookiecutter.json file - const cookieCutterJson = await this.fetchTemplateCookieCutter(templateDir); - - const { imageName, ...valuesForCookieCutterJson } = values; - const cookieInfo = { - ...cookieCutterJson, - ...valuesForCookieCutterJson, - }; - - await fs.writeJSON(path.join(templateDir, 'cookiecutter.json'), cookieInfo); - - // Directories to bind on container - const mountDirs = { - [templateDir]: '/input', - [intermediateDir]: '/output', - }; - - // the command-exists package returns `true` or throws an error - const cookieCutterInstalled = await commandExists('cookiecutter').catch( - () => false, - ); - if (cookieCutterInstalled) { - await runCommand({ - command: 'cookiecutter', - args: ['--no-input', '-o', intermediateDir, templateDir, '--verbose'], - logStream, - }); - } else { - await this.containerRunner.runContainer({ - imageName: (imageName as string) ?? 'spotify/backstage-cookiecutter', - command: 'cookiecutter', - args: ['--no-input', '-o', '/output', '/input', '--verbose'], - mountDirs, - workingDir: '/input', - // Set the home directory inside the container as something that applications can - // write to, otherwise they will just fail trying to write to / - envVars: { HOME: '/tmp' }, - logStream, - }); - } - - // if cookiecutter was successful, intermediateDir will contain - // exactly one directory. - - const [generated] = await fs.readdir(intermediateDir); - - if (generated === undefined) { - throw new Error('No data generated by cookiecutter'); - } - - await fs.move(path.join(intermediateDir, generated), resultDir); - } -} - -export function createFetchCookiecutterAction(options: { - reader: UrlReader; - integrations: ScmIntegrations; - containerRunner: ContainerRunner; -}) { - const { reader, containerRunner, integrations } = options; - - return createTemplateAction<{ - url: string; - targetPath?: string; - values: JsonObject; - copyWithoutRender?: string[]; - extensions?: string[]; - imageName?: string; - }>({ - id: 'fetch:cookiecutter', - description: - "Downloads a template from the given URL into the workspace, and runs cookiecutter on it. This action is deprecated in favor of 'fetch:template'. See https://backstage.io/docs/features/software-templates/builtin-actions#migrating-from-fetch-cookiecutter-to-fetch-template for more details.", - schema: { - input: { - type: 'object', - required: ['url'], - properties: { - url: { - title: 'Fetch URL', - description: - 'Relative path or absolute URL pointing to the directory tree to fetch', - type: 'string', - }, - targetPath: { - title: 'Target Path', - description: - 'Target path within the working directory to download the contents to.', - type: 'string', - }, - values: { - title: 'Template Values', - description: 'Values to pass on to cookiecutter for templating', - type: 'object', - }, - copyWithoutRender: { - title: 'Copy Without Render', - description: - 'Avoid rendering directories and files in the template', - type: 'array', - items: { - type: 'string', - }, - }, - extensions: { - title: 'Template Extensions', - description: - "Jinja2 extensions to add filters, tests, globals or extend the parser. Extensions must be installed in the container or on the host where Cookiecutter executes. See the contrib directory in Backstage's repo for more information", - type: 'array', - items: { - type: 'string', - }, - }, - imageName: { - title: 'Cookiecutter Docker image', - description: - "Specify a custom Docker image to run cookiecutter, to override the default: 'spotify/backstage-cookiecutter'. This can be used to execute cookiecutter with Template Extensions. Used only when a local cookiecutter is not found.", - type: 'string', - }, - }, - }, - }, - async handler(ctx) { - ctx.logger.info('Fetching and then templating using cookiecutter'); - const workDir = await ctx.createTemporaryDirectory(); - const templateDir = resolvePath(workDir, 'template'); - const templateContentsDir = resolvePath( - templateDir, - "{{cookiecutter and 'contents'}}", - ); - const resultDir = resolvePath(workDir, 'result'); - - if ( - ctx.input.copyWithoutRender && - !Array.isArray(ctx.input.copyWithoutRender) - ) { - throw new InputError( - 'Fetch action input copyWithoutRender must be an Array', - ); - } - if (ctx.input.extensions && !Array.isArray(ctx.input.extensions)) { - throw new InputError('Fetch action input extensions must be an Array'); - } - - await fetchContents({ - reader, - integrations, - baseUrl: ctx.baseUrl, - fetchUrl: ctx.input.url, - outputPath: templateContentsDir, - }); - - const cookiecutter = new CookiecutterRunner({ containerRunner }); - const values = { - ...ctx.input.values, - _copy_without_render: ctx.input.copyWithoutRender, - _extensions: ctx.input.extensions, - imageName: ctx.input.imageName, - }; - - // Will execute the template in ./template and put the result in ./result - await cookiecutter.run({ - workspacePath: workDir, - logStream: ctx.logStream, - values, - }); - - // Finally move the template result into the task workspace - const targetPath = ctx.input.targetPath ?? './'; - const outputPath = resolveSafeChildPath(ctx.workspacePath, targetPath); - await fs.copy(resultDir, outputPath); - }, - }); -} diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/index.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/index.ts index fea32df39c..c9a16c274e 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/index.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/index.ts @@ -15,6 +15,5 @@ */ export { createFetchPlainAction } from './plain'; -export { createFetchCookiecutterAction } from './cookiecutter'; export { createFetchTemplateAction } from './template'; export { fetchContents } from './helpers';