diff --git a/dist/index.js b/dist/index.js index 1166555..7e9b9eb 100755 --- a/dist/index.js +++ b/dist/index.js @@ -1,2 +1,2 @@ #!/usr/bin/env node -(()=>{var e={898:(e,s,r)=>{"use strict";var t=r(81).spawn;var o=r(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(s){var r=e[s];if(typeof r==="string"){e[s]=escapeSpaces(r)}else if(Array.isArray(r)===true){e[s]=r.map(escapeSpaces)}}));return e};e.exports=function(e,s){e=e||{};e=o._extend({},e);e=escapeSpacesInOptions(e);var r=e.platform||process.platform;var n=r==="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,s){c.push("--exclude="+e)}))}if(typeof e.include!=="undefined"&&o.isArray(e.include)){e.include.forEach((function(e,s){c.push("--include="+e)}))}if(typeof e.exclude!=="undefined"&&o.isArray(e.exclude)){e.exclude.forEach((function(e,s){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){s(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 r=null;if(e!==0){r=new Error("rsync exited with code "+e);r.code=e}s(r,p,h,f)}))}catch(e){s(e,null,null,f)}}},505:(e,s,r)=>{const{existsSync:t,mkdirSync:o,writeFileSync:n}=r(147);const{join:i}=r(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:s,content:r,isRequired:o})=>{validateDir(e);const c=i(e,s);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 ...`,r.length);n(c,r,{encoding:"utf8",mode:384})}catch(e){throw new Error(`⚠️[FILE] Writing to file error. filePath: ${c}, message: ${e.message}`)}};const validateRequiredInputs=e=>{const s=Object.keys(e);const r=s.filter((s=>{const r=e[s];if(!r){console.error(`⚠️ [INPUTS] ${s} is mandatory`)}return r}));if(r.length!==s.length){throw new Error("⚠️ [INPUTS] Inputs not valid, aborting ...")}};const snakeToCamel=e=>e.replace(/[^a-zA-Z0-9]+(.)/g,((e,s)=>s.toUpperCase()));e.exports={writeToFile:writeToFile,validateRequiredInputs:validateRequiredInputs,snakeToCamel:snakeToCamel}},229:(e,s,r)=>{const{snakeToCamel:t}=r(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 s=t(e.toLowerCase());const r=process.env[e]||process.env[`INPUT_${e}`];const o=r===undefined?c[s]:r;let i=o;switch(s){case"source":i=`${n}/${o}`;break;case"exclude":case"args":case"sshCmdArgs":i=o.split(",").map((e=>e.trim()));break}a[s]=i}));a.sshServer=`${a.remoteUser}@${a.remoteHost}:${a.target}`;e.exports=a},976:(e,s,r)=>{const{exec:t}=r(81);const{sshServer:o,githubWorkspace:n}=r(229);const{writeToFile:i}=r(505);const remoteCmd=(e,s)=>{const r=`local_ssh_script-${s}.sh`;try{i({dir:n,filename:r,content:e});t(`ssh ${o} 'bash -s' < ${r}`,((e,s,r)=>{if(e){console.log("⚠️ [CMD] Remote script failed. ",e.message)}else{console.log("✅ [CMD] Remote script executed. \n",s,r)}}))}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,s,r)=>{const{execSync:t}=r(81);const o=r(898);const n=new Promise((async(e,s)=>{let r=false;try{t("rsync --version",{stdio:"inherit"});r=true}catch(e){r=false;console.log("⚠️ [CLI] Rsync doesn't exists",e)}if(r){console.log("⚠️ [CLI] Rsync exists");t("rsync --version",{stdio:"inherit"});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){s(Error(`⚠️ [CLI] Rsync installation failed. Aborting ... error: ${e.message}`))}}));const rsyncCli=({source:e,sshServer:s,exclude:r,remotePort:t,privateKey:n,args:i,sshCmdArgs:c,callback:a})=>{console.log(`[Rsync] Starting Rsync Action: ${e} to ${s}`);if(r)console.log(`[Rsync] excluding folders ${r}`);const l={ssh:true,recursive:true};try{o({...l,src:e,dest:s,excludeFirst:r,port:t,privateKey:n,args:i,sshCmdArgs:c},((e,s,r,t)=>{if(e){console.error("⚠️ [Rsync] error: ",e.message);console.log("⚠️ [Rsync] stderr: ",r);console.log("⚠️ [Rsync] stdout: ",s);console.log("⚠️ [Rsync] cmd: ",t)}else{console.log("✅ [Rsync] finished.",s)}a(e,s,r,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,s,r)=>{const{join:t}=r(17);const{writeToFile:o}=r(505);const addSshKey=(e,s)=>{const{HOME:r}=process.env;const n=t(r||__dirname,".ssh");const i=t(n,s);o({dir:n,filename:"known_hosts",content:""});console.log("✅ [SSH] known_hosts file ensured",n,s,e.length);o({dir:n,filename:s,content:e,isRequired:true});console.log("✅ [SSH] key added to `.ssh` dir ",n);return i};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 s={};function __nccwpck_require__(r){var t=s[r];if(t!==undefined){return t.exports}var o=s[r]={exports:{}};var n=true;try{e[r](o,o.exports,__nccwpck_require__);n=false}finally{if(n)delete s[r]}return o.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var r={};(()=>{const{sshDeploy:e}=__nccwpck_require__(447);const{remoteCmdBefore:s,remoteCmdAfter:r}=__nccwpck_require__(976);const{addSshKey:t}=__nccwpck_require__(822);const{validateRequiredInputs:o}=__nccwpck_require__(505);const n=__nccwpck_require__(229);const run=()=>{const{source:i,remoteUser:c,remoteHost:a,remotePort:l,deployKeyName:d,sshPrivateKey:u,args:f,exclude:p,sshCmdArgs:h,scriptBefore:y,scriptAfter:g,sshServer:m}=n;o({sshPrivateKey:u,remoteHost:a,remoteUser:c});const _=t(u,d);if(y){s(y)}let callback=()=>{};if(g){callback=(...e)=>{r(g,e)}}e({source:i,sshServer:m,exclude:p,remotePort:l,privateKey:_,args:f,sshCmdArgs:h,callback:callback})};run()})();module.exports=r})(); \ 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 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=false;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");t("rsync --version",{stdio:"inherit"});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,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 diff --git a/src/index.js b/src/index.js index afd09cc..78d2d6a 100644 --- a/src/index.js +++ b/src/index.js @@ -11,7 +11,7 @@ const run = () => { deployKeyName, sshPrivateKey, args, exclude, sshCmdArgs, scriptBefore, scriptAfter, - sshServer + rsyncServer } = inputs; // Validate required inputs validateRequiredInputs({ sshPrivateKey, remoteHost, remoteUser }); @@ -30,7 +30,7 @@ const run = () => { } /* eslint-disable object-property-newline */ sshDeploy({ - source, sshServer, exclude, remotePort, + source, rsyncServer, exclude, remotePort, privateKey, args, sshCmdArgs, callback }); }; diff --git a/src/inputs.js b/src/inputs.js index 6eb970c..f636080 100644 --- a/src/inputs.js +++ b/src/inputs.js @@ -42,6 +42,7 @@ inputNames.forEach((input) => { inputs[inputName] = extendedVal; }); -inputs.sshServer = `${inputs.remoteUser}@${inputs.remoteHost}:${inputs.target}`; +inputs.sshServer = `${inputs.remoteUser}@${inputs.remoteHost}`; +inputs.rsyncServer = `${inputs.remoteUser}@${inputs.remoteHost}:${inputs.target}`; module.exports = inputs; diff --git a/src/rsyncCli.js b/src/rsyncCli.js index 1dd835f..939ec3e 100644 --- a/src/rsyncCli.js +++ b/src/rsyncCli.js @@ -29,10 +29,10 @@ const validateRsync = new Promise(async (resolve, reject) => { }); const rsyncCli = ({ - source, sshServer, exclude, remotePort, + source, rsyncServer, exclude, remotePort, privateKey, args, sshCmdArgs, callback }) => { - console.log(`[Rsync] Starting Rsync Action: ${source} to ${sshServer}`); + console.log(`[Rsync] Starting Rsync Action: ${source} to ${rsyncServer}`); if (exclude) console.log(`[Rsync] excluding folders ${exclude}`); const defaultOptions = { @@ -45,7 +45,7 @@ const rsyncCli = ({ /* eslint-disable object-property-newline */ nodeRsync({ ...defaultOptions, - src: source, dest: sshServer, excludeFirst: exclude, port: remotePort, + src: source, dest: rsyncServer, excludeFirst: exclude, port: remotePort, privateKey, args, sshCmdArgs, }, (error, stdout, stderr, cmd) => { if (error) {