@milaboratories/pl-deployments 2.15.17 → 2.15.19

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.
Files changed (104) hide show
  1. package/dist/_virtual/_rolldown/runtime.cjs +29 -0
  2. package/dist/common/os_and_arch.cjs +22 -28
  3. package/dist/common/os_and_arch.cjs.map +1 -1
  4. package/dist/common/os_and_arch.js +23 -26
  5. package/dist/common/os_and_arch.js.map +1 -1
  6. package/dist/common/pl_binary.cjs +35 -33
  7. package/dist/common/pl_binary.cjs.map +1 -1
  8. package/dist/common/pl_binary.d.ts +13 -18
  9. package/dist/common/pl_binary.js +33 -31
  10. package/dist/common/pl_binary.js.map +1 -1
  11. package/dist/common/pl_binary_download.cjs +143 -155
  12. package/dist/common/pl_binary_download.cjs.map +1 -1
  13. package/dist/common/pl_binary_download.d.ts +15 -48
  14. package/dist/common/pl_binary_download.js +138 -133
  15. package/dist/common/pl_binary_download.js.map +1 -1
  16. package/dist/common/pl_version.cjs +5 -6
  17. package/dist/common/pl_version.cjs.map +1 -1
  18. package/dist/common/pl_version.d.ts +4 -1
  19. package/dist/common/pl_version.js +5 -4
  20. package/dist/common/pl_version.js.map +1 -1
  21. package/dist/index.cjs +21 -25
  22. package/dist/index.d.ts +6 -6
  23. package/dist/index.js +7 -6
  24. package/dist/local/pid.cjs +14 -13
  25. package/dist/local/pid.cjs.map +1 -1
  26. package/dist/local/pid.js +11 -11
  27. package/dist/local/pid.js.map +1 -1
  28. package/dist/local/pl.cjs +194 -223
  29. package/dist/local/pl.cjs.map +1 -1
  30. package/dist/local/pl.d.ts +65 -65
  31. package/dist/local/pl.js +190 -202
  32. package/dist/local/pl.js.map +1 -1
  33. package/dist/local/process.cjs +37 -59
  34. package/dist/local/process.cjs.map +1 -1
  35. package/dist/local/process.d.ts +10 -10
  36. package/dist/local/process.js +36 -57
  37. package/dist/local/process.js.map +1 -1
  38. package/dist/local/trace.cjs +14 -17
  39. package/dist/local/trace.cjs.map +1 -1
  40. package/dist/local/trace.d.ts +6 -7
  41. package/dist/local/trace.js +15 -15
  42. package/dist/local/trace.js.map +1 -1
  43. package/dist/package.cjs +12 -0
  44. package/dist/package.cjs.map +1 -0
  45. package/dist/package.js +6 -0
  46. package/dist/package.js.map +1 -0
  47. package/dist/ssh/connection_info.cjs +36 -53
  48. package/dist/ssh/connection_info.cjs.map +1 -1
  49. package/dist/ssh/connection_info.d.ts +691 -713
  50. package/dist/ssh/connection_info.js +35 -51
  51. package/dist/ssh/connection_info.js.map +1 -1
  52. package/dist/ssh/pl.cjs +551 -638
  53. package/dist/ssh/pl.cjs.map +1 -1
  54. package/dist/ssh/pl.d.ts +120 -117
  55. package/dist/ssh/pl.js +548 -636
  56. package/dist/ssh/pl.js.map +1 -1
  57. package/dist/ssh/pl_paths.cjs +22 -24
  58. package/dist/ssh/pl_paths.cjs.map +1 -1
  59. package/dist/ssh/pl_paths.js +21 -19
  60. package/dist/ssh/pl_paths.js.map +1 -1
  61. package/dist/ssh/ssh.cjs +554 -618
  62. package/dist/ssh/ssh.cjs.map +1 -1
  63. package/dist/ssh/ssh.d.ts +139 -136
  64. package/dist/ssh/ssh.js +548 -616
  65. package/dist/ssh/ssh.js.map +1 -1
  66. package/dist/ssh/ssh_errors.cjs +45 -60
  67. package/dist/ssh/ssh_errors.cjs.map +1 -1
  68. package/dist/ssh/ssh_errors.js +45 -58
  69. package/dist/ssh/ssh_errors.js.map +1 -1
  70. package/dist/ssh/supervisord.cjs +50 -68
  71. package/dist/ssh/supervisord.cjs.map +1 -1
  72. package/dist/ssh/supervisord.d.ts +11 -21
  73. package/dist/ssh/supervisord.js +50 -66
  74. package/dist/ssh/supervisord.js.map +1 -1
  75. package/package.json +10 -10
  76. package/dist/common/os_and_arch.d.ts +0 -9
  77. package/dist/common/os_and_arch.d.ts.map +0 -1
  78. package/dist/common/pl_binary.d.ts.map +0 -1
  79. package/dist/common/pl_binary_download.d.ts.map +0 -1
  80. package/dist/common/pl_version.d.ts.map +0 -1
  81. package/dist/index.cjs.map +0 -1
  82. package/dist/index.d.ts.map +0 -1
  83. package/dist/index.js.map +0 -1
  84. package/dist/local/options.d.ts +0 -31
  85. package/dist/local/options.d.ts.map +0 -1
  86. package/dist/local/pid.d.ts +0 -4
  87. package/dist/local/pid.d.ts.map +0 -1
  88. package/dist/local/pl.d.ts.map +0 -1
  89. package/dist/local/process.d.ts.map +0 -1
  90. package/dist/local/trace.d.ts.map +0 -1
  91. package/dist/package.json.cjs +0 -8
  92. package/dist/package.json.cjs.map +0 -1
  93. package/dist/package.json.js +0 -6
  94. package/dist/package.json.js.map +0 -1
  95. package/dist/ssh/__tests__/common-utils.d.ts +0 -12
  96. package/dist/ssh/__tests__/common-utils.d.ts.map +0 -1
  97. package/dist/ssh/connection_info.d.ts.map +0 -1
  98. package/dist/ssh/pl.d.ts.map +0 -1
  99. package/dist/ssh/pl_paths.d.ts +0 -20
  100. package/dist/ssh/pl_paths.d.ts.map +0 -1
  101. package/dist/ssh/ssh.d.ts.map +0 -1
  102. package/dist/ssh/ssh_errors.d.ts +0 -29
  103. package/dist/ssh/ssh_errors.d.ts.map +0 -1
  104. package/dist/ssh/supervisord.d.ts.map +0 -1
package/dist/ssh/pl.cjs CHANGED
@@ -1,650 +1,563 @@
1
- 'use strict';
2
-
3
- var ssh = require('./ssh.cjs');
4
- var tsHelpers = require('@milaboratories/ts-helpers');
5
- var pl_binary_download = require('../common/pl_binary_download.cjs');
6
- var upath = require('upath');
7
- var pl_paths = require('./pl_paths.cjs');
8
- var pl_version = require('../common/pl_version.cjs');
9
- var plHttp = require('@milaboratories/pl-http');
10
- var net = require('node:net');
11
- var plConfig = require('@milaboratories/pl-config');
12
- var supervisord = require('./supervisord.cjs');
13
- var connection_info = require('./connection_info.cjs');
1
+ const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
2
+ const require_pl_binary_download = require('../common/pl_binary_download.cjs');
3
+ const require_pl_version = require('../common/pl_version.cjs');
4
+ const require_ssh = require('./ssh.cjs');
5
+ const require_pl_paths = require('./pl_paths.cjs');
6
+ const require_supervisord = require('./supervisord.cjs');
7
+ const require_connection_info = require('./connection_info.cjs');
8
+ let _milaboratories_ts_helpers = require("@milaboratories/ts-helpers");
9
+ let upath = require("upath");
10
+ upath = require_runtime.__toESM(upath);
11
+ let _milaboratories_pl_http = require("@milaboratories/pl-http");
12
+ let node_net = require("node:net");
13
+ node_net = require_runtime.__toESM(node_net);
14
+ let _milaboratories_pl_config = require("@milaboratories/pl-config");
14
15
 
16
+ //#region src/ssh/pl.ts
15
17
  const minRequiredGlibcVersion = 2.28;
