-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.js
318 lines (277 loc) · 10.4 KB
/
service.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
const os = require('os');
const { execSync } = require('child_process');
const { Service, EventLogger } = require('node-windows');
const path = require('path');
const fs = require('fs');
// Load configuration file
const configPath = path.join(__dirname, 'config.json');
// Check if the configuration file exists
if (!fs.existsSync(configPath)) {
console.error('Error: Configuration file not found.');
process.exit(1);
}
// Load configuration from the file
const config = require(configPath);
const platform = os.platform();
const log = new EventLogger(config.serviceName);
// Resolve the script path from configuration
const scriptPath = path.resolve(__dirname, config.scriptPath);
// Check if the script file exists
if (!fs.existsSync(scriptPath)) {
log.error(`Error: Cannot find API script at ${scriptPath}`);
process.exit(1);
}
/**
* Manages services on macOS using launchd.
* @param {string} action - The action to perform (install, uninstall, start, stop, restart).
*/
function manageMacService(action) {
const plistFilePath = `/Library/LaunchDaemons/com.${config.serviceName}.plist`;
// Define the launchd plist configuration
const launchdPlist = `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.${config.serviceName}</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/node</string>
<string>${scriptPath}</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StandardOutPath</key>
<string>/var/log/${config.serviceName}.log</string>
<key>StandardErrorPath</key>
<string>/var/log/${config.serviceName}.error.log</string>
</dict>
</plist>
`;
try {
switch (action) {
case 'install':
fs.writeFileSync(plistFilePath, launchdPlist);
execSync(`sudo launchctl load -w ${plistFilePath}`);
log.info('Service successfully installed and started on macOS.');
break;
case 'uninstall':
execSync(`sudo launchctl unload -w ${plistFilePath}`);
fs.unlinkSync(plistFilePath);
log.info('Service successfully uninstalled on macOS.');
break;
case 'start':
execSync(`sudo launchctl start com.${config.serviceName}`);
log.info('Service started on macOS.');
break;
case 'stop':
execSync(`sudo launchctl stop com.${config.serviceName}`);
log.warn('Service stopped on macOS.');
break;
case 'restart':
execSync(`sudo launchctl stop com.${config.serviceName}`);
execSync(`sudo launchctl start com.${config.serviceName}`);
log.info('Service restarted on macOS.');
break;
default:
log.warn('Usage: node service.js <install|uninstall|start|stop|restart>');
}
} catch (error) {
log.error(`Error managing the service on macOS: ${error.message}`);
}
}
/**
* Manages services on Linux using systemd.
* @param {string} action - The action to perform (install, uninstall, start, stop, restart).
*/
function manageLinuxService(action) {
const serviceName = config.serviceName.toLowerCase();
// Define the systemd service configuration
const systemdService = `
[Unit]
Description=${config.description}
After=network.target
[Service]
ExecStart=/usr/bin/node ${scriptPath}
Restart=on-failure
Environment=NODE_ENV=${config.env.NODE_ENV}
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=${serviceName}
[Install]
WantedBy=multi-user.target`;
const serviceFilePath = `/etc/systemd/system/${serviceName}.service`;
try {
switch (action) {
case 'install':
fs.writeFileSync(serviceFilePath, systemdService);
execSync(`sudo systemctl daemon-reload`);
execSync(`sudo systemctl enable ${serviceName}`);
execSync(`sudo systemctl start ${serviceName}`);
log.info('Service successfully installed and started on Linux.');
break;
case 'uninstall':
execSync(`sudo systemctl stop ${serviceName}`);
execSync(`sudo systemctl disable ${serviceName}`);
fs.unlinkSync(serviceFilePath);
execSync(`sudo systemctl daemon-reload`);
log.info('Service successfully uninstalled on Linux.');
break;
case 'start':
execSync(`sudo systemctl start ${serviceName}`);
log.info('Service started on Linux.');
break;
case 'stop':
execSync(`sudo systemctl stop ${serviceName}`);
log.warn('Service stopped on Linux.');
break;
case 'restart':
execSync(`sudo systemctl restart ${serviceName}`);
log.info('Service restarted on Linux.');
break;
default:
log.warn('Usage: node service.js <install|uninstall|start|stop|restart>');
}
} catch (error) {
log.error(`Error managing the service on Linux: ${error.message}`);
}
}
/**
* Manages services on Windows using node-windows.
* @param {string} action - The action to perform (install, uninstall, start, stop, restart).
*/
function manageWindowsService(action) {
const svc = new Service({
name: config.serviceName,
description: config.description,
script: scriptPath,
nodeOptions: config.nodeOptions,
env: config.env,
restartOnCrash: true,
logpath: path.join(__dirname, config.logPath),
maxRetries: config.retryStrategy.maxRetries,
wait: config.retryStrategy.wait,
grow: config.retryStrategy.grow
});
svc.on('install', () => {
log.info('Service successfully installed on Windows.');
svc.start();
});
svc.on('alreadyinstalled', () => {
log.info('Service is already installed on Windows.');
svc.start();
});
svc.on('start', () => {
log.info('Service started on Windows.');
});
svc.on('stop', () => {
log.warn('Service stopped on Windows.');
});
svc.on('restart', () => {
log.info('Service restarted on Windows.');
});
svc.on('uninstall', () => {
log.info('Service uninstalled on Windows.');
});
svc.on('error', (err) => {
log.error(`Service encountered an error on Windows: ${err}`);
});
switch (action) {
case 'install':
svc.exists((exists) => {
if (!exists) {
log.info('Installing the service on Windows...');
svc.install();
} else {
log.info('Service already installed on Windows.');
}
});
break;
case 'uninstall':
svc.exists((exists) => {
if (exists) {
log.info('Uninstalling the service on Windows...');
svc.uninstall();
} else {
log.info('Service is not installed on Windows.');
}
});
break;
case 'start':
svc.exists((exists) => {
if (exists) {
log.info('Starting the service on Windows...');
svc.start();
} else {
log.error('Service is not installed on Windows.');
}
});
break;
case 'stop':
svc.exists((exists) => {
if (exists) {
log.warn('Stopping the service on Windows...');
svc.stop();
} else {
log.error('Service is not installed on Windows.');
}
});
break;
case 'restart':
svc.exists((exists) => {
if (exists) {
log.info('Restarting the service on Windows...');
svc.restart();
} else {
log.error('Service is not installed on Windows.');
}
});
break;
default:
log.warn('Usage: node service.js <install|uninstall|start|stop|restart>');
}
}
/**
* Checks the status of the service based on the platform.
*/
function checkServiceStatus() {
if (platform === 'win32') {
// Windows: Check service status using `sc query`
try {
const status = execSync(`sc query ${config.serviceName}`).toString();
log.info(`Service status on Windows: ${status}`);
} catch (error) {
log.error(`Error checking service status on Windows: ${error.message}`);
}
} else if (platform === 'linux') {
// Linux: Check service status using `systemctl`
try {
const status = execSync(`systemctl status ${config.serviceName.toLowerCase()}`).toString();
log.info(`Service status on Linux: ${status}`);
} catch (error) {
log.error(`Error checking service status on Linux: ${error.message}`);
}
} else if (platform === 'darwin') {
// macOS: Check service status using `launchctl`
try {
const status = execSync(`launchctl list | grep com.${config.serviceName}`).toString();
log.info(`Service status on macOS: ${status}`);
} catch (error) {
log.error(`Error checking service status on macOS: ${error.message}`);
}
}
}
// Handle command-line arguments
const action = process.argv[2]; // Get the action from command-line arguments (install, uninstall, start, stop, restart, status)
if (action === 'status') {
checkServiceStatus();
} else if (platform === 'win32') {
manageWindowsService(action);
} else if (platform === 'linux') {
manageLinuxService(action);
} else if (platform === 'darwin') {
manageMacService(action);
} else {
log.error('Unsupported platform. This script only works on Windows, Linux, or macOS.');
}