-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathnotifier-server.js
More file actions
128 lines (113 loc) · 3.22 KB
/
Copy pathnotifier-server.js
File metadata and controls
128 lines (113 loc) · 3.22 KB
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
119
120
121
122
123
124
125
126
127
128
var mailer,
root = require( "path" ).dirname( __filename ),
directory = root + "/notifier.d",
os = require( "os" ),
opts = require( "optimist" )
.usage( "Start a server to listen for github post receives and execute scripts from a directory\n\t$0" )
.options( "p", {
alias: "port",
"default": 3333,
describe: "Port number for server"
})
.options( "d", {
alias: "directory",
"default": directory
})
.boolean( "debug" )
.describe( "debug", "Enable Debug" ),
argv = opts.argv,
port = argv.p,
http = require( "http" ),
Notifier = require( "git-notifier" ).Notifier,
notifier = new Notifier(),
server = http.createServer(),
fs = require( "fs" ),
proc = require( "child_process" );
var debug = require( "debug" );
var async = require( "async" );
if (argv.debug) {
debug.enable( "notifier-server:*" );
}
debug.enable( "notifier-server:error" );
var error = debug( "notifier-server:error" );
var log = debug( "notifier-server:server" ),
invalidSHA = /[^0-9a-f]/;
if ( fs.existsSync( "./mail-config.json" ) ) {
log( "Loading E-Mail Component" );
mailer = require( "./notify-mail.js" );
} else {
// without mail config, mailer is a noop
mailer = function() {};
}
directory = argv.d;
if ( argv.h ) {
console.log( opts.help() );
process.exit();
}
function makeExec( filename ) {
var log = debug( "notifier-server:script:" + filename );
var queue = async.queue(function spawn(eventData, callback) {
var commit = eventData.commit;
log( "spawn", commit );
var output = "",
exit = -1,
started = Date.now();
var process = proc.spawn( directory + "/" + filename, [ commit ] );
process.stdout.on( "data", function( data ) {
output += data;
doLog( "out", data );
});
process.stderr.on( "data", function( data ) {
output += data;
doLog( "err", data );
});
process.on( "exit", function( code ) {
exit = code;
log( "exit", code );
});
process.on( "close", function() {
var subject = os.hostname() + ": ";
if (exit) {
subject += "FAILED ";
}
subject += "Deployment: " + filename + " " + commit + " " + ((Date.now() - started)/1000).toFixed(0) + "s";
mailer( subject, output + "\nExit Code: " + exit );
callback( null, { subject: subject, filename: filename, eventData: eventData, output: output, exit: exit, time: Date.now() - started, started: started });
});
});
queue.drain = function() { log( "done" ); };
function doLog( prefix, text ) {
var parts = ("" + text).split(/\n/);
parts.forEach(function( line ) {
if ( line.length ) {
log( prefix, line );
}
});
}
return function( data, callback ) {
if ( invalidSHA.test( data.commit ) ) {
log( "Bad Request", data );
return;
}
log( "queue", data.commit );
if (callback) {
queue.push( data, callback );
} else {
queue.push( data );
}
};
}
fs.readdirSync( directory ).forEach( function( file ) {
if ( !/\.js$/.exec( file ) ) {
return;
}
log( "Including " + directory + "/" + file );
var js = directory + "/" + file,
sh = file.replace( /\.js$/, ".sh" );
require( js )( notifier, makeExec( sh ) );
});
server.on( "request", notifier.handler );
server.on( "error", error );
notifier.on( "error", error );
log( "Setting up post-receive server on port " + port );
server.listen( port );