-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathruntime.js
executable file
·259 lines (201 loc) · 7.29 KB
/
runtime.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env node
/*
* module dependencies
*/
var shell = require('shelljs');
var exec = require('child_process').execFileSync;
var defaults = require('defaults');
var settings = require('./settings');
var fs = require('fs');
var path = require('path');
var ejs = require('ejs');
var Table = require('cli-table');
var pathIsAbsolute = require('path-is-absolute');
var debug = require('debug')('run-time:debug');
/*
* api functions
*/
exports.init = function(opts) {
opts = getOptions(opts, false);
opts.initConfituration = opts.configuration;
debug('init options', opts);
try {
shell.mkdir('-p', opts.runtimePath);
var upstart = ejs.render(opts.upstartTemplate, opts);
var nginx = ejs.render(opts.nginxTemplate, opts);
fs.writeFileSync(opts.upstartRuntimePath, upstart);
fs.writeFileSync(opts.nginxRuntimePath, nginx);
fs.writeFileSync(opts.configurationRuntimePath, JSON.stringify(opts, null, 2));
fs.writeFileSync(opts.configurationRerunPath, opts.configuration);
shell.chmod('+x', opts.configurationRerunPath);
console.log('Generated upstart configuration:', opts.upstartRuntimePath);
console.log('Generated nginx configuration:', opts.nginxRuntimePath);
console.log('Stored configuration:', opts.configurationRuntimePath);
console.log('To rerun the configuration: run `', opts.configurationRerunPath, '`');
debug('upstart configuration', upstart);
debug('nginx configuration', nginx);
} catch (err) {
console.error('Generating configuration files FAILED:', err, err.stack.split('\n'));
}
};
exports.add = function(opts) {
opts = getOptions(opts, true);
debug('add options', opts);
try {
// add (copy) host config files
shell.cp('-f', opts.upstartRuntimePath, opts.upstartDestPath + opts.appName + '.conf');
shell.cp('-f', opts.nginxRuntimePath, opts.nginxDestPath + opts.appName + '.conf');
console.log('Copied upstart configuration from:', opts.upstartRuntimePath, 'to:', opts.upstartDestPath + opts.appName + '.conf');
console.log('Copied nginx configuration from:', opts.nginxRuntimePath, 'to:', opts.nginxDestPath + opts.appName + '.conf');
// reload nginx and start the service
console.log('Reloading the nginx configuration...');
exec('/usr/sbin/nginx', ['-s', 'reload']);
console.log('Starting the application', opts.appName, '...');
exec('start', [opts.appName]); // /sbin/start
console.log('Sucessfully added and started the new configuration:', opts.appName);
// update list
var list = readList(opts.listPath);
list[opts.appName] = opts.initConfituration;
storeList(opts.listPath, list);
} catch (err) {
console.error('Adding/Starting the configuration:', opts.appName, 'FAILED:', err, err.stack.split('\n'));
}
};
exports.remove = function(opts) {
opts = getOptions(opts, true);
debug('remove options', opts);
var p;
// stop the app first
try {
console.log('Stopping the application', opts.appName, '...');
exec('stop', [opts.appName]); // /sbin/stop
console.log('Sucessfully stopped:', opts.appName);
} catch (err) {
console.error('Stopping', opts.appName, 'FAILED:', err, err.stack.split('\n'));
}
try {
// remove the config files from the host paths
var upstartDest = opts.upstartDestPath + opts.appName + '.conf';
var nginxDest = opts.nginxDestPath + opts.appName + '.conf';
console.log('Removing', upstartDest, '...');
shell.rm(upstartDest);
console.log('Removing', nginxDest, '...');
shell.rm(nginxDest);
console.log('Successfully removed the config files');
// reload nginx configuration after removing the configuration for the app
console.log('Reloading the nginx configuration...');
exec('/usr/sbin/nginx', ['-s', 'reload']);
console.log('Sucessfully reloaded nginx.');
// update list
var list = readList(opts.listPath);
delete list[opts.appName];
storeList(opts.listPath, list);
} catch (err) {
console.error('Reloading nginx FAILED:', err, err.stack.split('\n'));
}
};
exports.list = function(opts) {
opts = getOptions(opts, false);
try {
var list = readList(opts.listPath);
printList(list);
} catch (err) {
debug('list error', err);
console.log('nothing to show, sooory!');
}
};
/*
* private helper functions
*/
function getOptions(opts, readStored) {
opts = opts || {};
if (opts.exec) opts.application = getAbsolutePath(opts.exec);
opts.configuration = "'" + process.argv.join("' '") + "'";
// merge paths
opts = defaults(opts, getPaths());
// read previously stored configuration
if (readStored) {
try {
var storedConfigJson = fs.readFileSync(opts.configurationRuntimePath, 'utf-8');
var storedConfig = JSON.parse(storedConfigJson);
// merge storedConfig
opts = defaults(opts, storedConfig);
debug('Loaded previously stored configuration.json', storedConfig);
} catch (err) {
// it's ok, if nothing was stored proviously.
}
}
// merge default settings
opts.application = opts.application || getAbsolutePath(opts.exec);
opts = defaults(opts, settings);
return opts;
}
function getPaths() {
var pwd = process.cwd();
var runtimePath = pwd + '/.runtime/';
var upstartRuntimePath = runtimePath + 'upstart.conf';
var nginxRuntimePath = runtimePath + 'nginx.conf';
var configurationRuntimePath = runtimePath + 'configuration.json';
var configurationRerunPath = runtimePath + 'rerun.sh';
var listDir = '/etc/runtime/';
var listPath = listDir + 'list.json';
var upstartTemplatePath = __dirname + '/templates/upstart.conf';
var nginxTemplatePath = __dirname + '/templates/nginx.conf';
var upstartTemplate = fs.readFileSync(upstartTemplatePath, 'utf-8');
var nginxTemplate = fs.readFileSync(nginxTemplatePath, 'utf-8');
var upstartDestPath = '/etc/init/';
var nginxDestPath = '/etc/nginx/conf.d/';
return {
pwd: pwd,
listDir: listDir,
listPath: listPath,
runtimePath: runtimePath,
upstartRuntimePath: upstartRuntimePath,
nginxRuntimePath: nginxRuntimePath,
configurationRuntimePath: configurationRuntimePath,
configurationRerunPath: configurationRerunPath,
upstartTemplatePath: upstartTemplatePath,
nginxTemplatePath: nginxTemplatePath,
upstartTemplate: upstartTemplate,
nginxTemplate: nginxTemplate,
upstartDestPath: upstartDestPath,
nginxDestPath: nginxDestPath
};
}
function getAbsolutePath(p) {
var dir = process.cwd() + '/';
if (!p) return dir;
if (pathIsAbsolute(p)) return p;
return process.cwd() + '/' + p;
}
function readList(location) {
try {
var listJson = fs.readFileSync(location, 'utf-8');
return JSON.parse(listJson);
} catch (err) {
debug('Reading List failed', location, err);
return {};
}
}
function storeList(location, list) {
try {
var dir = path.parse(location).dir;
shell.mkdir('-p', dir);
fs.writeFileSync(location, JSON.stringify(list, null, 2));
} catch (err) {
debug('Storing List failed', location, err);
}
}
function printList(list) {
var Table = require('cli-table');
// instantiate cli-table
var table = new Table({
head: ['app-name', 'configuration']
});
// push rows to the table
Object.keys(list).forEach(function(key) {
var value = list[key];
table.push([key, value])
});
console.log(table.toString());
}