-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathexporters.js
162 lines (133 loc) · 4.89 KB
/
exporters.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
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
// Node module: foreman
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
var colors = require('./colors');
var ppath = require('path');
var mu = require('mustache');
var fs = require('fs');
var display = require('./console').Console;
// Procfile to System Service Export //
function render(filename, conf, callback) {
fs.readFile(filename, {encoding: 'utf8'}, function(err, template) {
if (err) {
throw err;
}
callback(mu.render(template, conf));
});
}
function templatePath(conf, type, file) {
if(conf.template) {
return ppath.resolve(conf.template, file);
} else {
return ppath.resolve(__dirname, type, file);
}
}
function writeout(path) {
return function(data) {
if (fs.existsSync(path)) {
display.Warn(colors.bright_yellow('Replacing: %s'), path);
}
fs.writeFileSync(path,data);
display.Alert('Wrote :',ppath.normalize(path));
};
}
function upstart(conf, outdir) {
var path = outdir + "/" + conf.application + ".conf";
render(templatePath(conf, 'upstart', 'foreman.conf'), conf, writeout(path));
}
function upstart_app(conf, outdir) {
var path = outdir + "/" + conf.application + "-" + conf.process + ".conf";
render(templatePath(conf, 'upstart', 'foreman-APP.conf'), conf, writeout(path));
}
function upstart_app_n(conf, outdir) {
var path = outdir + "/" + conf.application + "-" + conf.process + "-" + conf.number + ".conf";
render(templatePath(conf, 'upstart','foreman-APP-N.conf'), conf, writeout(path));
}
function upstart_single(conf, outdir) {
var path = outdir + "/" + conf.application + ".conf";
render(templatePath(conf, 'upstart-single', 'foreman.conf'), conf, writeout(path));
display.Warn('upstart-single jobs attempt to raise limits and will fail ' +
'to start if the limits cannot be raised to the desired ' +
'levels. Some manual editing may be required.');
}
function upstart_single_app(conf, outdir) {
var path = outdir + "/" + conf.application + "-" + conf.process + ".conf";
render(templatePath(conf, 'upstart-single', 'foreman-APP.conf'), conf, writeout(path));
}
function systemd(conf, outdir){
var path = outdir + "/" + conf.application + ".target";
render(templatePath(conf, 'systemd', 'foreman.target'), conf, writeout(path));
}
function systemd_app(conf, outdir) {
var path = outdir + "/" + conf.application + "-" + conf.process + ".target";
render(templatePath(conf, 'systemd', 'foreman-APP.target'), conf, writeout(path));
}
function systemd_app_n(conf, outdir) {
var path = outdir + "/" + conf.application + "-" + conf.process + "-" + conf.number + ".service";
render(templatePath(conf, 'systemd', 'foreman-APP-N.service'), conf, writeout(path));
}
function supervisord(conf, outdir) {
var path = outdir + "/" + conf.application + ".conf";
var programs = [];
// Supervisord requires comma separated lists and they are
// quite ugly to handle in Moustache.
for(var i = 0; i < conf.processes.length; i++) {
var process = conf.processes[i].process;
var n = conf.processes[i].n;
for(var j = 1; j <= n; j++) {
programs.push(conf.application + "-" + process + "-" + j);
}
}
conf.programs = programs.join(',');
render(templatePath(conf, 'supervisord', 'foreman.conf'), conf, writeout(path));
}
function supervisord_app_n(conf, outdir) {
var path = outdir + "/" + conf.application + "-" + conf.process + "-" + conf.number + ".conf";
var envs = [];
// We have to do the same thing for env variables.
for(var i in conf.envs) {
var key = conf.envs[i].key;
var value = conf.envs[i].value;
// Some variables like 'web.1' breaks supervisor confg so we
// escape quotes and wrap values in quotes.
if(typeof value === 'string') {
value = value.replace(/"/, '\\"');
}
envs.push(key + "=" + '"' + value + '"');
}
conf.envs = envs.join(',');
render(templatePath(conf, 'supervisord', 'foreman-APP-N.conf'), conf, writeout(path));
}
function smf_app(conf, outdir) {
var path = outdir + "/" + conf.application + "-" + conf.process + ".xml";
render(templatePath(conf, 'smf', 'foreman-APP.xml'), conf, writeout(path));
}
var export_formats = {
"upstart": {
foreman : upstart,
foreman_app : upstart_app,
foreman_app_n : upstart_app_n,
},
"upstart-single": {
foreman : upstart_single,
foreman_app : upstart_single_app,
foreman_app_n : function noop() {},
},
"systemd": {
foreman : systemd,
foreman_app : systemd_app,
foreman_app_n : systemd_app_n,
},
"supervisord": {
foreman : supervisord,
foreman_app : function noop() {},
foreman_app_n : supervisord_app_n,
},
"smf": {
foreman : function noop() {},
foreman_app : smf_app,
foreman_app_n : function noop() {},
}
};
module.exports = export_formats;