forked from jhuckaby/pixl-boot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboot.js
More file actions
218 lines (185 loc) · 7.36 KB
/
boot.js
File metadata and controls
218 lines (185 loc) · 7.36 KB
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
// PixlBoot API
// Install service to run on server startup
// Works on Linux (RedHat / Ubuntu) and OS X
// Copyright (c) 2016 Joseph Huckaby and PixlCore.com
var fs = require('fs');
var cp = require('child_process');
module.exports = {
defaults: {
name: "MyService",
company: "Node",
script: "bin/control.sh",
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",
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 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, '');
args.service_file = "/etc/init.d/" + args.service_name;
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 chkconfig nor update-rc.d found.") );
}
else {
// we're on debian
self.install_linux_debian(args, callback);
}
});
}
else {
// we're on redhat
self.install_linux_redhat(args, callback);
}
});
},
install_linux_redhat: function(args, callback) {
// install service on redhat (chkconfig)
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)
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: " + args.debian_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) );
// 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, '');
args.service_file = "/etc/init.d/" + args.service_name;
if (process.platform == 'linux') {
// 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 );
} );
} );
}
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);
},
};