forked from Beldex-Coin/belnet-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbelnetProcessManagerSystemd.ts
94 lines (79 loc) · 2.73 KB
/
belnetProcessManagerSystemd.ts
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
/* eslint-disable @typescript-eslint/no-explicit-any */
import { IBelnetProcessManager, invoke } from './belnetProcessManager';
import util from 'util';
import { exec } from 'child_process';
import { logLineToAppSide } from './ipcNode';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const execPromisified = util.promisify(exec);
export const isSystemD = async (): Promise<boolean> => {
try {
logLineToAppSide('Checking for SystemD.');
const { stdout, stderr } = await execPromisified(
'ps --no-headers -o comm 1'
);
if (stdout && stdout.trim() === 'systemd') {
logLineToAppSide('SystemD: The current system is using systemd.');
return true;
}
console.log('isSystemD stderr:', stderr);
logLineToAppSide(`The current system is NOT using systemd: ${stderr}`);
return false;
} catch (e: any) {
logLineToAppSide(`The current system is NOT using systemd: ${e.message}`);
console.error(e); // should contain code (exit code) and signal (that caused the termination).
return false;
}
};
const belnetService = 'belnet.service';
export class BelnetSystemDProcessManager implements IBelnetProcessManager {
async checkForActiveBelnetService(): Promise<boolean> {
let result;
try {
logLineToAppSide('SystemD: checking if belnet is already running');
const cmdWithArgs = `systemctl is-active ${belnetService}`;
result = await execPromisified(cmdWithArgs);
if (result?.stdout?.trim() === 'active') {
logLineToAppSide('SystemD: belnet is already running');
return true;
}
} catch (e: any) {
if (e?.stdout?.trim() === 'inactive') {
logLineToAppSide(
'SystemD: belnet service is not running. About to try to start it'
);
} else {
logLineToAppSide(
`SystemD: checking if belnet is running failed with: ${e}`
);
console.info(e);
}
}
return false;
}
async doStartBelnetProcess(): Promise<string | null> {
const isRunning = await this.checkForActiveBelnetService();
if (isRunning) {
return null;
}
const result = await invoke('systemctl', [
'--no-block',
'start',
belnetService
]);
if (!result) {
logLineToAppSide('SystemD: belnet service started');
}
return result;
}
async doStopBelnetProcess(): Promise<string | null> {
const isRunning = await this.checkForActiveBelnetService();
if (!isRunning) {
return null;
}
logLineToAppSide('SystemD: belnet service stop action called');
return invoke('systemctl', ['--no-block', 'stop', belnetService]);
}
getDefaultBootstrapFileLocation(): string {
throw new Error('getDefaultBootstrapFileLocation TODO for Systemd');
}
}