ubuntu-service 1.0.0 → 3.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -15
- package/README.md +129 -117
- package/bin/index.js +8 -8
- package/package.json +50 -47
- package/src/cli.js +2 -2
- package/src/index.js +3 -3
- package/src/{v1 → v2}/actions.js +49 -58
- package/src/{v1 → v2}/cli.js +245 -245
- package/src/{v1 → v2}/detector.js +195 -194
- package/src/v2/index.js +11 -0
- package/src/{v1 → v2}/parser.js +113 -113
- package/src/{v1 → v2}/systemctl.js +147 -147
- package/src/v3/cli.js +126 -0
- package/src/v3/index.js +5 -0
- package/src/v3/service.js +175 -0
- package/src/v1/index.js +0 -55
|
@@ -1,147 +1,147 @@
|
|
|
1
|
-
import { execSync } from "node:child_process";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Checks if systemctl command is available in the environment.
|
|
5
|
-
*/
|
|
6
|
-
export function isSystemctlAvailable() {
|
|
7
|
-
try {
|
|
8
|
-
execSync("which systemctl", { stdio: "pipe" });
|
|
9
|
-
return true;
|
|
10
|
-
} catch {
|
|
11
|
-
return false;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Queries systemctl for the live status of a service.
|
|
17
|
-
* @param {string} unitName - Name of the service unit (e.g. appointment.keshavsoft.com.service)
|
|
18
|
-
* @returns {object} Status details
|
|
19
|
-
*/
|
|
20
|
-
export function getServiceLiveStatus(unitName) {
|
|
21
|
-
if (!isSystemctlAvailable()) {
|
|
22
|
-
return {
|
|
23
|
-
available: false,
|
|
24
|
-
activeState: "unknown",
|
|
25
|
-
subState: "unknown",
|
|
26
|
-
pid: null,
|
|
27
|
-
memory: null,
|
|
28
|
-
uptime: null,
|
|
29
|
-
raw: "systemctl not available on this platform"
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
try {
|
|
34
|
-
const showOutput = execSync(
|
|
35
|
-
`systemctl show ${unitName} --property=ActiveState,SubState,MainPID,ExecMainStartTimestamp,MemoryCurrent`,
|
|
36
|
-
{ encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] }
|
|
37
|
-
);
|
|
38
|
-
|
|
39
|
-
const props = {};
|
|
40
|
-
for (const line of showOutput.split(/\r?\n/)) {
|
|
41
|
-
const eq = line.indexOf("=");
|
|
42
|
-
if (eq !== -1) {
|
|
43
|
-
props[line.slice(0, eq).trim()] = line.slice(eq + 1).trim();
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const activeState = props.ActiveState || "unknown";
|
|
48
|
-
const subState = props.SubState || "unknown";
|
|
49
|
-
const mainPid = props.MainPID ? parseInt(props.MainPID, 10) : 0;
|
|
50
|
-
const startTime = props.ExecMainStartTimestamp || null;
|
|
51
|
-
|
|
52
|
-
let memoryFormatted = null;
|
|
53
|
-
if (props.MemoryCurrent && props.MemoryCurrent !== "[not set]") {
|
|
54
|
-
const bytes = parseInt(props.MemoryCurrent, 10);
|
|
55
|
-
if (!isNaN(bytes) && bytes > 0) {
|
|
56
|
-
memoryFormatted = (bytes / (1024 * 1024)).toFixed(1) + " MB";
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return {
|
|
61
|
-
available: true,
|
|
62
|
-
activeState,
|
|
63
|
-
subState,
|
|
64
|
-
isActive: activeState === "active",
|
|
65
|
-
pid: mainPid > 0 ? mainPid : null,
|
|
66
|
-
memory: memoryFormatted,
|
|
67
|
-
startTime,
|
|
68
|
-
raw: showOutput
|
|
69
|
-
};
|
|
70
|
-
} catch (err) {
|
|
71
|
-
return {
|
|
72
|
-
available: true,
|
|
73
|
-
activeState: "failed_to_query",
|
|
74
|
-
subState: "error",
|
|
75
|
-
isActive: false,
|
|
76
|
-
pid: null,
|
|
77
|
-
memory: null,
|
|
78
|
-
error: err.message
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Restarts a systemd service unit via systemctl restart.
|
|
85
|
-
* @param {string} unitName - Service unit name
|
|
86
|
-
* @returns {object} Execution result
|
|
87
|
-
*/
|
|
88
|
-
export function restartService(unitName) {
|
|
89
|
-
if (!isSystemctlAvailable()) {
|
|
90
|
-
return {
|
|
91
|
-
success: false,
|
|
92
|
-
error: "systemctl is not available in this environment (must run on Ubuntu Linux with systemd)"
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
try {
|
|
97
|
-
execSync(`systemctl restart ${unitName}`, {
|
|
98
|
-
encoding: "utf-8",
|
|
99
|
-
stdio: ["pipe", "pipe", "pipe"]
|
|
100
|
-
});
|
|
101
|
-
const freshStatus = getServiceLiveStatus(unitName);
|
|
102
|
-
return {
|
|
103
|
-
success: true,
|
|
104
|
-
unitName,
|
|
105
|
-
status: freshStatus
|
|
106
|
-
};
|
|
107
|
-
} catch (err) {
|
|
108
|
-
const msg = err.stderr ? err.stderr.toString() : err.message;
|
|
109
|
-
let hint = null;
|
|
110
|
-
if (msg.includes("Access denied") || msg.includes("interactive authentication")) {
|
|
111
|
-
hint = "Permission denied: try running with sudo (e.g., sudo npx ubuntu-service restart)";
|
|
112
|
-
}
|
|
113
|
-
return {
|
|
114
|
-
success: false,
|
|
115
|
-
unitName,
|
|
116
|
-
error: msg.trim(),
|
|
117
|
-
hint
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Fetches recent systemd journal logs for a service.
|
|
124
|
-
* @param {string} unitName - Service unit name
|
|
125
|
-
* @param {number} [lines=25] - Number of lines to retrieve
|
|
126
|
-
* @returns {object} Logs result
|
|
127
|
-
*/
|
|
128
|
-
export function getServiceLogs(unitName, lines = 25) {
|
|
129
|
-
try {
|
|
130
|
-
const output = execSync(`journalctl -u ${unitName} -n ${lines} --no-pager`, {
|
|
131
|
-
encoding: "utf-8",
|
|
132
|
-
stdio: ["pipe", "pipe", "pipe"]
|
|
133
|
-
});
|
|
134
|
-
return {
|
|
135
|
-
success: true,
|
|
136
|
-
unitName,
|
|
137
|
-
lines,
|
|
138
|
-
output: output.trim()
|
|
139
|
-
};
|
|
140
|
-
} catch (err) {
|
|
141
|
-
return {
|
|
142
|
-
success: false,
|
|
143
|
-
unitName,
|
|
144
|
-
error: err.stderr ? err.stderr.toString().trim() : err.message
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
}
|
|
1
|
+
import { execSync } from "node:child_process";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Checks if systemctl command is available in the environment.
|
|
5
|
+
*/
|
|
6
|
+
export function isSystemctlAvailable() {
|
|
7
|
+
try {
|
|
8
|
+
execSync("which systemctl", { stdio: "pipe" });
|
|
9
|
+
return true;
|
|
10
|
+
} catch {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Queries systemctl for the live status of a service.
|
|
17
|
+
* @param {string} unitName - Name of the service unit (e.g. appointment.keshavsoft.com.service)
|
|
18
|
+
* @returns {object} Status details
|
|
19
|
+
*/
|
|
20
|
+
export function getServiceLiveStatus(unitName) {
|
|
21
|
+
if (!isSystemctlAvailable()) {
|
|
22
|
+
return {
|
|
23
|
+
available: false,
|
|
24
|
+
activeState: "unknown",
|
|
25
|
+
subState: "unknown",
|
|
26
|
+
pid: null,
|
|
27
|
+
memory: null,
|
|
28
|
+
uptime: null,
|
|
29
|
+
raw: "systemctl not available on this platform"
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
const showOutput = execSync(
|
|
35
|
+
`systemctl show ${unitName} --property=ActiveState,SubState,MainPID,ExecMainStartTimestamp,MemoryCurrent`,
|
|
36
|
+
{ encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] }
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
const props = {};
|
|
40
|
+
for (const line of showOutput.split(/\r?\n/)) {
|
|
41
|
+
const eq = line.indexOf("=");
|
|
42
|
+
if (eq !== -1) {
|
|
43
|
+
props[line.slice(0, eq).trim()] = line.slice(eq + 1).trim();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const activeState = props.ActiveState || "unknown";
|
|
48
|
+
const subState = props.SubState || "unknown";
|
|
49
|
+
const mainPid = props.MainPID ? parseInt(props.MainPID, 10) : 0;
|
|
50
|
+
const startTime = props.ExecMainStartTimestamp || null;
|
|
51
|
+
|
|
52
|
+
let memoryFormatted = null;
|
|
53
|
+
if (props.MemoryCurrent && props.MemoryCurrent !== "[not set]") {
|
|
54
|
+
const bytes = parseInt(props.MemoryCurrent, 10);
|
|
55
|
+
if (!isNaN(bytes) && bytes > 0) {
|
|
56
|
+
memoryFormatted = (bytes / (1024 * 1024)).toFixed(1) + " MB";
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
available: true,
|
|
62
|
+
activeState,
|
|
63
|
+
subState,
|
|
64
|
+
isActive: activeState === "active",
|
|
65
|
+
pid: mainPid > 0 ? mainPid : null,
|
|
66
|
+
memory: memoryFormatted,
|
|
67
|
+
startTime,
|
|
68
|
+
raw: showOutput
|
|
69
|
+
};
|
|
70
|
+
} catch (err) {
|
|
71
|
+
return {
|
|
72
|
+
available: true,
|
|
73
|
+
activeState: "failed_to_query",
|
|
74
|
+
subState: "error",
|
|
75
|
+
isActive: false,
|
|
76
|
+
pid: null,
|
|
77
|
+
memory: null,
|
|
78
|
+
error: err.message
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Restarts a systemd service unit via systemctl restart.
|
|
85
|
+
* @param {string} unitName - Service unit name
|
|
86
|
+
* @returns {object} Execution result
|
|
87
|
+
*/
|
|
88
|
+
export function restartService(unitName) {
|
|
89
|
+
if (!isSystemctlAvailable()) {
|
|
90
|
+
return {
|
|
91
|
+
success: false,
|
|
92
|
+
error: "systemctl is not available in this environment (must run on Ubuntu Linux with systemd)"
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
try {
|
|
97
|
+
execSync(`systemctl restart ${unitName}`, {
|
|
98
|
+
encoding: "utf-8",
|
|
99
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
100
|
+
});
|
|
101
|
+
const freshStatus = getServiceLiveStatus(unitName);
|
|
102
|
+
return {
|
|
103
|
+
success: true,
|
|
104
|
+
unitName,
|
|
105
|
+
status: freshStatus
|
|
106
|
+
};
|
|
107
|
+
} catch (err) {
|
|
108
|
+
const msg = err.stderr ? err.stderr.toString() : err.message;
|
|
109
|
+
let hint = null;
|
|
110
|
+
if (msg.includes("Access denied") || msg.includes("interactive authentication")) {
|
|
111
|
+
hint = "Permission denied: try running with sudo (e.g., sudo npx ubuntu-service restart)";
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
success: false,
|
|
115
|
+
unitName,
|
|
116
|
+
error: msg.trim(),
|
|
117
|
+
hint
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Fetches recent systemd journal logs for a service.
|
|
124
|
+
* @param {string} unitName - Service unit name
|
|
125
|
+
* @param {number} [lines=25] - Number of lines to retrieve
|
|
126
|
+
* @returns {object} Logs result
|
|
127
|
+
*/
|
|
128
|
+
export function getServiceLogs(unitName, lines = 25) {
|
|
129
|
+
try {
|
|
130
|
+
const output = execSync(`journalctl -u ${unitName} -n ${lines} --no-pager`, {
|
|
131
|
+
encoding: "utf-8",
|
|
132
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
133
|
+
});
|
|
134
|
+
return {
|
|
135
|
+
success: true,
|
|
136
|
+
unitName,
|
|
137
|
+
lines,
|
|
138
|
+
output: output.trim()
|
|
139
|
+
};
|
|
140
|
+
} catch (err) {
|
|
141
|
+
return {
|
|
142
|
+
success: false,
|
|
143
|
+
unitName,
|
|
144
|
+
error: err.stderr ? err.stderr.toString().trim() : err.message
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
}
|
package/src/v3/cli.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { inspectDomainService } from "./service.js";
|
|
2
|
+
|
|
3
|
+
export function printHelp() {
|
|
4
|
+
console.log(`
|
|
5
|
+
ubuntu-service (v3) — Single-domain systemd service inspector
|
|
6
|
+
|
|
7
|
+
Usage:
|
|
8
|
+
npx ubuntu-service <domain> [options]
|
|
9
|
+
us <domain> [options]
|
|
10
|
+
|
|
11
|
+
Arguments:
|
|
12
|
+
<domain> Required domain or service name (only 1 parameter accepted)
|
|
13
|
+
|
|
14
|
+
Options:
|
|
15
|
+
--systemd-dir <path> Custom systemd unit directory (defaults to /etc/systemd/system)
|
|
16
|
+
--json Output machine-readable JSON
|
|
17
|
+
-h, --help Show this help message
|
|
18
|
+
|
|
19
|
+
Examples:
|
|
20
|
+
npx ubuntu-service appointment.keshavsoft.com
|
|
21
|
+
npx ubuntu-service lab14.keshavsoft.com --json
|
|
22
|
+
`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function formatLiveState(live) {
|
|
26
|
+
if (!live || !live.available) {
|
|
27
|
+
return "⚪ unqueried (systemctl unavailable)";
|
|
28
|
+
}
|
|
29
|
+
if (live.isActive) {
|
|
30
|
+
return `🟢 active (${live.subState || "running"})`;
|
|
31
|
+
}
|
|
32
|
+
if (live.activeState === "failed") {
|
|
33
|
+
return `🔴 failed (${live.subState || "failed"})`;
|
|
34
|
+
}
|
|
35
|
+
return `🟡 ${live.activeState || "inactive"} (${live.subState || "dead"})`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function runCli(argv = process.argv.slice(2)) {
|
|
39
|
+
const isHelp = argv.includes("-h") || argv.includes("--help");
|
|
40
|
+
if (isHelp) {
|
|
41
|
+
printHelp();
|
|
42
|
+
return 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const isJson = argv.includes("--json");
|
|
46
|
+
|
|
47
|
+
let systemdDir = null;
|
|
48
|
+
const sysIdx = argv.indexOf("--systemd-dir");
|
|
49
|
+
if (sysIdx !== -1 && argv[sysIdx + 1]) {
|
|
50
|
+
systemdDir = argv[sysIdx + 1];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const positionalArgs = argv.filter((arg, i) => {
|
|
54
|
+
if (arg.startsWith("-")) return false;
|
|
55
|
+
if (i > 0 && argv[i - 1] === "--systemd-dir") return false;
|
|
56
|
+
return true;
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
if (positionalArgs.length === 0) {
|
|
60
|
+
const msg = "Boss, you need to supply a valid domain name.";
|
|
61
|
+
if (isJson) {
|
|
62
|
+
console.error(JSON.stringify({ error: msg }, null, 2));
|
|
63
|
+
} else {
|
|
64
|
+
console.error(`\n❌ ${msg}`);
|
|
65
|
+
console.error(`Usage: npx ubuntu-service <domain>\n`);
|
|
66
|
+
}
|
|
67
|
+
return 1;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (positionalArgs.length > 1) {
|
|
71
|
+
const msg = `Expects only 1 parameter (<domain>), received ${positionalArgs.length}: ${positionalArgs.join(" ")}`;
|
|
72
|
+
if (isJson) {
|
|
73
|
+
console.error(JSON.stringify({ error: msg }, null, 2));
|
|
74
|
+
} else {
|
|
75
|
+
console.error(`\n❌ ${msg}`);
|
|
76
|
+
console.error(`Usage: npx ubuntu-service <domain>\n`);
|
|
77
|
+
}
|
|
78
|
+
return 1;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const domain = positionalArgs[0];
|
|
82
|
+
const info = inspectDomainService(domain, { systemdDir });
|
|
83
|
+
|
|
84
|
+
if (isJson) {
|
|
85
|
+
console.log(JSON.stringify(info, null, 2));
|
|
86
|
+
return info.found ? 0 : 1;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (!info.found) {
|
|
90
|
+
console.error(`\n===============================================================`);
|
|
91
|
+
console.error(` 🔍 ubuntu-service (v3) — Domain Not Found`);
|
|
92
|
+
console.error(`===============================================================`);
|
|
93
|
+
console.error(` 🌐 Domain: ${info.domain}`);
|
|
94
|
+
console.error(` ❌ Status: ${info.message}`);
|
|
95
|
+
console.error(` 📂 Searched: ${info.searchedDirs.join(", ")}`);
|
|
96
|
+
console.error(`===============================================================\n`);
|
|
97
|
+
return 1;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const live = info.liveStatus || {};
|
|
101
|
+
console.log(`\n===============================================================`);
|
|
102
|
+
console.log(` 🎯 ubuntu-service (v3) — Domain Story: ${info.domain}`);
|
|
103
|
+
console.log(`===============================================================`);
|
|
104
|
+
console.log(` 🎯 Service Unit: ${info.unitName}`);
|
|
105
|
+
console.log(` 📄 Unit File: ${info.filePath}`);
|
|
106
|
+
console.log(` ⚡ State: ${formatLiveState(live)}`);
|
|
107
|
+
if (live.pid) console.log(` 🆔 Main PID: ${live.pid}`);
|
|
108
|
+
if (live.memory) console.log(` 💾 Memory: ${live.memory}`);
|
|
109
|
+
console.log(` 🌐 Port: ${info.port ? `${info.port} (${info.portSource})` : "not detected"}`);
|
|
110
|
+
console.log(` 📁 WorkingDir: ${info.workingDirectory || "-"} ${info.workingDirectory ? (info.workingDirectoryExists ? "✅" : "❌ (missing)") : ""}`);
|
|
111
|
+
if (info.packageInfo) {
|
|
112
|
+
console.log(` 📦 Package: ${info.packageInfo.name || "-"} (${info.packageInfo.version || "-"})`);
|
|
113
|
+
}
|
|
114
|
+
console.log(` 👤 User: ${info.user}`);
|
|
115
|
+
if (info.restart) {
|
|
116
|
+
console.log(` 🔄 Restart: ${info.restart}${info.restartSec ? ` (${info.restartSec})` : ""}`);
|
|
117
|
+
}
|
|
118
|
+
console.log(` 🚀 ExecStart: ${info.execStart || "-"}`);
|
|
119
|
+
if (info.description) console.log(` 📝 Description: ${info.description}`);
|
|
120
|
+
console.log(`---------------------------------------------------------------`);
|
|
121
|
+
console.log(` 📜 Unit Content:`);
|
|
122
|
+
console.log(info.rawContent.trim());
|
|
123
|
+
console.log(`===============================================================\n`);
|
|
124
|
+
|
|
125
|
+
return 0;
|
|
126
|
+
}
|
package/src/v3/index.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { parseServiceFile, detectPort } from "../v2/parser.js";
|
|
4
|
+
import { getServiceLiveStatus } from "../v1/systemctl.js";
|
|
5
|
+
|
|
6
|
+
export const DEFAULT_SYSTEMD_DIRS = [
|
|
7
|
+
"/etc/systemd/system",
|
|
8
|
+
"/run/systemd/system",
|
|
9
|
+
"/usr/lib/systemd/system",
|
|
10
|
+
"/lib/systemd/system"
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
function readPortFromEnvPath(envPath) {
|
|
14
|
+
if (!envPath || typeof envPath !== "string") return null;
|
|
15
|
+
const cleanPath = envPath.replace(/^-/, "").trim();
|
|
16
|
+
if (!cleanPath || !fs.existsSync(cleanPath)) return null;
|
|
17
|
+
try {
|
|
18
|
+
const content = fs.readFileSync(cleanPath, "utf-8");
|
|
19
|
+
const m = content.match(/^\s*(?:PORT|APP_PORT|SERVER_PORT|LISTEN_PORT)\s*=\s*['"]?(\d{2,5})['"]?/m);
|
|
20
|
+
return m ? parseInt(m[1], 10) : null;
|
|
21
|
+
} catch {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function resolvePortWithSource(parsedConfig, workingDir) {
|
|
27
|
+
const service = parsedConfig?.Service || {};
|
|
28
|
+
|
|
29
|
+
const envs = Array.isArray(service.Environment)
|
|
30
|
+
? service.Environment
|
|
31
|
+
: service.Environment ? [service.Environment] : [];
|
|
32
|
+
for (const entry of envs) {
|
|
33
|
+
const m = String(entry).match(/(?:PORT|LISTEN_PORT|APP_PORT)\s*=\s*(\d{2,5})/i);
|
|
34
|
+
if (m) {
|
|
35
|
+
return { port: parseInt(m[1], 10), portSource: "Environment directive" };
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const execStart = Array.isArray(service.ExecStart)
|
|
40
|
+
? service.ExecStart.join(" ")
|
|
41
|
+
: String(service.ExecStart || "");
|
|
42
|
+
const execPort = execStart.match(/(?:PORT\s*=\s*|--port(?:=|\s+)|-p\s+)(\d{2,5})/i);
|
|
43
|
+
if (execPort) {
|
|
44
|
+
return { port: parseInt(execPort[1], 10), portSource: "ExecStart argument" };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const execEnvFile = execStart.match(/--env-file\s*=\s*([^\s"']+)|--env-file\s+([^\s"']+)/i);
|
|
48
|
+
if (execEnvFile) {
|
|
49
|
+
const envPath = execEnvFile[1] || execEnvFile[2];
|
|
50
|
+
const port = readPortFromEnvPath(envPath);
|
|
51
|
+
if (port) {
|
|
52
|
+
return { port, portSource: `ExecStart --env-file (${envPath})` };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const envFiles = Array.isArray(service.EnvironmentFile)
|
|
57
|
+
? service.EnvironmentFile
|
|
58
|
+
: service.EnvironmentFile ? [service.EnvironmentFile] : [];
|
|
59
|
+
for (const rawFile of envFiles) {
|
|
60
|
+
const port = readPortFromEnvPath(rawFile);
|
|
61
|
+
if (port) {
|
|
62
|
+
return { port, portSource: `EnvironmentFile (${rawFile})` };
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const fallbackPort = detectPort(parsedConfig, workingDir);
|
|
67
|
+
if (fallbackPort) {
|
|
68
|
+
return { port: fallbackPort, portSource: "WorkingDirectory/.env" };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return { port: null, portSource: null };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function readPackageInfo(workingDir) {
|
|
75
|
+
if (!workingDir || !fs.existsSync(workingDir)) return null;
|
|
76
|
+
const pkgPath = path.join(workingDir, "package.json");
|
|
77
|
+
if (!fs.existsSync(pkgPath)) return null;
|
|
78
|
+
try {
|
|
79
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
|
|
80
|
+
return {
|
|
81
|
+
name: pkg.name || null,
|
|
82
|
+
version: pkg.version || null
|
|
83
|
+
};
|
|
84
|
+
} catch {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Inspects a single systemd service strictly by the supplied domain name.
|
|
91
|
+
* Performs no automatic directory/cwd detection.
|
|
92
|
+
*
|
|
93
|
+
* @param {string} domain - Domain or unit base name (e.g. "appointment.keshavsoft.com")
|
|
94
|
+
* @param {object} [options] - Optional { systemdDir }
|
|
95
|
+
* @returns {object} Domain service story
|
|
96
|
+
*/
|
|
97
|
+
export function inspectDomainService(domain, options = {}) {
|
|
98
|
+
const clean = String(domain || "").trim().toLowerCase().replace(/\.service$/i, "");
|
|
99
|
+
if (!clean) {
|
|
100
|
+
throw new Error("Boss, you need to supply a valid domain name.");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const dirsToScan = options.systemdDir
|
|
104
|
+
? [path.resolve(options.systemdDir)]
|
|
105
|
+
: DEFAULT_SYSTEMD_DIRS;
|
|
106
|
+
|
|
107
|
+
const candidateNames = clean.includes(".")
|
|
108
|
+
? [`${clean}.service`]
|
|
109
|
+
: [`${clean}.service`, `${clean}.keshavsoft.com.service`];
|
|
110
|
+
|
|
111
|
+
let matchedFile = null;
|
|
112
|
+
let matchedDir = null;
|
|
113
|
+
|
|
114
|
+
for (const dir of dirsToScan) {
|
|
115
|
+
if (!fs.existsSync(dir)) continue;
|
|
116
|
+
for (const candidate of candidateNames) {
|
|
117
|
+
const fullPath = path.join(dir, candidate);
|
|
118
|
+
if (fs.existsSync(fullPath)) {
|
|
119
|
+
matchedFile = fullPath;
|
|
120
|
+
matchedDir = dir;
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (matchedFile) break;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (!matchedFile) {
|
|
128
|
+
return {
|
|
129
|
+
found: false,
|
|
130
|
+
domain: clean,
|
|
131
|
+
candidateUnits: candidateNames,
|
|
132
|
+
searchedDirs: dirsToScan,
|
|
133
|
+
message: `No systemd service found for '${clean}' (checked: ${candidateNames.join(", ")})`
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const unitName = path.basename(matchedFile);
|
|
138
|
+
const parsed = parseServiceFile(matchedFile);
|
|
139
|
+
const unit = parsed?.Unit || {};
|
|
140
|
+
const service = parsed?.Service || {};
|
|
141
|
+
const workingDir = service.WorkingDirectory || null;
|
|
142
|
+
const workingDirectoryExists = workingDir ? fs.existsSync(workingDir) : false;
|
|
143
|
+
const { port, portSource } = resolvePortWithSource(parsed, workingDir);
|
|
144
|
+
const packageInfo = readPackageInfo(workingDir);
|
|
145
|
+
const liveStatus = getServiceLiveStatus(unitName);
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
found: true,
|
|
149
|
+
domain: unitName.replace(/\.service$/i, ""),
|
|
150
|
+
unitName,
|
|
151
|
+
filePath: matchedFile,
|
|
152
|
+
systemdDir: matchedDir,
|
|
153
|
+
description: unit.Description || null,
|
|
154
|
+
after: unit.After || null,
|
|
155
|
+
user: service.User || "root",
|
|
156
|
+
type: service.Type || null,
|
|
157
|
+
restart: service.Restart || null,
|
|
158
|
+
restartSec: service.RestartSec || null,
|
|
159
|
+
workingDirectory: workingDir,
|
|
160
|
+
workingDirectoryExists,
|
|
161
|
+
execStart: service.ExecStart || null,
|
|
162
|
+
environment: service.Environment || null,
|
|
163
|
+
environmentFile: service.EnvironmentFile || null,
|
|
164
|
+
port,
|
|
165
|
+
portSource,
|
|
166
|
+
packageInfo,
|
|
167
|
+
liveStatus,
|
|
168
|
+
unitSections: {
|
|
169
|
+
Unit: unit,
|
|
170
|
+
Service: service,
|
|
171
|
+
Install: parsed?.Install || {}
|
|
172
|
+
},
|
|
173
|
+
rawContent: parsed?.raw || ""
|
|
174
|
+
};
|
|
175
|
+
}
|
package/src/v1/index.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { parseServiceFile, detectPort } from "./parser.js";
|
|
2
|
-
import {
|
|
3
|
-
findServiceForDirectory,
|
|
4
|
-
normalizePath,
|
|
5
|
-
inspectDirectoryMetadata,
|
|
6
|
-
DEFAULT_SYSTEMD_DIRS
|
|
7
|
-
} from "./detector.js";
|
|
8
|
-
import {
|
|
9
|
-
isSystemctlAvailable,
|
|
10
|
-
getServiceLiveStatus,
|
|
11
|
-
restartService,
|
|
12
|
-
getServiceLogs
|
|
13
|
-
} from "./systemctl.js";
|
|
14
|
-
import {
|
|
15
|
-
inspectCurrentService,
|
|
16
|
-
restartCurrentService,
|
|
17
|
-
getCurrentServiceLogs
|
|
18
|
-
} from "./actions.js";
|
|
19
|
-
import { runCli, printHelp } from "./cli.js";
|
|
20
|
-
|
|
21
|
-
export {
|
|
22
|
-
parseServiceFile,
|
|
23
|
-
detectPort,
|
|
24
|
-
findServiceForDirectory,
|
|
25
|
-
normalizePath,
|
|
26
|
-
inspectDirectoryMetadata,
|
|
27
|
-
DEFAULT_SYSTEMD_DIRS,
|
|
28
|
-
isSystemctlAvailable,
|
|
29
|
-
getServiceLiveStatus,
|
|
30
|
-
restartService,
|
|
31
|
-
getServiceLogs,
|
|
32
|
-
inspectCurrentService,
|
|
33
|
-
restartCurrentService,
|
|
34
|
-
getCurrentServiceLogs,
|
|
35
|
-
runCli,
|
|
36
|
-
printHelp
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export default {
|
|
40
|
-
parseServiceFile,
|
|
41
|
-
detectPort,
|
|
42
|
-
findServiceForDirectory,
|
|
43
|
-
normalizePath,
|
|
44
|
-
inspectDirectoryMetadata,
|
|
45
|
-
DEFAULT_SYSTEMD_DIRS,
|
|
46
|
-
isSystemctlAvailable,
|
|
47
|
-
getServiceLiveStatus,
|
|
48
|
-
restartService,
|
|
49
|
-
getServiceLogs,
|
|
50
|
-
inspectCurrentService,
|
|
51
|
-
restartCurrentService,
|
|
52
|
-
getCurrentServiceLogs,
|
|
53
|
-
runCli,
|
|
54
|
-
printHelp
|
|
55
|
-
};
|