-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
170 lines (147 loc) · 4.97 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
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
'use strict';
const debug = require('debug')('strider-docker-runner');
const initDocker = require('./lib/init');
const runDocker = require('./lib/run');
const Runner = require('strider-simple-runner').Runner;
function create(emitter, config, context, done) {
config = config || {};
config.processJob = runDocker;
const runner = new Runner(emitter, config);
runner.id = 'docker';
debug('Overriding runner.processJob');
runner.processJob = function processJob(job, config, next) {
debug('Running docker job...');
const self = this;
const now = new Date();
const oldnext = next;
next = () => {
delete self.callbackMap[job._id];
oldnext();
};
this.callbackMap[job._id] = next;
const dirs = {
base: '/home/strider/workspace',
data: '/home/strider/workspace',
cache: '/home/strider/workspace'
};
self.jobdata.get(job._id).started = now;
self.emitter.emit('browser.update', job.project.name, 'job.status.started', [job._id, now]);
debug(`[runner:${self.id}] Job started. Project: ${job.project.name} Job ID: ${job._id}`);
debug('Initializing plugins...');
self.plugins(job.project.creator, config, job, dirs, (err, workers) => {
if (err) {
let jobdata = self.jobdata.pop(job._id);
if (!jobdata) return next(err);
jobdata.errored = true;
jobdata.error = {
message: err.message,
stack: err.stack
};
// self.emitter.emit('browser.update', job.project.name, 'job.status.errored', [job._id, jobdata.error])
delete jobdata.data;
jobdata.finished = new Date();
self.emitter.emit('job.done', jobdata);
debug(`[runner:${self.id}] Job done with error. Project: ${job.project.name} Job ID: ${job._id}`);
return next(err);
}
const env = {};
if (config.envKeys) {
env.STRIDER_SSH_PUB = config.pubkey;
env.STRIDER_SSH_PRIV = config.privkey;
}
self.config.processJob(job, workers.provider, workers.jobplugins, {
cachier: Function.prototype,
baseDir: dirs.base,
dataDir: dirs.data,
cacheDir: dirs.cache,
io: self.config.io,
branchConfig: config,
env: env,
log: debug,
error: debug
}, err => {
var jobdata = self.jobdata.pop(job._id);
if (!jobdata) return next(err);
// Mark jobs as finished.
delete jobdata.data;
jobdata.finished = new Date();
if (err) {
jobdata.errored = true;
jobdata.error = {
message: err.message,
stack: err.stack
};
self.emitter.emit('browser.update', job.project.name, 'job.status.errored', [job._id, jobdata.error]);
debug(`[runner:${self.id}] Job done with error. Project: ${job.project.name} Job ID: ${job._id}`);
return next(err);
}
delete jobdata.data;
jobdata.finished = new Date();
self.emitter.emit('job.done', jobdata);
debug(`[runner:${self.id}] Job done without error. Project: ${job.project.name} Job ID: ${job._id}`);
return next();
});
});
};
debug('Fixing job queue handler');
runner.queue.handler = runner.processJob.bind(runner);
runner.loadExtensions(context.extensionPaths, err => {
done(err, runner);
});
}
/**
* List all running containers and check if their names are prefixed with "strider-".
* If so, then they were probably started during a previous run and were not shut down properly.
* In that case, we try to clean them up now.
*
* Note that this process does and can not know about containers that were started on any other but
* the default host. Meaning with a different host given in the job configuration.
*/
function cleanup() {
debug('Cleaning up...');
initDocker({}, (err, docker) => {
if (err) {
debug(err);
return;
}
docker.listContainers((err, containers) => {
if (err) {
debug(err);
return;
}
debug(`Found ${containers.length} running containers.`);
containers.forEach(function (containerInfo) {
if (!containerInfo.Names[0].match(/^\/strider-/)) {
debug(`Container "${containerInfo.Names[0]}" is not a strider runner. Skipping.`);
return;
}
debug(`Attempting to clean up container "${containerInfo.Names[0]}"...`);
docker.getContainer(containerInfo.Id)
.remove({
force: true, // Stop container and remove
v: true // Remove any attached volumes
}, err => {
if (err) {
debug(err);
return;
}
debug(`Cleaned up container "${containerInfo.Names[0]}".`);
});
});
});
});
}
// Cleanup on initial module load.
cleanup();
module.exports = {
create: create,
config: {
host: String,
port: Number,
socketPath: String,
dns: String,
docker_host: String,
docker_volumeBinds: String,
docker_links: String
}
};