forked from tralamazza/node-foreman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nf.js
executable file
·261 lines (195 loc) · 7.91 KB
/
nf.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
#!/usr/bin/env node
var util = require('util');
var path = require('path');
var events = require('events');
var fs = require('fs');
var program = require('commander');
var display = require('./lib/console').Console
var package = JSON.parse(
fs.readFileSync(path.join(__dirname,'package.json'),'utf-8')
);
program.version(package.version);
program.option('-j, --procfile <FILE>' , 'load profile FILE','Procfile');
program.option('-e, --env <FILE>' ,'use FILE to load environment','.env');
program.option('-p, --port <PORT>' ,'start indexing ports at number PORT',5000);
var command;
// Foreman Event Bus/Emitter //
var emitter = new events.EventEmitter();
emitter.once('killall',function(){
display.Done("Killing All Processes");
});
emitter.setMaxListeners(50);
var _proc = require('./lib/proc');
var start = _proc.start;
var _procfile = require('./lib/procfile');
var procs = _procfile.procs;
var loadProc = _procfile.loadProc;
var _envs = require('./lib/envs');
var loadEnvs = _envs.loadEnvs;
var _requirements = require('./lib/requirements');
var getreqs = _requirements.getreqs;
var calculatePadding = _requirements.calculatePadding;
var startProxies = require('./lib/proxy').startProxies;
var startForward = require('./lib/forward').startForward;
// Kill All Child Processes on SIGINT
process.once('SIGINT',function userkill(){
console.log();
display.Warn('Interrupted by User');
emitter.emit('killall');
});
program
.command('start')
.usage('[Options] [Processes] e.g. web=1,log=2,api')
.option('-s, --showenvs' , 'show ENV variables on start', false)
.option('-x, --proxy <PORT>' , 'start a load balancing proxy on PORT')
.option('-f, --forward <PORT>' , 'start a forward proxy on PORT')
.option('-i, --intercept <HOSTNAME>' , 'set forward proxy to intercept HOSTNAME', null)
.option('-t, --trim <N>' , 'trim logs to N characters', 0)
.option('-w, --wrap' , 'wrap logs (negates trim)')
.option('-d, --root' , 'specify an alternate application root. This defaults to the directory containing the Procfile. It is available as $CWD, if not otherwise set, in the Procfile')
.description('Start the jobs in the Procfile')
.action(function(command_left,command_right){
command = command_right || command_left;
var envs = loadEnvs(program.env);
var cwd = program.root || (program.procfile && fs.realpathSync(program.procfile.replace(/[^\/\\]*$/, ''))) || process.cwd();
program.procfile = program.procfile === 'Procfile' ? path.join(cwd, program.procfile) : program.procfile;
var proc = loadProc(program.procfile, envs);
if (!proc) return;
if (command.showenvs){
for (var key in envs){
display.Alert("env %s=%s", key, envs[key]);
}
}
var reqs = getreqs(program.args[0], proc);
display.padding = calculatePadding(reqs);
if (command.wrap){
display.wrapline = process.stdout.columns - display.padding - 7;
display.trimline = 0;
display.Alert('Wrapping display Output to %d Columns',display.wrapline);
} else {
display.trimline = command.trim || process.stdout.columns - display.padding - 5;
if (display.trimline > 0) {
display.Alert('Trimming display Output to %d Columns',display.trimline);
}
}
if (command.forward) startForward(command.forward, command.intercept, emitter);
startProxies(reqs,proc,command,emitter,program.port);
if( process.getuid && process.getuid() === 0) process.setuid(process.env.SUDO_USER);
start(proc, reqs, envs, program.port, emitter, cwd);
});
var upstart = require('./lib/upstart');
program
.command('export')
.option('-a, --app <NAME>' , 'export upstart application as NAME','foreman')
.option('-u, --user <NAME>' , 'export upstart user as NAME','root')
.option('-o, --out <DIR>' , 'export upstart files to DIR','.')
.option('-g, --gid <GID>' , 'set gid of upstart config to GID')
.option('-l, --log <DIR>' , 'specify upstart log directory','/var/log')
.option('-t, --type <TYPE>' , 'export file to TYPE (default upstart)','upstart')
.option('-m, --template <DIR>' , 'use template folder')
.option('-d, --root' , 'specify an alternate application root. This defaults to the directory containing the Procfile.')
.description('Export to an upstart job independent of foreman')
.action(function(command_left,command_right) {
var key, i;
command = command_right || command_left;
var envs = loadEnvs(program.env);
var cwd = program.root || (program.procfile && fs.realpathSync(program.procfile.replace(/[^\/\\]*$/, ''))) || process.cwd();
program.procfile = program.procfile === 'Procfile' ? path.join(cwd, program.procfile) : program.procfile;
var procs = loadProc(program.procfile, envs);
if (!procs) return;
var req = getreqs(program.args[0], procs);
// Variables for Upstart Template
var config = {
application : command.app,
cwd : cwd,
user : command.user,
logs : command.log,
envs : envs,
group : command.gid || command.user,
template : command.template
};
config.envfile = path.resolve(program.env);
var writeout;
if (upstart[command.type]) {
writeout = upstart[command.type];
} else {
return display.Error("Unknown Export Format",command.type);
}
// Check for Upstart User
// friendly warning - does not stop export
var user_exists = false;
fs.readFileSync('/etc/passwd')
.toString().split(/\n/).forEach(function(line){
if(line.match(/^[^:]*/)[0] == config.user){
user_exists = true;
}
});
if(!user_exists) display.Warn(display.fmt("User %s Does Not Exist on System",config.user));
// Remove Old Upstart Files
// Must Match App Name and Out Directory
fs.readdirSync(command.out).forEach(function(file){
var x = file.indexOf(command.app);
if (x === 0){
var p = path.join(command.out,file);
display.Warn("Unlink : %s".yellow.bold,p);
fs.unlinkSync(p);
}
});
var baseport = parseInt(program.port);
var baseport_i = 0;
var baseport_j = 0;
config.processes=[];
// This is ugly because of shitty support for array copying
// Cleanup is definitely required
for (key in req){
var c = {};
var proc = procs[key];
if (!proc){
display.Warn("Required Key '%s' Does Not Exist in Procfile Definition",key);
continue;
}
config.processes.push({process:key});
c.process = key;
c.command = proc.command + " " + proc.args.join(' ');
for (i in config){
c[i] = config[i];
}
var n = req[key];
c.numbers = [];
for (i = 1; i <= n; i++){
var conf = {};
conf.number = i;
for (i in c){
conf[i] = c[i];
}
conf.port = conf.envs.PORT = baseport + baseport_i + baseport_j * 100;
conf.envs.CWD = conf.envs.CWD || cwd;
var envl = [];
for (key in envs){
envl.push({
key: key,
value: envs[key]
});
}
conf.envs = envl;
// Write the APP-PROCESS-N.conf File
writeout.foreman_app_n(conf,command.out);
baseport_i++;
c.numbers.push({number:i});
}
// Write the APP-Process.conf File
writeout.foreman_app(c, command.out);
baseport_i = 0;
baseport_j++;
}
// Write the APP.conf File
writeout.foreman(config,command.out);
});
program.parse(process.argv);
if (program.args.length === 0) {
console.log(' _____ '.cyan);
console.log(' | __|___ ___ ___ _____ ___ ___ '.cyan);
console.log(' | __| . | _| -_| | | |'.yellow);
console.log(' |__| |___|_| |___|_|_|_|_^_|_|_|'.magenta);
program.help();
}