forked from apostrophecms/mechanic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
389 lines (343 loc) · 9.25 KB
/
app.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
var argv = require('boring')();
var _ = require('lodash');
var fs = require('fs');
var shelljs = require('shelljs');
var path = require('path');
var resolve = path.resolve;
var shellEscape = require('shell-escape');
var debug = false;
var dataFile;
if (argv.data) {
dataFile = argv.data;
delete argv.data;
} else {
dataFile = '/var/lib/misc/mechanic.json';
// The Unix File Hierarchy Standard says that all distros should
// have a /var/lib/misc folder for storage of "state files
// that don't need a directory." But create it if it's
// somehow missing (Mac for instance).
if (!debug && !fs.existsSync('/var/lib/misc')) {
fs.mkdirSync('/var/lib/misc', 0700);
}
}
var certProviders = {
letsencrypt: 'letsencrypt certonly --standalone {% for host in hosts %}-d {{host}} {% endfor %}; ln -s /etc/letsencrypt/live/{{host}}/fullchain.pem /etc/nginx/certs/{{name}}.cer; ln -s /etc/letsencrypt/live/{{host}}/privkey.pem /etc/nginx/certs/{{name}}.key'
}
var data = require('prettiest')({ json: dataFile });
var defaultSettings = {
conf: '/etc/nginx/conf.d',
certProviders: '/etc/nginx/mechanic-certproviders',
overrides: '/etc/nginx/mechanic-overrides',
logs: '/var/log/nginx',
restart: 'nginx -s reload',
bind: '*',
autoCert: 'false',
certProvider: null,
preConfig: 'service nginx stop',
postConfig: 'service nginx start'
};
if(shelljs.exec('which letsencrypt').code === 0) {
defaultSettings.certProvider = 'letsencrypt';
}
_.defaults(data, { settings: {} });
_.defaults(data.settings, defaultSettings);
var settings = data.settings;
if (!debug && !fs.existsSync(settings.certProviders)) {
fs.mkdirSync(settings.certProviders);
}
var nunjucks = require('nunjucks');
var command = argv._[0];
if (!command) {
usage();
}
var aliases = {
'backend': 'backends'
};
var options = {
'host': 'string',
'backends': 'addresses',
'aliases': 'strings',
'canonical': 'boolean',
'default': 'boolean',
'static': 'string',
'autoindex': 'boolean',
'https': 'boolean',
'redirect-to-https': 'boolean',
'certify': 'boolean',
'certProvider': 'string'
};
var parsers = {
string: function(s) {
return s.trim();
},
integer: function(s) {
return parseInt(s, 10);
},
integers: function(s) {
return _.map(parsers.strings(s), function(s) {
return parsers.integer(s);
});
},
addresses: function(s) {
return _.map(parsers.strings(s), function(s) {
var matches = s.match(/^(([^:]+)\:)?(\d+)$/);
if (!matches) {
throw 'A list of port numbers and/or address:port combinations is expected, separated by commas';
}
var host, port;
if (matches[2]) {
host = matches[2];
} else {
host = 'localhost';
}
port = matches[3];
return host + ':' + port;
});
},
strings: function(s) {
return s.toString().split(/\s*\,\s*/);
},
boolean: function(s) {
return (s === 'true') || (s === 'on') || (s == 1);
},
// Have a feeling we'll use this soon
keyValue: function(s) {
var s = parsers.string(s);
var o = {};
_.each(s, function(v) {
var matches = v.match(/^([^:]+):(.*)$/);
if (!matches) {
throw 'Key-value pairs expected, like this: key:value,key:value';
}
o[matches[1]] = matches[2];
});
return o;
}
};
var stringifiers = {
string: function(s) {
return s;
},
integer: function(s) {
return s;
},
strings: function(s) {
return s.join(',');
},
boolean: function(s) {
return s ? 'true' : 'false'
},
keyValue: function(o) {
return _.map(o, function(v, k) {
return k + ':' + v;r
}).join(',');
},
addresses: function(s) {
return s.join(',');
}
};
data.sites = data.sites || [];
if (command === 'add') {
update(true);
} else if (command === 'update') {
update(false);
} else if (command === 'remove') {
remove();
} else if (command === 'refresh') {
refresh();
} else if (command === 'list') {
list();
} else if (command === 'set') {
set();
} else if (command === 'reset') {
reset();
} else {
usage();
}
function usage(m) {
if (m) {
console.error(m);
}
console.error('See https://github.com/punkave/mechanic for usage.');
process.exit(1);
}
function set() {
// Top-level settings: nginx conf folder, logs folder,
// and restart command
if (argv._.length !== 3) {
usage("The \"set\" command requires two parameters:\n\nmechanic set key value");
}
var key = argv._[1];
var value = argv._[2];
data.settings[argv._[1]] = argv._[2];
go();
}
function update(add) {
if (argv._.length !== 2) {
usage('shortname argument is required; also --host');
}
var shortname = argv._[1];
var site;
if(settings.preConfig) {
if(shelljs.exec(settings.preConfig).code !== 0) {
console.error('ERROR: failed to run preConfig script: \'' + settings.preConfig + '\'.');
}
}
if (add) {
if (findSite(shortname)) {
usage('Site already exists, use update');
} else {
site = { shortname: shortname };
data.sites.push(site);
}
} else {
site = findSite(shortname);
if (!site) {
usage('Unknown site: ' + shortname);
}
}
_.each(argv, function(val, key) {
if (key === '_') {
return;
}
if (_.has(aliases, key)) {
key = aliases[key];
}
if (!_.has(options, key)) {
usage('Unrecognized option: ' + key);
}
try {
site[key] = parsers[options[key]](val);
} catch (e) {
console.error(e);
usage('Value for ' + key + ' must be of type: ' + options[key]);
}
});
var _hardProviders = debug?[]:fs.readdirSync(settings.certProviders),
hardProviders = {};
for(var provider of _hardProviders) {
hardProviders[provider] = fs.readFileSync(path.join(settings.certProviders,provider)).toString();
}
var providers = {};
Object.assign(providers, certProviders, hardProviders);
var _provider = site.certProvider || settings.certProvider;
if(site.certify && _provider) {
var provider = providers[_provider];
var aliases = site.aliases||[];
if(provider) {
var result = 0;
var parts = provider.replace(/\n/g,';').split(';');
for(var part of parts) {
result |= shelljs.exec(nunjucks.renderString(part, {
hosts: [site.host].concat(aliases),
name: site.shortname,
host: site.host
})).code;
}
if(result) console.error('ERROR: certProvider ' + _provider + ' terminated with non-zero exit code.');
}
}
if(settings.postConfig) {
if(shelljs.exec(settings.postConfig).code !== 0) {
console.error('ERROR: failed to run postConfig script: \'' + settings.postConfig + '\'.');
}
}
go();
}
function remove() {
if (argv._.length !== 2) {
usage();
}
var shortname = argv._[1];
var found = false;
data.sites = _.filter(data.sites || [], function(site) {
if (site.shortname === shortname) {
found = true;
return false;
}
return true;
});
if (!found) {
// It's not fatal but it's warning-worthy
console.error('Not found: ' + shortname);
return;
}
go();
}
function refresh() {
go();
}
function validSiteFilter(site) {
if ((!(site.backends && site.backends.length)) && (!site.static)) {
console.log('WARNING: skipping ' + site.shortname + ' because no backends have been specified (hint: --backends=portnumber)');
return false;
}
return true;
}
function go() {
var sites = _.filter(data.sites, validSiteFilter);
var template = fs.readFileSync(settings.template || (__dirname + '/template.conf'), 'utf8');
var output = nunjucks.renderString(template, {
sites: sites,
settings: settings
});
// Set up include-able files to allow
// easy customizations
_.each(sites, function(site) {
var folder = settings.overrides;
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder);
}
folder += '/' + site.shortname;
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder);
}
var files = [ 'location', 'proxy', 'server', 'top' ];
_.each(files, function(file) {
var filename = folder + '/' + file;
if (!fs.existsSync(filename)) {
fs.writeFileSync(filename, '# Your custom nginx directives go here\n');
}
});
});
if(!debug) fs.writeFileSync(settings.conf + '/mechanic.conf', output);
if (settings.restart !== false) {
var restart = settings.restart || 'service nginx reload';
if (shelljs.exec(restart).code !== 0) {
console.error('ERROR: unable to reload nginx configuration!');
process.exit(3);
}
}
// Under 0.12 (?) this doesn't want to terminate on its own,
// not sure who the culprit is
process.exit(0);
}
function findSite(shortname)
{
return _.find(data.sites, function(site) {
return site.shortname === shortname;
});
}
function list()
{
_.each(data.settings, function(val, key) {
if (val !== defaultSettings[key]) {
console.log(shellEscape([ 'mechanic', 'set', key, val ]));
}
});
_.each(data.sites, function(site) {
var words = [ 'mechanic', 'add', site.shortname ];
_.each(site, function(val, key) {
if (_.has(stringifiers, options[key])) {
words.push('--' + key + '=' + stringifiers[options[key]](val));
}
});
console.log(shellEscape(words));
});
}
function reset()
{
data.settings = defaultSettings;
data.sites = [];
go();
}