-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.js
More file actions
181 lines (162 loc) · 5.54 KB
/
Copy pathapp.js
File metadata and controls
181 lines (162 loc) · 5.54 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* This server deals with running Frisby.js tests.
*
* Endpoints:
* /pullCode - this endpoint instructs the server to pull code from github.
* /executeMonitorFrisby - this endpoints instructs the server to run the Frisby.js tests and then send
* the results into the raw_monitoring_results queue
* /testEndpoint - test endpoint to make sure the server is up and running
*
*/
var conf = require('./config.js');
var http = require('http'),
express = require('express');
var shell = require('shelljs');
var async = require('async');
var executeMonitor = require('./lib/ExecuteMonitor.js');
var repository = require('./lib/repository.js');
var resultHandler = require('./lib/results.js');
// Setup Params
var PORT = process.env.SERVER_PORT || conf.SERVER_PORT,
HOST = process.env.SERVER_HOST || conf.SERVER_HOST,
github_url = process.env.GITHUB_URL || conf.GITHUB_URL,
github_token = process.env.GITHUB_TOKEN || conf.GITHUB_TOKEN,
repository_path = process.env.REPOSITORY_PATH || conf.REPOSITORY_PATH,
result_output_path = process.env.RESULT_OUTPUT_PATH || conf.RESULT_OUTPUT_PATH,
monitor_interval = process.env.MONITOR_INTERVAL || conf.MONITOR_INTERVAL;
var app = express();
app.use(express.bodyParser());
/**
* Interval to hit the test interface to kick off a test.
*
*/
var intervalRunTests;
intervalRunTests = setInterval(function(){
shell.exec('curl -s -X POST http://localhost:'+PORT+'/executeMonitorFrisby > /dev/null', function(code, output) {
console.log('Exit code:', code);
console.log('Program output:', output);
});
}, monitor_interval);
/**
* -Pull new code into the local servers directory
* -Output userData to disk
*
* -input: github_url, oauth_token, user_id, username, email
*/
function pullCode(){
repository.setGithubURL(github_url);
repository.setOauthToken(github_token);
repository.setRepoPath(repository_path);
async.series([
function(callback){
// Remove current repository
repository.remove(function(err, result){
if(result){
callback(null, true);
}else{
callback(null, false);
}
});
},
function(callback){
// Clone repository
repository.clone(function(err, result){
if(result){
callback(null, true);
}else{
callback(null, false);
}
});
},
function(callback){
// Run npm install in repository
repository.runNPMInstall(function(err, result){
if(result){
callback(null, true);
}else{
callback(null, false);
}
})
}
],
// optional callback
function(err, results){
console.log(results);
});
};
/**
* Executes the local Frisby.js code and puts the results into a the "<env>_raw_monitoring_results"
* job queue.
*
* -input: <>
*/
app.post('/executeMonitorFrisby', function (req, res) {
executeMonitor.setRepoPath(repository_path);
executeMonitor.setResultOutputDir(result_output_path);
async.series([
function(callback){
// Check if directory exists
executeMonitor.doesExecuteRepoExist(function(err, result){
if(result){
callback(null, true);
}else{
callback(null, false);
}
});
},
function(callback){
// Remove old test results
executeMonitor.removePreviousTestResults(function(err, result){
if(result){
callback(null, true);
}else{
callback(null, false);
}
});
},
function(callback){
// Run the Frisby.js test
executeMonitor.runFrisbyjsTests(function(err, result){
if(result){
callback(null, true);
}else{
callback(null, false);
}
});
},
function(callback){
// Get the test results
executeMonitor.getResults(function(err, results){
if(err !== true){
console.log(results);
callback(null, results);
}else{
// Error
console.log(results);
callback(null, false);
}
});
}
],
// optional callback
function(err, results){
console.log('Final test output:');
console.log(results[3].body);
// Handle the frisby.js test results
resultHandler.setXmlResults(results[3].body);
resultHandler.parse();
resultHandler.triggers();
});
});
app.get('/heartbeat', function(req, res) {
res.json(200, { message: 'Alive'});
})
//
// Create HTTP server
//
server = http.createServer(app);
server.listen(PORT, HOST, null, function() {
console.log('Server listening on port %d in %s mode', PORT, app.settings.env);
});
// Run pull code on startup
pullCode();