@milaboratories/pl-deployments 2.4.2 → 2.4.4
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/README.md +18 -1
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/dist/ssh/__tests__/common-utils.d.ts +2 -8
- package/dist/ssh/__tests__/common-utils.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/ssh/__tests__/common-utils.ts +20 -81
- package/src/ssh/__tests__/pl-docker.test.ts +13 -11
- package/src/ssh/__tests__/ssh-docker.test.ts +51 -8
package/README.md
CHANGED
|
@@ -1 +1,18 @@
|
|
|
1
|
-
# pl-
|
|
1
|
+
# pl-deployments
|
|
2
|
+
|
|
3
|
+
## Test tips
|
|
4
|
+
|
|
5
|
+
### Regenerate `ssh` keys
|
|
6
|
+
`ssh2` lib has a way to generate private and public ssh keys.
|
|
7
|
+
|
|
8
|
+
`cd` into pl-deployments and then run it in `node` repl:
|
|
9
|
+
|
|
10
|
+
```js
|
|
11
|
+
let keys = require("ssh2").utils.generateKeyPairSync('ecdsa', { bits: 256, comment: 'node.js rules!', passphrase: 'password', cipher: 'aes256-cbc' });
|
|
12
|
+
|
|
13
|
+
require("fs").writeFileSync('test-assets/public-key.pem', keys.public);
|
|
14
|
+
require("fs").writeFileSync('test-assets/private-key.pem', keys.private);
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### Clean docker containers
|
|
18
|
+
If something went wrong with testcontainers, use `pnpm run cleanup-docker` from pl-deployments root.
|
package/dist/index.js
CHANGED
|
@@ -4,7 +4,7 @@ wd: ${t.opts.cwd}`),n.info(" spawning child process"),lt.spawn(t.cmd,t.args,t.o
|
|
|
4
4
|
URL: ${t}
|
|
5
5
|
Save to: ${e}`);const{body:i,statusCode:o}=await dt.request(t);if(r.statusCode=o,o!=200){const s=await ut.text(i);throw r.errorMsg=`failed to download archive: ${o}, response: ${s.slice(0,1e3)}`,n.error(r.errorMsg),new Error(r.errorMsg)}return r.tmpPath=e+".tmp",await U.Readable.toWeb(i).pipeTo(U.Writable.toWeb(x.createWriteStream(r.tmpPath))),r.wroteTmp=!0,r.tmpExisted=await p.fileExists(r.tmpPath),await y.rename(r.tmpPath,e),r.renamed=!0,r.newExisted=await p.fileExists(e),r}catch(i){const o=`downloadArchive: ${JSON.stringify(i)}, state: ${JSON.stringify(r)}`;throw n.error(o),new Error(o)}}const Pt=".ok";async function bt(n,t,e,r){if(n.info("extracting archive..."),n.info(` archive path: '${t}'`),n.info(` target dir: '${r}'`),!await p.fileExists(t)){const o=`Platforma Backend binary archive not found at '${t}'`;throw n.error(o),new Error(o)}const i=u.join(r,Pt);if(await p.fileExists(i)){n.info(`Platforma Backend binaries unpack skipped: '${r}' exists`);return}switch(await p.fileExists(r)&&(n.info(`Removing previous incompletely unpacked folder: '${r}'`),await y.rm(r,{recursive:!0})),n.info(` creating target dir '${r}'`),await y.mkdir(r,{recursive:!0}),n.info(`Unpacking Platforma Backend archive:
|
|
6
6
|
Archive: ${t}
|
|
7
|
-
Target dir: ${r}`),e){case"tgz":await ft.x({file:t,cwd:r,gzip:!0});break;case"zip":await pt(t,r);break;default:p.assertNever(e)}await y.writeFile(i,"ok"),n.info(" ... unpack done.")}const Ft={linux:"tgz",macos:"tgz",windows:"zip"};function A(){return"1.
|
|
7
|
+
Target dir: ${r}`),e){case"tgz":await ft.x({file:t,cwd:r,gzip:!0});break;case"zip":await pt(t,r);break;default:p.assertNever(e)}await y.writeFile(i,"ok"),n.info(" ... unpack done.")}const Ft={linux:"tgz",macos:"tgz",windows:"zip"};function A(){return"1.35.2"}function At(){return{type:"Download",version:A()}}async function Dt(n,t,e){switch(e.type){case"Download":const r=await Et(n,t,"pl",`pl-${e.version}`,P.arch(),P.platform());return u.join(r.baseName,"binaries",Ot[R(P.platform())]);case"Local":return e.path;default:p.assertNever(e)}}const Ot={linux:"platforma",macos:"platforma",windows:"platforma.exe"};function L(n){return u.join(n,"pl_pid")}async function xt(n){if(!await p.fileExists(n))return;const t=await y.readFile(n);return Number(t.toString())}async function kt(n,t){await y.writeFile(n,JSON.stringify(t))}function Rt(){return{}}function Nt(n,t,e){return n[t]=e,e}async function N(n,t){const e=Rt();try{return await t((i,o)=>Nt(e,i,o),e)}catch(r){throw n.error(`error ${r} while doing traced operation, state: ${JSON.stringify(e)}`),r}}const V="config-local.yaml";class W{constructor(t,e,r,i,o,s,a,c){w(this,"instance");w(this,"pid");w(this,"nRuns",0);w(this,"lastRunHistory",{});w(this,"wasStopped",!1);this.logger=t,this.workingDir=e,this.startOptions=r,this.initialStartHistory=i,this.onClose=o,this.onError=s,this.onCloseAndError=a,this.onCloseAndErrorNoStop=c}async start(){await N(this.logger,async(t,e)=>{this.wasStopped=!1;const r=yt(this.logger,this.startOptions);r.on("error",o=>{this.logger.error(`error '${o}', while running platforma, started opts: ${JSON.stringify(this.debugInfo())}`),this.onError!==void 0&&this.onError(this),this.onCloseAndError!==void 0&&this.onCloseAndError(this),this.onCloseAndErrorNoStop!==void 0&&!this.wasStopped&&this.onCloseAndErrorNoStop(this)}),r.on("close",()=>{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)}),t("started",!0);const i=t("pidFile",L(this.workingDir));t("pid",p.notEmpty(r.pid)),t("pidWritten",await kt(i,p.notEmpty(r.pid))),this.nRuns++,this.instance=r,this.pid=r.pid,this.lastRunHistory=e})}stop(){this.wasStopped=!0,G(p.notEmpty(this.pid))}async waitStopped(){await z(p.notEmpty(this.pid),15e3)}stopped(){return this.wasStopped}async isAlive(){return await k(p.notEmpty(this.pid))}debugInfo(){return{lastRunHistory:this.lastRunHistory,nRuns:this.nRuns,pid:this.pid,workingDir:this.workingDir,initialStartHistory:this.initialStartHistory,wasStopped:this.wasStopped}}}async function Bt(n,t){const e=Math.max(mt.cpus().length-2,1),r=K(t,e);return await N(n,async(i,o)=>{i("startOptions",{...r,config:"too wordy"});const s=u.resolve(r.workingDir);r.closeOld&&i("closeOld",await It(n,s));const a=u.join(s,V);n.info(`writing configuration '${a}'...`),await y.writeFile(a,r.config);const c=u.join(s,"binaries"),l=await Dt(n,c,r.plBinary),d=i("binaryPath",u.join("binaries",l)),h=Y(d,a,r,s,process.env);i("processOpts",{cmd:h.cmd,args:h.args,cwd:h.opts.cwd});const m=new W(n,r.workingDir,h,o,r.onClose,r.onError,r.onCloseAndError,r.onCloseAndErrorNoStop);return await m.start(),m})}async function It(n,t){return await N(n,async(e,r)=>{const i=e("pidFilePath",L(t)),o=e("pid",await xt(i)),s=e("wasAlive",await k(o));return o!==void 0&&s&&(e("stopped",G(o)),e("waitStopped",await z(o,1e4))),r})}function K(n,t){var i;const e={plBinary:At(),spawnOptions:{env:{GOMAXPROCS:String(t)}},closeOld:!0};if((i=n.spawnOptions)!=null&&i.env&&(e.spawnOptions.env={...e.spawnOptions.env,...n.spawnOptions.env}),n.spawnOptions){const o={...n.spawnOptions};delete o.env,e.spawnOptions={...e.spawnOptions,...o}}const r={...n};return delete r.spawnOptions,{...e,...r}}function Y(n,t,e,r,i){var a;const o={cmd:n,args:["--config",t],opts:{env:{...i},cwd:r,stdio:["pipe","ignore","inherit"],windowsHide:!0}};(a=e.spawnOptions)!=null&&a.env&&(o.opts.env={...o.opts.env,...e.spawnOptions.env});const s={...e.spawnOptions};return delete s.env,o.opts={...o.opts,...s},o}const Ut={keepaliveInterval:6e4,keepaliveCountMax:10};class D{constructor(t,e){w(this,"config");w(this,"homeDir");w(this,"forwardedServers",[]);this.logger=t,this.client=e}static async init(t,e){const r={...Ut,...e},i=new D(t,new C.Client);return await i.connect(r),i}getForwardedServers(){return this.forwardedServers}getFullHostName(){var t,e;return`${(t=this.config)==null?void 0:t.host}:${(e=this.config)==null?void 0:e.port}`}getUserName(){var t;return(t=this.config)==null?void 0:t.username}async connect(t){return this.config=t,await _t(this.client,t)}async exec(t){return new Promise((e,r)=>{this.client.exec(t,(i,o)=>{if(i)return r(new Error(`ssh.exec: ${t}: ${i}`));let s="",a="";o.on("close",c=>{c===0?e({stdout:s,stderr:a}):r(new Error(`Command ${t} exited with code ${c}, stdout: ${s}, stderr: ${a}`))}).on("data",c=>{s+=c.toString()}).stderr.on("data",c=>{a+=c.toString()})})})}static async getAuthTypes(t,e){return new Promise(r=>{let i="";const o=new C.Client;o.on("ready",()=>{o.end();const s=this.extractAuthMethods(i);r(s.length===0?["publickey","password"]:s)}),o.on("error",()=>{o.end(),r(["publickey","password"])}),o.connect({host:t,port:e,username:new Date().getTime().toString(),debug:s=>{i+=`${s}
|
|
8
8
|
`}})})}static extractAuthMethods(t){const e=t.match(/Inbound: Received USERAUTH_FAILURE \((.+)\)/);return e&&e[1]?e[1].split(",").map(r=>r.trim()):[]}async forwardPort(t,e){const r=`ssh.forward:${t.localPort}:${t.remotePort}.id_${b.randomBytes(1).toString("hex")}`;e=e??this.config;const i=new p.RetryablePromise(o=>new Promise((s,a)=>{const c=new C.Client;c.on("ready",()=>{this.logger.info(`${r}.client.ready`),s(c)}),c.on("error",l=>{this.logger.info(`${r}.client.error: ${l}`),o.reset(),a(l)}),c.on("close",()=>{this.logger.info(`${r}.client.closed`),o.reset()}),c.connect(e)}));return await i.ensure(),new Promise((o,s)=>{const a=H.createServer({pauseOnConnect:!0},async c=>{const l=`${r}.sock_${b.randomBytes(1).toString("hex")}`;let d;try{d=await i.ensure()}catch(m){this.logger.info(`${l}.persistentClient.catch: ${m}`),c.end();return}d.setNoDelay(!0),c.setNoDelay(!0);let h;try{h=await Tt(this.logger,d,"127.0.0.1",0,"127.0.0.1",t.remotePort)}catch(m){this.logger.error(`${l}.forwardOut.err: ${m}`),c.end();return}c.pipe(h),h.pipe(c),c.resume(),h.on("error",m=>{this.logger.error(`${l}.stream.error: ${m}`),c.end(),h.end()}),h.on("close",()=>{c.end(),h.end()}),c.on("close",()=>{this.logger.info(`${l}.localSocket: closed`),c.end(),h.end()})});a.listen(t.localPort,"127.0.0.1",()=>{this.logger.info(`${r}.server: started listening`),this.forwardedServers.push(a),o({server:a})}),a.on("error",c=>{a.close(),s(new Error(`${r}.server: error: ${JSON.stringify(c)}`))}),a.on("close",()=>{this.logger.info(`${r}.server: closed ${JSON.stringify(t)}`),this.forwardedServers=this.forwardedServers.filter(c=>c!==a)})})}closeForwardedPorts(){this.logger.info("[SSH] Closing all forwarded ports..."),this.forwardedServers.forEach(t=>{const e=t.address();if(e&&typeof e!="string"){const r=e;this.logger.info(`[SSH] Closing port forward for server ${r.address}:${r.port}`)}t.close()}),this.forwardedServers=[]}static async checkHostAvailability(t){return new Promise(e=>{wt.lookup(t,r=>{e(!r)})})}static async isPassphraseRequiredForKey(t){return new Promise((e,r)=>{try{return C.utils.parseKey(t)instanceof Error&&e(!0),e(!1)}catch(i){console.log("Error parsing privateKey"),r(new Error(`ssh.isPassphraseRequiredForKey: err ${i}`))}})}async uploadFile(t,e){return await this.withSftp(async r=>new Promise((i,o)=>{r.fastPut(t,e,s=>{if(s){const a=new Error(`ssh.uploadFile: err: ${s}, localPath: ${t}, remotePath: ${e}`);return o(a)}i(!0)})}))}async withSftp(t){return new Promise((e,r)=>{this.client.sftp((i,o)=>{if(i)return r(new Error(`ssh.withSftp: sftp err: ${i}`));t(o).then(e).catch(s=>{r(new Error(`ssh.withSftp.callback: err ${s}`))}).finally(()=>{o==null||o.end()})})})}async writeFileOnTheServer(t,e,r=432){return this.withSftp(async i=>this.writeFile(i,t,e,r))}async getForderStructure(t,e,r={files:[],directories:[]}){return new Promise((i,o)=>{t.readdir(e,async(s,a)=>{if(s)return o(s);for(const c of a){const l=`${e}/${c.filename}`;if(c.attrs.isDirectory()){r.directories.push(l);try{await this.getForderStructure(t,l,r)}catch(d){return o(d instanceof Error?d:new Error(String(d)))}}else r.files.push(l)}i(r)})})}rmdir(t,e){return new Promise((r,i)=>{t.rmdir(e,o=>o?i(o):r(!0))})}unlink(t,e){return new Promise((r,i)=>{t.unlink(e,o=>o?i(o):r(!0))})}async deleteFolder(t){return this.withSftp(async e=>{try{const r=await this.getForderStructure(e,t);this.logger.info("ssh.deleteFolder list of files and directories"),this.logger.info(`ssh.deleteFolder list of files: ${r.files}`),this.logger.info(`ssh.deleteFolder list of directories: ${r.directories}`);for(const i of r.files)this.logger.info(`ssh.deleteFolder unlink file ${i}`),await this.unlink(e,i);r.directories.sort((i,o)=>o.length-i.length);for(const i of r.directories)this.logger.info(`ssh.deleteFolder rmdir ${i}`),await this.rmdir(e,i);return await this.rmdir(e,t),!0}catch(r){this.logger.error(r);const i=r instanceof Error?r.message:"";throw new Error(`ssh.deleteFolder: path: ${t}, message: ${i}`)}})}async readFile(t){return this.withSftp(async e=>new Promise((r,i)=>{e.readFile(t,(o,s)=>{if(o)return i(new Error(`ssh.readFile: ${o}`));r(s.toString())})}))}async chmod(t,e){return this.withSftp(async r=>new Promise((i,o)=>{r.chmod(t,e,s=>s?o(new Error(`ssh.chmod: ${s}, path: ${t}, mode: ${e}`)):i(void 0))}))}async checkFileExists(t){return this.withSftp(async e=>new Promise((r,i)=>{e.stat(t,(o,s)=>{if(o)return(o==null?void 0:o.code)===2?r(!1):i(new Error(`ssh.checkFileExists: err ${o}`));r(s.isFile())})}))}async checkPathExists(t){return this.withSftp(async e=>new Promise((r,i)=>{e.stat(t,(o,s)=>{if(o)return o.code===2?r({exists:!1,isFile:!1,isDirectory:!1}):i(new Error(`ssh.checkPathExists: ${o}`));r({exists:!0,isFile:s.isFile(),isDirectory:s.isDirectory()})})}))}async writeFile(t,e,r,i=432){return new Promise((o,s)=>{t.writeFile(e,r,{mode:i},a=>{if(a)return s(new Error(`ssh.writeFile: err ${a}, remotePath: ${e}`));o(!0)})})}uploadFileUsingExistingSftp(t,e,r,i=432){return new Promise((o,s)=>{y.readFile(e).then(async a=>this.writeFile(t,r,a,i).then(()=>{o(void 0)}).catch(c=>{const l=`uploadFileUsingExistingSftp: ${c}`;this.logger.error(l),s(new Error(l))}))})}async __uploadDirectory(t,e,r,i=432){return new Promise((o,s)=>{x.readdir(e,async(a,c)=>{if(a)return s(new Error(`ssh.__uploadDir: err ${a}, localDir: ${e}, remoteDir: ${r}`));try{await this.__createRemoteDirectory(t,r);for(const l of c){const d=u.join(e,l),h=`${r}/${l}`;x.lstatSync(d).isDirectory()?await this.__uploadDirectory(t,d,h,i):await this.uploadFileUsingExistingSftp(t,d,h,i)}o()}catch(l){const d=`ssh.__uploadDir: catched err ${l}`;this.logger.error(d),s(new Error(d))}})})}async uploadDirectory(t,e,r=432){return new Promise((i,o)=>{this.withSftp(async s=>{try{await this.__uploadDirectory(s,t,e,r),i()}catch(a){o(new Error(`ssh.uploadDirectory: ${a}`))}})})}__createRemoteDirectory(t,e){return new Promise((r,i)=>{const o=e.split("/");let s="";const a=c=>{if(c>=o.length)return r();s+=`${o[c]}/`,t.stat(s,l=>{l?t.mkdir(s,d=>{if(d)return i(new Error(`ssh.__createRemDir: err ${d}, remotePath: ${e}`));a(c+1)}):a(c+1)})};a(0)})}ensureRemoteDirCreated(t,e=493){return this.withSftp(async r=>{const i=t.split("/");let o="";for(const s of i){o+=`${s}/`;try{await new Promise((a,c)=>{r.stat(o,l=>{if(!l)return a();r.mkdir(o,{mode:e},d=>{if(d)return c(new Error(`ssh.createRemoteDir: err ${d}, remotePath: ${t}`));a()})})})}catch(a){throw console.error(`Failed to create directory: ${o}`,a),a}}})}async downloadFile(t,e){return this.withSftp(async r=>new Promise((i,o)=>{r.fastGet(t,e,s=>{if(s)return o(new Error(`ssh.downloadFile: err ${s}, remotePath: ${t}, localPath: ${e}`));i(!0)})}))}close(){this.closeForwardedPorts(),this.client.end()}}async function _t(n,t,e,r){return new Promise((i,o)=>{n.on("ready",()=>{i(n)}),n.on("error",s=>{o(new Error(`ssh.connect: ${s}`))}),n.on("close",()=>{}),n.connect(t),n.setNoDelay(!0)})}async function Tt(n,t,e,r,i,o){return new Promise((s,a)=>{t.forwardOut(e,r,i,o,(c,l)=>c?(n.error(`forwardOut.error: ${c}`),a(c)):s(l))})}const jt="minio-2024-12-18T13-15-44Z",Mt="supervisord-0.7.3",Ht="supervisord_0.7.3_Linux_64-bit";function $(n){return u.join(n,".platforma_ssh")}function O(n){return u.join($(n),"binaries")}function qt(n,t){return u.join(O(n),`pl-${A()}-${S(t)}`)}function Z(n,t){return u.join(qt(n,t),"binaries")}function _(n,t){return u.join(Z(n,t),"platforma")}function Gt(n,t){return u.join(Z(n,t),"free-port")}function X(n,t){return u.join(O(n),`minio-2024-12-18T13-15-44Z-${S(t)}`)}function zt(n,t){return u.join(X(n,t),"minio")}function Jt(n,t){return u.join(O(n),`supervisord-0.7.3-${S(t)}`,Ht)}function Q(n,t){return u.join(Jt(n,t),"supervisord")}function tt(n){return u.join($(n),"supervisor.conf")}function T(n){return u.join($(n),"connection.txt")}async function Lt(n,t,e){const r=await B(n,t,e,"--daemon");if(r.stderr)throw new Error(`Can not run ssh Platforma ${r.stderr}`)}async function Vt(n,t,e){const r=await B(n,t,e,"ctl shutdown");if(r.stderr)throw new Error(`Can not stop ssh Platforma ${r.stderr}`)}function E(n,t){return t?n.platforma&&n.minio:n.platforma}function Wt(n){return n.execError===void 0}async function Kt(n,t,e,r){let i;try{i=await B(t,e,r,"ctl status")}catch(c){return{execError:String(c)}}if(i.stderr)return n.info(`supervisord ctl status: stderr occurred: ${i.stderr}, stdout: ${i.stdout}`),{rawResult:i};const o=j(i.stdout,"platforma"),s=j(i.stdout,"minio"),a={rawResult:i,platforma:o,minio:s};return a.minio||n.warn("Minio is not running on the server"),a.platforma||n.warn("Platforma is not running on the server"),a}function Yt(n,t,e,r){const i=b.randomBytes(16).toString("hex"),o=n;return`
|
|
9
9
|
[supervisord]
|
|
10
10
|
logfile=${t}/supervisord.log
|
package/dist/index.mjs
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
import { StartedTestContainer } from 'testcontainers';
|
|
2
2
|
import { ConnectConfig } from 'ssh2';
|
|
3
|
-
export declare const
|
|
4
|
-
export declare
|
|
5
|
-
export declare const downloadsFolder: string;
|
|
6
|
-
export declare const recUpload: string;
|
|
7
|
-
export declare function createTestDirForRecursiveUpload(): Promise<void>;
|
|
8
|
-
export declare function getPathForFile(fileName: string): string;
|
|
9
|
-
export declare function generateKeys(): void;
|
|
10
|
-
export declare function initPrivateKey(): string;
|
|
3
|
+
export declare const testAssetsPath: string;
|
|
4
|
+
export declare function readPrivateKey(): string;
|
|
11
5
|
export declare function initContainer(name: string): Promise<StartedTestContainer>;
|
|
12
6
|
export declare function getContainerHostAndPort(container: StartedTestContainer): {
|
|
13
7
|
port: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"common-utils.d.ts","sourceRoot":"","sources":["../../../src/ssh/__tests__/common-utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"common-utils.d.ts","sourceRoot":"","sources":["../../../src/ssh/__tests__/common-utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAI3D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,MAAM,CAAC;AAK1C,eAAO,MAAM,cAAc,QAA2D,CAAC;AASvF,wBAAgB,cAAc,IAAI,MAAM,CAEvC;AAED,wBAAsB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAgB/E;AAED,wBAAgB,uBAAuB,CAAC,SAAS,EAAE,oBAAoB;;;EAKtE;AAgBD,wBAAgB,mBAAmB,CAAC,SAAS,EAAE,oBAAoB,EAAE,KAAK,GAAE,OAAe,GAAG,aAAa,CAa1G;AAED,wBAAsB,OAAO,CAAC,SAAS,EAAE,oBAAoB,iBAE5D"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@milaboratories/pl-deployments",
|
|
3
|
-
"version": "2.4.
|
|
4
|
-
"pl-version": "1.
|
|
3
|
+
"version": "2.4.4",
|
|
4
|
+
"pl-version": "1.35.2",
|
|
5
5
|
"description": "MiLaboratories Platforma Backend code service run wrapper",
|
|
6
6
|
"engines": {
|
|
7
7
|
"node": ">=20.16.0"
|
|
@@ -38,12 +38,12 @@
|
|
|
38
38
|
"testcontainers": "^10.18.0",
|
|
39
39
|
"ts-jest": "^29.2.6",
|
|
40
40
|
"tsconfig-paths": "^4.2.0",
|
|
41
|
-
"typescript": "~5.
|
|
41
|
+
"typescript": "~5.6.3",
|
|
42
42
|
"utility-types": "^3.11.0",
|
|
43
43
|
"vite": "^6.3.5",
|
|
44
44
|
"vitest": "^2.1.9",
|
|
45
|
-
"@milaboratories/
|
|
46
|
-
"@milaboratories/
|
|
45
|
+
"@milaboratories/eslint-config": "^1.0.4",
|
|
46
|
+
"@milaboratories/build-configs": "1.0.4"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"decompress": "^4.2.1",
|
|
@@ -1,90 +1,41 @@
|
|
|
1
1
|
import type { StartedTestContainer } from 'testcontainers';
|
|
2
2
|
import { GenericContainer } from 'testcontainers';
|
|
3
|
-
import {
|
|
4
|
-
import { mkdir, rm } from 'node:fs/promises';
|
|
3
|
+
import { readFileSync, existsSync } from 'node:fs';
|
|
5
4
|
import path from 'node:path';
|
|
6
5
|
import type { ConnectConfig } from 'ssh2';
|
|
7
|
-
import ssh from 'ssh2';
|
|
8
6
|
import fs from 'node:fs';
|
|
9
7
|
|
|
10
8
|
const SSH_PORT = [22, 3001];
|
|
11
9
|
|
|
12
|
-
const
|
|
13
|
-
const privateKeyPath = getPathForFile('private-key.private');
|
|
10
|
+
export const testAssetsPath = path.resolve(__dirname, '..', '..', '..', 'test-assets');
|
|
14
11
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
export async function createTestDirForRecursiveUpload() {
|
|
21
|
-
const pathBase = path.resolve(__dirname, '..', '..', '..', 'test-assets', 'downloads', 'rec-upload', 'sub-1');
|
|
22
|
-
const path2 = path.resolve(__dirname, '..', '..', '..', 'test-assets', 'downloads', 'rec-upload', 'sub-1', 'sub-1-1');
|
|
23
|
-
|
|
24
|
-
await mkdir(pathBase, { recursive: true });
|
|
25
|
-
await mkdir(path2, { recursive: true });
|
|
26
|
-
|
|
27
|
-
for (let i = 0; i < 19; i++) {
|
|
28
|
-
const path2 = path.resolve(__dirname, '..', '..', '..', 'test-assets', 'downloads', 'rec-upload', 'sub-1', `sub-1-${i}`);
|
|
29
|
-
await mkdir(path2, { recursive: true });
|
|
30
|
-
|
|
31
|
-
for (let i = 0; i < 3; i++) {
|
|
32
|
-
writeFileSync(path.resolve(path2, `test-${i}.txt`), `test-${i}`);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
for (let i = 1; i < 100; i++) {
|
|
37
|
-
writeFileSync(path.resolve(pathBase, `test-${i}.txt`), `test-${i}`);
|
|
38
|
-
}
|
|
39
|
-
writeFileSync(path.resolve(pathBase, `test.txt`), `test-1`);
|
|
40
|
-
writeFileSync(path.resolve(path2, 'test-5.txt'), 'test-5');
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function getPathForFile(fileName: string) {
|
|
44
|
-
return path.resolve(__dirname, '..', '..', '..', 'test-assets', fileName);
|
|
12
|
+
const publicKeyPath = path.resolve(testAssetsPath, 'public-key.pem');
|
|
13
|
+
const privateKeyPath = path.resolve(testAssetsPath, 'private-key.pem');
|
|
14
|
+
if (!existsSync(publicKeyPath) || !existsSync(privateKeyPath)) {
|
|
15
|
+
console.error('SSH keys does not exist, see pl-deployments/README.md and regenerate them');
|
|
16
|
+
process.exit(1);
|
|
45
17
|
}
|
|
46
18
|
|
|
47
|
-
export function
|
|
48
|
-
const keys = ssh.utils.generateKeyPairSync('ecdsa', { bits: 256, comment: 'node.js rules!', passphrase: 'password', cipher: 'aes256-cbc' });
|
|
49
|
-
if (!existsSync(publicKeyPath) || !existsSync(privateKeyPath)) {
|
|
50
|
-
writeFileSync(publicKeyPath, keys.public);
|
|
51
|
-
writeFileSync(privateKeyPath, keys.private);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function initPrivateKey(): string {
|
|
56
|
-
generateKeys();
|
|
19
|
+
export function readPrivateKey(): string {
|
|
57
20
|
return readFileSync(privateKeyPath, { encoding: 'utf-8' });
|
|
58
21
|
}
|
|
59
22
|
|
|
60
23
|
export async function initContainer(name: string): Promise<StartedTestContainer> {
|
|
61
|
-
await createTestDirForRecursiveUpload();
|
|
62
|
-
|
|
63
24
|
const imageName = `pl-ssh-test-container-${name}:1.0.0`;
|
|
64
25
|
const containerName = `pl-ssh-test-${name}`;
|
|
65
26
|
|
|
66
|
-
|
|
67
|
-
const container = new GenericContainer(imageName)
|
|
68
|
-
.withExposedPorts(...SSH_PORT)
|
|
69
|
-
.withReuse()
|
|
70
|
-
.withName(containerName);
|
|
71
|
-
|
|
72
|
-
return await container.start();
|
|
73
|
-
} catch {
|
|
74
|
-
console.log('No worries, creating a new container');
|
|
27
|
+
console.log('No worries, creating a new container');
|
|
75
28
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
.build(imageName, { deleteOnExit: false });
|
|
29
|
+
const container = await GenericContainer
|
|
30
|
+
.fromDockerfile(path.resolve(__dirname, '..', '..', '..'))
|
|
31
|
+
.withCache(true)
|
|
32
|
+
.build(imageName, { deleteOnExit: false });
|
|
81
33
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
34
|
+
return await container
|
|
35
|
+
.withExposedPorts(...SSH_PORT)
|
|
36
|
+
.withReuse()
|
|
37
|
+
.withName(containerName)
|
|
38
|
+
.start();
|
|
88
39
|
}
|
|
89
40
|
|
|
90
41
|
export function getContainerHostAndPort(container: StartedTestContainer) {
|
|
@@ -96,7 +47,7 @@ export function getContainerHostAndPort(container: StartedTestContainer) {
|
|
|
96
47
|
|
|
97
48
|
function logToFile(message: string) {
|
|
98
49
|
const logFileName = 'log.txt';
|
|
99
|
-
const logFilePath = path.join(
|
|
50
|
+
const logFilePath = path.join(testAssetsPath, logFileName);
|
|
100
51
|
|
|
101
52
|
const timestamp = new Date().toISOString();
|
|
102
53
|
const logMessage = `[${timestamp}] ${message}\n`;
|
|
@@ -110,7 +61,7 @@ function logToFile(message: string) {
|
|
|
110
61
|
|
|
111
62
|
export function getConnectionForSsh(container: StartedTestContainer, debug: boolean = false): ConnectConfig {
|
|
112
63
|
const hostData = getContainerHostAndPort(container);
|
|
113
|
-
const privateKey =
|
|
64
|
+
const privateKey = readPrivateKey();
|
|
114
65
|
const config = {
|
|
115
66
|
host: hostData.host,
|
|
116
67
|
port: hostData.port,
|
|
@@ -125,16 +76,4 @@ export function getConnectionForSsh(container: StartedTestContainer, debug: bool
|
|
|
125
76
|
|
|
126
77
|
export async function cleanUp(container: StartedTestContainer) {
|
|
127
78
|
await container.stop();
|
|
128
|
-
|
|
129
|
-
if (existsSync(localFileUpload)) {
|
|
130
|
-
unlinkSync(localFileUpload);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (existsSync(localFileDownload)) {
|
|
134
|
-
unlinkSync(localFileDownload);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
if (existsSync(recUpload)) {
|
|
138
|
-
await rm(recUpload, { recursive: true });
|
|
139
|
-
}
|
|
140
79
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, it, beforeAll, expect, afterAll } from 'vitest';
|
|
2
|
-
import { initContainer, getConnectionForSsh, cleanUp as cleanUpT } from './common-utils';
|
|
2
|
+
import { initContainer, getConnectionForSsh, cleanUp as cleanUpT, testAssetsPath } from './common-utils';
|
|
3
3
|
import { SshPl } from '../pl';
|
|
4
4
|
import upath from 'upath';
|
|
5
5
|
import { getDefaultPlVersion } from '../../common/pl_version';
|
|
@@ -10,19 +10,11 @@ import { ConsoleLoggerAdapter } from '@milaboratories/ts-helpers';
|
|
|
10
10
|
import * as plpath from '../pl_paths';
|
|
11
11
|
|
|
12
12
|
let sshPl: SshPl;
|
|
13
|
-
const testContainer = await initContainer('pl');
|
|
14
13
|
|
|
15
|
-
const
|
|
14
|
+
const testContainer = await initContainer('pl');
|
|
16
15
|
|
|
17
|
-
|
|
18
|
-
const version = getDefaultPlVersion();
|
|
19
|
-
const arch = await sshPl.getArch();
|
|
20
|
-
const tgzName = 'supervisord-0.7.3';
|
|
16
|
+
const downloadDestination = upath.resolve(testAssetsPath, 'downloads');
|
|
21
17
|
|
|
22
|
-
unlinkSync(`${downloadDestination}/pl-${version}-${newArch(arch!.arch)}.tgz`);
|
|
23
|
-
unlinkSync(`${downloadDestination}/${tgzName}-${newArch(arch!.arch)}.tgz`);
|
|
24
|
-
rmSync(downloadDestination, { recursive: true });
|
|
25
|
-
}
|
|
26
18
|
beforeAll(async () => {
|
|
27
19
|
const logger = new ConsoleLoggerAdapter();
|
|
28
20
|
sshPl = await SshPl.init(logger, getConnectionForSsh(testContainer));
|
|
@@ -240,6 +232,16 @@ describe('SshPl', async () => {
|
|
|
240
232
|
});
|
|
241
233
|
});
|
|
242
234
|
|
|
235
|
+
async function cleanUp() {
|
|
236
|
+
const version = getDefaultPlVersion();
|
|
237
|
+
const arch = await sshPl.getArch();
|
|
238
|
+
const tgzName = 'supervisord-0.7.3';
|
|
239
|
+
|
|
240
|
+
unlinkSync(`${downloadDestination}/pl-${version}-${newArch(arch!.arch)}.tgz`);
|
|
241
|
+
unlinkSync(`${downloadDestination}/${tgzName}-${newArch(arch!.arch)}.tgz`);
|
|
242
|
+
rmSync(downloadDestination, { recursive: true });
|
|
243
|
+
}
|
|
244
|
+
|
|
243
245
|
afterAll(async () => {
|
|
244
246
|
await cleanUp();
|
|
245
247
|
await cleanUpT(testContainer);
|
|
@@ -1,20 +1,25 @@
|
|
|
1
1
|
import type { StartedTestContainer } from 'testcontainers';
|
|
2
2
|
import { describe, it, beforeAll, afterAll, expect } from 'vitest';
|
|
3
|
-
import
|
|
3
|
+
import * as fs from 'node:fs/promises';
|
|
4
4
|
import { SshClient } from '../ssh';
|
|
5
5
|
import ssh from 'ssh2';
|
|
6
|
-
import {
|
|
6
|
+
import { cleanUp, getConnectionForSsh, getContainerHostAndPort, initContainer, testAssetsPath } from './common-utils';
|
|
7
7
|
import { ConsoleLoggerAdapter } from '@milaboratories/ts-helpers';
|
|
8
|
+
import path from 'path';
|
|
9
|
+
import { fileExists } from '@milaboratories/ts-helpers';
|
|
8
10
|
|
|
9
11
|
let client: SshClient;
|
|
10
12
|
let testContainer: StartedTestContainer;
|
|
11
13
|
|
|
14
|
+
export const localFileUpload = path.resolve(testAssetsPath, 'test-file.txt');
|
|
15
|
+
export const localFileDownload = path.resolve(testAssetsPath, 'test-file-download.txt');
|
|
16
|
+
export const downloadsFolder = path.resolve(testAssetsPath, 'downloads');
|
|
17
|
+
export const recUpload = path.resolve(testAssetsPath, 'downloads', 'rec-upload');
|
|
18
|
+
|
|
12
19
|
beforeAll(async () => {
|
|
13
|
-
// FIXME: sometimes it fails with the error here.
|
|
14
|
-
// Then we will have a container available in docker ps.
|
|
15
|
-
// If it happened, try to retry to build the container the second time here and ask me (Gleb).
|
|
16
20
|
testContainer = await initContainer('ssh');
|
|
17
21
|
client = await SshClient.init(new ConsoleLoggerAdapter(), getConnectionForSsh(testContainer));
|
|
22
|
+
await createTestDirForRecursiveUpload();
|
|
18
23
|
}, 200000);
|
|
19
24
|
|
|
20
25
|
describe('SSH Tests', () => {
|
|
@@ -94,7 +99,7 @@ describe('SSH Tests', () => {
|
|
|
94
99
|
it('should upload a file to the SSH server', async () => {
|
|
95
100
|
const data = `Test data ${new Date().getTime()}`;
|
|
96
101
|
const remoteFile = '/home/pl-doctor/uploaded-file.txt';
|
|
97
|
-
|
|
102
|
+
await fs.writeFile(localFileUpload, data);
|
|
98
103
|
const result = await client.uploadFile(localFileUpload, remoteFile);
|
|
99
104
|
expect(result).toBe(true);
|
|
100
105
|
const execResult = await testContainer.exec(['cat', remoteFile]);
|
|
@@ -107,14 +112,14 @@ describe('SSH Tests', () => {
|
|
|
107
112
|
// const localFile = './test-file-upload.txt';
|
|
108
113
|
const remoteFile = '/home/pl-doctor/uploaded-file.txt';
|
|
109
114
|
|
|
110
|
-
|
|
115
|
+
await fs.writeFile(localFileDownload, data);
|
|
111
116
|
|
|
112
117
|
const uploadResult = await client.uploadFile(localFileDownload, remoteFile);
|
|
113
118
|
expect(uploadResult).toBe(true);
|
|
114
119
|
|
|
115
120
|
const downloadResult = await client.downloadFile(remoteFile, localFileDownload);
|
|
116
121
|
expect(downloadResult).toBe(true);
|
|
117
|
-
expect(
|
|
122
|
+
expect(await fs.readFile(localFileDownload, { encoding: 'utf-8' })).toBe(data);
|
|
118
123
|
});
|
|
119
124
|
|
|
120
125
|
it('Simple server should forward remote SSH port to a local port', async () => {
|
|
@@ -246,6 +251,44 @@ describe('sshExec', () => {
|
|
|
246
251
|
});
|
|
247
252
|
});
|
|
248
253
|
|
|
254
|
+
export async function createTestDirForRecursiveUpload() {
|
|
255
|
+
const pathBase = path.resolve(testAssetsPath, 'downloads', 'rec-upload', 'sub-1');
|
|
256
|
+
const path2 = path.resolve(testAssetsPath, 'downloads', 'rec-upload', 'sub-1', 'sub-1-1');
|
|
257
|
+
|
|
258
|
+
await fs.mkdir(pathBase, { recursive: true });
|
|
259
|
+
await fs.mkdir(path2, { recursive: true });
|
|
260
|
+
|
|
261
|
+
for (let i = 0; i < 19; i++) {
|
|
262
|
+
const path2 = path.resolve(testAssetsPath, 'downloads', 'rec-upload', 'sub-1', `sub-1-${i}`);
|
|
263
|
+
await fs.mkdir(path2, { recursive: true });
|
|
264
|
+
|
|
265
|
+
for (let i = 0; i < 3; i++) {
|
|
266
|
+
await fs.writeFile(path.resolve(path2, `test-${i}.txt`), `test-${i}`);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
for (let i = 1; i < 100; i++) {
|
|
271
|
+
await fs.writeFile(path.resolve(pathBase, `test-${i}.txt`), `test-${i}`);
|
|
272
|
+
}
|
|
273
|
+
await fs.writeFile(path.resolve(pathBase, `test.txt`), `test-1`);
|
|
274
|
+
await fs.writeFile(path.resolve(path2, 'test-5.txt'), 'test-5');
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export async function cleanUpTestAssets() {
|
|
278
|
+
if (await fileExists(localFileUpload)) {
|
|
279
|
+
await fs.unlink(localFileUpload);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (await fileExists(localFileDownload)) {
|
|
283
|
+
await fs.unlink(localFileDownload);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
if (await fileExists(recUpload)) {
|
|
287
|
+
await fs.rm(recUpload, { recursive: true });
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
249
291
|
afterAll(async () => {
|
|
292
|
+
await cleanUpTestAssets();
|
|
250
293
|
await cleanUp(testContainer);
|
|
251
294
|
}, 200000);
|