Skip to content

Commit 59282a9

Browse files
Demo with ansible combo
1 parent d2f8afb commit 59282a9

3 files changed

Lines changed: 34 additions & 8 deletions

File tree

genghistask.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ module.exports = function(RED) {
1414
const workspace = new Workspace(context, RED.nodes.getNode(config.workspace));
1515

1616
this.on("input", (msg, nodeSend, nodeDone) => {
17+
18+
msg.WORKSPACE = workspace.getDirectory();
19+
1720
if (this.activeProcesses) {
1821
this.activeProcesses.kill();
1922
Ornament.kill(this, workspace, msg);
@@ -92,8 +95,8 @@ module.exports = function(RED) {
9295
const workspace = new Workspace(context, req.query);
9396
if (req.query.tab_id == "shell") {
9497
res.setHeader('content-type', 'text/plain');
95-
return workspace.getScriptStream(req.query.script, {}).pipe(res);
98+
return workspace.getScriptStream(req.query.script, {WORKSPACE:workspace.getDirectory()}).pipe(res);
9699
}
97100
res.send(workspace.getLogFile(req.query.nodeId, req.query.tab_id));
98101
});
99-
}
102+
}

helper/workspace.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,30 @@ Workspace.prototype.clone = async function() {
2121
if (!fs.existsSync(parentDir)){
2222
fs.mkdirSync(parentDir, { recursive: true });
2323
}
24+
if (!this.workspace.workspace) {
25+
return
26+
}
27+
if (fs.existsSync(parentDir + "/" + this.workspace.id)){
28+
/* cleanup if workspace url change for same id : delete workspace */
29+
const currentRemote = execSync("git remote get-url origin", { cwd: parentDir + "/" + this.workspace.id}).toString().trim();
30+
if (currentRemote != this.workspace.workspace) {
31+
try {
32+
/* check access to the futur remote before deleting the workspace of the old one */
33+
execSync(`git remote set-url origin ${this.workspace.workspace}`, { cwd: parentDir + "/" + this.workspace.id});
34+
execSync(`git ls-remote`, { cwd: parentDir + "/" + this.workspace.id});
35+
/* delete workspace */
36+
fs.rmSync(parentDir + "/" + this.workspace.id, {recursive: true});
37+
} catch (e) {
38+
/* rollback cleanup */
39+
execSync(`git remote set-url origin ${currentRemote}`, { cwd: parentDir + "/" + this.workspace.id});
40+
}
41+
}
42+
}
2443
if (!fs.existsSync(parentDir + "/" + this.workspace.id)){
25-
await exec(`git clone ${this.workspace.workspace} ${this.workspace.id}`, { cwd: parentDir});
44+
execSync(`git clone ${this.workspace.workspace} ${this.workspace.id}`, { cwd: parentDir});
2645
} else {
27-
await exec("git pull", { cwd: parentDir + "/" + this.workspace.id});
46+
/* update workspace */
47+
execSync("git pull", { cwd: parentDir + "/" + this.workspace.id});
2848
/* prune log */
2949
exec(`find ${path.join(parentDir, this.workspace.id, 'log')} -type f -exec sed -ne':a;$p;N;11,$D;ba' -i {} \\;`).catch(e=>{});
3050
}
@@ -61,7 +81,7 @@ Workspace.prototype.getEnvironementById = async function (id) {
6181
Workspace.prototype.getUnixShebang = function(relativeScriptPath) {
6282
const filename = this.context.getFilenameInUserDir("genghistaskdata/"+this.workspace.id+"/shell/"+relativeScriptPath);
6383
if (!fs.existsSync(filename)) {
64-
return '/bin/bash';
84+
return ' /bin/bash';
6585
}
6686
return (
6787
(new String(fs.readFileSync(filename)).match(/(^#!.*)/) || [])[1] || ''
@@ -75,7 +95,7 @@ Workspace.prototype.getScriptStream = function(relativeScriptPath, env) {
7595
if (fs.existsSync(filename)) {
7696
script = fs.readFileSync(filename)
7797
}
78-
const declaredEnv = execSync('export -p', { env: env });
98+
const declaredEnv = execSync('export -p', { env: env, cwd: env.WORKSPACE });
7999
return Readable.from(declaredEnv + script);
80100
}
81101

@@ -87,12 +107,15 @@ Workspace.prototype.getLogFile = function(logname, suffix) {
87107
}
88108
return fs.readFileSync(logfile);
89109
}
110+
Workspace.prototype.getDirectory = function() {
111+
return this.context.getFilenameInUserDir("genghistaskdata/"+this.workspace.id);
112+
}
90113

91114
Workspace.prototype.launch = function(remote, script, payload, logname) {
92115
//@TODO implement log.log to trace last execution date
93116
const logfile = this.context.getFilenameInUserDir("genghistaskdata/"+this.workspace.id+"/log/");
94117
fs.mkdirSync(logfile + logname, { recursive: true });
95-
const child = spawn("sh",["-c" , remote + this.getUnixShebang(script)], payload);
118+
const child = spawn("sh",["-c" , remote + this.getUnixShebang(script)], {env:payload, cwd:payload.WORKSPACE});
96119
if (!child) {
97120
return false;
98121
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@genghistask/node-red",
3-
"version": "1.0.0",
3+
"version": "1.0.2",
44
"description": "Run a script from a repository",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)