forked from the1laz/cgateweb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuninstall-service.js
More file actions
executable file
·87 lines (73 loc) · 3.04 KB
/
uninstall-service.js
File metadata and controls
executable file
·87 lines (73 loc) · 3.04 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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const { runCommand, checkRoot } = require('./src/systemUtils');
const SERVICE_NAME = 'cgateweb.service';
const TARGET_SYSTEMD_DIR = '/etc/systemd/system';
const TARGET_SERVICE_FILE = path.join(TARGET_SYSTEMD_DIR, SERVICE_NAME);
function uninstallService() {
console.log('--- cgateweb Systemd Service Uninstaller ---');
checkRoot('uninstall-service.js');
// 1. Check if service file exists
if (!fs.existsSync(TARGET_SERVICE_FILE)) {
console.log(`Service file not found: ${TARGET_SERVICE_FILE}`);
console.log('Service may not be installed or already removed.');
return;
}
// 2. Stop the service if it's running
try {
const status = execSync(`systemctl is-active ${SERVICE_NAME}`, { encoding: 'utf8' }).trim();
if (status === 'active') {
console.log(`Stopping ${SERVICE_NAME} service...`);
if (!runCommand(`systemctl stop ${SERVICE_NAME}`)) {
console.error(`Failed to stop ${SERVICE_NAME}. Continuing with uninstallation...`);
}
} else {
console.log(`Service ${SERVICE_NAME} is not active.`);
}
} catch {
console.log(`Service ${SERVICE_NAME} status check failed (may not exist).`);
}
// 3. Disable the service
try {
const enabled = execSync(`systemctl is-enabled ${SERVICE_NAME}`, { encoding: 'utf8' }).trim();
if (enabled === 'enabled') {
console.log(`Disabling ${SERVICE_NAME} service...`);
if (!runCommand(`systemctl disable ${SERVICE_NAME}`)) {
console.error(`Failed to disable ${SERVICE_NAME}. Continuing with uninstallation...`);
}
} else {
console.log(`Service ${SERVICE_NAME} is not enabled.`);
}
} catch {
console.log(`Service ${SERVICE_NAME} enable status check failed (may not exist).`);
}
// 4. Remove the service file
try {
console.log(`Removing service file: ${TARGET_SERVICE_FILE}`);
fs.unlinkSync(TARGET_SERVICE_FILE);
console.log('Service file removed successfully.');
} catch (error) {
console.error(`Failed to remove service file: ${error.message}`);
process.exit(1);
}
// 5. Reload systemd daemon
if (!runCommand('systemctl daemon-reload')) {
console.error('Failed to reload systemd daemon. Service may still appear in systemctl.');
}
// 6. Reset failed state (if any)
runCommand(`systemctl reset-failed ${SERVICE_NAME}`);
console.log('---');
console.log(`cgateweb service uninstallation completed.`);
console.log(`The service ${SERVICE_NAME} has been stopped, disabled, and removed.`);
console.log('Application files in the installation directory were not removed.');
console.log('---');
}
// Run the uninstallation
if (require.main === module || (require.main && require.main.filename === __filename)) {
uninstallService();
}
module.exports = {
uninstallService
};