forked from strongloop/node-foreman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nf.js
executable file
·285 lines (214 loc) · 7.82 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env node
var util = require('util');
var path = require('path');
var events = require('events');
var fs = require('fs');
var colors = require('./lib/colors')
var quote = require('shell-quote').quote;
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 procfile 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 once = _proc.once
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)')
.description('Start the jobs in the Procfile')
.action(function(command_left,command_right){
command = command_right || command_left;
var envs = loadEnvs(program.env);
var proc = loadProc(program.procfile);
if(!proc) return;
if(command.showenvs){
for(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);
});
program
.command('run')
.usage('[Options]')
.option('-s, --showenvs' ,'show ENV variables on start',false)
.description('Run a one off process using the ENV variables')
.action(function(command_left,command_right){
command = command_right || command_left;
var envs = loadEnvs(program.env);
var arguments = Array.prototype.slice.apply(this.args);
arguments.pop();
var callback = function(code) {
process.exit(code);
}
if(!command) return;
var input = quote(arguments);
if(command.showenvs){
for(key in envs){
display.Alert("env %s=%s",key,envs[key]);
}
}
display.trimline = process.stdout.columns - 5;
once(input,envs,callback);
});
var exporters = require('./lib/exporters')
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('-c, --cwd <DIR>' ,'change current working directory 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')
.description('Export to an upstart job independent of foreman')
.action(function(command_left,command_right){
command = command_right || command_left;
var envs = loadEnvs(program.env);
var procs = loadProc(program.procfile);
if(!procs) return;
var req = getreqs(program.args[0],procs);
// Variables for Upstart Template
var config = {
application : command.app,
cwd : path.resolve(process.cwd(), command.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(exporters[command.type]){
writeout = exporters[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));
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 cmd = procs[key];
if (!cmd){
display.Warn("Required Key '%s' Does Not Exist in Procfile Definition",key);
continue;
}
var n = req[key];
config.processes.push({process:key, n: n})
c.process=key;
c.command=cmd;
for(_ in config){
c[_] = config[_];
}
c.numbers = [];
for(var i=1;i<=n;i++){
var conf = {};
conf.number = i;
for(_ in c){
conf[_] = c[_];
}
conf.port = baseport + baseport_i + baseport_j*100;
var envl = [];
for(key in envs){
envl.push({
key: key,
value: envs[key]
})
}
envl.push({ key: 'PORT', value: conf.port });
envl.push({ key: 'FOREMAN_WORKER_NAME', value: conf.process+'.'+conf.number });
conf.envs = envl;
// Write the APP-PROCESS-N.conf File
writeout.foreman_app_n(conf,command.out);
baseport_i++;
c.numbers.push({number:i})
}
var envl = [];
for(key in envs){
envl.push({
key: key,
value: envs[key]
})
}
c.envs = envl;
// 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(colors.cyan(' _____ '))
console.log(colors.cyan(' | __|___ ___ ___ _____ ___ ___ '))
console.log(colors.yellow(' | __| . | _| -_| | | |'))
console.log(colors.magenta(' |__| |___|_| |___|_|_|_|_^_|_|_|'))
program.help();
}