-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbelnetProcessManager.ts
More file actions
147 lines (122 loc) · 4.37 KB
/
belnetProcessManager.ts
File metadata and controls
147 lines (122 loc) · 4.37 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* eslint-disable @typescript-eslint/no-explicit-any */
import util from 'util';
import {
getEventByJobId,
logLineToAppSide,
sendGlobalErrorToAppSide
} from './ipcNode';
import { BelnetLinuxProcessManager } from './belnetProcessManagerLinux';
import {
BelnetSystemDProcessManager,
isSystemD
} from './belnetProcessManagerSystemd';
import { BelnetWindowsProcessManager } from './belnetProcessManagerWindows';
import { IPC_CHANNEL_KEY } from './sharedIpc';
import { exec } from 'child_process';
import { BelnetMacOSProcessManager } from './belnetProcessManagerMacOS';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const execPromisified = util.promisify(exec);
// eslint-disable-next-line @typescript-eslint/no-var-requires
const LINUX = 'linux';
const WIN = 'win32';
const MACOS = 'darwin';
export const invoke = async (
cmd: string,
args: Array<string>
): Promise<string | null> => {
const cmdWithArgs = `${cmd} ${args.join(' ')}`;
console.log('running cmdWithArgs', cmdWithArgs);
try {
const result = await execPromisified(cmdWithArgs);
if (result && (result.stdout || result.stderr)) {
console.info(`Failed to invoke: '${cmdWithArgs}'`);
console.info(`result: `, result);
return result.stdout || result.stderr || null;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
console.info('invoke failed with', e);
const stderr = e.stderr ? e.stderr : '';
const stdout = e.stdout ? e.stdout : '';
const cmd = e.cmd ? `${e.cmd}: ` : '';
logLineToAppSide(`invoke failed with: ${e}`);
return `${cmd}${stdout} ${stderr}`;
}
return null;
};
export interface IBelnetProcessManager {
doStartBelnetProcess: () => Promise<string | null>;
doStopBelnetProcess: () => Promise<string | null>;
// /var/lib/belnet/bootstrap.signed for MacOS
getDefaultBootstrapFileLocation: () => string;
}
let belnetProcessManager: IBelnetProcessManager;
const getBelnetProcessManager = async () => {
logLineToAppSide('Checking the current System OS');
if (belnetProcessManager) {
return belnetProcessManager;
}
if (process.platform === WIN) {
logLineToAppSide('Current system is windows');
belnetProcessManager = new BelnetWindowsProcessManager();
return belnetProcessManager;
}
if (process.platform === MACOS) {
logLineToAppSide('Current system is macos');
belnetProcessManager = new BelnetMacOSProcessManager();
return belnetProcessManager;
}
if (process.platform === LINUX) {
if (await isSystemD()) {
belnetProcessManager = new BelnetSystemDProcessManager();
return belnetProcessManager;
}
logLineToAppSide('Current system is linux but not systemd');
belnetProcessManager = new BelnetLinuxProcessManager();
return belnetProcessManager;
}
logLineToAppSide('Current system is UNSUPPORTED');
throw new Error(
`BelnetProcessManager not implemented for ${process.platform}`
);
};
export const doStartBelnetProcess = async (jobId: string): Promise<void> => {
let result: string | undefined;
if (!process.env.DISABLE_AUTO_START_STOP) {
try {
logLineToAppSide('About to start Belnet process');
const manager = await getBelnetProcessManager();
const startStopResult = await manager.doStartBelnetProcess();
if (startStopResult) {
sendGlobalErrorToAppSide('error-start-stop');
}
} catch (e: any) {
logLineToAppSide(`Belnet process start failed with ${e.message}`);
console.info('doStartBelnetProcess failed with', e);
sendGlobalErrorToAppSide('error-start-stop');
}
} else {
logLineToAppSide(
'ENV "DISABLE_AUTO_START_STOP" is set, not auto starting belnet daemon'
);
}
const event = getEventByJobId(jobId);
event.sender.send(`${IPC_CHANNEL_KEY}-done`, jobId, null, result);
};
/**
* doStopBelnetProcess is only called when exiting the app so there is no point to wait
* for the event return and so no jobId argument required
*/
export const doStopBelnetProcess = async (): Promise<void> => {
try {
logLineToAppSide('About to stop Belnet process');
const manager = await getBelnetProcessManager();
await manager.doStopBelnetProcess();
} catch (e: any) {
logLineToAppSide(`Belnet process stop failed with ${e.message}`);
console.info('doStopBelnetProcess failed with', e);
}
};
export const doGetProcessPid = (): number => {
return 0;
};