16
- class SshPl {
17
- logger;
18
- sshClient;
19
- username;
20
- initState = { step: "init" };
21
- constructor(logger, sshClient, username) {
22
- this.logger = logger;
23
- this.sshClient = sshClient;
24
- this.username = username;
25
- }
26
- info() {
27
- return {
28
- username: this.username,
29
- initState: this.initState,
30
- };
31
- }
32
- static async init(logger, config) {
33
- try {
34
- const sshClient = await ssh.SshClient.init(logger, config);
35
- return new SshPl(logger, sshClient, tsHelpers.notEmpty(config.username));
36
- }
37
- catch (e) {
38
- logger.error(`Connection error in SshClient.init: ${e}`);
39
- throw e;
40
- }
41
- }
42
- cleanUp() {
43
- this.sshClient.close();
44
- }
45
- /** Provides an info if the platforma and minio are running along with the debug info. */
46
- async isAlive() {
47
- const arch = await this.getArch();
48
- const remoteHome = await this.getUserHomeDirectory();
49
- return await supervisord.supervisorStatus(this.logger, this.sshClient, remoteHome, arch.arch);
50
- }
51
- /** Starts all the services on the server.
52
- * Idempotent semantic: we could call it several times. */
53
- async start(shouldUseMinio) {
54
- const arch = await this.getArch();
55
- const remoteHome = await this.getUserHomeDirectory();
56
- try {
57
- if (!supervisord.isAllAlive(await this.isAlive(), shouldUseMinio)) {
58
- await supervisord.supervisorCtlStart(this.sshClient, remoteHome, arch.arch);
59
- // We are waiting for Platforma to run to ensure that it has started.
60
- return await this.checkIsAliveWithInterval(shouldUseMinio);
61
- }
62
- }
63
- catch (e) {
64
- let msg = `SshPl.start: ${e}`;
65
- let logs = "";
66
- try {
67
- logs = await this.sshClient.readFile(pl_paths.platformaCliLogs(remoteHome));
68
- msg += `, platforma cli logs: ${logs}`;
69
- }
70
- catch (e) {
71
- msg += `, Can not read platforma cli logs: ${e}`;
72
- }
73
- this.logger.error(msg);
74
- throw new Error(msg);
75
- }
76
- }
77
- /** Stops all the services on the server.
78
- * Idempotent semantic: we could call it several times. */
79
- async stop() {
80
- const arch = await this.getArch();
81
- const remoteHome = await this.getUserHomeDirectory();
82
- try {
83
- const alive = await this.isAlive();
84
- if (supervisord.isSupervisordRunning(alive)) {
85
- await supervisord.supervisorStop(this.sshClient, remoteHome, arch.arch);
86
- // Check if Minio is running by looking at the alive status
87
- const shouldUseMinio = alive.minio === true;
88
- return await this.checkIsAliveWithInterval(shouldUseMinio, 1000, 15, false);
89
- }
90
- }
91
- catch (e) {
92
- const msg = `PlSsh.stop: ${e}`;
93
- this.logger.error(msg);
94
- throw new Error(msg);
95
- }
96
- }
97
- /** Stops the services, deletes a directory with the state and closes SSH connection. */
98
- async reset() {
99
- await this.stopAndClean();
100
- this.cleanUp();
101
- return true;
102
- }
103
- /** Stops platforma and deletes its state. */
104
- async stopAndClean() {
105
- const remoteHome = await this.getUserHomeDirectory();
106
- this.logger.info(`pl.reset: Stop Platforma on the server`);
107
- await this.stop();
108
- this.logger.info(`pl.reset: Deleting Platforma workDir ${pl_paths.workDir(remoteHome)} on the server`);
109
- await this.sshClient.deleteFolder(pl_paths.workDir(remoteHome));
110
- }
111
- /** Downloads binaries and untar them on the server,
112
- * generates all the configs, creates necessary dirs,
113
- * and finally starts all the services. */
114
- async platformaInit(options) {
115
- const state = { localWorkdir: options.localWorkdir, step: "init" };
116
- const { onProgress } = options;
117
- // merge options with default ops.
118
- const ops = {
119
- ...defaultSshPlConfig,
120
- ...options,
121
- };
122
- state.plBinaryOps = ops.plBinary;
123
- try {
124
- await this.doStepDetectArch(state, onProgress);
125
- await this.doStepDetectHome(state, onProgress);
126
- const needRestartPlatforma = await this.doStepReadExistedConfig(state, ops, onProgress);
127
- if (!needRestartPlatforma) {
128
- await onProgress?.("Platforma is already running. Skipping initialization.");
129
- return state.existedSettings;
130
- }
131
- await this.doStepStopExistedPlatforma(state, onProgress);
132
- await this.doStepCheckDbLock(state, onProgress);
133
- await onProgress?.("Installation platforma...");
134
- await this.doStepDownloadBinaries(state, onProgress, ops);
135
- await this.doStepFetchPorts(state);
136
- await this.doStepGenerateNewConfig(state, onProgress, ops);
137
- await this.doStepCreateFoldersAndSaveFiles(state, onProgress);
138
- await this.doStepConfigureSupervisord(state, onProgress);
139
- await this.doStepSaveNewConnectionInfo(state, onProgress, ops);
140
- await this.doStepStartPlatforma(state, onProgress);
141
- return state.connectionInfo;
142
- }
143
- catch (e) {
144
- const msg = `SshPl.platformaInit: ${e}, state: ${JSON.stringify(this.removeSensitiveData(state))}`;
145
- this.logger.error(msg);
146
- throw new Error(msg);
147
- }
148
- }
149
- async doStepStopExistedPlatforma(state, onProgress) {
150
- state.step = "stopExistedPlatforma";
151
- if (!supervisord.isAllAlive(state.alive, state.shouldUseMinio ?? false)) {
152
- return;
153
- }
154
- await onProgress?.("Stopping services...");
155
- await this.stop();
156
- }
157
- removeSensitiveData(state) {
158
- const stateCopy = { ...state };
159
- stateCopy.generatedConfig = {
160
- ...stateCopy.generatedConfig,
161
- filesToCreate: { skipped: "sanitized" },
162
- };
163
- return stateCopy;
164
- }
165
- async doStepStartPlatforma(state, onProgress) {
166
- state.step = "startPlatforma";
167
- await onProgress?.("Starting Platforma on the server...");
168
- await this.start(state.shouldUseMinio ?? false);
169
- state.started = true;
170
- this.initState = state;
171
- await onProgress?.("Platforma has been started successfully.");
172
- }
173
- async doStepSaveNewConnectionInfo(state, onProgress, ops) {
174
- state.step = "saveNewConnectionInfo";
175
- const config = state.generatedConfig;
176
- await onProgress?.("Saving connection information...");
177
- state.connectionInfo = connection_info.newConnectionInfo(config.plUser, config.plPassword, state.ports, tsHelpers.notEmpty(ops.useGlobalAccess), ops.plBinary.version, state.shouldUseMinio ?? false);
178
- await this.sshClient.writeFileOnTheServer(pl_paths.connectionInfo(state.remoteHome), connection_info.stringifyConnectionInfo(state.connectionInfo));
179
- await onProgress?.("Connection information saved.");
180
- }
181
- async doStepCheckDbLock(state, onProgress) {
182
- const removeLockFile = async (lockFilePath) => {
183
- try {
184
- await this.sshClient.exec(`rm -f ${lockFilePath}`);
185
- this.logger.info(`Removed stale lock file ${lockFilePath}`);
186
- }
187
- catch (e) {
188
- const msg = `Failed to remove stale lock file ${lockFilePath}: ${e}`;
189
- this.logger.error(msg);
190
- throw new Error(msg);
191
- }
192
- };
193
- state.step = "checkDbLock";
194
- await onProgress?.("Checking for DB lock...");
195
- const lockFilePath = pl_paths.platformaDbLock(state.remoteHome);
196
- const lockFileExists = await this.sshClient.checkFileExists(lockFilePath);
197
- if (!lockFileExists) {
198
- await onProgress?.("No DB lock found. Proceeding...");
199
- return;
200
- }
201
- this.logger.info(`DB lock file found at ${lockFilePath}. Checking which process holds it...`);
202
- const lockProcessInfo = await this.findLockHolder(lockFilePath);
203
- if (!lockProcessInfo) {
204
- this.logger.warn("Lock file exists but no process is holding it. Removing stale lock file...");
205
- await removeLockFile(lockFilePath);
206
- return;
207
- }
208
- this.logger.info(`Found process ${lockProcessInfo.pid} (user: ${lockProcessInfo.user}) holding DB lock`);
209
- if (lockProcessInfo.user !== this.username) {
210
- const msg = `DB lock is held by process ${lockProcessInfo.pid} ` +
211
- `owned by user '${lockProcessInfo.user}', but current user is '${this.username}'. ` +
212
- "Cannot kill process owned by different user.";
213
- this.logger.error(msg);
214
- throw new Error(msg);
215
- }
216
- this.logger.info(`Process ${lockProcessInfo.pid} belongs to current user ${this.username}. Killing it...`);
217
- await this.killRemoteProcess(lockProcessInfo.pid);
218
- this.logger.info("Process holding DB lock has been terminated.");
219
- // Verify lock file is gone or can be removed
220
- const lockStillExists = await this.sshClient.checkFileExists(lockFilePath);
221
- if (lockStillExists) {
222
- await removeLockFile(lockFilePath);
223
- }
224
- }
225
- async doStepConfigureSupervisord(state, onProgress) {
226
- await onProgress?.("Writing supervisord configuration...");
227
- state.step = "configureSupervisord";
228
- const config = state.generatedConfig;
229
- let supervisorConfig;
230
- if (state.shouldUseMinio) {
231
- supervisorConfig = supervisord.generateSupervisordConfigWithMinio(config.minioConfig.storageDir, config.minioConfig.envs, await this.getFreePortForPlatformaOnServer(state.remoteHome, state.arch), config.workingDir, config.plConfig.configPath, state.binPaths.minioRelPath, state.binPaths.downloadedPl);
232
- }
233
- else {
234
- supervisorConfig = supervisord.generateSupervisordConfig(await this.getFreePortForPlatformaOnServer(state.remoteHome, state.arch), config.workingDir, config.plConfig.configPath, state.binPaths.downloadedPl);
235
- }
236
- const writeResult = await this.sshClient.writeFileOnTheServer(pl_paths.supervisorConf(state.remoteHome), supervisorConfig);
237
- if (!writeResult) {
238
- throw new Error(`Can not write supervisord config on the server ${pl_paths.workDir(state.remoteHome)}`);
239
- }
240
- await onProgress?.("Supervisord configuration written.");
241
- }
242
- async doStepCreateFoldersAndSaveFiles(state, onProgress) {
243
- state.step = "createFoldersAndSaveFiles";
244
- const config = state.generatedConfig;
245
- await onProgress?.("Generating folder structure...");
246
- for (const [filePath, content] of Object.entries(config.filesToCreate)) {
247
- await this.sshClient.writeFileOnTheServer(filePath, content);
248
- this.logger.info(`Created file ${filePath}`);
249
- }
250
- for (const dir of config.dirsToCreate) {
251
- await this.sshClient.ensureRemoteDirCreated(dir);
252
- this.logger.info(`Created directory ${dir}`);
253
- }
254
- await onProgress?.("Folder structure created.");
255
- }
256
- async doStepGenerateNewConfig(state, onProgress, ops) {
257
- state.step = "generateNewConfig";
258
- await onProgress?.("Generating new config...");
259
- const config = await plConfig.generateSshPlConfigs({
260
- logger: this.logger,
261
- workingDir: pl_paths.workDir(state.remoteHome),
262
- portsMode: {
263
- type: "customWithMinio",
264
- ports: {
265
- debug: state.ports.debug.remote,
266
- grpc: state.ports.grpc.remote,
267
- http: state.ports.http.remote,
268
- minio: state.ports.minioPort.remote,
269
- minioConsole: state.ports.minioConsolePort.remote,
270
- monitoring: state.ports.monitoring.remote,
271
- httpLocal: state.ports.http.local,
272
- grpcLocal: state.ports.grpc.local,
273
- minioLocal: state.ports.minioPort.local,
274
- },
275
- },
276
- licenseMode: ops.license,
277
- useGlobalAccess: tsHelpers.notEmpty(ops.useGlobalAccess),
278
- plConfigPostprocessing: ops.plConfigPostprocessing,
279
- useMinio: state.shouldUseMinio ?? false,
280
- });
281
- state.generatedConfig = { ...config };
282
- await onProgress?.("New config generated");
283
- }
284
- async doStepFetchPorts(state) {
285
- state.step = "fetchPorts";
286
- state.ports = await this.fetchPorts(state.remoteHome, state.arch);
287
- if (!state.ports.debug.remote ||
288
- !state.ports.grpc.remote ||
289
- !state.ports.minioPort.remote ||
290
- !state.ports.minioConsolePort.remote ||
291
- !state.ports.monitoring.remote ||
292
- !state.ports.http?.remote) {
293
- throw new Error(`SshPl.platformaInit: remote ports are not defined`);
294
- }
295
- }
296
- async doStepDownloadBinaries(state, onProgress, ops) {
297
- state.step = "downloadBinaries";
298
- await onProgress?.("Downloading and uploading required binaries...");
299
- const glibcVersion = await getGlibcVersion(this.logger, this.sshClient);
300
- if (glibcVersion < minRequiredGlibcVersion)
301
- throw new Error(`glibc version ${glibcVersion} is too old. Version ${minRequiredGlibcVersion} or higher is required for Platforma.`);
302
- const downloadRes = await this.downloadBinariesAndUploadToTheServer(ops.localWorkdir, ops.plBinary, state.remoteHome, state.arch, state.shouldUseMinio ?? false, ops.proxy);
303
- await onProgress?.("All required binaries have been downloaded and uploaded.");
304
- state.binPaths = { ...downloadRes, history: undefined };
305
- state.downloadedBinaries = downloadRes.history;
306
- }
307
- async doStepDetectArch(state, onProgress) {
308
- state.step = "detectArch";
309
- await onProgress?.("Detecting server architecture...");
310
- state.arch = await this.getArch();
311
- await onProgress?.("Server architecture detected.");
312
- }
313
- async doStepDetectHome(state, onProgress) {
314
- state.step = "detectHome";
315
- await onProgress?.("Fetching user home directory...");
316
- state.remoteHome = await this.getUserHomeDirectory();
317
- await onProgress?.("User home directory retrieved.");
318
- }
319
- async doStepReadExistedConfig(state, ops, onProgress) {
320
- state.step = "checkAlive";
321
- await onProgress?.("Checking platform status...");
322
- state.alive = await this.isAlive();
323
- if (!state.alive?.platforma) {
324
- return true;
325
- }
326
- await onProgress?.("All required services are running.");
327
- state.existedSettings = await this.readExistedConfig(state.remoteHome);
328
- if (!state.existedSettings) {
329
- throw new Error(`SshPl.platformaInit: platforma is alive but existed settings are not found`);
330
- }
331
- const sameGA = state.existedSettings.useGlobalAccess == ops.useGlobalAccess;
332
- const samePlVersion = state.existedSettings.plVersion == ops.plBinary.version;
333
- state.needRestart = !(sameGA && samePlVersion);
334
- this.logger.info(`SshPl.platformaInit: need restart? ${state.needRestart}`);
335
- state.shouldUseMinio = state.existedSettings.minioIsUsed;
336
- if (state.shouldUseMinio) {
337
- this.logger.info(`SshPl.platformaInit: minio is used`);
338
- }
339
- else {
340
- this.logger.info(`SshPl.platformaInit: minio is not used`);
341
- }
342
- if (!state.needRestart) {
343
- await onProgress?.("Server setup completed.");
344
- return false;
345
- }
346
- await onProgress?.("Stopping services...");
347
- await this.stop();
348
- return true;
349
- }
350
- async downloadBinariesAndUploadToTheServer(localWorkdir, plBinary, remoteHome, arch, shouldUseMinio, proxy) {
351
- const state = [];
352
- const dispatcher = plHttp.defaultHttpDispatcher(proxy);
353
- try {
354
- const pl = await this.downloadAndUntar(localWorkdir, remoteHome, arch, "pl", `pl-${plBinary.version}`, dispatcher);
355
- state.push(pl);
356
- const supervisor = await this.downloadAndUntar(localWorkdir, remoteHome, arch, "supervisord", pl_paths.supervisordDirName, dispatcher);
357
- state.push(supervisor);
358
- const minioPath = pl_paths.minioBin(remoteHome, arch.arch);
359
- if (shouldUseMinio) {
360
- const minio = await this.downloadAndUntar(localWorkdir, remoteHome, arch, "minio", pl_paths.minioDirName, dispatcher);
361
- state.push(minio);
362
- await this.sshClient.chmod(minioPath, 0o750);
363
- }
364
- return {
365
- history: state,
366
- minioRelPath: shouldUseMinio ? minioPath : undefined,
367
- downloadedPl: pl_paths.platformaBin(remoteHome, arch.arch),
368
- };
369
- }
370
- catch (e) {
371
- const msg = `SshPl.downloadBinariesAndUploadToServer: ${e}, state: ${JSON.stringify(state)}`;
372
- this.logger.error(msg);
373
- throw e;
374
- }
375
- finally {
376
- await dispatcher.close();
377
- }
378
- }
379
- async findLockHolderWithLsof(lockFilePath) {
380
- try {
381
- const { stdout } = await this.sshClient.exec(`lsof ${lockFilePath} 2>/dev/null || true`);
382
- const output = stdout.trim();
383
- if (!output) {
384
- return null;
385
- }
386
- // Example:
387
- // COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
388
- // platforma 11628 rfiskov 10u REG 1,16 0 66670038 ./LOCK
389
- const lines = output.split("\n");
390
- if (lines.length <= 1) {
391
- return null;
392
- }
393
- const parts = lines[1].trim().split(/\s+/);
394
- if (parts.length < 3) {
395
- return null;
396
- }
397
- const pid = Number.parseInt(parts[1], 10);
398
- const user = parts[2];
399
- return Number.isNaN(pid) || !user ? null : { pid, user };
400
- }
401
- catch (e) {
402
- this.logger.warn(`Failed to use lsof to check lock: ${e}`);
403
- return null;
404
- }
405
- }
406
- async findLockHolderWithFuser(lockFilePath) {
407
- try {
408
- const { stdout } = await this.sshClient.exec(`fuser ${lockFilePath} 2>/dev/null || true`);
409
- const output = stdout.trim();
410
- if (!output) {
411
- return null;
412
- }
413
- // Example: ./LOCK: 11628
414
- const match = output.match(/: (\d+)/);
415
- if (!match) {
416
- return null;
417
- }
418
- const pid = Number.parseInt(match[1], 10);
419
- if (Number.isNaN(pid)) {
420
- return null;
421
- }
422
- try {
423
- const psResult = await this.sshClient.exec(`ps -o user= -p ${pid} 2>/dev/null || true`);
424
- const user = psResult.stdout.trim();
425
- return user ? { pid, user } : null;
426
- }
427
- catch (e) {
428
- this.logger.warn(`Failed to get user for PID ${pid}: ${e}`);
429
- return null;
430
- }
431
- }
432
- catch (e) {
433
- this.logger.warn(`Failed to use fuser to check lock: ${e}`);
434
- return null;
435
- }
436
- }
437
- async findLockHolder(lockFilePath) {
438
- const viaLsof = await this.findLockHolderWithLsof(lockFilePath);
439
- if (viaLsof) {
440
- return viaLsof;
441
- }
442
- return this.findLockHolderWithFuser(lockFilePath);
443
- }
444
- async killRemoteProcess(pid) {
445
- this.logger.info(`Killing process ${pid}...`);
446
- try {
447
- // Try graceful termination first
448
- await this.sshClient.exec(`kill ${pid} 2>/dev/null || true`);
449
- await tsHelpers.sleep(1000);
450
- // Check if process still exists
451
- try {
452
- await this.sshClient.exec(`kill -0 ${pid} 2>/dev/null`);
453
- // Process still exists, force kill
454
- this.logger.warn(`Process ${pid} still alive after SIGTERM, forcing kill...`);
455
- await this.sshClient.exec(`kill -9 ${pid} 2>/dev/null || true`);
456
- await tsHelpers.sleep(500);
457
- }
458
- catch {
459
- // Process is dead, nothing to do
460
- }
461
- }
462
- catch (e) {
463
- const msg = `Failed to kill process ${pid}: ${e}`;
464
- this.logger.error(msg);
465
- throw new Error(msg);
466
- }
467
- }
468
- /** We have to extract pl in the remote server,
469
- * because Windows doesn't support symlinks
470
- * that are found in Linux pl binaries tgz archive.
471
- * For this reason, we extract all to the remote server.
472
- * It requires `tar` to be installed on the server
473
- * (it's not installed for Rocky Linux for example). */
474
- async downloadAndUntar(localWorkdir, remoteHome, arch, softwareName, tgzName, dispatcher) {
475
- const state = {};
476
- state.binBasePath = pl_paths.binariesDir(remoteHome);
477
- await this.sshClient.ensureRemoteDirCreated(state.binBasePath);
478
- state.binBasePathCreated = true;
479
- let downloadBinaryResult = null;
480
- const attempts = 5;
481
- for (let i = 1; i <= attempts; i++) {
482
- try {
483
- downloadBinaryResult = await pl_binary_download.downloadBinaryNoExtract({
484
- logger: this.logger,
485
- baseDir: localWorkdir,
486
- softwareName,
487
- tgzName,
488
- arch: arch.arch,
489
- platform: arch.platform,
490
- dispatcher,
491
- });
492
- break;
493
- }
494
- catch (e) {
495
- await tsHelpers.sleep(300);
496
- if (i == attempts) {
497
- throw new Error(`downloadAndUntar: ${attempts} attempts, last error: ${e}`);
498
- }
499
- }
500
- }
501
- state.downloadResult = tsHelpers.notEmpty(downloadBinaryResult);
502
- state.localArchivePath = upath.resolve(state.downloadResult.archivePath);
503
- state.remoteDir = upath.join(state.binBasePath, state.downloadResult.baseName);
504
- state.remoteArchivePath = state.remoteDir + ".tgz";
505
- await this.sshClient.ensureRemoteDirCreated(state.remoteDir);
506
- await this.sshClient.uploadFile(state.localArchivePath, state.remoteArchivePath);
507
- state.uploadDone = true;
508
- try {
509
- await this.sshClient.exec("hash tar");
510
- }
511
- catch {
512
- throw new Error(`tar is not installed on the server. Please install it before running Platforma.`);
513
- }
514
- // TODO: Create a proper archive to avoid xattr warnings
515
- const untarResult = await this.sshClient.exec(`tar --warning=no-all -xvf ${state.remoteArchivePath} --directory=${state.remoteDir}`);
516
- if (untarResult.stderr)
517
- throw new Error(`downloadAndUntar: untar: stderr occurred: ${untarResult.stderr}, stdout: ${untarResult.stdout}`);
518
- state.untarDone = true;
519
- return state;
520
- }
521
- async needDownload(remoteHome, arch) {
522
- const checkPathSupervisor = pl_paths.supervisorBin(remoteHome, arch.arch);
523
- const checkPathMinio = pl_paths.minioDir(remoteHome, arch.arch);
524
- const checkPathPlatforma = pl_paths.platformaBin(remoteHome, arch.arch);
525
- if (!(await this.sshClient.checkFileExists(checkPathPlatforma)) ||
526
- !(await this.sshClient.checkFileExists(checkPathMinio)) ||
527
- !(await this.sshClient.checkFileExists(checkPathSupervisor))) {
528
- return true;
529
- }
530
- return false;
531
- }
532
- async checkIsAliveWithInterval(shouldUseMinio, interval = 1000, count = 15, shouldStart = true) {
533
- const maxMs = count * interval;
534
- let total = 0;
535
- let alive = await this.isAlive();
536
- while (shouldStart ? !supervisord.isAllAlive(alive, shouldUseMinio) : supervisord.isAllAlive(alive, shouldUseMinio)) {
537
- await tsHelpers.sleep(interval);
538
- total += interval;
539
- if (total > maxMs) {
540
- throw new Error(`isAliveWithInterval: The process did not ${shouldStart ? "started" : "stopped"} after ${maxMs} ms. Live status: ${JSON.stringify(alive)}`);
541
- }
542
- alive = await this.isAlive();
543
- }
544
- }
545
- async readExistedConfig(remoteHome) {
546
- const connectionInfo = await this.sshClient.readFile(pl_paths.connectionInfo(remoteHome));
547
- return connection_info.parseConnectionInfo(connectionInfo);
548
- }
549
- async fetchPorts(remoteHome, arch) {
550
- const ports = {
551
- grpc: {
552
- local: await plConfig.getFreePort(),
553
- remote: await this.getFreePortForPlatformaOnServer(remoteHome, arch),
554
- },
555
- monitoring: {
556
- local: await plConfig.getFreePort(),
557
- remote: await this.getFreePortForPlatformaOnServer(remoteHome, arch),
558
- },
559
- debug: {
560
- local: await plConfig.getFreePort(),
561
- remote: await this.getFreePortForPlatformaOnServer(remoteHome, arch),
562
- },
563
- http: {
564
- local: await plConfig.getFreePort(),
565
- remote: await this.getFreePortForPlatformaOnServer(remoteHome, arch),
566
- },
567
- minioPort: {
568
- local: await plConfig.getFreePort(),
569
- remote: await this.getFreePortForPlatformaOnServer(remoteHome, arch),
570
- },
571
- minioConsolePort: {
572
- local: await plConfig.getFreePort(),
573
- remote: await this.getFreePortForPlatformaOnServer(remoteHome, arch),
574
- },
575
- };
576
- return ports;
577
- }
578
- async getLocalFreePort() {
579
- return new Promise((res) => {
580
- const srv = net.createServer();
581
- srv.listen(0, () => {
582
- const port = srv.address().port;
583
- srv.close((_) => res(port));
584
- });
585
- });
586
- }
587
- async getFreePortForPlatformaOnServer(remoteHome, arch) {
588
- const freePortBin = pl_paths.platformaFreePortBin(remoteHome, arch.arch);
589
- const { stdout, stderr } = await this.sshClient.exec(`${freePortBin}`);
590
- if (stderr) {
591
- throw new Error(`getFreePortForPlatformaOnServer: stderr is not empty: ${stderr}, stdout: ${stdout}`);
592
- }
593
- return +stdout;
594
- }
595
- async getArch() {
596
- const { stdout, stderr } = await this.sshClient.exec("uname -s && uname -m");
597
- if (stderr)
598
- throw new Error(`getArch: stderr is not empty: ${stderr}, stdout: ${stdout}`);
599
- const arr = stdout.split("\n");
600
- return {
601
- platform: arr[0],
602
- arch: arr[1],
603
- };
604
- }
605
- async getUserHomeDirectory() {
606
- const { stdout, stderr } = await this.sshClient.exec("echo $HOME");
607
- if (stderr) {
608
- const home = `/home/${this.username}`;
609
- console.warn(`getUserHomeDirectory: stderr is not empty: ${stderr}, stdout: ${stdout}, will get a default home: ${home}`);
610
- return home;
611
- }
612
- return stdout.trim();
613
- }
614
- }
18
+ var SshPl = class SshPl {
19
+ initState = { step: "init" };
20
+ constructor(logger, sshClient, username) {
21
+ this.logger = logger;
22
+ this.sshClient = sshClient;
23
+ this.username = username;
24
+ }
25
+ info() {
26
+ return {
27
+ username: this.username,
28
+ initState: this.initState
29
+ };
30
+ }
31
+ static async init(logger, config) {
32
+ try {
33
+ return new SshPl(logger, await require_ssh.SshClient.init(logger, config), (0, _milaboratories_ts_helpers.notEmpty)(config.username));
34
+ } catch (e) {
35
+ logger.error(`Connection error in SshClient.init: ${e}`);
36
+ throw e;
37
+ }
38
+ }
39
+ cleanUp() {
40
+ this.sshClient.close();
41
+ }
42
+ /** Provides an info if the platforma and minio are running along with the debug info. */
43
+ async isAlive() {
44
+ const arch = await this.getArch();
45
+ const remoteHome = await this.getUserHomeDirectory();
46
+ return await require_supervisord.supervisorStatus(this.logger, this.sshClient, remoteHome, arch.arch);
47
+ }
48
+ /** Starts all the services on the server.
49
+ * Idempotent semantic: we could call it several times. */
50
+ async start(shouldUseMinio) {
51
+ const arch = await this.getArch();
52
+ const remoteHome = await this.getUserHomeDirectory();
53
+ try {
54
+ if (!require_supervisord.isAllAlive(await this.isAlive(), shouldUseMinio)) {
55
+ await require_supervisord.supervisorCtlStart(this.sshClient, remoteHome, arch.arch);
56
+ return await this.checkIsAliveWithInterval(shouldUseMinio);
57
+ }
58
+ } catch (e) {
59
+ let msg = `SshPl.start: ${e}`;
60
+ let logs = "";
61
+ try {
62
+ logs = await this.sshClient.readFile(require_pl_paths.platformaCliLogs(remoteHome));
63
+ msg += `, platforma cli logs: ${logs}`;
64
+ } catch (e) {
65
+ msg += `, Can not read platforma cli logs: ${e}`;
66
+ }
67
+ this.logger.error(msg);
68
+ throw new Error(msg);
69
+ }
70
+ }
71
+ /** Stops all the services on the server.
72
+ * Idempotent semantic: we could call it several times. */
73
+ async stop() {
74
+ const arch = await this.getArch();
75
+ const remoteHome = await this.getUserHomeDirectory();
76
+ try {
77
+ const alive = await this.isAlive();
78
+ if (require_supervisord.isSupervisordRunning(alive)) {
79
+ await require_supervisord.supervisorStop(this.sshClient, remoteHome, arch.arch);
80
+ const shouldUseMinio = alive.minio === true;
81
+ return await this.checkIsAliveWithInterval(shouldUseMinio, 1e3, 15, false);
82
+ }
83
+ } catch (e) {
84
+ const msg = `PlSsh.stop: ${e}`;
85
+ this.logger.error(msg);
86
+ throw new Error(msg);
87
+ }
88
+ }
89
+ /** Stops the services, deletes a directory with the state and closes SSH connection. */
90
+ async reset() {
91
+ await this.stopAndClean();
92
+ this.cleanUp();
93
+ return true;
94
+ }
95
+ /** Stops platforma and deletes its state. */
96
+ async stopAndClean() {
97
+ const remoteHome = await this.getUserHomeDirectory();
98
+ this.logger.info(`pl.reset: Stop Platforma on the server`);
99
+ await this.stop();
100
+ this.logger.info(`pl.reset: Deleting Platforma workDir ${require_pl_paths.workDir(remoteHome)} on the server`);
101
+ await this.sshClient.deleteFolder(require_pl_paths.workDir(remoteHome));
102
+ }
103
+ /** Downloads binaries and untar them on the server,
104
+ * generates all the configs, creates necessary dirs,
105
+ * and finally starts all the services. */
106
+ async platformaInit(options) {
107
+ const state = {
108
+ localWorkdir: options.localWorkdir,
109
+ step: "init"
110
+ };
111
+ const { onProgress } = options;
112
+ const ops = {
113
+ ...defaultSshPlConfig,
114
+ ...options
115
+ };
116
+ state.plBinaryOps = ops.plBinary;
117
+ try {
118
+ await this.doStepDetectArch(state, onProgress);
119
+ await this.doStepDetectHome(state, onProgress);
120
+ if (!await this.doStepReadExistedConfig(state, ops, onProgress)) {
121
+ await onProgress?.("Platforma is already running. Skipping initialization.");
122
+ return state.existedSettings;
123
+ }
124
+ await this.doStepStopExistedPlatforma(state, onProgress);
125
+ await this.doStepCheckDbLock(state, onProgress);
126
+ await onProgress?.("Installation platforma...");
127
+ await this.doStepDownloadBinaries(state, onProgress, ops);
128
+ await this.doStepFetchPorts(state);
129
+ await this.doStepGenerateNewConfig(state, onProgress, ops);
130
+ await this.doStepCreateFoldersAndSaveFiles(state, onProgress);
131
+ await this.doStepConfigureSupervisord(state, onProgress);
132
+ await this.doStepSaveNewConnectionInfo(state, onProgress, ops);
133
+ await this.doStepStartPlatforma(state, onProgress);
134
+ return state.connectionInfo;
135
+ } catch (e) {
136
+ const msg = `SshPl.platformaInit: ${e}, state: ${JSON.stringify(this.removeSensitiveData(state))}`;
137
+ this.logger.error(msg);
138
+ throw new Error(msg);
139
+ }
140
+ }
141
+ async doStepStopExistedPlatforma(state, onProgress) {
142
+ state.step = "stopExistedPlatforma";
143
+ if (!require_supervisord.isAllAlive(state.alive, state.shouldUseMinio ?? false)) return;
144
+ await onProgress?.("Stopping services...");
145
+ await this.stop();
146
+ }
147
+ removeSensitiveData(state) {
148
+ const stateCopy = { ...state };
149
+ stateCopy.generatedConfig = {
150
+ ...stateCopy.generatedConfig,
151
+ filesToCreate: { skipped: "sanitized" }
152
+ };
153
+ return stateCopy;
154
+ }
155
+ async doStepStartPlatforma(state, onProgress) {
156
+ state.step = "startPlatforma";
157
+ await onProgress?.("Starting Platforma on the server...");
158
+ await this.start(state.shouldUseMinio ?? false);
159
+ state.started = true;
160
+ this.initState = state;
161
+ await onProgress?.("Platforma has been started successfully.");
162
+ }
163
+ async doStepSaveNewConnectionInfo(state, onProgress, ops) {
164
+ state.step = "saveNewConnectionInfo";
165
+ const config = state.generatedConfig;
166
+ await onProgress?.("Saving connection information...");
167
+ state.connectionInfo = require_connection_info.newConnectionInfo(config.plUser, config.plPassword, state.ports, (0, _milaboratories_ts_helpers.notEmpty)(ops.useGlobalAccess), ops.plBinary.version, state.shouldUseMinio ?? false);
168
+ await this.sshClient.writeFileOnTheServer(require_pl_paths.connectionInfo(state.remoteHome), require_connection_info.stringifyConnectionInfo(state.connectionInfo));
169
+ await onProgress?.("Connection information saved.");
170
+ }
171
+ async doStepCheckDbLock(state, onProgress) {
172
+ const removeLockFile = async (lockFilePath) => {
173
+ try {
174
+ await this.sshClient.exec(`rm -f ${lockFilePath}`);
175
+ this.logger.info(`Removed stale lock file ${lockFilePath}`);
176
+ } catch (e) {
177
+ const msg = `Failed to remove stale lock file ${lockFilePath}: ${e}`;
178
+ this.logger.error(msg);
179
+ throw new Error(msg);
180
+ }
181
+ };
182
+ state.step = "checkDbLock";
183
+ await onProgress?.("Checking for DB lock...");
184
+ const lockFilePath = require_pl_paths.platformaDbLock(state.remoteHome);
185
+ if (!await this.sshClient.checkFileExists(lockFilePath)) {
186
+ await onProgress?.("No DB lock found. Proceeding...");
187
+ return;
188
+ }
189
+ this.logger.info(`DB lock file found at ${lockFilePath}. Checking which process holds it...`);
190
+ const lockProcessInfo = await this.findLockHolder(lockFilePath);
191
+ if (!lockProcessInfo) {
192
+ this.logger.warn("Lock file exists but no process is holding it. Removing stale lock file...");
193
+ await removeLockFile(lockFilePath);
194
+ return;
195
+ }
196
+ this.logger.info(`Found process ${lockProcessInfo.pid} (user: ${lockProcessInfo.user}) holding DB lock`);
197
+ if (lockProcessInfo.user !== this.username) {
198
+ const msg = `DB lock is held by process ${lockProcessInfo.pid} owned by user '${lockProcessInfo.user}', but current user is '${this.username}'. Cannot kill process owned by different user.`;
199
+ this.logger.error(msg);
200
+ throw new Error(msg);
201
+ }
202
+ this.logger.info(`Process ${lockProcessInfo.pid} belongs to current user ${this.username}. Killing it...`);
203
+ await this.killRemoteProcess(lockProcessInfo.pid);
204
+ this.logger.info("Process holding DB lock has been terminated.");
205
+ if (await this.sshClient.checkFileExists(lockFilePath)) await removeLockFile(lockFilePath);
206
+ }
207
+ async doStepConfigureSupervisord(state, onProgress) {
208
+ await onProgress?.("Writing supervisord configuration...");
209
+ state.step = "configureSupervisord";
210
+ const config = state.generatedConfig;
211
+ let supervisorConfig;
212
+ if (state.shouldUseMinio) supervisorConfig = require_supervisord.generateSupervisordConfigWithMinio(config.minioConfig.storageDir, config.minioConfig.envs, await this.getFreePortForPlatformaOnServer(state.remoteHome, state.arch), config.workingDir, config.plConfig.configPath, state.binPaths.minioRelPath, state.binPaths.downloadedPl);
213
+ else supervisorConfig = require_supervisord.generateSupervisordConfig(await this.getFreePortForPlatformaOnServer(state.remoteHome, state.arch), config.workingDir, config.plConfig.configPath, state.binPaths.downloadedPl);
214
+ if (!await this.sshClient.writeFileOnTheServer(require_pl_paths.supervisorConf(state.remoteHome), supervisorConfig)) throw new Error(`Can not write supervisord config on the server ${require_pl_paths.workDir(state.remoteHome)}`);
215
+ await onProgress?.("Supervisord configuration written.");
216
+ }
217
+ async doStepCreateFoldersAndSaveFiles(state, onProgress) {
218
+ state.step = "createFoldersAndSaveFiles";
219
+ const config = state.generatedConfig;
220
+ await onProgress?.("Generating folder structure...");
221
+ for (const [filePath, content] of Object.entries(config.filesToCreate)) {
222
+ await this.sshClient.writeFileOnTheServer(filePath, content);
223
+ this.logger.info(`Created file ${filePath}`);
224
+ }
225
+ for (const dir of config.dirsToCreate) {
226
+ await this.sshClient.ensureRemoteDirCreated(dir);
227
+ this.logger.info(`Created directory ${dir}`);
228
+ }
229
+ await onProgress?.("Folder structure created.");
230
+ }
231
+ async doStepGenerateNewConfig(state, onProgress, ops) {
232
+ state.step = "generateNewConfig";
233
+ await onProgress?.("Generating new config...");
234
+ state.generatedConfig = { ...await (0, _milaboratories_pl_config.generateSshPlConfigs)({
235
+ logger: this.logger,
236
+ workingDir: require_pl_paths.workDir(state.remoteHome),
237
+ portsMode: {
238
+ type: "customWithMinio",
239
+ ports: {
240
+ debug: state.ports.debug.remote,
241
+ grpc: state.ports.grpc.remote,
242
+ http: state.ports.http.remote,
243
+ minio: state.ports.minioPort.remote,
244
+ minioConsole: state.ports.minioConsolePort.remote,
245
+ monitoring: state.ports.monitoring.remote,
246
+ httpLocal: state.ports.http.local,
247
+ grpcLocal: state.ports.grpc.local,
248
+ minioLocal: state.ports.minioPort.local
249
+ }
250
+ },
251
+ licenseMode: ops.license,
252
+ useGlobalAccess: (0, _milaboratories_ts_helpers.notEmpty)(ops.useGlobalAccess),
253
+ plConfigPostprocessing: ops.plConfigPostprocessing,
254
+ useMinio: state.shouldUseMinio ?? false
255
+ }) };
256
+ await onProgress?.("New config generated");
257
+ }
258
+ async doStepFetchPorts(state) {
259
+ state.step = "fetchPorts";
260
+ state.ports = await this.fetchPorts(state.remoteHome, state.arch);
261
+ if (!state.ports.debug.remote || !state.ports.grpc.remote || !state.ports.minioPort.remote || !state.ports.minioConsolePort.remote || !state.ports.monitoring.remote || !state.ports.http?.remote) throw new Error(`SshPl.platformaInit: remote ports are not defined`);
262
+ }
263
+ async doStepDownloadBinaries(state, onProgress, ops) {
264
+ state.step = "downloadBinaries";
265
+ await onProgress?.("Downloading and uploading required binaries...");
266
+ const glibcVersion = await getGlibcVersion(this.logger, this.sshClient);
267
+ if (glibcVersion < minRequiredGlibcVersion) throw new Error(`glibc version ${glibcVersion} is too old. Version ${minRequiredGlibcVersion} or higher is required for Platforma.`);
268
+ const downloadRes = await this.downloadBinariesAndUploadToTheServer(ops.localWorkdir, ops.plBinary, state.remoteHome, state.arch, state.shouldUseMinio ?? false, ops.proxy);
269
+ await onProgress?.("All required binaries have been downloaded and uploaded.");
270
+ state.binPaths = {
271
+ ...downloadRes,
272
+ history: void 0
273
+ };
274
+ state.downloadedBinaries = downloadRes.history;
275
+ }
276
+ async doStepDetectArch(state, onProgress) {
277
+ state.step = "detectArch";
278
+ await onProgress?.("Detecting server architecture...");
279
+ state.arch = await this.getArch();
280
+ await onProgress?.("Server architecture detected.");
281
+ }
282
+ async doStepDetectHome(state, onProgress) {
283
+ state.step = "detectHome";
284
+ await onProgress?.("Fetching user home directory...");
285
+ state.remoteHome = await this.getUserHomeDirectory();
286
+ await onProgress?.("User home directory retrieved.");
287
+ }
288
+ async doStepReadExistedConfig(state, ops, onProgress) {
289
+ state.step = "checkAlive";
290
+ await onProgress?.("Checking platform status...");
291
+ state.alive = await this.isAlive();
292
+ if (!state.alive?.platforma) return true;
293
+ await onProgress?.("All required services are running.");
294
+ state.existedSettings = await this.readExistedConfig(state.remoteHome);
295
+ if (!state.existedSettings) throw new Error(`SshPl.platformaInit: platforma is alive but existed settings are not found`);
296
+ const sameGA = state.existedSettings.useGlobalAccess == ops.useGlobalAccess;
297
+ const samePlVersion = state.existedSettings.plVersion == ops.plBinary.version;
298
+ state.needRestart = !(sameGA && samePlVersion);
299
+ this.logger.info(`SshPl.platformaInit: need restart? ${state.needRestart}`);
300
+ state.shouldUseMinio = state.existedSettings.minioIsUsed;
301
+ if (state.shouldUseMinio) this.logger.info(`SshPl.platformaInit: minio is used`);
302
+ else this.logger.info(`SshPl.platformaInit: minio is not used`);
303
+ if (!state.needRestart) {
304
+ await onProgress?.("Server setup completed.");
305
+ return false;
306
+ }
307
+ await onProgress?.("Stopping services...");
308
+ await this.stop();
309
+ return true;
310
+ }
311
+ async downloadBinariesAndUploadToTheServer(localWorkdir, plBinary, remoteHome, arch, shouldUseMinio, proxy) {
312
+ const state = [];
313
+ const dispatcher = (0, _milaboratories_pl_http.defaultHttpDispatcher)(proxy);
314
+ try {
315
+ const pl = await this.downloadAndUntar(localWorkdir, remoteHome, arch, "pl", `pl-${plBinary.version}`, dispatcher);
316
+ state.push(pl);
317
+ const supervisor = await this.downloadAndUntar(localWorkdir, remoteHome, arch, "supervisord", require_pl_paths.supervisordDirName, dispatcher);
318
+ state.push(supervisor);
319
+ const minioPath = require_pl_paths.minioBin(remoteHome, arch.arch);
320
+ if (shouldUseMinio) {
321
+ const minio = await this.downloadAndUntar(localWorkdir, remoteHome, arch, "minio", require_pl_paths.minioDirName, dispatcher);
322
+ state.push(minio);
323
+ await this.sshClient.chmod(minioPath, 488);
324
+ }
325
+ return {
326
+ history: state,
327
+ minioRelPath: shouldUseMinio ? minioPath : void 0,
328
+ downloadedPl: require_pl_paths.platformaBin(remoteHome, arch.arch)
329
+ };
330
+ } catch (e) {
331
+ const msg = `SshPl.downloadBinariesAndUploadToServer: ${e}, state: ${JSON.stringify(state)}`;
332
+ this.logger.error(msg);
333
+ throw e;
334
+ } finally {
335
+ await dispatcher.close();
336
+ }
337
+ }
338
+ async findLockHolderWithLsof(lockFilePath) {
339
+ try {
340
+ const { stdout } = await this.sshClient.exec(`lsof ${lockFilePath} 2>/dev/null || true`);
341
+ const output = stdout.trim();
342
+ if (!output) return null;
343
+ const lines = output.split("\n");
344
+ if (lines.length <= 1) return null;
345
+ const parts = lines[1].trim().split(/\s+/);
346
+ if (parts.length < 3) return null;
347
+ const pid = Number.parseInt(parts[1], 10);
348
+ const user = parts[2];
349
+ return Number.isNaN(pid) || !user ? null : {
350
+ pid,
351
+ user
352
+ };
353
+ } catch (e) {
354
+ this.logger.warn(`Failed to use lsof to check lock: ${e}`);
355
+ return null;
356
+ }
357
+ }
358
+ async findLockHolderWithFuser(lockFilePath) {
359
+ try {
360
+ const { stdout } = await this.sshClient.exec(`fuser ${lockFilePath} 2>/dev/null || true`);
361
+ const output = stdout.trim();
362
+ if (!output) return null;
363
+ const match = output.match(/: (\d+)/);
364
+ if (!match) return null;
365
+ const pid = Number.parseInt(match[1], 10);
366
+ if (Number.isNaN(pid)) return null;
367
+ try {
368
+ const user = (await this.sshClient.exec(`ps -o user= -p ${pid} 2>/dev/null || true`)).stdout.trim();
369
+ return user ? {
370
+ pid,
371
+ user
372
+ } : null;
373
+ } catch (e) {
374
+ this.logger.warn(`Failed to get user for PID ${pid}: ${e}`);
375
+ return null;
376
+ }
377
+ } catch (e) {
378
+ this.logger.warn(`Failed to use fuser to check lock: ${e}`);
379
+ return null;
380
+ }
381
+ }
382
+ async findLockHolder(lockFilePath) {
383
+ const viaLsof = await this.findLockHolderWithLsof(lockFilePath);
384
+ if (viaLsof) return viaLsof;
385
+ return this.findLockHolderWithFuser(lockFilePath);
386
+ }
387
+ async killRemoteProcess(pid) {
388
+ this.logger.info(`Killing process ${pid}...`);
389
+ try {
390
+ await this.sshClient.exec(`kill ${pid} 2>/dev/null || true`);
391
+ await (0, _milaboratories_ts_helpers.sleep)(1e3);
392
+ try {
393
+ await this.sshClient.exec(`kill -0 ${pid} 2>/dev/null`);
394
+ this.logger.warn(`Process ${pid} still alive after SIGTERM, forcing kill...`);
395
+ await this.sshClient.exec(`kill -9 ${pid} 2>/dev/null || true`);
396
+ await (0, _milaboratories_ts_helpers.sleep)(500);
397
+ } catch {}
398
+ } catch (e) {
399
+ const msg = `Failed to kill process ${pid}: ${e}`;
400
+ this.logger.error(msg);
401
+ throw new Error(msg);
402
+ }
403
+ }
404
+ /** We have to extract pl in the remote server,
405
+ * because Windows doesn't support symlinks
406
+ * that are found in Linux pl binaries tgz archive.
407
+ * For this reason, we extract all to the remote server.
408
+ * It requires `tar` to be installed on the server
409
+ * (it's not installed for Rocky Linux for example). */
410
+ async downloadAndUntar(localWorkdir, remoteHome, arch, softwareName, tgzName, dispatcher) {
411
+ const state = {};
412
+ state.binBasePath = require_pl_paths.binariesDir(remoteHome);
413
+ await this.sshClient.ensureRemoteDirCreated(state.binBasePath);
414
+ state.binBasePathCreated = true;
415
+ let downloadBinaryResult = null;
416
+ const attempts = 5;
417
+ for (let i = 1; i <= attempts; i++) try {
418
+ downloadBinaryResult = await require_pl_binary_download.downloadBinaryNoExtract({
419
+ logger: this.logger,
420
+ baseDir: localWorkdir,
421
+ softwareName,
422
+ tgzName,
423
+ arch: arch.arch,
424
+ platform: arch.platform,
425
+ dispatcher
426
+ });
427
+ break;
428
+ } catch (e) {
429
+ await (0, _milaboratories_ts_helpers.sleep)(300);
430
+ if (i == attempts) throw new Error(`downloadAndUntar: ${attempts} attempts, last error: ${e}`);
431
+ }
432
+ state.downloadResult = (0, _milaboratories_ts_helpers.notEmpty)(downloadBinaryResult);
433
+ state.localArchivePath = upath.default.resolve(state.downloadResult.archivePath);
434
+ state.remoteDir = upath.default.join(state.binBasePath, state.downloadResult.baseName);
435
+ state.remoteArchivePath = state.remoteDir + ".tgz";
436
+ await this.sshClient.ensureRemoteDirCreated(state.remoteDir);
437
+ await this.sshClient.uploadFile(state.localArchivePath, state.remoteArchivePath);
438
+ state.uploadDone = true;
439
+ try {
440
+ await this.sshClient.exec("hash tar");
441
+ } catch {
442
+ throw new Error(`tar is not installed on the server. Please install it before running Platforma.`);
443
+ }
444
+ const untarResult = await this.sshClient.exec(`tar --warning=no-all -xvf ${state.remoteArchivePath} --directory=${state.remoteDir}`);
445
+ if (untarResult.stderr) throw new Error(`downloadAndUntar: untar: stderr occurred: ${untarResult.stderr}, stdout: ${untarResult.stdout}`);
446
+ state.untarDone = true;
447
+ return state;
448
+ }
449
+ async needDownload(remoteHome, arch) {
450
+ const checkPathSupervisor = require_pl_paths.supervisorBin(remoteHome, arch.arch);
451
+ const checkPathMinio = require_pl_paths.minioDir(remoteHome, arch.arch);
452
+ const checkPathPlatforma = require_pl_paths.platformaBin(remoteHome, arch.arch);
453
+ if (!await this.sshClient.checkFileExists(checkPathPlatforma) || !await this.sshClient.checkFileExists(checkPathMinio) || !await this.sshClient.checkFileExists(checkPathSupervisor)) return true;
454
+ return false;
455
+ }
456
+ async checkIsAliveWithInterval(shouldUseMinio, interval = 1e3, count = 15, shouldStart = true) {
457
+ const maxMs = count * interval;
458
+ let total = 0;
459
+ let alive = await this.isAlive();
460
+ while (shouldStart ? !require_supervisord.isAllAlive(alive, shouldUseMinio) : require_supervisord.isAllAlive(alive, shouldUseMinio)) {
461
+ await (0, _milaboratories_ts_helpers.sleep)(interval);
462
+ total += interval;
463
+ if (total > maxMs) throw new Error(`isAliveWithInterval: The process did not ${shouldStart ? "started" : "stopped"} after ${maxMs} ms. Live status: ${JSON.stringify(alive)}`);
464
+ alive = await this.isAlive();
465
+ }
466
+ }
467
+ async readExistedConfig(remoteHome) {
468
+ return require_connection_info.parseConnectionInfo(await this.sshClient.readFile(require_pl_paths.connectionInfo(remoteHome)));
469
+ }
470
+ async fetchPorts(remoteHome, arch) {
471
+ return {
472
+ grpc: {
473
+ local: await (0, _milaboratories_pl_config.getFreePort)(),
474
+ remote: await this.getFreePortForPlatformaOnServer(remoteHome, arch)
475
+ },
476
+ monitoring: {
477
+ local: await (0, _milaboratories_pl_config.getFreePort)(),
478
+ remote: await this.getFreePortForPlatformaOnServer(remoteHome, arch)
479
+ },
480
+ debug: {
481
+ local: await (0, _milaboratories_pl_config.getFreePort)(),
482
+ remote: await this.getFreePortForPlatformaOnServer(remoteHome, arch)
483
+ },
484
+ http: {
485
+ local: await (0, _milaboratories_pl_config.getFreePort)(),
486
+ remote: await this.getFreePortForPlatformaOnServer(remoteHome, arch)
487
+ },
488
+ minioPort: {
489
+ local: await (0, _milaboratories_pl_config.getFreePort)(),
490
+ remote: await this.getFreePortForPlatformaOnServer(remoteHome, arch)
491
+ },
492
+ minioConsolePort: {
493
+ local: await (0, _milaboratories_pl_config.getFreePort)(),
494
+ remote: await this.getFreePortForPlatformaOnServer(remoteHome, arch)
495
+ }
496
+ };
497
+ }
498
+ async getLocalFreePort() {
499
+ return new Promise((res) => {
500
+ const srv = node_net.default.createServer();
501
+ srv.listen(0, () => {
502
+ const port = srv.address().port;
503
+ srv.close((_) => res(port));
504
+ });
505
+ });
506
+ }
507
+ async getFreePortForPlatformaOnServer(remoteHome, arch) {
508
+ const freePortBin = require_pl_paths.platformaFreePortBin(remoteHome, arch.arch);
509
+ const { stdout, stderr } = await this.sshClient.exec(`${freePortBin}`);
510
+ if (stderr) throw new Error(`getFreePortForPlatformaOnServer: stderr is not empty: ${stderr}, stdout: ${stdout}`);
511
+ return +stdout;
512
+ }
513
+ async getArch() {
514
+ const { stdout, stderr } = await this.sshClient.exec("uname -s && uname -m");
515
+ if (stderr) throw new Error(`getArch: stderr is not empty: ${stderr}, stdout: ${stdout}`);
516
+ const arr = stdout.split("\n");
517
+ return {
518
+ platform: arr[0],
519
+ arch: arr[1]
520
+ };
521
+ }
522
+ async getUserHomeDirectory() {
523
+ const { stdout, stderr } = await this.sshClient.exec("echo $HOME");
524
+ if (stderr) {
525
+ const home = `/home/${this.username}`;
526
+ console.warn(`getUserHomeDirectory: stderr is not empty: ${stderr}, stdout: ${stdout}, will get a default home: ${home}`);
527
+ return home;
528
+ }
529
+ return stdout.trim();
530
+ }
531
+ };
615
532
  const defaultSshPlConfig = {
616
- useGlobalAccess: false,
617
- plBinary: {
618
- type: "Download",
619
- version: pl_version.getDefaultPlVersion(),
620
- },
533
+ useGlobalAccess: false,
534
+ plBinary: {
535
+ type: "Download",
536
+ version: require_pl_version.getDefaultPlVersion()
537
+ }
621
538
  };
