From 0d7f989499c228767501e95b539fe83d1d4edda6 Mon Sep 17 00:00:00 2001 From: Emmanuel Nyachoke Date: Tue, 9 Apr 2024 15:11:38 +0300 Subject: [PATCH] INFRA-412: Add support for arbitrary scripts on host (#45) * INFRA-412: Add support for arbitrary scripts on host * Remove debug lines * Update bash to shell --- node-scripts/spec/pipeline3/scripts.spec.js | 14 ++++++++++---- .../spec/utils/resources/test_instances_1.json | 10 +++++----- .../pipeline3/impl/dockerComposeFromArtifact.js | 5 +++++ .../pipeline3/impl/dockerComposeGenericMaven.js | 5 +++++ .../src/pipeline3/impl/dockerComposeGit.js | 5 +++++ .../src/pipeline3/impl/dockerComposeMaven.js | 1 + node-scripts/src/pipeline3/impl/dockerMonolith.js | 6 ++++++ node-scripts/src/pipeline3/scripts.js | 10 ++++++++-- node-scripts/src/pipeline3/start-instance.js | 14 ++++++++++++-- 9 files changed, 57 insertions(+), 13 deletions(-) diff --git a/node-scripts/spec/pipeline3/scripts.spec.js b/node-scripts/spec/pipeline3/scripts.spec.js index 3098492..9a14e0e 100644 --- a/node-scripts/spec/pipeline3/scripts.spec.js +++ b/node-scripts/spec/pipeline3/scripts.spec.js @@ -188,10 +188,17 @@ describe("Scripts", function() { uuid: "1-22-333", scripts: [ { - type: "shell", + type: "dockerExecShell", executionStage: "6", conditions: ["data"], value: "/a/script.sh" + }, + { + type: "hostExecShell", + executionStage: "5", + conditions: ["data", "artifacts", "deployment"], + value: + "sudo chmod 777 -R /var/docker-volumes/ozone/ozone-dev/artifacts" } ], deployment: { @@ -289,7 +296,7 @@ describe("Scripts", function() { script = []; script.push("some commands"); instanceDef.scripts.push({ - type: "shell", + type: "dockerExecShell", executionStage: "6", conditions: ["artifacts"], restart: "true", @@ -303,7 +310,6 @@ describe("Scripts", function() { config, process.env ); - expect(scriptsToRun.script).toEqual([ "some commands", scripts.remote( @@ -435,7 +441,7 @@ describe("Scripts", function() { uuid: "1-22-333", scripts: [ { - type: "shell", + type: "dockerExecShell", executionStage: "6", conditions: ["data"], value: "/a/script.sh" diff --git a/node-scripts/spec/utils/resources/test_instances_1.json b/node-scripts/spec/utils/resources/test_instances_1.json index 65e2e35..bb3e51b 100644 --- a/node-scripts/spec/utils/resources/test_instances_1.json +++ b/node-scripts/spec/utils/resources/test_instances_1.json @@ -212,33 +212,33 @@ ], "scripts": [ { - "type": "shell", + "type": "dockerExecShell", "executionStage": "5", "conditions": ["data"], "value": "/stage/5/script.sh" }, { - "type": "shell", + "type": "dockerExecShell", "executionStage": "5", "conditions": ["data", "artifacts"], "value": "/usr/bin/bahmni -i local.inventory concat-configs" }, { - "type": "shell", + "type": "dockerExecShell", "executionStage": "7", "conditions": ["data", "deployment"], "restart": "false", "value": "/some/script.sh" }, { - "type": "shell", + "type": "dockerExecShell", "executionStage": "7", "conditions": ["data", "deployment"], "restart": "true", "value": "/some/script.sh" }, { - "type": "shell", + "type": "dockerExecShell", "executionStage": "6", "conditions": ["data", "deployment"], "value": "/another/script.sh" diff --git a/node-scripts/src/pipeline3/impl/dockerComposeFromArtifact.js b/node-scripts/src/pipeline3/impl/dockerComposeFromArtifact.js index a62e598..3dadb9d 100644 --- a/node-scripts/src/pipeline3/impl/dockerComposeFromArtifact.js +++ b/node-scripts/src/pipeline3/impl/dockerComposeFromArtifact.js @@ -310,6 +310,11 @@ module.exports = { return script + "\n"; }, + hostExec: command => { + let script = ""; + script += command + "\n"; + return script + "\n"; + }, setProperties: function(instanceDef, property, output) { let script = ""; let path = require("path"); diff --git a/node-scripts/src/pipeline3/impl/dockerComposeGenericMaven.js b/node-scripts/src/pipeline3/impl/dockerComposeGenericMaven.js index 5ce17e0..4f31a86 100644 --- a/node-scripts/src/pipeline3/impl/dockerComposeGenericMaven.js +++ b/node-scripts/src/pipeline3/impl/dockerComposeGenericMaven.js @@ -409,6 +409,11 @@ module.exports = { return script + "\n"; }, + hostExec: command => { + let script = ""; + script += command + "\n"; + return script + "\n"; + }, setProperties: function(instanceDef, property, output) { let script = ""; let path = require("path"); diff --git a/node-scripts/src/pipeline3/impl/dockerComposeGit.js b/node-scripts/src/pipeline3/impl/dockerComposeGit.js index f566379..059fabd 100644 --- a/node-scripts/src/pipeline3/impl/dockerComposeGit.js +++ b/node-scripts/src/pipeline3/impl/dockerComposeGit.js @@ -367,6 +367,11 @@ module.exports = { return script + "\n"; }, + hostExec: command => { + let script = ""; + script += command + "\n"; + return script + "\n"; + }, setProperties: function(instanceDef, property, output) { let script = ""; let path = require("path"); diff --git a/node-scripts/src/pipeline3/impl/dockerComposeMaven.js b/node-scripts/src/pipeline3/impl/dockerComposeMaven.js index 062b401..9fbfd9b 100644 --- a/node-scripts/src/pipeline3/impl/dockerComposeMaven.js +++ b/node-scripts/src/pipeline3/impl/dockerComposeMaven.js @@ -157,6 +157,7 @@ module.exports = { down: dockerComposeGit.down, pull: dockerComposeGit.pull, exec: dockerComposeGit.exec, + hostExec: dockerComposeGit.hostExec, setProperties: dockerComposeGit.setProperties, setLinks: dockerComposeGit.setLinks, getInstanceServicesAsStringList: diff --git a/node-scripts/src/pipeline3/impl/dockerMonolith.js b/node-scripts/src/pipeline3/impl/dockerMonolith.js index 09751fb..ec7f199 100644 --- a/node-scripts/src/pipeline3/impl/dockerMonolith.js +++ b/node-scripts/src/pipeline3/impl/dockerMonolith.js @@ -331,6 +331,12 @@ module.exports = { return script + "\n"; }, + hostExec: command => { + let script = ""; + script += command + "\n"; + return script + "\n"; + }, + /** * Copy 'source' located on the host to the container's 'destination'. * diff --git a/node-scripts/src/pipeline3/scripts.js b/node-scripts/src/pipeline3/scripts.js index 0003cf4..c80fc60 100644 --- a/node-scripts/src/pipeline3/scripts.js +++ b/node-scripts/src/pipeline3/scripts.js @@ -479,7 +479,8 @@ module.exports = { instanceDef, currentStage, config, - processEnv + processEnv, + postStart = true ) { var changes = []; if (processEnv[config.varArtifactsChanges()] === "true") { @@ -511,11 +512,16 @@ module.exports = { var deploymentTypeScripts = require("./impl/" + instanceDef.deployment.type); var runScript = ""; - if (item.type === "shell") { + if (item.type === "dockerExecShell" && postStart) { runScript += module.exports.remote( ssh, deploymentTypeScripts.exec(instanceDef, item.value, item.service) ); + } else if (item.type === "hostExecShell" && !postStart) { + runScript += module.exports.remote( + ssh, + deploymentTypeScripts.hostExec(item.value) + ); } else if (item.type === "python") { // TODO: to be implemented } else if (item.type === "sql") { diff --git a/node-scripts/src/pipeline3/start-instance.js b/node-scripts/src/pipeline3/start-instance.js index 252b6db..fa64962 100644 --- a/node-scripts/src/pipeline3/start-instance.js +++ b/node-scripts/src/pipeline3/start-instance.js @@ -49,6 +49,16 @@ script.headComment = "# Autogenerated script for the instance start/restart..."; script.body = []; script.body.push("set -e\n"); +var computedPreStartScripts = scripts.computeAdditionalScripts( + script.body, + instanceDef, + currentStage, + config, + process.env, + false +); +script.body = computedPreStartScripts.script; + // 'artifacts' if (process.env[config.varArtifactsChanges()] === "true") { @@ -96,14 +106,14 @@ if (process.env[config.varPropertiesChanges()] === "true") { } // Set the Timezone if provided -var computedScript = scripts.computeAdditionalScripts( +var computedPostStartScript = scripts.computeAdditionalScripts( script.body, instanceDef, currentStage, config, process.env ); -script.body = computedScript.script; +script.body = computedPostStartScript.script; // Final restart if (!(process.env[config.varDeploymentChanges()] === "true")) {