forked from AutoDevBot/API-Monitor-Runner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
220 lines (189 loc) · 6.66 KB
/
Copy pathapp.js
File metadata and controls
220 lines (189 loc) · 6.66 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* 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
*
*
* Starting server:
* -You will have to export an environment variable with the name of one of the configs:
* export NODE_ENV=default -used to pull environment config
* export USER_ID=1234 -used to subscried to a Redis channel to bootstrap this container to pullCode
*
* Directory access and dependancies
* -This worker needs write permission to /opt - it will put and delete the "repo" directory at this location
* -This working will output the xml result to: /tmp/frisby_output/
* -Frisby.js lib is expected at this location: /opt/AutoDevBotContainerNodes/node_modules/frisby/lib/frisby.js
*
*/
var CONFIG = require('config').frisbyRunner;
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 PORT = CONFIG.port,
HOST = CONFIG.host;
var app = express();
app.use(express.bodyParser());
// TODO: put this path in the config file so that it can be changed for each environment
var frisby_path = 'not_used';
// User data holding info needed to run this container
// data: {"user_id":"b9e45b2320a544b8b017fbf60fb04247","github_url":"https://github.com/AutoDevBot/monitor-examples.git","oauth_token":"1234","username":"garland2","email":"garland@example.org"}
var userData = new Object();
// Holds persistent data on file system
var persistent_data_file = './AutoDevBot_userData.txt';
var repository_path = '/tmp/repo';
var result_output_path = '/tmp/frisby_output/';
userData = checkPersistentData(shell, persistent_data_file);
console.log('Setting user data to:');
console.log(userData);
/**
* Interval to hit the test interface to kick off a test.
*
* TODO: make the interval time user settable. Probably should also add another endpoint
* to listen for a broadcast for an "on demand" 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);
});
}, 60000);
/**
* -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(userData.github_url);
repository.setOauthToken(userData.oauth_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 !== null){
console.log(results);
}else{
// Error
console.log(results);
}
});
}
],
// optional callback
function(err, results){
console.log(results);
});
});
//
// 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);
});
/**
* Check if the file which is holding the persistent data is on the file system.
* If so, return those values.
*
* @param shell
* @param userDataFilePath
* @returns {*}
*/
function checkPersistentData(shell, userDataFilePath){
if (shell.test('-f', userDataFilePath)){
console.log('File exist: '+userDataFilePath);
// Read file
var userDataObj = JSON.parse(shell.cat(userDataFilePath));
return userDataObj;
}else{
console.log('File does not exist: '+userDataFilePath);
return {};
}
}
// Run pull code on startup
pullCode();