From 7306f6bca0c763b00a2408e24129c0f1df92fbea Mon Sep 17 00:00:00 2001 From: Dragan Filipovic Date: Mon, 2 Jan 2023 11:41:31 +0100 Subject: [PATCH] update helpers --- dist/index.js | 2 +- src/index.js | 7 ++++--- src/remoteCmd.js | 4 ++-- src/rsyncCli.js | 14 ++++---------- src/sshKey.js | 19 ++++++++++++------- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/dist/index.js b/dist/index.js index 03502b5..9e898a6 100755 --- a/dist/index.js +++ b/dist/index.js @@ -1,2 +1,2 @@ #!/usr/bin/env node -(()=>{var e={898:(e,r,s)=>{"use strict";var t=s(81).spawn;var o=s(837);var escapeSpaces=function(e){if(typeof e==="string"){return e.replace(/\b\s/g,"\\ ")}else{return e}};var escapeSpacesInOptions=function(e){["src","dest","include","exclude","excludeFirst"].forEach((function(r){var s=e[r];if(typeof s==="string"){e[r]=escapeSpaces(s)}else if(Array.isArray(s)===true){e[r]=s.map(escapeSpaces)}}));return e};e.exports=function(e,r){e=e||{};e=o._extend({},e);e=escapeSpacesInOptions(e);var s=e.platform||process.platform;var n=s==="win32";if(typeof e.src==="undefined"){throw new Error("'src' directory is missing from options")}if(typeof e.dest==="undefined"){throw new Error("'dest' directory is missing from options")}var c=e.dest;if(typeof e.host!=="undefined"){c=e.host+":"+e.dest}if(!Array.isArray(e.src)){e.src=[e.src]}var i=[].concat(e.src);i.push(c);var a=(e.args||[]).find((function(e){return e.match(/--chmod=/)}));if(n&&!a){i.push("--chmod=ugo=rwX")}if(typeof e.host!=="undefined"||e.ssh){i.push("--rsh");var l="ssh";if(typeof e.port!=="undefined"){l+=" -p "+e.port}if(typeof e.privateKey!=="undefined"){l+=" -i "+e.privateKey}if(typeof e.sshCmdArgs!=="undefined"){l+=" "+e.sshCmdArgs.join(" ")}i.push(l)}if(e.recursive===true){i.push("--recursive")}if(e.times===true){i.push("--times")}if(e.syncDest===true||e.deleteAll===true){i.push("--delete");i.push("--delete-excluded")}if(e.syncDestIgnoreExcl===true||e.delete===true){i.push("--delete")}if(e.dryRun===true){i.push("--dry-run");i.push("--verbose")}if(typeof e.excludeFirst!=="undefined"&&o.isArray(e.excludeFirst)){e.excludeFirst.forEach((function(e,r){i.push("--exclude="+e)}))}if(typeof e.include!=="undefined"&&o.isArray(e.include)){e.include.forEach((function(e,r){i.push("--include="+e)}))}if(typeof e.exclude!=="undefined"&&o.isArray(e.exclude)){e.exclude.forEach((function(e,r){i.push("--exclude="+e)}))}switch(e.compareMode){case"sizeOnly":i.push("--size-only");break;case"checksum":i.push("--checksum");break}if(typeof e.args!=="undefined"&&o.isArray(e.args)){i=[...new Set([...i,...e.args])]}i=[...new Set(i)];var noop=function(){};var d=e.onStdout||noop;var u=e.onStderr||noop;var f="rsync ";i.forEach((function(e){if(e.substr(0,4)==="ssh "){e='"'+e+'"'}f+=e+" "}));f=f.trim();if(e.noExec){r(null,null,null,f);return}try{var p="";var h="";var y;if(n){y=t("cmd.exe",["/s","/c",'"'+f+'"'],{windowsVerbatimArguments:true,stdio:[process.stdin,"pipe","pipe"]})}else{y=t("/bin/sh",["-c",f])}y.stdout.on("data",(function(e){d(e);p+=e}));y.stderr.on("data",(function(e){u(e);h+=e}));y.on("exit",(function(e){var s=null;if(e!==0){s=new Error("rsync exited with code "+e);s.code=e}r(s,p,h,f)}))}catch(e){r(e,null,null,f)}}},505:(e,r,s)=>{const{existsSync:t,mkdirSync:o,writeFileSync:n}=s(147);const{join:c}=s(17);const validateDir=e=>{if(!e){console.log("[DIR] dir is not defined");return}if(t(e)){console.log(`[DIR] ${e} dir exist`);return}console.log(`[DIR] Creating ${e} dir in workspace root`);o(e);console.log("✅ [DIR] dir created.")};const writeToFile=({dir:e,filename:r,content:s,isRequired:o})=>{validateDir(e);const i=c(e,r);if(t(i)){console.log(`[FILE] ${i} file exist`);if(o){throw new Error(`⚠️ [FILE] ${i} Required file exist, aborting ...`)}return}try{console.log(`[FILE] writing ${i} file ...`,s.length);n(i,s,{encoding:"utf8",mode:384})}catch(e){throw new Error(`⚠️[FILE] Writing to file error. filePath: ${i}, message: ${e.message}`)}};const validateRequiredInputs=e=>{const r=Object.keys(e);const s=r.filter((r=>{const s=e[r];if(!s){console.error(`⚠️ [INPUTS] ${r} is mandatory`)}return s}));if(s.length!==r.length){throw new Error("⚠️ [INPUTS] Inputs not valid, aborting ...")}};const snakeToCamel=e=>e.replace(/[^a-zA-Z0-9]+(.)/g,((e,r)=>r.toUpperCase()));e.exports={writeToFile:writeToFile,validateRequiredInputs:validateRequiredInputs,snakeToCamel:snakeToCamel}},229:(e,r,s)=>{const{snakeToCamel:t}=s(505);const o=["REMOTE_HOST","REMOTE_USER","REMOTE_PORT","SSH_PRIVATE_KEY","DEPLOY_KEY_NAME","SOURCE","TARGET","ARGS","SSH_CMD_ARGS","EXCLUDE","SCRIPT_BEFORE","SCRIPT_AFTER"];const n=process.env.GITHUB_WORKSPACE;const c=process.env.REMOTE_USER;const i={source:"",target:`/home/${c}/`,exclude:"",args:"-rltgoDzvO",sshCmdArgs:"-o StrictHostKeyChecking=no",deployKeyName:"deploy_key"};const a={githubWorkspace:n};o.forEach((e=>{const r=t(e.toLowerCase());const s=process.env[e]||process.env[`INPUT_${e}`];const o=s===undefined?i[r]:s;let c=o;switch(r){case"source":c=`${n}/${o}`;break;case"exclude":case"args":case"sshCmdArgs":c=o.split(",").map((e=>e.trim()));break}a[r]=c}));a.sshServer=`${a.remoteUser}@${a.remoteHost}`;a.rsyncServer=`${a.remoteUser}@${a.remoteHost}:${a.target}`;e.exports=a},976:(e,r,s)=>{const{exec:t}=s(81);const{sshServer:o,githubWorkspace:n}=s(229);const{writeToFile:c}=s(505);const remoteCmd=(e,r)=>{const s=`local_ssh_script-${r}.sh`;try{c({dir:n,filename:s,content:e});t(`ssh ${o} 'bash -s' < ${s}`,((e,r,s)=>{if(e){console.log("⚠️ [CMD] Remote script failed. ",e.message)}else{console.log("✅ [CMD] Remote script executed. \n",r,s)}}))}catch(e){console.log("⚠️ [CMD] Starting Remote script execution failed. ",e.message)}};e.exports={remoteCmdBefore:e=>remoteCmd(e,"before"),remoteCmdAfter:e=>remoteCmd(e,"after")}},447:(e,r,s)=>{const{execSync:t}=s(81);const o=s(898);const n=new Promise((async(e,r)=>{let s;try{t("rsync --version",{stdio:"inherit"});s=true}catch(e){s=false;console.log("⚠️ [CLI] Rsync doesn't exists",e)}if(s){console.log("⚠️ [CLI] Rsync exists");e();return}console.log('⚠️ [CLI] Rsync doesn\'t exists. Start installation with "apt-get" \n');try{t("sudo DEBIAN_FRONTEND=noninteractive apt-get -y update && sudo DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends -y install rsync",{stdio:"inherit"});console.log("✅ [CLI] Rsync installed. \n");e()}catch(e){r(Error(`⚠️ [CLI] Rsync installation failed. Aborting ... error: ${e.message}`))}}));const rsyncCli=({source:e,rsyncServer:r,exclude:s,remotePort:t,privateKey:n,args:c,sshCmdArgs:i,callback:a})=>{console.log(`[Rsync] Starting Rsync Action: ${e} to ${r}`);if(s)console.log(`[Rsync] excluding folders ${s}`);const l={ssh:true,recursive:true};try{o({...l,src:e,dest:r,excludeFirst:s,port:t,privateKey:n,args:c,sshCmdArgs:i},((e,r,s,t)=>{if(e){console.error("⚠️ [Rsync] error: ",e.message);console.log("⚠️ [Rsync] stderr: ",s);console.log("⚠️ [Rsync] stdout: ",r);console.log("⚠️ [Rsync] cmd: ",t)}else{console.log("✅ [Rsync] finished.",r)}a(e,r,s,t);if(e){process.abort()}}))}catch(e){console.error("⚠️ [Rsync] command error: ",e.message,e.stack);process.abort()}};const sshDeploy=e=>{n.then((()=>{rsyncCli(e)})).catch((e=>{throw e}))};e.exports={sshDeploy:sshDeploy}},822:(e,r,s)=>{const{join:t}=s(17);const{writeToFile:o}=s(505);const addSshKey=(e,r)=>{const{HOME:s}=process.env;const n=t(s||__dirname,".ssh");const c=t(n,r);o({dir:n,filename:"known_hosts",content:""});console.log("✅ [SSH] known_hosts file ensured",n,r,e.length);o({dir:n,filename:r,content:e,isRequired:true});console.log("✅ [SSH] key added to `.ssh` dir ",n);return c};e.exports={addSshKey:addSshKey}},81:e=>{"use strict";e.exports=require("child_process")},147:e=>{"use strict";e.exports=require("fs")},17:e=>{"use strict";e.exports=require("path")},837:e=>{"use strict";e.exports=require("util")}};var r={};function __nccwpck_require__(s){var t=r[s];if(t!==undefined){return t.exports}var o=r[s]={exports:{}};var n=true;try{e[s](o,o.exports,__nccwpck_require__);n=false}finally{if(n)delete r[s]}return o.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var s={};(()=>{const{sshDeploy:e}=__nccwpck_require__(447);const{remoteCmdBefore:r,remoteCmdAfter:s}=__nccwpck_require__(976);const{addSshKey:t}=__nccwpck_require__(822);const{validateRequiredInputs:o}=__nccwpck_require__(505);const n=__nccwpck_require__(229);const run=()=>{const{source:c,remoteUser:i,remoteHost:a,remotePort:l,deployKeyName:d,sshPrivateKey:u,args:f,exclude:p,sshCmdArgs:h,scriptBefore:y,scriptAfter:m,rsyncServer:g}=n;o({sshPrivateKey:u,remoteHost:a,remoteUser:i});const _=t(u,d);if(y){r(y)}let callback=()=>{};if(m){callback=(...e)=>{s(m,e)}}e({source:c,rsyncServer:g,exclude:p,remotePort:l,privateKey:_,args:f,sshCmdArgs:h,callback:callback})};run()})();module.exports=s})(); \ No newline at end of file +(()=>{var e={898:(e,r,s)=>{"use strict";var t=s(81).spawn;var o=s(837);var escapeSpaces=function(e){if(typeof e==="string"){return e.replace(/\b\s/g,"\\ ")}else{return e}};var escapeSpacesInOptions=function(e){["src","dest","include","exclude","excludeFirst"].forEach((function(r){var s=e[r];if(typeof s==="string"){e[r]=escapeSpaces(s)}else if(Array.isArray(s)===true){e[r]=s.map(escapeSpaces)}}));return e};e.exports=function(e,r){e=e||{};e=o._extend({},e);e=escapeSpacesInOptions(e);var s=e.platform||process.platform;var n=s==="win32";if(typeof e.src==="undefined"){throw new Error("'src' directory is missing from options")}if(typeof e.dest==="undefined"){throw new Error("'dest' directory is missing from options")}var i=e.dest;if(typeof e.host!=="undefined"){i=e.host+":"+e.dest}if(!Array.isArray(e.src)){e.src=[e.src]}var c=[].concat(e.src);c.push(i);var a=(e.args||[]).find((function(e){return e.match(/--chmod=/)}));if(n&&!a){c.push("--chmod=ugo=rwX")}if(typeof e.host!=="undefined"||e.ssh){c.push("--rsh");var l="ssh";if(typeof e.port!=="undefined"){l+=" -p "+e.port}if(typeof e.privateKey!=="undefined"){l+=" -i "+e.privateKey}if(typeof e.sshCmdArgs!=="undefined"){l+=" "+e.sshCmdArgs.join(" ")}c.push(l)}if(e.recursive===true){c.push("--recursive")}if(e.times===true){c.push("--times")}if(e.syncDest===true||e.deleteAll===true){c.push("--delete");c.push("--delete-excluded")}if(e.syncDestIgnoreExcl===true||e.delete===true){c.push("--delete")}if(e.dryRun===true){c.push("--dry-run");c.push("--verbose")}if(typeof e.excludeFirst!=="undefined"&&o.isArray(e.excludeFirst)){e.excludeFirst.forEach((function(e,r){c.push("--exclude="+e)}))}if(typeof e.include!=="undefined"&&o.isArray(e.include)){e.include.forEach((function(e,r){c.push("--include="+e)}))}if(typeof e.exclude!=="undefined"&&o.isArray(e.exclude)){e.exclude.forEach((function(e,r){c.push("--exclude="+e)}))}switch(e.compareMode){case"sizeOnly":c.push("--size-only");break;case"checksum":c.push("--checksum");break}if(typeof e.args!=="undefined"&&o.isArray(e.args)){c=[...new Set([...c,...e.args])]}c=[...new Set(c)];var noop=function(){};var d=e.onStdout||noop;var u=e.onStderr||noop;var f="rsync ";c.forEach((function(e){if(e.substr(0,4)==="ssh "){e='"'+e+'"'}f+=e+" "}));f=f.trim();if(e.noExec){r(null,null,null,f);return}try{var p="";var h="";var y;if(n){y=t("cmd.exe",["/s","/c",'"'+f+'"'],{windowsVerbatimArguments:true,stdio:[process.stdin,"pipe","pipe"]})}else{y=t("/bin/sh",["-c",f])}y.stdout.on("data",(function(e){d(e);p+=e}));y.stderr.on("data",(function(e){u(e);h+=e}));y.on("exit",(function(e){var s=null;if(e!==0){s=new Error("rsync exited with code "+e);s.code=e}r(s,p,h,f)}))}catch(e){r(e,null,null,f)}}},505:(e,r,s)=>{const{existsSync:t,mkdirSync:o,writeFileSync:n}=s(147);const{join:i}=s(17);const validateDir=e=>{if(!e){console.log("[DIR] dir is not defined");return}if(t(e)){console.log(`[DIR] ${e} dir exist`);return}console.log(`[DIR] Creating ${e} dir in workspace root`);o(e);console.log("✅ [DIR] dir created.")};const writeToFile=({dir:e,filename:r,content:s,isRequired:o})=>{validateDir(e);const c=i(e,r);if(t(c)){console.log(`[FILE] ${c} file exist`);if(o){throw new Error(`⚠️ [FILE] ${c} Required file exist, aborting ...`)}return}try{console.log(`[FILE] writing ${c} file ...`,s.length);n(c,s,{encoding:"utf8",mode:384})}catch(e){throw new Error(`⚠️[FILE] Writing to file error. filePath: ${c}, message: ${e.message}`)}};const validateRequiredInputs=e=>{const r=Object.keys(e);const s=r.filter((r=>{const s=e[r];if(!s){console.error(`⚠️ [INPUTS] ${r} is mandatory`)}return s}));if(s.length!==r.length){throw new Error("⚠️ [INPUTS] Inputs not valid, aborting ...")}};const snakeToCamel=e=>e.replace(/[^a-zA-Z0-9]+(.)/g,((e,r)=>r.toUpperCase()));e.exports={writeToFile:writeToFile,validateRequiredInputs:validateRequiredInputs,snakeToCamel:snakeToCamel}},229:(e,r,s)=>{const{snakeToCamel:t}=s(505);const o=["REMOTE_HOST","REMOTE_USER","REMOTE_PORT","SSH_PRIVATE_KEY","DEPLOY_KEY_NAME","SOURCE","TARGET","ARGS","SSH_CMD_ARGS","EXCLUDE","SCRIPT_BEFORE","SCRIPT_AFTER"];const n=process.env.GITHUB_WORKSPACE;const i=process.env.REMOTE_USER;const c={source:"",target:`/home/${i}/`,exclude:"",args:"-rltgoDzvO",sshCmdArgs:"-o StrictHostKeyChecking=no",deployKeyName:"deploy_key"};const a={githubWorkspace:n};o.forEach((e=>{const r=t(e.toLowerCase());const s=process.env[e]||process.env[`INPUT_${e}`];const o=s===undefined?c[r]:s;let i=o;switch(r){case"source":i=`${n}/${o}`;break;case"exclude":case"args":case"sshCmdArgs":i=o.split(",").map((e=>e.trim()));break}a[r]=i}));a.sshServer=`${a.remoteUser}@${a.remoteHost}`;a.rsyncServer=`${a.remoteUser}@${a.remoteHost}:${a.target}`;e.exports=a},976:(e,r,s)=>{const{exec:t}=s(81);const{privateKey:o,sshServer:n,githubWorkspace:i}=s(229);const{writeToFile:c}=s(505);const remoteCmd=(e,r)=>{const s=`local_ssh_script-${r}.sh`;try{c({dir:i,filename:s,content:e});t(`ssh -i ${o} ${n} 'bash -s' < ${s}`,((e,r,s)=>{if(e){console.log("⚠️ [CMD] Remote script failed. ",e.message)}else{console.log("✅ [CMD] Remote script executed. \n",r,s)}}))}catch(e){console.log("⚠️ [CMD] Starting Remote script execution failed. ",e.message)}};e.exports={remoteCmdBefore:e=>remoteCmd(e,"before"),remoteCmdAfter:e=>remoteCmd(e,"after")}},447:(e,r,s)=>{const{execSync:t}=s(81);const o=s(898);const n=new Promise((async(e,r)=>{try{t("rsync --version",{stdio:"inherit"});console.log("⚠️ [CLI] Rsync exists");e();return}catch(e){console.log("⚠️ [CLI] Rsync doesn't exists",e)}console.log('⚠️ [CLI] Rsync doesn\'t exists. Start installation with "apt-get" \n');try{t("sudo DEBIAN_FRONTEND=noninteractive apt-get -y update && sudo DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends -y install rsync",{stdio:"inherit"});console.log("✅ [CLI] Rsync installed. \n");e()}catch(e){r(Error(`⚠️ [CLI] Rsync installation failed. Aborting ... error: ${e.message}`))}}));const rsyncCli=({source:e,rsyncServer:r,exclude:s,remotePort:t,privateKeyPath:n,args:i,sshCmdArgs:c,callback:a})=>{console.log(`[Rsync] Starting Rsync Action: ${e} to ${r}`);if(s)console.log(`[Rsync] excluding folders ${s}`);const l={ssh:true,recursive:true};try{o({...l,src:e,dest:r,excludeFirst:s,port:t,privateKey:n,args:i,sshCmdArgs:c},((e,r,s,t)=>{if(e){console.error("⚠️ [Rsync] error: ",e.message);console.log("⚠️ [Rsync] stderr: ",s);console.log("⚠️ [Rsync] stdout: ",r);console.log("⚠️ [Rsync] cmd: ",t)}else{console.log("✅ [Rsync] finished.",r)}a(e,r,s,t);if(e){process.abort()}}))}catch(e){console.error("⚠️ [Rsync] command error: ",e.message,e.stack);process.abort()}};const sshDeploy=e=>{n.then((()=>{rsyncCli(e)})).catch((e=>{throw e}))};e.exports={sshDeploy:sshDeploy}},822:(e,r,s)=>{const{join:t}=s(17);const{writeToFile:o}=s(505);const getPrivateKeyPath=e=>{const{HOME:r}=process.env;const s=t(r||__dirname,".ssh");return{dir:s,filename:e,path:t(s,e)}};const addSshKey=(e,r)=>{const{dir:s,filename:t}=getPrivateKeyPath(r);o({dir:s,filename:"known_hosts",content:""});console.log("✅ [SSH] known_hosts file ensured",s);o({dir:s,filename:t,content:e,isRequired:true});console.log("✅ [SSH] key added to `.ssh` dir ",s,t)};e.exports={getPrivateKeyPath:getPrivateKeyPath,addSshKey:addSshKey}},81:e=>{"use strict";e.exports=require("child_process")},147:e=>{"use strict";e.exports=require("fs")},17:e=>{"use strict";e.exports=require("path")},837:e=>{"use strict";e.exports=require("util")}};var r={};function __nccwpck_require__(s){var t=r[s];if(t!==undefined){return t.exports}var o=r[s]={exports:{}};var n=true;try{e[s](o,o.exports,__nccwpck_require__);n=false}finally{if(n)delete r[s]}return o.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var s={};(()=>{const{sshDeploy:e}=__nccwpck_require__(447);const{remoteCmdBefore:r,remoteCmdAfter:s}=__nccwpck_require__(976);const{addSshKey:t,getPrivateKeyPath:o}=__nccwpck_require__(822);const{validateRequiredInputs:n}=__nccwpck_require__(505);const i=__nccwpck_require__(229);const run=()=>{const{source:c,remoteUser:a,remoteHost:l,remotePort:d,deployKeyName:u,sshPrivateKey:f,args:p,exclude:h,sshCmdArgs:y,scriptBefore:m,scriptAfter:g,rsyncServer:_}=i;n({sshPrivateKey:f,remoteHost:l,remoteUser:a});t(f,u);const{path:v}=o(u);if(m){r(m)}let callback=()=>{};if(g){callback=(...e)=>{s(g,e)}}e({source:c,rsyncServer:_,exclude:h,remotePort:d,privateKeyPath:v,args:p,sshCmdArgs:y,callback:callback})};run()})();module.exports=s})(); \ No newline at end of file diff --git a/src/index.js b/src/index.js index 78d2d6a..6bb6ee2 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ #!/usr/bin/env node const { sshDeploy } = require('./rsyncCli'); const { remoteCmdBefore, remoteCmdAfter } = require('./remoteCmd'); -const { addSshKey } = require('./sshKey'); +const { addSshKey, getPrivateKeyPath } = require('./sshKey'); const { validateRequiredInputs } = require('./helpers'); const inputs = require('./inputs'); @@ -16,7 +16,8 @@ const run = () => { // Validate required inputs validateRequiredInputs({ sshPrivateKey, remoteHost, remoteUser }); // Add SSH key - const privateKey = addSshKey(sshPrivateKey, deployKeyName); + addSshKey(sshPrivateKey, deployKeyName); + const { path: privateKeyPath } = getPrivateKeyPath(deployKeyName); // Check Script before if (scriptBefore) { remoteCmdBefore(scriptBefore); @@ -31,7 +32,7 @@ const run = () => { /* eslint-disable object-property-newline */ sshDeploy({ source, rsyncServer, exclude, remotePort, - privateKey, args, sshCmdArgs, callback + privateKeyPath, args, sshCmdArgs, callback }); }; diff --git a/src/remoteCmd.js b/src/remoteCmd.js index f274421..9e73140 100644 --- a/src/remoteCmd.js +++ b/src/remoteCmd.js @@ -1,6 +1,6 @@ const { exec } = require('child_process'); -const { sshServer, githubWorkspace } = require('./inputs'); +const { privateKey, sshServer, githubWorkspace } = require('./inputs'); const { writeToFile } = require('./helpers'); const remoteCmd = (content, label) => { @@ -8,7 +8,7 @@ const remoteCmd = (content, label) => { try { writeToFile({ dir: githubWorkspace, filename, content }); - exec(`ssh ${sshServer} 'bash -s' < ${filename}`, (err, data, stderr) => { + exec(`ssh -i ${privateKey} ${sshServer} 'bash -s' < ${filename}`, (err, data, stderr) => { if (err) { console.log('⚠️ [CMD] Remote script failed. ', err.message); } else { diff --git a/src/rsyncCli.js b/src/rsyncCli.js index c6740b1..e3db592 100644 --- a/src/rsyncCli.js +++ b/src/rsyncCli.js @@ -3,19 +3,13 @@ const nodeRsync = require('rsyncwrapper'); // eslint-disable-next-line no-async-promise-executor const validateRsync = new Promise(async (resolve, reject) => { - let rsyncCli; try { execSync('rsync --version', { stdio: 'inherit' }); - rsyncCli = true; - } catch (e) { - rsyncCli = false; - console.log('⚠️ [CLI] Rsync doesn\'t exists', e); - } - - if (rsyncCli) { console.log('⚠️ [CLI] Rsync exists'); resolve(); return; + } catch (e) { + console.log('⚠️ [CLI] Rsync doesn\'t exists', e); } console.log('⚠️ [CLI] Rsync doesn\'t exists. Start installation with "apt-get" \n'); @@ -30,7 +24,7 @@ const validateRsync = new Promise(async (resolve, reject) => { const rsyncCli = ({ source, rsyncServer, exclude, remotePort, - privateKey, args, sshCmdArgs, callback + privateKeyPath, args, sshCmdArgs, callback }) => { console.log(`[Rsync] Starting Rsync Action: ${source} to ${rsyncServer}`); if (exclude) console.log(`[Rsync] excluding folders ${exclude}`); @@ -46,7 +40,7 @@ const rsyncCli = ({ nodeRsync({ ...defaultOptions, src: source, dest: rsyncServer, excludeFirst: exclude, port: remotePort, - privateKey, args, sshCmdArgs, + privateKey: privateKeyPath, args, sshCmdArgs }, (error, stdout, stderr, cmd) => { if (error) { console.error('⚠️ [Rsync] error: ', error.message); diff --git a/src/sshKey.js b/src/sshKey.js index 47d205b..2dac7c4 100644 --- a/src/sshKey.js +++ b/src/sshKey.js @@ -1,20 +1,25 @@ const { join } = require('path'); - const { writeToFile } = require('./helpers'); -const addSshKey = (content, filename) => { +const getPrivateKeyPath = (filename) => { const { HOME } = process.env; const dir = join(HOME || __dirname, '.ssh'); - const filePath = join(dir, filename); + return { + dir, + filename, + path: join(dir, filename) + }; +}; +const addSshKey = (content, deployKeyName) => { + const { dir, filename } = getPrivateKeyPath(deployKeyName); writeToFile({ dir, filename: 'known_hosts', content: '' }); - console.log('✅ [SSH] known_hosts file ensured', dir, filename, content.length); + console.log('✅ [SSH] known_hosts file ensured', dir); writeToFile({ dir, filename, content, isRequired: true }); - console.log('✅ [SSH] key added to `.ssh` dir ', dir); - - return filePath; + console.log('✅ [SSH] key added to `.ssh` dir ', dir, filename); }; module.exports = { + getPrivateKeyPath, addSshKey };