Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execute Command CNOE Custom plugin #38

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions examples/template/template-command-execute.yaml
Original file line number Diff line number Diff line change
@@ -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'
]

52 changes: 52 additions & 0 deletions packages/backend/src/plugins/execute-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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.');
}

ctx.logger.info("Running command "+command+" "+commandArgs.join(' '));

// Execute the shell command with optional arguments
await executeShellCommand({
command: command,
args: commandArgs,
logStream: ctx.logStream,
});
ctx.logger.info('Command executed successfully!');
},
});
};
2 changes: 2 additions & 0 deletions packages/backend/src/plugins/scaffolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -37,6 +38,7 @@ export const cnoeScaffolderActions = createBackendModule({
createKubernetesApply(config),
createSanitizeResource(),
createVerifyDependency(),
executeCommand()
);
},
});
Expand Down