ubuntu-service 1.2.1 → 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 -48
- package/src/cli.js +2 -2
- package/src/index.js +3 -3
- package/src/v2/actions.js +49 -58
- package/src/v2/cli.js +245 -245
- package/src/v2/detector.js +195 -195
- package/src/v2/index.js +11 -55
- package/src/v2/parser.js +113 -113
- package/src/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/actions.js +0 -58
- package/src/v1/cli.js +0 -245
- package/src/v1/detector.js +0 -194
- package/src/v1/index.js +0 -55
- package/src/v1/parser.js +0 -113
- package/src/v1/systemctl.js +0 -147
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
|
-
};
|
package/src/v1/parser.js
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Parses a systemd .service INI file into structured sections.
|
|
6
|
-
* @param {string} filePath - Absolute path to .service file
|
|
7
|
-
* @returns {object} Parsed service configuration
|
|
8
|
-
*/
|
|
9
|
-
export function parseServiceFile(filePath) {
|
|
10
|
-
if (!fs.existsSync(filePath)) {
|
|
11
|
-
return null;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const content = fs.readFileSync(filePath, "utf-8");
|
|
15
|
-
const lines = content.split(/\r?\n/);
|
|
16
|
-
const result = {
|
|
17
|
-
Unit: {},
|
|
18
|
-
Service: {},
|
|
19
|
-
Install: {},
|
|
20
|
-
raw: content
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
let currentSection = null;
|
|
24
|
-
|
|
25
|
-
for (const rawLine of lines) {
|
|
26
|
-
const line = rawLine.trim();
|
|
27
|
-
if (!line || line.startsWith("#") || line.startsWith(";")) {
|
|
28
|
-
continue;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const sectionMatch = line.match(/^\[(.*?)\]$/);
|
|
32
|
-
if (sectionMatch) {
|
|
33
|
-
currentSection = sectionMatch[1];
|
|
34
|
-
if (!result[currentSection]) {
|
|
35
|
-
result[currentSection] = {};
|
|
36
|
-
}
|
|
37
|
-
continue;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const eqIdx = line.indexOf("=");
|
|
41
|
-
if (eqIdx !== -1 && currentSection) {
|
|
42
|
-
const key = line.slice(0, eqIdx).trim();
|
|
43
|
-
let value = line.slice(eqIdx + 1).trim();
|
|
44
|
-
|
|
45
|
-
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
|
|
46
|
-
value = value.slice(1, -1);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (result[currentSection][key] !== undefined) {
|
|
50
|
-
if (Array.isArray(result[currentSection][key])) {
|
|
51
|
-
result[currentSection][key].push(value);
|
|
52
|
-
} else {
|
|
53
|
-
result[currentSection][key] = [result[currentSection][key], value];
|
|
54
|
-
}
|
|
55
|
-
} else {
|
|
56
|
-
result[currentSection][key] = value;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return result;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Extracts port numbers from Environment strings, ExecStart args, or .env files.
|
|
66
|
-
* @param {object} parsedConfig - Output from parseServiceFile
|
|
67
|
-
* @param {string} [workingDir] - Service working directory
|
|
68
|
-
* @returns {number|null} Detected port or null
|
|
69
|
-
*/
|
|
70
|
-
export function detectPort(parsedConfig, workingDir) {
|
|
71
|
-
if (!parsedConfig) return null;
|
|
72
|
-
const service = parsedConfig.Service || {};
|
|
73
|
-
|
|
74
|
-
// 1. Check Environment key(s)
|
|
75
|
-
const envs = service.Environment;
|
|
76
|
-
if (envs) {
|
|
77
|
-
const envList = Array.isArray(envs) ? envs : [envs];
|
|
78
|
-
for (const e of envList) {
|
|
79
|
-
const portMatch = e.match(/(?:PORT|LISTEN_PORT|APP_PORT)\s*=\s*(\d{2,5})/i);
|
|
80
|
-
if (portMatch) {
|
|
81
|
-
return parseInt(portMatch[1], 10);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// 2. Check ExecStart for --port 3000, -p 3000, or port number
|
|
87
|
-
const execStart = service.ExecStart;
|
|
88
|
-
if (execStart) {
|
|
89
|
-
const execMatch = execStart.match(/(?:--port|-p)\s+([0-9]{2,5})/i);
|
|
90
|
-
if (execMatch) {
|
|
91
|
-
return parseInt(execMatch[1], 10);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// 3. Check .env file in WorkingDirectory if it exists
|
|
96
|
-
const dir = workingDir || service.WorkingDirectory;
|
|
97
|
-
if (dir && fs.existsSync(dir)) {
|
|
98
|
-
const envFilePath = path.join(dir, ".env");
|
|
99
|
-
if (fs.existsSync(envFilePath)) {
|
|
100
|
-
try {
|
|
101
|
-
const envContent = fs.readFileSync(envFilePath, "utf-8");
|
|
102
|
-
const m = envContent.match(/(?:PORT|APP_PORT|SERVER_PORT)\s*=\s*(\d{2,5})/i);
|
|
103
|
-
if (m) {
|
|
104
|
-
return parseInt(m[1], 10);
|
|
105
|
-
}
|
|
106
|
-
} catch {
|
|
107
|
-
// ignore unreadable .env
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return null;
|
|
113
|
-
}
|
package/src/v1/systemctl.js
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
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
|
-
}
|