-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
159 lines (135 loc) · 4.79 KB
/
worker.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
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
const User = require('./pkg/user');
const config = require('./pkg/config');
const JobConf = require('./pkg/config-jobs');
const { parentPort, workerData } = require("worker_threads");
jobconf = new JobConf();
user = new User({
oauth_token: config.oauth_token,
username: config.github_user,
organization: config.organization
});
user.getAuthStatus().catch((error) => {
console.log(error);
});
async function runScript({script, repoName, ref, ocp_project} = {}){
// script: list of commands
env_clone = process.env;
var env_clone = Object.create( process.env );
env_clone.OCP_PROJECT = ocp_project;
env_clone.HOME = '/var/tmp/' + repoName + '/' + ref;
let complete_comm = "set -e;";
for (let comm of script) {
complete_comm += comm + ';'
}
return await user.runInRepo({
repoName: repoName,
command: complete_comm,
ref: ref,
env_vars: env_clone,
timeout: jobconf.global.timeout
});
}
async function execJob(chunkObj) {
let refSanitized = chunkObj.ref.replace(/\//g, '-');
// update local repo with remote changes
user.registerRepo(chunkObj.repository.name);
// kill any previous jobs running in this repo
await user.killRepoJob(chunkObj.repository.name, refSanitized, () => {
user.updateStatus({
repoName: chunkObj.repository.name,
ref: refSanitized,
sha: chunkObj.after,
status: ''
})
});
// sync new changes to local repo
await user.syncRepo({
repoName: chunkObj.repository.name,
ref: refSanitized,
tree_sha: chunkObj.head_commit.tree_id
});
// read in job config
jobconf.reset();
try{
jobconf.read('/var/tmp/' + chunkObj.repository.name + '/' + refSanitized + '/ci.yml');
} catch(err) {
console.log(err);
return
}
// post pending status
if( jobconf.global.report.github && config.reporting ){
user.updateStatus({
repoName: chunkObj.repository.name,
ref: refSanitized,
sha: chunkObj.after,
status: 'pending'
}).then( (data) => {
if(data.body.message) {
console.log("[CI Server] Failed to update 'pending' statuses to " + chunkObj.repository.name + " with: " + body.message);
} else {
console.log("[CI Server] Posted status 'pending' to repo " + chunkObj.repository.name);
}
}).catch((err) => {
console.log("Failed to update status: " + err);
})
}
//run scripts
console.log("\n\nRunning Scripts");
results = await runScript({
script: jobconf.script,
repoName: chunkObj.repository.name,
ref: refSanitized,
ocp_project: chunkObj.after
})
let end_status = "";
if(results.code == 2) {
await user.killRepoJob(chunkObj.repository.name, refSanitized);
end_status = "failure";
} else if(results.code != 0) {
end_status = "failure";
} else {
end_status = "success";
}
console.log('Script for ' + chunkObj.repository.name + ':' + chunkObj.after + ' ended with status ' + end_status)
// run after_script
console.log("\n\nRunning After_Script");
asResults = await runScript({
script: jobconf.after_script,
repoName: chunkObj.repository.name,
ref: refSanitized,
ocp_project: chunkObj.after
});
if(asResults.code == 2) { //timeout happened
await user.killRepoJob(chunkObj.repository.name, refSanitized);
}
if( jobconf.global.report.github && config.reporting ){
user.postGist("## Script results \n```" + results.data + "```\n\n## After Scripts\n```" + asResults.data + "```", (error, url) => {
if(error) {
console.log('Could not post test output to gist: ');
console.log(error);
} else {
console.log("Gist available at: " + url);
}
// post final script statuses
user.updateStatus({
repoName: chunkObj.repository.name,
ref: refSanitized,
sha: chunkObj.after,
status: end_status,
url: url
}).then( (data) => {
if(data.body.message) {
console.log("[CI Server] Failed to update statuses: " + body.message);
} else {
console.log("[CI Server] Posted status '" + end_status + "' to repo " + chunkObj.repository.name);
}
}).catch((err) => {
console.log("Failed to update status: " + err);
})
});
}
}
parentPort.on('message', async (chunk) => {
await execJob(chunk);
parentPort.postMessage('complete');
});