1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 | 1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
| var spawn = require('child_process').spawn;
var path = require('path');
function escape(command){
return command.split("\\").join("\\\\").split('"').join('\\"')
}
/* istanbul ignore next: function to test with 'sails console' */
function debugChildProcess(child){
child.stdout.on("data", function(data){sails.log.debug(data.toString())})
child.stderr.on("data", function(data){sails.log.error(data.toString())})
child.on("exit", function(code){sails.log.info("command finished with code ",code)})
}
/* istanbul ignore next: function to test with 'sails console' */
function sailsExecuteWithDocker (command,projectId){
debugChildProcess(ExecutionService.executeWithDocker(command,projectId))
}
/* istanbul ignore next: function to test with 'sails console' */
function sailsExecuteFromShell(command,projectId){
debugChildProcess(ExecutionService.executeFromShell(command,projectId))
}
/* istanbul ignore next: function to test with 'sails console' */
function sailsExecute(command,projectId){
debugChildProcess(ExecutionService.execute(command,projectId))
}
/**
* ExecutionService.js
*
* @description :: Service that takes care of executing the commands in a linux container.
* @docs :: http://sailsjs.org/#!documentation/services
*/
module.exports = {
execute: function execute (command,projectId){
/* istanbul ignore if: stay as that untill I find a CI that supports docker */
Iif (sails.config.shusee.useDocker){
return ExecutionService.executeWithDocker(command,projectId)
} else {
return ExecutionService.executeFromShell(command,projectId);
}
},
executeWithDocker:
/* istanbul ignore next: stay as that untill I find a CI that supports docker */
function executeWithDocker (command,projectId) {
projectId = projectId + '';
var config = sails.config.shusee;
var fsPath = config.fsPath;
var dockerConfig = config.dockerConfig;
var exec = dockerConfig.executable;
var image = dockerConfig.image;
var sh = dockerConfig.sh || config.sh;
var dockerPath = dockerConfig.dockerPath;
var cwd = path.resolve(fsPath,'projects',projectId)
var dockerCommand = "cd " + dockerPath + ' ; ' + command
var args = ['run','-v',[cwd,dockerPath].join(':'),"-e","SHELL=/bin/"+sh,image,sh,'-c',dockerCommand];
sails.log("running command - ",exec, args, 'on project', projectId)
return spawn(exec,args);
},
executeFromShell:function executeFromShell (command,projectId) {
projectId = projectId + '';
var config = sails.config.shusee;
var fsPath = config.fsPath;
var exec = config.sh;
var cwd = path.resolve(fsPath,'projects',projectId)
var args = ['-c',escape(command)];
sails.log("running command - ",exec, args.slice(0,-1).join(" "), '"'+args.slice(-1)+ '" on project', projectId)
return spawn(exec,args, {cwd:cwd});
},
sailsExecuteWithDocker: sailsExecuteWithDocker,
sailsExecuteFromShell: sailsExecuteFromShell,
sailsExecute:sailsExecute
} |