forked from Strider-CD/strider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
150 lines (128 loc) · 4.22 KB
/
main.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
'use strict';
var path = require('path');
var passport = require('passport');
var async = require('async');
var Loader = require('strider-extension-loader');
var globalTunnel = require('global-tunnel');
var app = require('./lib/app');
var common = require('./lib/common');
var config = require('./lib/config');
var middleware = require('./lib/middleware');
var auth = require('./lib/auth');
var models = require('./lib/models');
var pluginTemplates = require('./lib/plugin-templates');
var upgrade = require('./lib/models/upgrade').ensure;
var loadExtensions = require('./lib/utils/load-extensions');
var killZombies = require('./lib/utils/kill-zombies');
var registerPanel = require('./lib/utils/register-panel');
var Job = models.Job;
var Config = models.Config;
common.extensions = {};
//
// Use globa-tunnel to provide proxy support.
// The http_proxy environment variable will be used if the first parameter to globalTunnel.initialize is null.
//
globalTunnel.initialize();
module.exports = function (extdir, c, callback) {
var appConfig = config;
var k;
// override with c
for (k in c) {
appConfig[k] = c[k];
}
// Initialize the (web) app
var appInstance = app.init(appConfig);
var cb = callback || defaultCallback;
function defaultCallback(err) {
if (err) {
throw err;
}
}
if (typeof Loader !== 'function') {
throw new Error('Your version of strider-extension-loader is out of date');
}
var loader = new Loader([path.join(__dirname, 'client/styles')], true);
appInstance.loader = loader;
common.loader = loader;
//
// ### Strider Context Object
//
// Context object is passed to each extension. It carries various config
// settings, as well as handles to enable functions to register things.
// Context can also be accessed as a singleton within Strider as
// common.context.
var context = {
serverName: appConfig.strider_server_name,
config: appConfig,
enablePty: config.enablePty,
emitter: common.emitter,
extensionRoutes: [],
extensionPaths: extdir,
extdir: extdir,
loader: loader,
models: models,
logger: console,
middleware: middleware,
auth: auth, //TODO - may want to make this a subset of the auth module
passport: passport,
registerPanel: registerPanel(common),
registerBlock: pluginTemplates.registerBlock,
app: appInstance
};
// Make extension context available throughout application.
common.context = context;
var SCHEMA_VERSION = Config.SCHEMA_VERSION;
upgrade(SCHEMA_VERSION, function (err) {
if (err) {
return cb(err);
}
loadExtensions(loader, extdir, context, appInstance, function () {
// kill zombie jobs
killZombies(function () {
var tasks = [];
if (!common.extensions.runner || typeof common.extensions.runner !== 'object') {
console.error('Strider seems to have been misconfigured - there are no available runner plugins. ' +
'Please make sure all dependencies are up to date.');
process.exit(1);
}
Object.keys(common.extensions.runner).forEach(function (name) {
var runner = common.extensions.runner[name];
if (!runner) {
console.log('no runner', name);
return;
}
tasks.push(function (next) {
Job.find({
'runner.id': name,
finished: null
}, function (error, jobs) {
if (error) {
return next(error);
}
runner.findZombies(jobs, next);
});
});
});
async.parallel(tasks, function (err, zombies) {
if (err) return cb(err);
var ids = [].concat.apply([], zombies).map(function (job) {
return job._id;
});
var now = new Date();
Job.update({_id: {$in: ids}}, {
$set: {
finished: now,
errored: true,
error: {message: 'Job timeout', stack: ''}
}
}, function () {
Job.update({_id: {$in: ids}, started: null}, {$set: {started: now}}, function (err) {
cb(err, appInstance);
});
});
});
});
});
});
return appInstance;
};