-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
118 lines (112 loc) · 4.73 KB
/
index.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'use strict';
const http = require('http');
const exec = require('child_process').exec;
const execFile = require('child_process').execFile;
const os = require('os');
const iconv = require('iconv-lite');
const buffer = require('buffer');
const system = os.platform();
const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');
let binaryEncoding = 'binary';
let encoding = 'cp936';
let options = {
encoding: binaryEncoding,
windowsHide:false,
};
function func(){}
func.prototype = {
execute:function(cmd,callback){
exec(cmd,options, function(error, stdout, stderr) {
if(error){
console.log(cmd+ ' fail');
if(callback && typeof callback === 'function') {
let result = iconv.decode(Buffer.from(stderr,binaryEncoding), encoding);
callback(true,result);
}
} else{
console.log(cmd+ ' success');
if(callback && typeof callback === 'function') {
console.log('cmd:'+cmd);
let result = iconv.decode(Buffer.from(stdout,binaryEncoding), encoding);
callback(true,result);
}
}
});
return this;
}
};
function run() { return new func(); }
let init = function(option){
http.createServer(function(req, res){
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.setHeader('X-Foo', 'bar');
console.log(req.method + ':' + req.url);
if(req.headers['x-gitee-token']===option.acceptToken && req.method===option.method && req.url === option.url && req.headers['user-agent']==='git-oschina-hook') {
// 验证成功
// 判断执行类型 file or command 默认:command
console.log('async:'+option.async);
if(option.type!=='file') {
let resText = '';
let loop = function loop(i){
run().execute(option.cmd[i],(success,result)=>{
resText+=result;
if(i<option.cmd.length-1) {
if(success===true) {
i++;
loop(i);
} else {
if(option.async===false) {
console.log('fail');
res.writeHead(500, { 'Content-Type': 'text/plain;charset=UTF-8' });
res.end(resText);
}
return false;
}
} else {
if(option.async===false) {
console.log('All success');
res.writeHead(200, { 'Content-Type': 'text/plain;charset=UTF-8' });
res.end(resText);
return false;
}
}
});
};
if(option.async===true) {
res.writeHead(200, { 'Content-Type': 'text/plain;charset=UTF-8' });
res.end('async success');
loop(0);
} else {
loop(0);
}
} else {
let bash = option.executeFile;
if(option.async===true) {
execFile(bash);
res.writeHead(200, { 'Content-Type': 'text/plain;charset=UTF-8' });
res.end('async success');
} else {
execFile(bash,options,(err, stdout, stderr)=>{
if(!stderr) {
console.log('All success');
res.writeHead(200, { 'Content-Type': 'text/plain;charset=UTF-8' });
let result = iconv.decode(Buffer.from(stdout,binaryEncoding), encoding);
res.end(result);
} else {
console.log('fail');
res.writeHead(500, { 'Content-Type': 'text/plain;charset=UTF-8' });
let result = iconv.decode(Buffer.from(stderr,binaryEncoding), encoding);
res.end(result);
}
return false;
});
}
}
} else {
res.writeHead(402, { 'Content-Type': 'text/plain' });
res.end('fail');
}
}).listen(option.port);
console.log('自动部署服务启动于:' + system + '操作系统,端口号:' + option.port);
};
module.exports = init;