@milaboratories/pl-deployments 1.1.4 → 1.1.6

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/dist/index.mjs CHANGED
@@ -1,45 +1,45 @@
1
- var K = Object.defineProperty;
2
- var q = (s, r, t) => r in s ? K(s, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[r] = t;
3
- var d = (s, r, t) => q(s, typeof r != "symbol" ? r + "" : r, t);
4
- import { spawn as G } from "node:child_process";
5
- import { sleep as S, fileExists as f, assertNever as N, notEmpty as p } from "@milaboratories/ts-helpers";
1
+ var G = Object.defineProperty;
2
+ var Z = (s, t, r) => t in s ? G(s, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : s[t] = r;
3
+ var w = (s, t, r) => Z(s, typeof t != "symbol" ? t + "" : t, r);
4
+ import { spawn as V } from "node:child_process";
5
+ import { sleep as C, fileExists as m, assertNever as T, notEmpty as p, RetryablePromise as Y } from "@milaboratories/ts-helpers";
6
6
  import E from "node:fs";
7
- import w, { readFile as Z } from "node:fs/promises";
8
- import u from "upath";
9
- import { request as V } from "undici";
10
- import { Readable as Y, Writable as Q } from "node:stream";
11
- import { text as X } from "node:stream/consumers";
12
- import * as tt from "tar";
13
- import rt from "decompress";
7
+ import f, { readFile as Q } from "node:fs/promises";
8
+ import d from "upath";
9
+ import { request as X } from "undici";
10
+ import { Readable as rr, Writable as tr } from "node:stream";
11
+ import { text as er } from "node:stream/consumers";
12
+ import * as ir from "tar";
13
+ import or from "decompress";
14
14
  import k from "node:os";
15
- import et, { Client as v } from "ssh2";
16
- import U from "node:net";
17
- import it from "node:dns";
18
- import { generateSshPlConfigs as ot, getFreePort as g } from "@milaboratories/pl-config";
19
- import { randomBytes as nt } from "node:crypto";
20
- function st(s, r) {
15
+ import sr, { Client as S } from "ssh2";
16
+ import B from "node:net";
17
+ import nr from "node:dns";
18
+ import { randomBytes as F } from "node:crypto";
19
+ import { generateSshPlConfigs as ar, getFreePort as y } from "@milaboratories/pl-config";
20
+ function cr(s, t) {
21
21
  return s.info(`Running:
22
- cmd: ${JSON.stringify([r.cmd, ...r.args])}
23
- wd: ${r.opts.cwd}`), s.info(" spawning child process"), G(r.cmd, r.args, r.opts);
22
+ cmd: ${JSON.stringify([t.cmd, ...t.args])}
23
+ wd: ${t.opts.cwd}`), s.info(" spawning child process"), V(t.cmd, t.args, t.opts);
24
24
  }
25
- async function C(s) {
25
+ async function D(s) {
26
26
  try {
27
27
  return process.kill(s, 0), !0;
28
28
  } catch {
29
29
  return !1;
30
30
  }
31
31
  }
32
- function T(s) {
32
+ function _(s) {
33
33
  return process.kill(s, "SIGINT");
34
34
  }
35
- async function B(s, r) {
35
+ async function j(s, t) {
36
36
  let e = 0;
37
- for (; await C(s); )
38
- if (await S(100), e += 100, e > r)
39
- throw new Error(`The process did not stopped after ${r} ms.`);
37
+ for (; await D(s); )
38
+ if (await C(100), e += 100, e > t)
39
+ throw new Error(`The process did not stopped after ${t} ms.`);
40
40
  }
41
- const at = ["linux", "macos", "windows"];
42
- function H(s) {
41
+ const lr = ["linux", "macos", "windows"];
42
+ function I(s) {
43
43
  switch (s.toLowerCase()) {
44
44
  case "darwin":
45
45
  return "macos";
@@ -49,12 +49,12 @@ function H(s) {
49
49
  return "windows";
50
50
  default:
51
51
  throw new Error(
52
- `operating system '${s}' is not currently supported by Platforma ecosystem. The list of OSes supported: ` + JSON.stringify(at)
52
+ `operating system '${s}' is not currently supported by Platforma ecosystem. The list of OSes supported: ` + JSON.stringify(lr)
53
53
  );
54
54
  }
55
55
  }
56
- const ct = ["amd64", "arm64"];
57
- function $(s) {
56
+ const hr = ["amd64", "arm64"];
57
+ function P(s) {
58
58
  switch (s) {
59
59
  case "aarch64":
60
60
  case "aarch64_be":
@@ -65,177 +65,177 @@ function $(s) {
65
65
  return "amd64";
66
66
  default:
67
67
  throw new Error(
68
- `processor architecture '${s}' is not currently supported by Platforma ecosystem. The list of architectures supported: ` + JSON.stringify(ct)
68
+ `processor architecture '${s}' is not currently supported by Platforma ecosystem. The list of architectures supported: ` + JSON.stringify(hr)
69
69
  );
70
70
  }
71
71
  }
72
- async function lt(s, r, t, e, i, o) {
73
- const n = ut(t, e, r, $(i), H(o)), { archiveUrl: a, archivePath: c } = n;
74
- return await _(s, a, c), n;
72
+ async function dr(s, t, r, e, i, o) {
73
+ const n = wr(r, e, t, P(i), I(o)), { archiveUrl: c, archivePath: a } = n;
74
+ return await H(s, c, a), n;
75
75
  }
76
- async function ht(s, r, t, e, i) {
77
- const o = dt(t, r, $(e), H(i)), { archiveUrl: n, archivePath: a, archiveType: c, targetFolder: l, binaryPath: h } = o;
78
- return await _(s, n, a), await pt(s, a, c, l), o;
76
+ async function ur(s, t, r, e, i) {
77
+ const o = fr(r, t, P(e), I(i)), { archiveUrl: n, archivePath: c, archiveType: a, targetFolder: l, binaryPath: h } = o;
78
+ return await H(s, n, c), await mr(s, c, a, l), o;
79
79
  }
80
- function ut(s, r, t, e, i) {
81
- const o = `${r}-${e}`, n = I[i], a = `${o}.${n}`, c = `https://cdn.platforma.bio/software/${s}/${i}/${a}`, l = u.join(t, a), h = u.join(t, o);
80
+ function wr(s, t, r, e, i) {
81
+ const o = `${t}-${e}`, n = M[i], c = `${o}.${n}`, a = `https://cdn.platforma.bio/software/${s}/${i}/${c}`, l = d.join(r, c), h = d.join(r, o);
82
82
  return {
83
- archiveUrl: c,
83
+ archiveUrl: a,
84
84
  archivePath: l,
85
85
  archiveType: n,
86
86
  targetFolder: h,
87
87
  baseName: o
88
88
  };
89
89
  }
90
- function dt(s, r, t, e) {
91
- const i = `pl-${s}-${t}`, o = I[e], n = `${i}.${o}`, a = `https://cdn.platforma.bio/software/pl/${e}/${n}`, c = u.join(r, n), l = u.join(r, i), h = u.join(i, "binaries", ft[e]);
90
+ function fr(s, t, r, e) {
91
+ const i = `pl-${s}-${r}`, o = M[e], n = `${i}.${o}`, c = `https://cdn.platforma.bio/software/pl/${e}/${n}`, a = d.join(t, n), l = d.join(t, i), h = d.join(i, "binaries", gr[e]);
92
92
  return {
93
- archiveUrl: a,
94
- archivePath: c,
93
+ archiveUrl: c,
94
+ archivePath: a,
95
95
  archiveType: o,
96
96
  targetFolder: l,
97
97
  binaryPath: h,
98
98
  baseName: i
99
99
  };
100
100
  }
101
- async function _(s, r, t) {
101
+ async function H(s, t, r) {
102
102
  const e = {};
103
- e.dstArchive = t;
103
+ e.dstArchive = r;
104
104
  try {
105
- if (e.fileExisted = await f(t), e.fileExisted)
106
- return s.info(`Platforma Backend archive download skipped: '${t}' already exists`), e;
107
- await w.mkdir(u.dirname(t), { recursive: !0 }), e.dirnameCreated = !0, s.info(`Downloading archive:
108
- URL: ${r}
109
- Save to: ${t}`);
110
- const { body: i, statusCode: o } = await V(r);
105
+ if (e.fileExisted = await m(r), e.fileExisted)
106
+ return s.info(`Platforma Backend archive download skipped: '${r}' already exists`), e;
107
+ await f.mkdir(d.dirname(r), { recursive: !0 }), e.dirnameCreated = !0, s.info(`Downloading archive:
108
+ URL: ${t}
109
+ Save to: ${r}`);
110
+ const { body: i, statusCode: o } = await X(t);
111
111
  if (e.statusCode = o, o != 200) {
112
- const n = await X(i);
112
+ const n = await er(i);
113
113
  throw e.errorMsg = `failed to download archive: ${o}, response: ${n.slice(0, 1e3)}`, s.error(e.errorMsg), new Error(e.errorMsg);
114
114
  }
115
- return e.tmpPath = t + ".tmp", await Y.toWeb(i).pipeTo(Q.toWeb(E.createWriteStream(e.tmpPath))), e.wroteTmp = !0, e.tmpExisted = await f(e.tmpPath), await w.rename(e.tmpPath, t), e.renamed = !0, e.newExisted = await f(t), e;
115
+ return e.tmpPath = r + ".tmp", await rr.toWeb(i).pipeTo(tr.toWeb(E.createWriteStream(e.tmpPath))), e.wroteTmp = !0, e.tmpExisted = await m(e.tmpPath), await f.rename(e.tmpPath, r), e.renamed = !0, e.newExisted = await m(r), e;
116
116
  } catch (i) {
117
117
  const o = `downloadArchive: error ${JSON.stringify(i)} occurred, state: ${JSON.stringify(e)}`;
118
118
  throw s.error(o), new Error(o);
119
119
  }
120
120
  }
121
- const wt = ".ok";
122
- async function pt(s, r, t, e) {
123
- if (s.info("extracting archive..."), s.info(` archive path: '${r}'`), s.info(` target dir: '${e}'`), !await f(r)) {
124
- const o = `Platforma Backend binary archive not found at '${r}'`;
121
+ const pr = ".ok";
122
+ async function mr(s, t, r, e) {
123
+ if (s.info("extracting archive..."), s.info(` archive path: '${t}'`), s.info(` target dir: '${e}'`), !await m(t)) {
124
+ const o = `Platforma Backend binary archive not found at '${t}'`;
125
125
  throw s.error(o), new Error(o);
126
126
  }
127
- const i = u.join(e, wt);
128
- if (await f(i)) {
127
+ const i = d.join(e, pr);
128
+ if (await m(i)) {
129
129
  s.info(`Platforma Backend binaries unpack skipped: '${e}' exists`);
130
130
  return;
131
131
  }
132
- switch (await f(e) && (s.info(`Removing previous incompletely unpacked folder: '${e}'`), await w.rm(e, { recursive: !0 })), s.info(` creating target dir '${e}'`), await w.mkdir(e, { recursive: !0 }), s.info(
132
+ switch (await m(e) && (s.info(`Removing previous incompletely unpacked folder: '${e}'`), await f.rm(e, { recursive: !0 })), s.info(` creating target dir '${e}'`), await f.mkdir(e, { recursive: !0 }), s.info(
133
133
  `Unpacking Platforma Backend archive:
134
- Archive: ${r}
134
+ Archive: ${t}
135
135
  Target dir: ${e}`
136
- ), t) {
136
+ ), r) {
137
137
  case "tgz":
138
- await tt.x({
139
- file: r,
138
+ await ir.x({
139
+ file: t,
140
140
  cwd: e,
141
141
  gzip: !0
142
142
  });
143
143
  break;
144
144
  case "zip":
145
- await rt(r, e);
145
+ await or(t, e);
146
146
  break;
147
147
  default:
148
- N(t);
148
+ T(r);
149
149
  }
150
- await w.writeFile(i, "ok"), s.info(" ... unpack done.");
150
+ await f.writeFile(i, "ok"), s.info(" ... unpack done.");
151
151
  }
152
- const I = {
152
+ const M = {
153
153
  linux: "tgz",
154
154
  macos: "tgz",
155
155
  windows: "zip"
156
- }, ft = {
156
+ }, gr = {
157
157
  linux: "platforma",
158
158
  macos: "platforma",
159
159
  windows: "platforma.exe"
160
160
  };
161
- function F() {
161
+ function b() {
162
162
  return "1.18.3";
163
163
  }
164
- function mt() {
165
- return { type: "Download", version: F() };
164
+ function yr() {
165
+ return { type: "Download", version: b() };
166
166
  }
167
- async function gt(s, r, t) {
168
- switch (t.type) {
167
+ async function $r(s, t, r) {
168
+ switch (r.type) {
169
169
  case "Download":
170
- return (await ht(s, r, t.version, k.arch(), k.platform())).binaryPath;
170
+ return (await ur(s, t, r.version, k.arch(), k.platform())).binaryPath;
171
171
  case "Local":
172
- return t.path;
172
+ return r.path;
173
173
  default:
174
- N(t);
174
+ T(r);
175
175
  }
176
176
  }
177
- function j(s) {
178
- return u.join(s, "pl_pid");
177
+ function J(s) {
178
+ return d.join(s, "pl_pid");
179
179
  }
180
- async function yt(s) {
181
- if (!await f(s))
180
+ async function Pr(s) {
181
+ if (!await m(s))
182
182
  return;
183
- const r = await w.readFile(s);
184
- return Number(r.toString());
183
+ const t = await f.readFile(s);
184
+ return Number(t.toString());
185
185
  }
186
- async function $t(s, r) {
187
- await w.writeFile(s, JSON.stringify(r));
186
+ async function vr(s, t) {
187
+ await f.writeFile(s, JSON.stringify(t));
188
188
  }
189
- function Pt() {
189
+ function Sr() {
190
190
  return {};
191
191
  }
192
- function vt(s, r, t) {
193
- return s[r] = t, t;
192
+ function Cr(s, t, r) {
193
+ return s[t] = r, r;
194
194
  }
195
- async function b(s, r) {
196
- const t = Pt();
195
+ async function A(s, t) {
196
+ const r = Sr();
197
197
  try {
198
- return await r((i, o) => vt(t, i, o), t);
198
+ return await t((i, o) => Cr(r, i, o), r);
199
199
  } catch (e) {
200
- throw s.error(`error ${e} while doing traced operation, state: ${JSON.stringify(t)}`), e;
200
+ throw s.error(`error ${e} while doing traced operation, state: ${JSON.stringify(r)}`), e;
201
201
  }
202
202
  }
203
- const St = "config-local.yaml";
204
- class Et {
205
- constructor(r, t, e, i, o, n, a, c) {
206
- d(this, "instance");
207
- d(this, "pid");
208
- d(this, "nRuns", 0);
209
- d(this, "lastRunHistory", {});
210
- d(this, "wasStopped", !1);
211
- this.logger = r, this.workingDir = t, this.startOptions = e, this.initialStartHistory = i, this.onClose = o, this.onError = n, this.onCloseAndError = a, this.onCloseAndErrorNoStop = c;
203
+ const Er = "config-local.yaml";
204
+ class Fr {
205
+ constructor(t, r, e, i, o, n, c, a) {
206
+ w(this, "instance");
207
+ w(this, "pid");
208
+ w(this, "nRuns", 0);
209
+ w(this, "lastRunHistory", {});
210
+ w(this, "wasStopped", !1);
211
+ this.logger = t, this.workingDir = r, this.startOptions = e, this.initialStartHistory = i, this.onClose = o, this.onError = n, this.onCloseAndError = c, this.onCloseAndErrorNoStop = a;
212
212
  }
213
213
  async start() {
214
- await b(this.logger, async (r, t) => {
214
+ await A(this.logger, async (t, r) => {
215
215
  this.wasStopped = !1;
216
- const e = st(this.logger, this.startOptions);
216
+ const e = cr(this.logger, this.startOptions);
217
217
  e.on("error", (o) => {
218
218
  this.logger.error(
219
219
  `error '${o}', while running platforma, started opts: ${JSON.stringify(this.debugInfo())}`
220
220
  ), this.onError !== void 0 && this.onError(this), this.onCloseAndError !== void 0 && this.onCloseAndError(this), this.onCloseAndErrorNoStop !== void 0 && !this.wasStopped && this.onCloseAndErrorNoStop(this);
221
221
  }), e.on("close", () => {
222
222
  this.logger.warn(`platforma was closed, started opts: ${JSON.stringify(this.debugInfo())}`), this.onClose !== void 0 && this.onClose(this), this.onCloseAndError !== void 0 && this.onCloseAndError(this), this.onCloseAndErrorNoStop !== void 0 && !this.wasStopped && this.onCloseAndErrorNoStop(this);
223
- }), r("started", !0);
224
- const i = r("pidFile", j(this.workingDir));
225
- r("pid", p(e.pid)), r("pidWritten", await $t(i, p(e.pid))), this.nRuns++, this.instance = e, this.pid = e.pid, this.lastRunHistory = t;
223
+ }), t("started", !0);
224
+ const i = t("pidFile", J(this.workingDir));
225
+ t("pid", p(e.pid)), t("pidWritten", await vr(i, p(e.pid))), this.nRuns++, this.instance = e, this.pid = e.pid, this.lastRunHistory = r;
226
226
  });
227
227
  }
228
228
  stop() {
229
- this.wasStopped = !0, T(p(this.pid));
229
+ this.wasStopped = !0, _(p(this.pid));
230
230
  }
231
231
  async waitStopped() {
232
- await B(p(this.pid), 15e3);
232
+ await j(p(this.pid), 15e3);
233
233
  }
234
234
  stopped() {
235
235
  return this.wasStopped;
236
236
  }
237
237
  async isAlive() {
238
- return await C(p(this.pid));
238
+ return await D(p(this.pid));
239
239
  }
240
240
  debugInfo() {
241
241
  return {
@@ -248,21 +248,21 @@ class Et {
248
248
  };
249
249
  }
250
250
  }
251
- async function Xt(s, r) {
252
- const t = {
253
- plBinary: mt(),
251
+ async function it(s, t) {
252
+ const r = {
253
+ plBinary: yr(),
254
254
  spawnOptions: {},
255
255
  closeOld: !0,
256
- ...r
256
+ ...t
257
257
  };
258
- return await b(s, async (e, i) => {
259
- e("startOptions", { ...t, config: "too wordy" });
260
- const o = u.resolve(t.workingDir);
261
- t.closeOld && e("closeOld", await Ct(s, o));
262
- const n = u.join(o, St);
263
- s.info(`writing configuration '${n}'...`), await w.writeFile(n, t.config);
264
- const a = await gt(s, u.join(o, "binaries"), t.plBinary), l = {
265
- cmd: e("binaryPath", u.join("binaries", a)),
258
+ return await A(s, async (e, i) => {
259
+ e("startOptions", { ...r, config: "too wordy" });
260
+ const o = d.resolve(r.workingDir);
261
+ r.closeOld && e("closeOld", await Dr(s, o));
262
+ const n = d.join(o, Er);
263
+ s.info(`writing configuration '${n}'...`), await f.writeFile(n, r.config);
264
+ const c = await $r(s, d.join(o, "binaries"), r.plBinary), l = {
265
+ cmd: e("binaryPath", d.join("binaries", c)),
266
266
  args: ["-config", n],
267
267
  opts: {
268
268
  env: { ...process.env },
@@ -270,7 +270,7 @@ async function Xt(s, r) {
270
270
  stdio: ["pipe", "ignore", "inherit"],
271
271
  windowsHide: !0,
272
272
  // hide a terminal on Windows
273
- ...t.spawnOptions
273
+ ...r.spawnOptions
274
274
  }
275
275
  };
276
276
  e("processOpts", {
@@ -278,88 +278,84 @@ async function Xt(s, r) {
278
278
  args: l.args,
279
279
  cwd: l.opts.cwd
280
280
  });
281
- const h = new Et(
281
+ const h = new Fr(
282
282
  s,
283
- t.workingDir,
283
+ r.workingDir,
284
284
  l,
285
285
  i,
286
- t.onClose,
287
- t.onError,
288
- t.onCloseAndError,
289
- t.onCloseAndErrorNoStop
286
+ r.onClose,
287
+ r.onError,
288
+ r.onCloseAndError,
289
+ r.onCloseAndErrorNoStop
290
290
  );
291
291
  return await h.start(), h;
292
292
  });
293
293
  }
294
- async function Ct(s, r) {
295
- return await b(s, async (t, e) => {
296
- const i = t("pidFilePath", j(r)), o = t("pid", await yt(i)), n = t("wasAlive", await C(o));
297
- return o !== void 0 && n && (t("stopped", T(o)), t("waitStopped", await B(o, 1e4))), e;
294
+ async function Dr(s, t) {
295
+ return await A(s, async (r, e) => {
296
+ const i = r("pidFilePath", J(t)), o = r("pid", await Pr(i)), n = r("wasAlive", await D(o));
297
+ return o !== void 0 && n && (r("stopped", _(o)), r("waitStopped", await j(o, 1e4))), e;
298
298
  });
299
299
  }
300
- const Ft = {
300
+ const br = {
301
301
  keepaliveInterval: 6e4,
302
302
  keepaliveCountMax: 10
303
303
  };
304
- class D {
305
- constructor(r, t) {
306
- d(this, "config");
307
- d(this, "homeDir");
308
- this.logger = r, this.client = t;
304
+ class x {
305
+ constructor(t, r) {
306
+ w(this, "config");
307
+ w(this, "homeDir");
308
+ w(this, "forwardedServers", []);
309
+ this.logger = t, this.client = r;
309
310
  }
310
311
  /**
311
312
  * Initializes the SshClient and establishes a connection using the provided configuration.
312
313
  * @param config - The connection configuration object for the SSH client.
313
314
  * @returns A new instance of SshClient with an active connection.
314
315
  */
315
- static async init(r, t) {
316
+ static async init(t, r) {
316
317
  const e = {
317
- ...Ft,
318
- ...t
319
- }, i = new D(r, new v());
318
+ ...br,
319
+ ...r
320
+ }, i = new x(t, new S());
320
321
  return await i.connect(e), i;
321
322
  }
323
+ getForwardedServers() {
324
+ return this.forwardedServers;
325
+ }
322
326
  getFullHostName() {
323
- var r, t;
324
- return `${(r = this.config) == null ? void 0 : r.host}:${(t = this.config) == null ? void 0 : t.port}`;
327
+ var t, r;
328
+ return `${(t = this.config) == null ? void 0 : t.host}:${(r = this.config) == null ? void 0 : r.port}`;
325
329
  }
326
330
  getUserName() {
327
- var r;
328
- return (r = this.config) == null ? void 0 : r.username;
331
+ var t;
332
+ return (t = this.config) == null ? void 0 : t.username;
329
333
  }
330
334
  /**
331
335
  * Connects to the SSH server using the specified configuration.
332
336
  * @param config - The connection configuration object for the SSH client.
333
337
  * @returns A promise that resolves when the connection is established or rejects on error.
334
338
  */
335
- async connect(r) {
336
- return this.config = r, new Promise((t, e) => {
337
- this.client.on("ready", () => {
338
- t(void 0);
339
- }).on("error", (i) => {
340
- e(new Error(`ssh.connect: error occurred: ${i}`));
341
- }).on("timeout", () => {
342
- e(new Error("timeout was occurred while waiting for SSH connection."));
343
- }).connect(r);
344
- });
339
+ async connect(t) {
340
+ return this.config = t, await Ar(this.client, t);
345
341
  }
346
342
  /**
347
343
  * Executes a command on the SSH server.
348
344
  * @param command - The command to execute on the remote server.
349
345
  * @returns A promise resolving with the command's stdout and stderr outputs.
350
346
  */
351
- async exec(r) {
352
- return new Promise((t, e) => {
353
- this.client.exec(r, (i, o) => {
347
+ async exec(t) {
348
+ return new Promise((r, e) => {
349
+ this.client.exec(t, (i, o) => {
354
350
  if (i)
355
- return e(`ssh.exec: ${r}, error occurred: ${i}`);
356
- let n = "", a = "";
357
- o.on("close", (c) => {
358
- c === 0 ? t({ stdout: n, stderr: a }) : e(new Error(`Command ${r} exited with code ${c}`));
359
- }).on("data", (c) => {
360
- n += c.toString();
361
- }).stderr.on("data", (c) => {
362
- a += c.toString();
351
+ return e(`ssh.exec: ${t}, error occurred: ${i}`);
352
+ let n = "", c = "";
353
+ o.on("close", (a) => {
354
+ a === 0 ? r({ stdout: n, stderr: c }) : e(new Error(`Command ${t} exited with code ${a}`));
355
+ }).on("data", (a) => {
356
+ n += a.toString();
357
+ }).stderr.on("data", (a) => {
358
+ c += a.toString();
363
359
  });
364
360
  });
365
361
  });
@@ -370,10 +366,10 @@ class D {
370
366
  * @param port - The port number to connect to on the server.
371
367
  * @returns 'publickey' | 'password'[] A promise resolving with a list of supported authentication methods.
372
368
  */
373
- static async getAuthTypes(r, t) {
369
+ static async getAuthTypes(t, r) {
374
370
  return new Promise((e) => {
375
371
  let i = "";
376
- const o = new v();
372
+ const o = new S();
377
373
  o.on("ready", () => {
378
374
  o.end();
379
375
  const n = this.extractAuthMethods(i);
@@ -381,8 +377,8 @@ class D {
381
377
  }), o.on("error", () => {
382
378
  o.end(), e(["publickey", "password"]);
383
379
  }), o.connect({
384
- host: r,
385
- port: t,
380
+ host: t,
381
+ port: r,
386
382
  username: (/* @__PURE__ */ new Date()).getTime().toString(),
387
383
  debug: (n) => {
388
384
  i += `${n}
@@ -396,9 +392,9 @@ class D {
396
392
  * @param log - The debug log output containing authentication information.
397
393
  * @returns An array of extracted authentication methods.
398
394
  */
399
- static extractAuthMethods(r) {
400
- const t = r.match(/Inbound: Received USERAUTH_FAILURE \((.+)\)/);
401
- return t && t[1] ? t[1].split(",").map((e) => e.trim()) : [];
395
+ static extractAuthMethods(t) {
396
+ const r = t.match(/Inbound: Received USERAUTH_FAILURE \((.+)\)/);
397
+ return r && r[1] ? r[1].split(",").map((e) => e.trim()) : [];
402
398
  }
403
399
  /**
404
400
  * Sets up port forwarding between a remote port on the SSH server and a local port.
@@ -407,52 +403,72 @@ class D {
407
403
  * @param config - Optional connection configuration for the SSH client.
408
404
  * @returns { server: net.Server } A promise resolving with the created server instance.
409
405
  */
410
- async forwardPort(r, t) {
411
- return t = t ?? this.config, new Promise((e, i) => {
412
- if (!t) {
413
- i("No config defined");
414
- return;
415
- }
416
- const o = new v();
417
- let n;
418
- o.on("ready", () => {
419
- this.logger.info(`[SSH] Connection to ${t.host}. Remote port ${r.remotePort} will be available locally on the ${r.localPort}`), n = U.createServer({ pauseOnConnect: !0 }, (a) => {
420
- o.forwardOut(
421
- "127.0.0.1",
422
- 0,
423
- "127.0.0.1",
424
- r.remotePort,
425
- (c, l) => {
426
- if (c) {
427
- console.error("Error opening SSH channel:", c.message), a.end();
428
- return;
429
- }
430
- a.pipe(l), l.pipe(a), a.resume();
431
- }
432
- );
433
- }), n.listen(r.localPort, "127.0.0.1", () => {
434
- this.logger.info(`[+] Port local ${r.localPort} available locally for remote port → :${r.remotePort}`), e({ server: n });
435
- }), n.on("error", (a) => {
436
- o.end(), n.close(), i(new Error(`ssh.forwardPort: server error: ${a}`));
437
- }), n.on("close", () => {
438
- this.logger.info(`Server closed ${JSON.stringify(r)}`), o && (this.logger.info("End SSH connection"), o.end());
406
+ async forwardPort(t, r) {
407
+ const e = `ssh.forward:${t.localPort}:${t.remotePort}.id_${F(1).toString("hex")}`;
408
+ r = r ?? this.config;
409
+ const i = new Y((o) => new Promise((n, c) => {
410
+ const a = new S();
411
+ a.on("ready", () => {
412
+ this.logger.info(`${e}.client.ready`), n(a);
413
+ }), a.on("error", (l) => {
414
+ this.logger.info(`${e}.client.error: ${l}`), o.reset(), c(l);
415
+ }), a.on("close", () => {
416
+ this.logger.info(`${e}.client.closed`), o.reset();
417
+ }), a.connect(r);
418
+ }));
419
+ return await i.ensure(), new Promise((o, n) => {
420
+ const c = B.createServer({ pauseOnConnect: !0 }, async (a) => {
421
+ const l = `${e}.sock_${F(1).toString("hex")}`;
422
+ let h;
423
+ try {
424
+ h = await i.ensure();
425
+ } catch (g) {
426
+ this.logger.info(`${l}.persistentClient.catch: ${g}`), a.end();
427
+ return;
428
+ }
429
+ let u;
430
+ try {
431
+ u = await xr(this.logger, h, "127.0.0.1", 0, "127.0.0.1", t.remotePort);
432
+ } catch (g) {
433
+ this.logger.error(`${l}.forwardOut.err: ${g}`), a.end();
434
+ return;
435
+ }
436
+ a.pipe(u), u.pipe(a), a.resume(), u.on("error", (g) => {
437
+ this.logger.error(`${l}.stream.error: ${g}`), a.end(), u.end();
438
+ }), u.on("close", () => {
439
+ a.end(), u.end();
440
+ }), a.on("close", () => {
441
+ this.logger.info(`${l}.localSocket: closed`), a.end(), u.end();
439
442
  });
440
- }), o.on("error", (a) => {
441
- this.logger.error(`[SSH] SSH connection error, ports: ${JSON.stringify(r)}, err: ${a.message}`), n == null || n.close(), i(`ssh.forwardPort: conn.err: ${a}`);
442
- }), o.on("close", () => {
443
- this.logger.info(`[SSH] Connection closed, ports: ${JSON.stringify(r)}`);
444
- }), o.connect(t);
443
+ });
444
+ c.listen(t.localPort, "127.0.0.1", () => {
445
+ this.logger.info(`${e}.server: started listening`), this.forwardedServers.push(c), o({ server: c });
446
+ }), c.on("error", (a) => {
447
+ c.close(), n(new Error(`${e}.server: error: ${JSON.stringify(a)}`));
448
+ }), c.on("close", () => {
449
+ this.logger.info(`${e}.server: closed ${JSON.stringify(t)}`), this.forwardedServers = this.forwardedServers.filter((a) => a !== c);
450
+ });
445
451
  });
446
452
  }
453
+ closeForwardedPorts() {
454
+ this.logger.info("[SSH] Closing all forwarded ports..."), this.forwardedServers.forEach((t) => {
455
+ const r = t.address();
456
+ if (r && typeof r != "string") {
457
+ const e = r;
458
+ this.logger.info(`[SSH] Closing port forward for server ${e.address}:${e.port}`);
459
+ }
460
+ t.close();
461
+ }), this.forwardedServers = [];
462
+ }
447
463
  /**
448
464
  * Checks if a specified host is available by performing a DNS lookup.
449
465
  * @param hostname - The hostname or IP address to check.
450
466
  * @returns A promise resolving with `true` if the host is reachable, otherwise `false`.
451
467
  */
452
- static async checkHostAvailability(r) {
453
- return new Promise((t) => {
454
- it.lookup(r, (e) => {
455
- t(!e);
468
+ static async checkHostAvailability(t) {
469
+ return new Promise((r) => {
470
+ nr.lookup(t, (e) => {
471
+ r(!e);
456
472
  });
457
473
  });
458
474
  }
@@ -461,10 +477,10 @@ class D {
461
477
  * @param privateKey - The private key content to check.
462
478
  * @returns A promise resolving with `true` if a passphrase is required, otherwise `false`.
463
479
  */
464
- static async isPassphraseRequiredForKey(r) {
465
- return new Promise((t, e) => {
480
+ static async isPassphraseRequiredForKey(t) {
481
+ return new Promise((r, e) => {
466
482
  try {
467
- return et.utils.parseKey(r) instanceof Error && t(!0), t(!1);
483
+ return sr.utils.parseKey(t) instanceof Error && r(!0), r(!1);
468
484
  } catch (i) {
469
485
  console.log("Error parsing privateKey"), e(new Error(`ssh.isPassphraseRequiredForKey: err ${i}`));
470
486
  }
@@ -477,30 +493,30 @@ class D {
477
493
  * @param remotePath - The remote file path on the server.
478
494
  * @returns A promise resolving with `true` if the file was successfully uploaded.
479
495
  */
480
- async uploadFile(r, t) {
496
+ async uploadFile(t, r) {
481
497
  return await this.withSftp(async (e) => new Promise((i, o) => {
482
- e.fastPut(r, t, (n) => {
498
+ e.fastPut(t, r, (n) => {
483
499
  if (n) {
484
- const a = new Error(
485
- `ssh.uploadFile: err: ${n}, localPath: ${r}, remotePath: ${t}`
500
+ const c = new Error(
501
+ `ssh.uploadFile: err: ${n}, localPath: ${t}, remotePath: ${r}`
486
502
  );
487
- return o(a);
503
+ return o(c);
488
504
  }
489
505
  i(!0);
490
506
  });
491
507
  }));
492
508
  }
493
- delay(r) {
494
- return new Promise((t, e) => {
495
- setTimeout(() => t(), r);
509
+ delay(t) {
510
+ return new Promise((r, e) => {
511
+ setTimeout(() => r(), t);
496
512
  });
497
513
  }
498
- async withSftp(r) {
499
- return new Promise((t, e) => {
514
+ async withSftp(t) {
515
+ return new Promise((r, e) => {
500
516
  this.client.sftp((i, o) => {
501
517
  if (i)
502
518
  return e(new Error(`ssh.withSftp: sftp err: ${i}`));
503
- r(o).then(t).catch((n) => {
519
+ t(o).then(r).catch((n) => {
504
520
  e(new Error(`ssh.withSftp.callback: err ${n}`));
505
521
  }).finally(() => {
506
522
  o == null || o.end();
@@ -508,20 +524,20 @@ class D {
508
524
  });
509
525
  });
510
526
  }
511
- async writeFileOnTheServer(r, t, e = 432) {
512
- return this.withSftp(async (i) => this.writeFile(i, r, t, e));
527
+ async writeFileOnTheServer(t, r, e = 432) {
528
+ return this.withSftp(async (i) => this.writeFile(i, t, r, e));
513
529
  }
514
- async getForderStructure(r, t, e = { files: [], directories: [] }) {
530
+ async getForderStructure(t, r, e = { files: [], directories: [] }) {
515
531
  return new Promise((i, o) => {
516
- r.readdir(t, async (n, a) => {
532
+ t.readdir(r, async (n, c) => {
517
533
  if (n)
518
534
  return o(n);
519
- for (const c of a) {
520
- const l = `${t}/${c.filename}`;
521
- if (c.attrs.isDirectory()) {
535
+ for (const a of c) {
536
+ const l = `${r}/${a.filename}`;
537
+ if (a.attrs.isDirectory()) {
522
538
  e.directories.push(l);
523
539
  try {
524
- await this.getForderStructure(r, l, e);
540
+ await this.getForderStructure(t, l, e);
525
541
  } catch (h) {
526
542
  return o(h);
527
543
  }
@@ -532,60 +548,60 @@ class D {
532
548
  });
533
549
  });
534
550
  }
535
- rmdir(r, t) {
551
+ rmdir(t, r) {
536
552
  return new Promise((e, i) => {
537
- r.rmdir(t, (o) => o ? i(o) : e(!0));
553
+ t.rmdir(r, (o) => o ? i(o) : e(!0));
538
554
  });
539
555
  }
540
- unlink(r, t) {
556
+ unlink(t, r) {
541
557
  return new Promise((e, i) => {
542
- r.unlink(t, (o) => o ? i(o) : e(!0));
558
+ t.unlink(r, (o) => o ? i(o) : e(!0));
543
559
  });
544
560
  }
545
- async deleteFolder(r) {
546
- return this.withSftp(async (t) => {
561
+ async deleteFolder(t) {
562
+ return this.withSftp(async (r) => {
547
563
  try {
548
- const e = await this.getForderStructure(t, r);
564
+ const e = await this.getForderStructure(r, t);
549
565
  this.logger.info("ssh.deleteFolder list of files and directories"), this.logger.info(`ssh.deleteFolder list of files: ${e.files}`), this.logger.info(`ssh.deleteFolder list of directories: ${e.directories}`);
550
566
  for (const i of e.files)
551
- this.logger.info(`ssh.deleteFolder unlink file ${i}`), await this.unlink(t, i);
567
+ this.logger.info(`ssh.deleteFolder unlink file ${i}`), await this.unlink(r, i);
552
568
  e.directories.sort((i, o) => o.length - i.length);
553
569
  for (const i of e.directories)
554
- this.logger.info(`ssh.deleteFolder rmdir ${i}`), await this.rmdir(t, i);
555
- return await this.rmdir(t, r), !0;
570
+ this.logger.info(`ssh.deleteFolder rmdir ${i}`), await this.rmdir(r, i);
571
+ return await this.rmdir(r, t), !0;
556
572
  } catch (e) {
557
573
  this.logger.error(e);
558
574
  const i = e instanceof Error ? e.message : "";
559
- throw new Error(`ssh.deleteFolder: path: ${r}, message: ${i}`);
575
+ throw new Error(`ssh.deleteFolder: path: ${t}, message: ${i}`);
560
576
  }
561
577
  });
562
578
  }
563
- async readFile(r) {
564
- return this.withSftp(async (t) => new Promise((e, i) => {
565
- t.readFile(r, (o, n) => {
579
+ async readFile(t) {
580
+ return this.withSftp(async (r) => new Promise((e, i) => {
581
+ r.readFile(t, (o, n) => {
566
582
  if (o)
567
583
  return i(new Error(`ssh.readFile: err occurred ${o}`));
568
584
  e(n.toString());
569
585
  });
570
586
  }));
571
587
  }
572
- async chmod(r, t) {
588
+ async chmod(t, r) {
573
589
  return this.withSftp(async (e) => new Promise((i, o) => {
574
- e.chmod(r, t, (n) => n ? o(new Error(`ssh.chmod: ${n}, path: ${r}, mode: ${t}`)) : i(void 0));
590
+ e.chmod(t, r, (n) => n ? o(new Error(`ssh.chmod: ${n}, path: ${t}, mode: ${r}`)) : i(void 0));
575
591
  }));
576
592
  }
577
- async checkFileExists(r) {
578
- return this.withSftp(async (t) => new Promise((e, i) => {
579
- t.stat(r, (o, n) => {
593
+ async checkFileExists(t) {
594
+ return this.withSftp(async (r) => new Promise((e, i) => {
595
+ r.stat(t, (o, n) => {
580
596
  if (o)
581
597
  return o.code === 2 ? e(!1) : i(new Error(`ssh.checkFileExists: err ${o}`));
582
598
  e(n.isFile());
583
599
  });
584
600
  }));
585
601
  }
586
- async checkPathExists(r) {
587
- return this.withSftp(async (t) => new Promise((e, i) => {
588
- t.stat(r, (o, n) => {
602
+ async checkPathExists(t) {
603
+ return this.withSftp(async (r) => new Promise((e, i) => {
604
+ r.stat(t, (o, n) => {
589
605
  if (o)
590
606
  return o.code === 2 ? e({ exists: !1, isFile: !1, isDirectory: !1 }) : i(new Error(`ssh.checkPathExists: ${o}`));
591
607
  e({
@@ -596,37 +612,37 @@ class D {
596
612
  });
597
613
  }));
598
614
  }
599
- async writeFile(r, t, e, i = 432) {
615
+ async writeFile(t, r, e, i = 432) {
600
616
  return new Promise((o, n) => {
601
- r.writeFile(t, e, { mode: i }, (a) => {
602
- if (a)
603
- return n(new Error(`ssh.writeFile: err ${a}, remotePath: ${t}`));
617
+ t.writeFile(r, e, { mode: i }, (c) => {
618
+ if (c)
619
+ return n(new Error(`ssh.writeFile: err ${c}, remotePath: ${r}`));
604
620
  o(!0);
605
621
  });
606
622
  });
607
623
  }
608
- uploadFileUsingExistingSftp(r, t, e, i = 432) {
624
+ uploadFileUsingExistingSftp(t, r, e, i = 432) {
609
625
  return new Promise((o, n) => {
610
- Z(t).then(async (a) => {
611
- this.writeFile(r, e, a, i).then(() => {
626
+ Q(r).then(async (c) => {
627
+ this.writeFile(t, e, c, i).then(() => {
612
628
  o(void 0);
613
- }).catch((c) => {
614
- const l = `uploadFileUsingExistingSftp: error ${c} occurred`;
629
+ }).catch((a) => {
630
+ const l = `uploadFileUsingExistingSftp: error ${a} occurred`;
615
631
  this.logger.error(l), n(new Error(l));
616
632
  });
617
633
  });
618
634
  });
619
635
  }
620
- async __uploadDirectory(r, t, e, i = 432) {
636
+ async __uploadDirectory(t, r, e, i = 432) {
621
637
  return new Promise((o, n) => {
622
- E.readdir(t, async (a, c) => {
623
- if (a)
624
- return n(new Error(`ssh.__uploadDir: err ${a}, localDir: ${t}, remoteDir: ${e}`));
638
+ E.readdir(r, async (c, a) => {
639
+ if (c)
640
+ return n(new Error(`ssh.__uploadDir: err ${c}, localDir: ${r}, remoteDir: ${e}`));
625
641
  try {
626
- await this.__createRemoteDirectory(r, e);
627
- for (const l of c) {
628
- const h = u.join(t, l), m = `${e}/${l}`;
629
- E.lstatSync(h).isDirectory() ? await this.__uploadDirectory(r, h, m, i) : await this.uploadFileUsingExistingSftp(r, h, m, i);
642
+ await this.__createRemoteDirectory(t, e);
643
+ for (const l of a) {
644
+ const h = d.join(r, l), u = `${e}/${l}`;
645
+ E.lstatSync(h).isDirectory() ? await this.__uploadDirectory(t, h, u, i) : await this.uploadFileUsingExistingSftp(t, h, u, i);
630
646
  }
631
647
  o();
632
648
  } catch (l) {
@@ -642,10 +658,10 @@ class D {
642
658
  * @param remoteDir - The path to the remote directory on the server.
643
659
  * @returns A promise that resolves when the directory and its contents are uploaded.
644
660
  */
645
- async uploadDirectory(r, t, e = 432) {
661
+ async uploadDirectory(t, r, e = 432) {
646
662
  return new Promise((i, o) => {
647
663
  this.withSftp(async (n) => {
648
- await this.__uploadDirectory(n, r, t, e), i();
664
+ await this.__uploadDirectory(n, t, r, e), i();
649
665
  });
650
666
  });
651
667
  }
@@ -655,22 +671,22 @@ class D {
655
671
  * @param remotePath - The path to the remote directory.
656
672
  * @returns A promise that resolves when the directory is created.
657
673
  */
658
- __createRemoteDirectory(r, t) {
674
+ __createRemoteDirectory(t, r) {
659
675
  return new Promise((e, i) => {
660
- const o = t.split("/");
676
+ const o = r.split("/");
661
677
  let n = "";
662
- const a = (c) => {
663
- if (c >= o.length)
678
+ const c = (a) => {
679
+ if (a >= o.length)
664
680
  return e();
665
- n += `${o[c]}/`, r.stat(n, (l) => {
666
- l ? r.mkdir(n, (h) => {
681
+ n += `${o[a]}/`, t.stat(n, (l) => {
682
+ l ? t.mkdir(n, (h) => {
667
683
  if (h)
668
- return i(new Error(`ssh.__createRemDir: err ${h}, remotePath: ${t}`));
669
- a(c + 1);
670
- }) : a(c + 1);
684
+ return i(new Error(`ssh.__createRemDir: err ${h}, remotePath: ${r}`));
685
+ c(a + 1);
686
+ }) : c(a + 1);
671
687
  });
672
688
  };
673
- a(0);
689
+ c(0);
674
690
  });
675
691
  }
676
692
  /**
@@ -679,25 +695,25 @@ class D {
679
695
  * @param remotePath - The path to the remote directory.
680
696
  * @returns A promise that resolves when the directory is created.
681
697
  */
682
- createRemoteDirectory(r, t = 493) {
698
+ createRemoteDirectory(t, r = 493) {
683
699
  return this.withSftp(async (e) => {
684
- const i = r.split("/");
700
+ const i = t.split("/");
685
701
  let o = "";
686
702
  for (const n of i) {
687
703
  o += `${n}/`;
688
704
  try {
689
- await new Promise((a, c) => {
705
+ await new Promise((c, a) => {
690
706
  e.stat(o, (l) => {
691
- if (!l) return a();
692
- e.mkdir(o, { mode: t }, (h) => {
707
+ if (!l) return c();
708
+ e.mkdir(o, { mode: r }, (h) => {
693
709
  if (h)
694
- return c(new Error(`ssh.createRemoteDir: err ${h}, remotePath: ${r}`));
695
- a();
710
+ return a(new Error(`ssh.createRemoteDir: err ${h}, remotePath: ${t}`));
711
+ c();
696
712
  });
697
713
  });
698
714
  });
699
- } catch (a) {
700
- throw console.error(`Failed to create directory: ${o}`, a), a;
715
+ } catch (c) {
716
+ throw console.error(`Failed to create directory: ${o}`, c), c;
701
717
  }
702
718
  }
703
719
  });
@@ -708,81 +724,96 @@ class D {
708
724
  * @param localPath - The local file path to save the file.
709
725
  * @returns A promise resolving with `true` if the file was successfully downloaded.
710
726
  */
711
- async downloadFile(r, t) {
727
+ async downloadFile(t, r) {
712
728
  return this.withSftp(async (e) => new Promise((i, o) => {
713
- e.fastGet(r, t, (n) => {
729
+ e.fastGet(t, r, (n) => {
714
730
  if (n)
715
- return o(new Error(`ssh.downloadFile: err ${n}, remotePath: ${r}, localPath: ${t}`));
731
+ return o(new Error(`ssh.downloadFile: err ${n}, remotePath: ${t}, localPath: ${r}`));
716
732
  i(!0);
717
733
  });
718
734
  }));
719
735
  }
720
736
  /**
721
- * Closes the SSH client connection.
737
+ * Closes the SSH client connection and forwarded ports.
722
738
  */
723
739
  close() {
724
- this.client.end();
740
+ this.closeForwardedPorts(), this.client.end();
725
741
  }
726
742
  }
727
- const bt = "minio-2024-12-18T13-15-44Z", Dt = "supervisord-0.7.3", At = "supervisord_0.7.3_Linux_64-bit";
728
- function y(s) {
729
- return u.join(s, "platforma_ssh");
743
+ async function Ar(s, t, r, e) {
744
+ return new Promise((i, o) => {
745
+ s.on("ready", () => {
746
+ i(s);
747
+ }), s.on("error", (n) => {
748
+ o(new Error(`ssh.connect: error occurred: ${n}`));
749
+ }), s.on("close", () => {
750
+ }), s.connect(t);
751
+ });
730
752
  }
731
- function P(s) {
732
- return u.join(s, "platforma_ssh", "binaries");
753
+ async function xr(s, t, r, e, i, o) {
754
+ return new Promise((n, c) => {
755
+ t.forwardOut(r, e, i, o, (a, l) => a ? (s.error(`forwardOut.error: ${a}`), c(a)) : n(l));
756
+ });
757
+ }
758
+ const Or = "minio-2024-12-18T13-15-44Z", kr = "supervisord-0.7.3", Rr = "supervisord_0.7.3_Linux_64-bit";
759
+ function $(s) {
760
+ return d.join(s, "platforma_ssh");
733
761
  }
734
- function kt(s, r) {
735
- return u.join(P(s), `pl-${F()}-${$(r)}`);
762
+ function v(s) {
763
+ return d.join(s, "platforma_ssh", "binaries");
736
764
  }
737
- function J(s, r) {
738
- return u.join(kt(s, r), "binaries");
765
+ function Nr(s, t) {
766
+ return d.join(v(s), `pl-${b()}-${P(t)}`);
739
767
  }
740
- function x(s, r) {
741
- return u.join(J(s, r), "platforma");
768
+ function L(s, t) {
769
+ return d.join(Nr(s, t), "binaries");
742
770
  }
743
- function xt(s, r) {
744
- return u.join(J(s, r), "free-port");
771
+ function R(s, t) {
772
+ return d.join(L(s, t), "platforma");
745
773
  }
746
- function M(s, r) {
747
- return u.join(P(s), `minio-2024-12-18T13-15-44Z-${$(r)}`);
774
+ function Ur(s, t) {
775
+ return d.join(L(s, t), "free-port");
748
776
  }
749
- function Ot(s, r) {
750
- return u.join(M(s, r), "minio");
777
+ function z(s, t) {
778
+ return d.join(v(s), `minio-2024-12-18T13-15-44Z-${P(t)}`);
751
779
  }
752
- function Rt(s, r) {
753
- return u.join(P(s), `supervisord-0.7.3-${$(r)}`, At);
780
+ function Tr(s, t) {
781
+ return d.join(z(s, t), "minio");
754
782
  }
755
- function L(s, r) {
756
- return u.join(Rt(s, r), "supervisord");
783
+ function Br(s, t) {
784
+ return d.join(v(s), `supervisord-0.7.3-${P(t)}`, Rr);
757
785
  }
758
- function z(s) {
759
- return u.join(y(s), "supervisor.conf");
786
+ function W(s, t) {
787
+ return d.join(Br(s, t), "supervisord");
760
788
  }
761
- function O(s) {
762
- return u.join(y(s), "connection.txt");
789
+ function K(s) {
790
+ return d.join($(s), "supervisor.conf");
763
791
  }
764
- async function Nt(s, r, t) {
765
- const e = await A(s, r, t, "--daemon");
792
+ function N(s) {
793
+ return d.join($(s), "connection.txt");
794
+ }
795
+ async function _r(s, t, r) {
796
+ const e = await O(s, t, r, "--daemon");
766
797
  if (e.stderr)
767
798
  throw new Error(`Can not run ssh Platforma ${e.stderr}`);
768
799
  }
769
- async function Ut(s, r, t) {
770
- const e = await A(s, r, t, "ctl shutdown");
800
+ async function jr(s, t, r) {
801
+ const e = await O(s, t, r, "ctl shutdown");
771
802
  if (e.stderr)
772
803
  throw new Error(`Can not stop ssh Platforma ${e.stderr}`);
773
804
  }
774
- async function Tt(s, r, t, e) {
775
- const i = await A(r, t, e, "ctl status");
805
+ async function Ir(s, t, r, e) {
806
+ const i = await O(t, r, e, "ctl status");
776
807
  if (i.stderr)
777
808
  return s.info(`supervisord ctl status: stderr occurred: ${i.stderr}, stdout: ${i.stdout}`), !1;
778
809
  const o = {
779
- platforma: R(i.stdout, "platforma"),
780
- minio: R(i.stdout, "minio")
810
+ platforma: U(i.stdout, "platforma"),
811
+ minio: U(i.stdout, "minio")
781
812
  };
782
813
  return o.platforma && o.minio ? !0 : (o.minio || s.warn("Minio is not running on the server"), o.platforma || s.warn("Platforma is not running on the server"), !1);
783
814
  }
784
- function Bt(s, r, t, e, i, o, n) {
785
- const a = Object.entries(r).map(([h, m]) => `${h}="${m}"`).join(","), c = nt(16).toString("hex"), l = t;
815
+ function Hr(s, t, r, e, i, o, n) {
816
+ const c = Object.entries(t).map(([h, u]) => `${h}="${u}"`).join(","), a = F(16).toString("hex"), l = r;
786
817
  return `
787
818
  [supervisord]
788
819
  logfile=${e}/supervisord.log
@@ -792,12 +823,12 @@ pidfile=${e}/supervisord.pid
792
823
  [inet_http_server]
793
824
  port=127.0.0.1:${l}
794
825
  username=default-user
795
- password=${c}
826
+ password=${a}
796
827
 
797
828
  [supervisorctl]
798
829
  serverurl=http://127.0.0.1:${l}
799
830
  username=default-user
800
- password=${c}
831
+ password=${a}
801
832
 
802
833
  [program:platforma]
803
834
  autostart=true
@@ -808,27 +839,27 @@ autorestart=true
808
839
 
809
840
  [program:minio]
810
841
  autostart=true
811
- environment=${a}
842
+ environment=${c}
812
843
  command=${o} server ${s}
813
844
  directory=${e}
814
845
  autorestart=true
815
846
  `;
816
847
  }
817
- async function A(s, r, t, e) {
818
- const i = L(r, t), o = z(r), n = `${i} --configuration ${o} ${e}`;
848
+ async function O(s, t, r, e) {
849
+ const i = W(t, r), o = K(t), n = `${i} --configuration ${o} ${e}`;
819
850
  return await s.exec(n);
820
851
  }
821
- function R(s, r) {
852
+ function U(s, t) {
822
853
  return ((i) => i.replace(/\x1B\[[0-9;]*m/g, ""))(s).split(`
823
854
  `).some((i) => {
824
855
  const [o, n] = i.trim().split(/\s{2,}/);
825
- return o === r && n === "Running";
856
+ return o === t && n === "Running";
826
857
  });
827
858
  }
828
- class W {
829
- constructor(r, t, e) {
830
- d(this, "initState", {});
831
- this.logger = r, this.sshClient = t, this.username = e;
859
+ class q {
860
+ constructor(t, r, e) {
861
+ w(this, "initState", {});
862
+ this.logger = t, this.sshClient = r, this.username = e;
832
863
  }
833
864
  info() {
834
865
  return {
@@ -836,141 +867,144 @@ class W {
836
867
  initState: this.initState
837
868
  };
838
869
  }
839
- static async init(r, t) {
870
+ static async init(t, r) {
840
871
  try {
841
- const e = await D.init(r, t);
842
- return new W(r, e, p(t.username));
872
+ const e = await x.init(t, r);
873
+ return new q(t, e, p(r.username));
843
874
  } catch (e) {
844
- throw r.error(`Connection error in SshClient.init: ${e}`), e;
875
+ throw t.error(`Connection error in SshClient.init: ${e}`), e;
845
876
  }
846
877
  }
878
+ cleanUp() {
879
+ this.sshClient.close();
880
+ }
847
881
  async isAlive() {
848
- const r = await this.getArch(), t = await this.getUserHomeDirectory();
882
+ const t = await this.getArch(), r = await this.getUserHomeDirectory();
849
883
  try {
850
- return await Tt(this.logger, this.sshClient, t, r.arch);
884
+ return await Ir(this.logger, this.sshClient, r, t.arch);
851
885
  } catch {
852
886
  return !1;
853
887
  }
854
888
  }
855
889
  async start() {
856
- const r = await this.getArch(), t = await this.getUserHomeDirectory();
890
+ const t = await this.getArch(), r = await this.getUserHomeDirectory();
857
891
  try {
858
- return await Nt(this.sshClient, t, r.arch), await this.checkIsAliveWithInterval();
892
+ return await _r(this.sshClient, r, t.arch), await this.checkIsAliveWithInterval();
859
893
  } catch (e) {
860
894
  const i = `ssh.start: error occurred ${e}`;
861
895
  throw this.logger.error(i), new Error(i);
862
896
  }
863
897
  }
864
898
  async stop() {
865
- const r = await this.getArch(), t = await this.getUserHomeDirectory();
899
+ const t = await this.getArch(), r = await this.getUserHomeDirectory();
866
900
  try {
867
- return await Ut(this.sshClient, t, r.arch), await this.checkIsAliveWithInterval(void 0, void 0, !1);
901
+ return await jr(this.sshClient, r, t.arch), await this.checkIsAliveWithInterval(void 0, void 0, !1);
868
902
  } catch (e) {
869
903
  const i = `ssh.stop: error occurred ${e}`;
870
904
  throw this.logger.error(i), new Error(i);
871
905
  }
872
906
  }
873
907
  async reset() {
874
- const r = await this.getUserHomeDirectory();
875
- return this.logger.info("pl.reset: Stop Platforma on the server"), await this.stop(), this.logger.info(`pl.reset: Deleting Platforma workDir ${r} on the server`), await this.sshClient.deleteFolder(y(r)), !0;
908
+ const t = await this.getUserHomeDirectory();
909
+ return this.logger.info("pl.reset: Stop Platforma on the server"), await this.stop(), this.logger.info(`pl.reset: Deleting Platforma workDir ${t} on the server`), await this.sshClient.deleteFolder($(t)), this.cleanUp(), !0;
876
910
  }
877
- async platformaInit(r) {
878
- const t = { localWorkdir: r };
911
+ async platformaInit(t) {
912
+ const r = { localWorkdir: t };
879
913
  try {
880
- if (t.arch = await this.getArch(), t.remoteHome = await this.getUserHomeDirectory(), t.isAlive = await this.isAlive(), t.isAlive) {
881
- if (t.userCredentials = await this.getUserCredentials(t.remoteHome), !t.userCredentials)
914
+ if (r.arch = await this.getArch(), r.remoteHome = await this.getUserHomeDirectory(), r.isAlive = await this.isAlive(), r.isAlive) {
915
+ if (r.userCredentials = await this.getUserCredentials(r.remoteHome), !r.userCredentials)
882
916
  throw new Error("SshPl.platformaInit: platforma is alive but userCredentials are not found");
883
- return t.userCredentials;
917
+ return r.userCredentials;
884
918
  }
885
919
  const e = await this.downloadBinariesAndUploadToTheServer(
886
- r,
887
- t.remoteHome,
888
- t.arch
920
+ t,
921
+ r.remoteHome,
922
+ r.arch
889
923
  );
890
- if (t.binPaths = { ...e, history: void 0 }, t.downloadedBinaries = e.history, t.ports = await this.fetchPorts(t.remoteHome, t.arch), !t.ports.debug.remote || !t.ports.grpc.remote || !t.ports.minioPort.remote || !t.ports.minioConsolePort.remote || !t.ports.monitoring.remote)
924
+ if (r.binPaths = { ...e, history: void 0 }, r.downloadedBinaries = e.history, r.ports = await this.fetchPorts(r.remoteHome, r.arch), !r.ports.debug.remote || !r.ports.grpc.remote || !r.ports.minioPort.remote || !r.ports.minioConsolePort.remote || !r.ports.monitoring.remote)
891
925
  throw new Error("SshPl.platformaInit: remote ports are not defined");
892
- const i = await ot({
926
+ const i = await ar({
893
927
  logger: this.logger,
894
- workingDir: y(t.remoteHome),
928
+ workingDir: $(r.remoteHome),
895
929
  portsMode: {
896
930
  type: "customWithMinio",
897
931
  ports: {
898
- debug: t.ports.debug.remote,
899
- grpc: t.ports.grpc.remote,
900
- minio: t.ports.minioPort.remote,
901
- minioConsole: t.ports.minioConsolePort.remote,
902
- monitoring: t.ports.monitoring.remote,
903
- grpcLocal: t.ports.grpc.local,
904
- minioLocal: t.ports.minioPort.local
932
+ debug: r.ports.debug.remote,
933
+ grpc: r.ports.grpc.remote,
934
+ minio: r.ports.minioPort.remote,
935
+ minioConsole: r.ports.minioConsolePort.remote,
936
+ monitoring: r.ports.monitoring.remote,
937
+ grpcLocal: r.ports.grpc.local,
938
+ minioLocal: r.ports.minioPort.local
905
939
  }
906
940
  },
907
941
  licenseMode: {
908
942
  type: "env"
909
943
  }
910
944
  });
911
- t.generatedConfig = { ...i, filesToCreate: { skipped: "it is too wordy" } };
912
- for (const [a, c] of Object.entries(i.filesToCreate))
913
- await this.sshClient.writeFileOnTheServer(a, c), this.logger.info(`Created file ${a}`);
914
- for (const a of i.dirsToCreate)
915
- await this.sshClient.createRemoteDirectory(a), this.logger.info(`Created directory ${a}`);
916
- const o = Bt(
945
+ r.generatedConfig = { ...i, filesToCreate: { skipped: "it is too wordy" } };
946
+ for (const [c, a] of Object.entries(i.filesToCreate))
947
+ await this.sshClient.writeFileOnTheServer(c, a), this.logger.info(`Created file ${c}`);
948
+ for (const c of i.dirsToCreate)
949
+ await this.sshClient.createRemoteDirectory(c), this.logger.info(`Created directory ${c}`);
950
+ const o = Hr(
917
951
  i.minioConfig.storageDir,
918
952
  i.minioConfig.envs,
919
- await this.getFreePortForPlatformaOnServer(t.remoteHome, t.arch),
953
+ await this.getFreePortForPlatformaOnServer(r.remoteHome, r.arch),
920
954
  i.workingDir,
921
955
  i.plConfig.configPath,
922
- t.binPaths.minioRelPath,
923
- t.binPaths.downloadedPl
956
+ r.binPaths.minioRelPath,
957
+ r.binPaths.downloadedPl
924
958
  );
925
- if (!await this.sshClient.writeFileOnTheServer(z(t.remoteHome), o))
926
- throw new Error(`Can not write supervisord config on the server ${y(t.remoteHome)}`);
927
- return t.connectionInfo = {
959
+ if (!await this.sshClient.writeFileOnTheServer(K(r.remoteHome), o))
960
+ throw new Error(`Can not write supervisord config on the server ${$(r.remoteHome)}`);
961
+ return r.connectionInfo = {
928
962
  plUser: i.plUser,
929
963
  plPassword: i.plPassword,
930
- ports: t.ports
964
+ ports: r.ports
931
965
  }, await this.sshClient.writeFileOnTheServer(
932
- O(t.remoteHome),
933
- JSON.stringify(t.connectionInfo, void 0, 2)
934
- ), await this.start(), t.started = !0, this.initState = t, {
966
+ N(r.remoteHome),
967
+ JSON.stringify(r.connectionInfo, void 0, 2)
968
+ ), await this.start(), r.started = !0, this.initState = r, {
935
969
  plUser: i.plUser,
936
970
  plPassword: i.plPassword,
937
- ports: t.ports
971
+ ports: r.ports
938
972
  };
939
973
  } catch (e) {
940
- const i = `SshPl.platformaInit: error occurred: ${e}, state: ${JSON.stringify(t)}`;
974
+ const i = `SshPl.platformaInit: error occurred: ${e}, state: ${JSON.stringify(r)}`;
941
975
  throw this.logger.error(i), new Error(i);
942
976
  }
943
977
  }
944
- async downloadBinariesAndUploadToTheServer(r, t, e) {
978
+ async downloadBinariesAndUploadToTheServer(t, r, e) {
945
979
  const i = [];
946
980
  try {
947
981
  const o = await this.downloadAndUntar(
948
- r,
949
982
  t,
983
+ r,
950
984
  e,
951
985
  "pl",
952
- `pl-${F()}`
986
+ `pl-${b()}`
953
987
  );
954
988
  i.push(o);
955
989
  const n = await this.downloadAndUntar(
956
- r,
957
990
  t,
991
+ r,
958
992
  e,
959
993
  "supervisord",
960
- Dt
994
+ kr
961
995
  );
962
996
  i.push(n);
963
- const a = Ot(t, e.arch), c = await this.downloadAndUntar(
964
- r,
997
+ const c = Tr(r, e.arch), a = await this.downloadAndUntar(
965
998
  t,
999
+ r,
966
1000
  e,
967
1001
  "minio",
968
- bt
1002
+ Or
969
1003
  );
970
- return i.push(c), await this.sshClient.chmod(a, 488), {
1004
+ return i.push(a), await this.sshClient.chmod(c, 488), {
971
1005
  history: i,
972
- minioRelPath: a,
973
- downloadedPl: x(t, e.arch)
1006
+ minioRelPath: c,
1007
+ downloadedPl: R(r, e.arch)
974
1008
  };
975
1009
  } catch (o) {
976
1010
  const n = `SshPl.downloadBinariesAndUploadToServer: error ${o} occurred, state: ${JSON.stringify(i)}`;
@@ -981,27 +1015,27 @@ class W {
981
1015
  * because Windows doesn't support symlinks
982
1016
  * that are found in linux pl binaries tgz archive.
983
1017
  * For this reason, we extract all to the remote server. */
984
- async downloadAndUntar(r, t, e, i, o) {
1018
+ async downloadAndUntar(t, r, e, i, o) {
985
1019
  const n = {};
986
- n.binBasePath = P(t), await this.sshClient.createRemoteDirectory(n.binBasePath), n.binBasePathCreated = !0;
987
- let a = null;
988
- const c = 5;
989
- for (let h = 1; h <= c; h++)
1020
+ n.binBasePath = v(r), await this.sshClient.createRemoteDirectory(n.binBasePath), n.binBasePathCreated = !0;
1021
+ let c = null;
1022
+ const a = 5;
1023
+ for (let h = 1; h <= a; h++)
990
1024
  try {
991
- a = await lt(
1025
+ c = await dr(
992
1026
  this.logger,
993
- r,
1027
+ t,
994
1028
  i,
995
1029
  o,
996
1030
  e.arch,
997
1031
  e.platform
998
1032
  );
999
1033
  break;
1000
- } catch (m) {
1001
- if (await S(300), h == c)
1002
- throw new Error(`downloadAndUntar: ${c} attempts, last error: ${m}`);
1034
+ } catch (u) {
1035
+ if (await C(300), h == a)
1036
+ throw new Error(`downloadAndUntar: ${a} attempts, last error: ${u}`);
1003
1037
  }
1004
- n.downloadResult = p(a), n.localArchivePath = u.resolve(n.downloadResult.archivePath), n.remoteDir = u.join(n.binBasePath, n.downloadResult.baseName), n.remoteArchivePath = n.remoteDir + ".tgz", await this.sshClient.createRemoteDirectory(n.remoteDir), await this.sshClient.uploadFile(n.localArchivePath, n.remoteArchivePath);
1038
+ n.downloadResult = p(c), n.localArchivePath = d.resolve(n.downloadResult.archivePath), n.remoteDir = d.join(n.binBasePath, n.downloadResult.baseName), n.remoteArchivePath = n.remoteDir + ".tgz", await this.sshClient.createRemoteDirectory(n.remoteDir), await this.sshClient.uploadFile(n.localArchivePath, n.remoteArchivePath);
1005
1039
  const l = await this.sshClient.exec(
1006
1040
  `tar xvf ${n.remoteArchivePath} --directory=${n.remoteDir}`
1007
1041
  );
@@ -1009,67 +1043,67 @@ class W {
1009
1043
  throw new Error(`downloadAndUntar: untar: stderr occurred: ${l.stderr}, stdout: ${l.stdout}`);
1010
1044
  return n.plUntarDone = !0, n;
1011
1045
  }
1012
- async needDownload(r, t) {
1013
- const e = L(r, t.arch), i = M(r, t.arch), o = x(r, t.arch);
1046
+ async needDownload(t, r) {
1047
+ const e = W(t, r.arch), i = z(t, r.arch), o = R(t, r.arch);
1014
1048
  return !await this.sshClient.checkFileExists(o) || !await this.sshClient.checkFileExists(i) || !await this.sshClient.checkFileExists(e);
1015
1049
  }
1016
- async checkIsAliveWithInterval(r = 1e3, t = 15, e = !0) {
1017
- const i = t * r;
1050
+ async checkIsAliveWithInterval(t = 1e3, r = 15, e = !0) {
1051
+ const i = r * t;
1018
1052
  let o = 0, n = await this.isAlive();
1019
1053
  for (; e ? !n : n; ) {
1020
- if (await S(r), o += r, o > i)
1054
+ if (await C(t), o += t, o > i)
1021
1055
  throw new Error(`isAliveWithInterval: The process did not ${e ? "started" : "stopped"} after ${i} ms.`);
1022
1056
  n = await this.isAlive();
1023
1057
  }
1024
1058
  }
1025
- async getUserCredentials(r) {
1026
- const t = await this.sshClient.readFile(O(r));
1027
- return JSON.parse(t);
1059
+ async getUserCredentials(t) {
1060
+ const r = await this.sshClient.readFile(N(t));
1061
+ return JSON.parse(r);
1028
1062
  }
1029
- async fetchPorts(r, t) {
1063
+ async fetchPorts(t, r) {
1030
1064
  return {
1031
1065
  grpc: {
1032
- local: await g(),
1033
- remote: await this.getFreePortForPlatformaOnServer(r, t)
1066
+ local: await y(),
1067
+ remote: await this.getFreePortForPlatformaOnServer(t, r)
1034
1068
  },
1035
1069
  monitoring: {
1036
- local: await g(),
1037
- remote: await this.getFreePortForPlatformaOnServer(r, t)
1070
+ local: await y(),
1071
+ remote: await this.getFreePortForPlatformaOnServer(t, r)
1038
1072
  },
1039
1073
  debug: {
1040
- local: await g(),
1041
- remote: await this.getFreePortForPlatformaOnServer(r, t)
1074
+ local: await y(),
1075
+ remote: await this.getFreePortForPlatformaOnServer(t, r)
1042
1076
  },
1043
1077
  minioPort: {
1044
- local: await g(),
1045
- remote: await this.getFreePortForPlatformaOnServer(r, t)
1078
+ local: await y(),
1079
+ remote: await this.getFreePortForPlatformaOnServer(t, r)
1046
1080
  },
1047
1081
  minioConsolePort: {
1048
- local: await g(),
1049
- remote: await this.getFreePortForPlatformaOnServer(r, t)
1082
+ local: await y(),
1083
+ remote: await this.getFreePortForPlatformaOnServer(t, r)
1050
1084
  }
1051
1085
  };
1052
1086
  }
1053
1087
  async getLocalFreePort() {
1054
- return new Promise((r) => {
1055
- const t = U.createServer();
1056
- t.listen(0, () => {
1057
- const e = t.address().port;
1058
- t.close((i) => r(e));
1088
+ return new Promise((t) => {
1089
+ const r = B.createServer();
1090
+ r.listen(0, () => {
1091
+ const e = r.address().port;
1092
+ r.close((i) => t(e));
1059
1093
  });
1060
1094
  });
1061
1095
  }
1062
- async getFreePortForPlatformaOnServer(r, t) {
1063
- const e = xt(r, t.arch), { stdout: i, stderr: o } = await this.sshClient.exec(`${e}`);
1096
+ async getFreePortForPlatformaOnServer(t, r) {
1097
+ const e = Ur(t, r.arch), { stdout: i, stderr: o } = await this.sshClient.exec(`${e}`);
1064
1098
  if (o)
1065
1099
  throw new Error(`getFreePortForPlatformaOnServer: stderr is not empty: ${o}, stdout: ${i}`);
1066
1100
  return +i;
1067
1101
  }
1068
1102
  async getArch() {
1069
- const { stdout: r, stderr: t } = await this.sshClient.exec("uname -s && uname -m");
1070
- if (t)
1071
- throw new Error(`getArch: stderr is not empty: ${t}, stdout: ${r}`);
1072
- const e = r.split(`
1103
+ const { stdout: t, stderr: r } = await this.sshClient.exec("uname -s && uname -m");
1104
+ if (r)
1105
+ throw new Error(`getArch: stderr is not empty: ${r}, stdout: ${t}`);
1106
+ const e = t.split(`
1073
1107
  `);
1074
1108
  return {
1075
1109
  platform: e[0],
@@ -1077,20 +1111,20 @@ class W {
1077
1111
  };
1078
1112
  }
1079
1113
  async getUserHomeDirectory() {
1080
- const { stdout: r, stderr: t } = await this.sshClient.exec("echo $HOME");
1081
- if (t) {
1114
+ const { stdout: t, stderr: r } = await this.sshClient.exec("echo $HOME");
1115
+ if (r) {
1082
1116
  const e = `/home/${this.username}`;
1083
- return console.warn(`getUserHomeDirectory: stderr is not empty: ${t}, stdout: ${r}, will get a default home: ${e}`), e;
1117
+ return console.warn(`getUserHomeDirectory: stderr is not empty: ${r}, stdout: ${t}, will get a default home: ${e}`), e;
1084
1118
  }
1085
- return r.trim();
1119
+ return t.trim();
1086
1120
  }
1087
1121
  }
1088
1122
  export {
1089
- St as LocalConfigYaml,
1090
- Et as LocalPl,
1091
- D as SshClient,
1092
- W as SshPl,
1093
- F as getDefaultPlVersion,
1094
- Xt as localPlatformaInit
1123
+ Er as LocalConfigYaml,
1124
+ Fr as LocalPl,
1125
+ x as SshClient,
1126
+ q as SshPl,
1127
+ b as getDefaultPlVersion,
1128
+ it as localPlatformaInit
1095
1129
  };
1096
1130
  //# sourceMappingURL=index.mjs.map