1. /**
  2. * Bootsmaps user-defined scripts for monitoring with dry-run mode.
  3. * @param {Array<Object>} scripts An array of script objects. Each object should have:
  4. * - name: string, the name of the script.
  5. * - input: Object, an object containing input parameters for the script.
  6. * - command: string, the command to execute (e.g., "node script.js").
  7. * - dryRun: boolean, whether to run in dry-run mode (default: false).
  8. */
  9. function bootstrapScripts(scripts) {
  10. if (!Array.isArray(scripts)) {
  11. console.error("Error: scripts must be an array.");
  12. return;
  13. }
  14. scripts.forEach(script => {
  15. if (typeof script !== 'object' || script === null) {
  16. console.warn("Skipping invalid script: ", script);
  17. return;
  18. }
  19. if (!script.name || typeof script.name !== 'string') {
  20. console.warn("Skipping script, missing name: ", script);
  21. return;
  22. }
  23. if (!script.command || typeof script.command !== 'string') {
  24. console.warn("Skipping script, missing command: ", script);
  25. return;
  26. }
  27. if (script.dryRun !== undefined && typeof script.dryRun !== 'boolean') {
  28. console.warn("Skipping script, invalid dryRun value: ", script);
  29. return;
  30. }
  31. const execute = async () => {
  32. try {
  33. const command = script.command;
  34. const input = script.input;
  35. const dryRun = script.dryRun || false; // Default to false if not specified
  36. console.log(`[Monitoring] Running script: ${script.name} (${command})`);
  37. console.log(`[Monitoring] Input: ${JSON.stringify(input)}`);
  38. if (dryRun) {
  39. console.log("[Monitoring] DRY RUN: Would execute the following command:");
  40. console.log(`[Monitoring] ${command}`);
  41. console.log("[Monitoring] with input:", JSON.stringify(input));
  42. return;
  43. }
  44. const { exec } = require('child_process'); // Node.js specific
  45. exec(command, input, (error, stdout, stderr) => {
  46. if (error) {
  47. console.error(`[Monitoring] Error executing ${script.name}:`, error);
  48. if (stderr) {
  49. console.error(`[Monitoring] stderr: ${stderr}`);
  50. }
  51. } else {
  52. console.log(`[Monitoring] Script ${script.name} executed successfully.`);
  53. if (stdout) {
  54. console.log(`[Monitoring] stdout: ${stdout}`);
  55. }
  56. }
  57. });
  58. } catch (err) {
  59. console.error(`[Monitoring] Error during script execution:`, err);
  60. }
  61. };
  62. execute();
  63. });
  64. }

Add your comment