622
539
  /**
623
- * Gets the glibc version on the remote system
624
- * @returns The glibc version as a number
625
- * @throws Error if version cannot be determined
626
- */
540
+ * Gets the glibc version on the remote system
541
+ * @returns The glibc version as a number
542
+ * @throws Error if version cannot be determined
543
+ */
627
544
  async function getGlibcVersion(logger, sshClient) {
628
- try {
629
- const { stdout, stderr } = await sshClient.exec("ldd --version | head -n 1");
630
- if (stderr) {
631
- throw new Error(`Failed to check glibc version: ${stderr}`);
632
- }
633
- return parseGlibcVersion(stdout);
634
- }
635
- catch (e) {
636
- logger.error(`glibc version check failed: ${e}`);
637
- throw e;
638
- }
545
+ try {
546
+ const { stdout, stderr } = await sshClient.exec("ldd --version | head -n 1");
547
+ if (stderr) throw new Error(`Failed to check glibc version: ${stderr}`);
548
+ return parseGlibcVersion(stdout);
549
+ } catch (e) {
550
+ logger.error(`glibc version check failed: ${e}`);
551
+ throw e;
552
+ }
639
553
  }
640
554
  function parseGlibcVersion(output) {
641
- const versionMatch = output.match(/\d+\.\d+/);
642
- if (!versionMatch) {
643
- throw new Error(`Could not parse glibc version from: ${output}`);
644
- }
645
- return parseFloat(versionMatch[0]);
555
+ const versionMatch = output.match(/\d+\.\d+/);
556
+ if (!versionMatch) throw new Error(`Could not parse glibc version from: ${output}`);
557
+ return parseFloat(versionMatch[0]);
646
558
  }
647
559
 
560
+ //#endregion
648
561
  exports.SshPl = SshPl;
649
562
  exports.parseGlibcVersion = parseGlibcVersion;
650
- //# sourceMappingURL=pl.cjs.map
563
+ //# sourceMappingURL=pl.cjs.map