-
Notifications
You must be signed in to change notification settings - Fork 2
/
boot.js
297 lines (254 loc) · 9.9 KB
/
boot.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
// PixlBoot API
// Install service to run on server startup
// Works on Linux (RedHat / Ubuntu) and OS X
// Copyright (c) 2016 - 2019 Joseph Huckaby and PixlCore.com
// MIT License
var fs = require('fs');
var cp = require('child_process');
module.exports = {
defaults: {
name: "MyService",
company: "Node",
script: "bin/control.sh",
// systemd stuff (modern)
linux_type: "forking",
linux_after: "network.target",
linux_wanted_by: "multi-user.target",
// init.d stuff (legacy)
linux_runlevels: "3,4,5",
redhat_start_priority: "99",
redhat_stop_priority: "01",
debian_requires: "local_fs remote_fs network syslog named",
debian_stoplevels: "0,1,6",
// mac stuff
darwin_type: "agent"
},
install: function(args, callback) {
// install service as a startup item
var uid = process.geteuid ? process.geteuid() : process.getuid();
if (uid != 0) return callback( new Error("Must be root to register a startup service.") );
// merge in default args
for (var key in this.defaults) {
if (!(key in args)) args[key] = this.defaults[key];
}
// get abs path to script
args.script = require('path').resolve( args.script );
// install steps differ based on platform
switch (process.platform) {
case 'linux': this.install_linux(args, callback); break;
case 'darwin': this.install_darwin(args, callback); break;
default:
callback( new Error("Unsupported platform: " + process.platform) );
break;
}
},
install_linux: function(args, callback) {
// install linux systemd or init.d service
// first determine if we're on RedHat (CentOS / Fedora) or Debian (Ubuntu)
var self = this;
args.service_name = args.name.toLowerCase().replace(/\W+/g, '');
cp.exec("which systemctl", function(err, stdout, stderr) {
if (err) {
// oops, try one of the legacy methods
cp.exec("which chkconfig", function(err, stdout, stderr) {
if (err) {
// not redhat, but are we on debian?
cp.exec("which update-rc.d", function(err, stdout, stderr) {
if (err) {
// not a supported linux platform
callback( new Error("Unsupported platform: No systemctl, chkconfig nor update-rc.d found.") );
}
else {
// we're on legacy debian
self.install_linux_debian(args, callback);
}
});
}
else {
// we're on legacy redhat
self.install_linux_redhat(args, callback);
}
});
return;
}
else {
// proceed with modern linux systemd
self.install_linux_systemd(args, callback);
}
});
},
install_linux_systemd: function(args, callback) {
// install service on linux with systemd (systemctl)
args.service_file = "/etc/systemd/system/" + args.service_name + ".service";
var service_type = args.linux_type;
var unit_after = args.linux_after;
var wanted_by = args.linux_wanted_by;
var service_contents = [
"[Unit]",
"Description=" + args.company + " " + args.name,
"After=" + unit_after,
"",
"[Service]",
"Type=" + service_type,
"ExecStart=" + args.script + " start",
"ExecStop=" + args.script + " stop",
"",
"[Install]",
"WantedBy=" + wanted_by
].join("\n") + "\n";
// write init.d config file
fs.writeFile( args.service_file, service_contents, { mode: 0o644 }, function(err) {
if (err) return callback( new Error("Failed to write file: " + args.service_file + ": " + err.message) );
// reload systemd
cp.exec("systemctl daemon-reload", function(err, stdout, stderr) {
if (err) callback( new Error("Failed to activate service: " + args.service_name + ": " + err.message) );
// activate service
cp.exec("systemctl enable " + args.service_name + ".service", function(err, stdout, stderr) {
if (err) callback( new Error("Failed to activate service: " + args.service_name + ": " + err.message) );
// success
callback();
}); // cp.exec
}); // cp.exec
}); // fs.writeFile
},
install_linux_redhat: function(args, callback) {
// install service on redhat (chkconfig)
// (this is legacy, only used if systemd/systemctl is not on system)
args.service_file = "/etc/init.d/" + args.service_name;
var runlevels = args.linux_runlevels.toString().replace(/\D+/g, '');
var rh_start_priority = this.zeroPad(args.redhat_start_priority, 2);
var rh_stop_priority = this.zeroPad(args.redhat_stop_priority, 2);
var init_contents = [
"#!/bin/sh",
"#",
"# init.d script for " + args.name,
"#",
"# chkconfig: " + runlevels + " " + rh_start_priority + " " + rh_stop_priority,
"# description: " + args.company + " " + args.name,
"",
args.script + " $1"
].join("\n") + "\n";
// write init.d config file
fs.writeFile( args.service_file, init_contents, { mode: '755' }, function(err) {
if (err) return callback( new Error("Failed to write file: " + args.service_file + ": " + err.message) );
// activate service
cp.exec("chkconfig " + args.service_name + " on", function(err, stdout, stderr) {
if (err) callback( new Error("Failed to activate service: " + args.service_name + ": " + err.message) );
// success
callback();
}); // cp.exec
}); // fs.writeFile
},
install_linux_debian: function(args, callback) {
// install service on debian (update-rc.d)
// (this is legacy, only used if systemd/systemctl is not on system)
args.service_file = "/etc/init.d/" + args.service_name;
var runlevels = args.linux_runlevels.toString().replace(/\D+/g, '').split('').join(" ");
var deb_stoplevels = args.debian_stoplevels.toString().replace(/\D+/g, '').split('').join(" ");
var deb_requires = args.debian_requires.split(/\W+/).map( function(req) { return '$' + req; } ).join(" ");
var init_contents = [
"#!/bin/sh",
"",
"### BEGIN INIT INFO",
"# Provides: " + args.service_name,
"# Required-Start: " + deb_requires,
"# Required-Stop: " + deb_requires,
"# Default-Start: " + runlevels,
"# Default-Stop: " + deb_stoplevels,
"# X-Interactive: true",
"# Short-Description: Start/stop " + args.company + " " + args.name,
"### END INIT INFO",
"",
args.script + " $1"
].join("\n") + "\n";
// write init.d config file
fs.writeFile( args.service_file, init_contents, { mode: '755' }, function(err) {
if (err) return callback( new Error("Failed to write file: " + args.service_file + ": " + err.message) );
// activate service
cp.exec("update-rc.d " + args.service_name + " defaults", function(err, stdout, stderr) {
if (err) {
callback( new Error("Failed to activate service: " + args.service_name + ": " + err.message) );
} else {
// success
callback();
}
}); // cp.exec
}); // fs.writeFile
},
install_darwin: function(args, callback) {
// install service as Darwin (OS X) agent or daemon
args.service_name = args.name.toLowerCase().replace(/\W+/g, '');
args.company_name = args.company.toLowerCase().replace(/\W+/g, '');
args.plist_file = "/Library/" + (args.darwin_type.match(/agent/i) ? 'LaunchAgents' : 'LaunchDaemons') + "/com." + args.company_name + "." + args.service_name + ".plist";
var plist_contents = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">',
'<plist version="1.0">',
'<dict>',
"\t" + '<key>Label</key>',
"\t" + '<string>com.' + args.company_name + '.' + args.service_name + '</string>',
"\t" + '<key>ProgramArguments</key>',
"\t" + '<array>',
"\t\t" + '<string>' + args.script + '</string>',
"\t\t" + '<string>start</string>',
"\t" + '</array>',
"\t" + '<key>RunAtLoad</key>',
"\t" + '<true/>',
"\t" + '<key>KeepAlive</key>',
"\t" + '<false/>',
'</dict>',
'</plist>'
].join("\n") + "\n";
// write plist config file
fs.writeFile( args.plist_file, plist_contents, { mode: '644' }, function(err) {
if (err) return callback( new Error("Failed to write file: " + args.plist_file + ": " + err.message) );
// must be root/wheel
cp.exec( "chown root:wheel " + args.plist_file, function(err) {
if (err) return callback( new Error("Failed to chmod plist file: " + args.plist_file + ": " + err.message) );
// success
callback();
});
});
},
uninstall: function(args, callback) {
// remove service from startup (brute force)
var uid = process.geteuid ? process.geteuid() : process.getuid();
if (uid != 0) return callback( new Error("Must be root to deregister a startup service.") );
// merge in default args
for (var key in this.defaults) {
if (!(key in args)) args[key] = this.defaults[key];
}
args.service_name = args.name.toLowerCase().replace(/\W+/g, '');
args.company_name = args.company.toLowerCase().replace(/\W+/g, '');
if (process.platform == 'linux') {
// try every which way to remove service
args.service_file = "/etc/systemd/system/" + args.service_name + ".service";
fs.access( args.service_file, function(err) {
if (err) {
// nope, try legacy methods
args.service_file = "/etc/init.d/" + args.service_name;
// chkconfig may or may not work, so ignore error
cp.exec( "chkconfig " + args.service_name + " off", function() {
cp.exec( "rm -f /etc/rc*.d/*" + args.service_name, function() {
fs.unlink( args.service_file, callback );
} );
} );
return;
}
// looks like we have a systemd service
cp.exec( "systemctl disable " + args.service_name + ".service", function() {
fs.unlink( args.service_file, callback );
} );
}); // fs.access
}
else {
// non-linux (darwin)
args.plist_file = "/Library/" + (args.darwin_type.match(/agent/i) ? 'LaunchAgents' : 'LaunchDaemons') + "/com." + args.company_name + "." + args.service_name + ".plist";
fs.unlink( args.plist_file, callback );
}
},
zeroPad: function(value, len) {
// Pad a number with zeroes to achieve a desired total length (max 10)
return ('0000000000' + value).slice(0 - len);
},
};