From 3004c289c7965dca4a1e532462c2bb4fa09de9fa Mon Sep 17 00:00:00 2001 From: Mani Marothu Date: Wed, 18 Sep 2024 15:51:16 -0700 Subject: [PATCH 1/2] Create execute-command.ts Added Execute command Signed-off-by: Mani Marothu --- .../backend/src/plugins/execute-command.ts | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 packages/backend/src/plugins/execute-command.ts diff --git a/packages/backend/src/plugins/execute-command.ts b/packages/backend/src/plugins/execute-command.ts new file mode 100644 index 0000000..48be418 --- /dev/null +++ b/packages/backend/src/plugins/execute-command.ts @@ -0,0 +1,48 @@ +import { createTemplateAction } from '@backstage/plugin-scaffolder-node'; +import { executeShellCommand } from '@backstage/plugin-scaffolder-node'; + +export const executeCommand = () => { + return createTemplateAction<{ + command: string; + arguments?: string[]; + }>({ + id: 'cnoe:command:execute', + schema: { + input: { + type: 'object', + required: ['command'], + properties: { + command: { + type: 'string', + title: 'Command to run', + description: 'Command to execute', + }, + arguments: { + type: 'array', + items: { + type: 'string', + }, + title: 'Command Arguments', + description: 'Command arguments list', + }, + }, + }, + output: {}, + }, + async handler(ctx) { + const command = ctx.input.command; + const commandArgs = ctx.input.arguments || []; + + if (!command) { + throw new Error('The command must be provided.'); + } + + // Execute the shell command with optional arguments + await executeShellCommand({ + command: command, + args: commandArgs, + logStream: ctx.logStream, + }); + }, + }); +}; From 8f5cf49093a4f8dc5c37e44db7d13af1a57277c1 Mon Sep 17 00:00:00 2001 From: Mani Marothu Date: Wed, 18 Sep 2024 16:46:30 -0700 Subject: [PATCH 2/2] Created command execution custom plugin Signed-off-by: Mani Marothu --- .../template/template-command-execute.yaml | 32 +++++++++++++++++++ .../backend/src/plugins/execute-command.ts | 4 +++ packages/backend/src/plugins/scaffolder.ts | 2 ++ 3 files changed, 38 insertions(+) create mode 100644 examples/template/template-command-execute.yaml diff --git a/examples/template/template-command-execute.yaml b/examples/template/template-command-execute.yaml new file mode 100644 index 0000000..dda07d6 --- /dev/null +++ b/examples/template/template-command-execute.yaml @@ -0,0 +1,32 @@ +apiVersion: scaffolder.backstage.io/v1beta3 +kind: Template +metadata: + name: deploy-resources + title: Deploy Resources + description: Deploy Resource to Kubernetes +spec: + owner: guest + type: service + # these are the steps which are rendered in the frontend with the form input + parameters: + - title: file name + properties: + path: + type: string + description: file name + default: cm.yaml + steps: + - id: wait-for-few-mins + name: Wait to run next step + action: debug:wait + input: + seconds: 10 + - id: shell-command + name: Run Command + action: cnoe:command:execute + input: + command: curl + arguments: [ + 'https://fake-json-api.mock.beeceptor.com/users' + ] + \ No newline at end of file diff --git a/packages/backend/src/plugins/execute-command.ts b/packages/backend/src/plugins/execute-command.ts index 48be418..b703a77 100644 --- a/packages/backend/src/plugins/execute-command.ts +++ b/packages/backend/src/plugins/execute-command.ts @@ -2,6 +2,7 @@ import { createTemplateAction } from '@backstage/plugin-scaffolder-node'; import { executeShellCommand } from '@backstage/plugin-scaffolder-node'; export const executeCommand = () => { + return createTemplateAction<{ command: string; arguments?: string[]; @@ -36,6 +37,8 @@ export const executeCommand = () => { if (!command) { throw new Error('The command must be provided.'); } + + ctx.logger.info("Running command "+command+" "+commandArgs.join(' ')); // Execute the shell command with optional arguments await executeShellCommand({ @@ -43,6 +46,7 @@ export const executeCommand = () => { args: commandArgs, logStream: ctx.logStream, }); + ctx.logger.info('Command executed successfully!'); }, }); }; diff --git a/packages/backend/src/plugins/scaffolder.ts b/packages/backend/src/plugins/scaffolder.ts index 0b6fb2f..2416471 100644 --- a/packages/backend/src/plugins/scaffolder.ts +++ b/packages/backend/src/plugins/scaffolder.ts @@ -11,6 +11,7 @@ import { getRootLogger } from '@backstage/backend-common'; import { createKubernetesApply } from './k8s-apply'; import { createSanitizeResource } from './sanitize'; import { createVerifyDependency } from './verify'; +import { executeCommand } from './execute-command'; export const cnoeScaffolderActions = createBackendModule({ pluginId: 'scaffolder', @@ -37,6 +38,7 @@ export const cnoeScaffolderActions = createBackendModule({ createKubernetesApply(config), createSanitizeResource(), createVerifyDependency(), + executeCommand() ); }, });