-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy pathsystemTools.js
More file actions
116 lines (98 loc) · 3.94 KB
/
systemTools.js
File metadata and controls
116 lines (98 loc) · 3.94 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
import fs from "fs-extra";
import { resolvePath } from "../utils/pathHelper.js";
import { exec } from "child_process";
import util from "util";
import path from "path";
const execPromise = util.promisify(exec);
// Helper for config management
function getNestedValue(obj, path) {
return path.split('.').reduce((acc, part) => acc && acc[part], obj);
}
function setNestedValue(obj, path, value) {
const parts = path.split('.');
const last = parts.pop();
const target = parts.reduce((acc, part) => {
if (!acc[part]) acc[part] = {};
return acc[part];
}, obj);
target[last] = value;
}
export const read_logs = async (args) => {
const linesToRead = args?.lines || 50;
const logDir = resolvePath("logs");
if (!await fs.pathExists(logDir)) {
return { content: [{ type: "text", text: "No logs directory found." }] };
}
// Find latest log file
const files = await fs.readdir(logDir);
// Filter for .log.txt files
const logFiles = files.filter(f => f.endsWith('.log.txt')).sort().reverse();
if (logFiles.length === 0) {
return { content: [{ type: "text", text: "No log files found." }] };
}
const latestLog = path.join(logDir, logFiles[0]);
const content = await fs.readFile(latestLog, "utf-8");
const lines = content.trim().split('\n');
const lastLines = lines.slice(-linesToRead).join('\n');
return {
content: [{
type: "text",
text: `Reading from ${logFiles[0]}:\n\n${lastLines}`
}]
};
};
export const manage_config = async (args) => {
const { action, key, value } = args;
const configPath = resolvePath("config/env.json");
const lockPath = resolvePath("config/env.json.lock");
if (!await fs.pathExists(configPath)) {
// Fallback if config doesn't exist
return { isError: true, content: [{ type: "text", text: "Config file not found." }] };
}
// Check lock
if (await fs.pathExists(lockPath)) {
return { isError: true, content: [{ type: "text", text: "Config is locked by another process." }] };
}
try {
const configContent = await fs.readFile(configPath, 'utf-8');
let config = JSON.parse(configContent);
if (action === 'get') {
if (key) {
const val = getNestedValue(config, key);
return { content: [{ type: "text", text: JSON.stringify(val, null, 2) }] };
} else {
return { content: [{ type: "text", text: JSON.stringify(config, null, 2) }] };
}
} else if (action === 'set') {
if (!key || value === undefined) {
return { isError: true, content: [{ type: "text", text: "Key and value required for set action." }] };
}
// Create lock
await fs.outputFile(lockPath, process.pid.toString());
try {
let parsedValue = value;
try {
parsedValue = JSON.parse(value);
} catch (e) {
// Keep as string
}
setNestedValue(config, key, parsedValue);
await fs.outputFile(configPath, JSON.stringify(config, null, 2));
return { content: [{ type: "text", text: `Successfully set ${key} to ${JSON.stringify(parsedValue)}` }] };
} finally {
// Remove lock
await fs.remove(lockPath);
}
}
} catch (e) {
return { isError: true, content: [{ type: "text", text: `Config Error: ${e.message}` }] };
}
};
export const restart_service = async () => {
try {
await execPromise("pm2 restart drpys");
return { content: [{ type: "text", text: "Service restart command issued." }] };
} catch (e) {
return { isError: true, content: [{ type: "text", text: `Failed to restart service: ${e.message}` }] };
}
};