diff --git a/.eslintrc.js b/.eslintrc.js index 1324c84..df1d632 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -12,14 +12,14 @@ module.exports = { SharedArrayBuffer: 'readonly' }, parserOptions: { - ecmaVersion: 2018, + ecmaVersion: 2018 }, rules: { - "comma-dangle": [ - "error", - "never" + 'comma-dangle': [ + 'error', + 'never' ], - "no-console": "off", - "object-curly-newline": "off" + 'no-console': 'off', + 'object-curly-newline': 'off' } }; diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index f2b81cc..619e4d2 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -2,7 +2,7 @@ name: e2e Test on: push: - branches: [ 'main' ] + branches: [ 'feature/ssh-cmd' ] env: TEST_HOST_DOCKER: ./test @@ -55,7 +55,7 @@ jobs: cat index.html - name: e2e Test published ssh-deploy action - uses: easingthemes/ssh-deploy@v3.1.0 + uses: easingthemes/ssh-deploy@feature/ssh-cmd with: # SSH_PRIVATE_KEY: $EXAMPLE_SSH_PRIVATE_KEY # REMOTE_HOST: $EXAMPLE_REMOTE_HOST1 @@ -64,6 +64,9 @@ jobs: SOURCE: "test_project/" TARGET: "/var/www/html/" EXCLUDE: "/dist/, /node_modules/" - SCRIPT: | + SCRIPT_BEFORE: | + whoami + ls -al + SCRIPT_AFTER: | whoami ls -al diff --git a/README.md b/README.md index fdc7162..ee08028 100644 --- a/README.md +++ b/README.md @@ -107,13 +107,13 @@ jobs: ## Issues -This is a Github Action wrapping `rsync` via `ssh`. Only issues with action functionality can be fixed here. +This is a GitHub Action wrapping `rsync` via `ssh`. Only issues with action functionality can be fixed here. Almost 95% of the issues are related to wrong SSH connection or `rsync` params and permissions. -This issues are not related to the action itself. +These issues are not related to the action itself. - Check manually your ssh connection from your client before opening a bug report. -- Check `rsync` params for your usecase. Default params are not going to be enough wor everyone, it highly depends on your setup. +- Check `rsync` params for your use-case. Default params are not going to be enough wor everyone, it highly depends on your setup. - Check manually your rsync command from your client before opening a bug report. I've added e2e test for this action. diff --git a/action.yml b/action.yml index 0e01857..ca7935b 100644 --- a/action.yml +++ b/action.yml @@ -31,10 +31,14 @@ inputs: description: "An array of folder to exclude" required: false default: "" - SCRIPT: - description: "Script to run on host machine" + SCRIPT_BEFORE: + description: "Script to run on host machine before rsync" required: false - default: "ls" + default: "echo 'Before rsync'" + SCRIPT_AFTER: + description: "Script to run on host machine after rsync" + required: false + default: "echo 'After rsync'" outputs: status: description: "Status" diff --git a/dist/index.js b/dist/index.js index 3d77b41..4ea71ac 100755 --- a/dist/index.js +++ b/dist/index.js @@ -1,2 +1,2 @@ #!/usr/bin/env node -(()=>{var e={569:(e,r,s)=>{e.exports=s(325)},325:(e,r,s)=>{"use strict";var t=s(81).exec;var n=s(81).execSync;var o=s(147);var c=s(17);var i=o.access;var a=o.accessSync;var u=o.constants||o;var l=process.platform=="win32";var fileNotExists=function(e,r){i(e,u.F_OK,(function(e){r(!e)}))};var fileNotExistsSync=function(e){try{a(e,u.F_OK);return false}catch(e){return true}};var localExecutable=function(e,r){i(e,u.F_OK|u.X_OK,(function(e){r(null,!e)}))};var localExecutableSync=function(e){try{a(e,u.F_OK|u.X_OK);return true}catch(e){return false}};var commandExistsUnix=function(e,r,s){fileNotExists(e,(function(n){if(!n){var o=t("command -v "+r+" 2>/dev/null"+" && { echo >&1 "+r+"; exit 0; }",(function(e,r,t){s(null,!!r)}));return}localExecutable(e,s)}))};var commandExistsWindows=function(e,r,s){if(!/^(?!(?:.*\s|.*\.|\W+)$)(?:[a-zA-Z]:)?(?:(?:[^<>:"\|\?\*\n])+(?:\/\/|\/|\\\\|\\)?)+$/m.test(e)){s(null,false);return}var n=t("where "+r,(function(e){if(e!==null){s(null,false)}else{s(null,true)}}))};var commandExistsUnixSync=function(e,r){if(fileNotExistsSync(e)){try{var s=n("command -v "+r+" 2>/dev/null"+" && { echo >&1 "+r+"; exit 0; }");return!!s}catch(e){return false}}return localExecutableSync(e)};var commandExistsWindowsSync=function(e,r,s){if(!/^(?!(?:.*\s|.*\.|\W+)$)(?:[a-zA-Z]:)?(?:(?:[^<>:"\|\?\*\n])+(?:\/\/|\/|\\\\|\\)?)+$/m.test(e)){return false}try{var t=n("where "+r,{stdio:[]});return!!t}catch(e){return false}};var cleanInput=function(e){if(/[^A-Za-z0-9_\/:=-]/.test(e)){e="'"+e.replace(/'/g,"'\\''")+"'";e=e.replace(/^(?:'')+/g,"").replace(/\\'''/g,"\\'")}return e};if(l){cleanInput=function(e){var r=/[\\]/.test(e);if(r){var s='"'+c.dirname(e)+'"';var t='"'+c.basename(e)+'"';return s+":"+t}return'"'+e+'"'}}e.exports=function commandExists(e,r){var s=cleanInput(e);if(!r&&typeof Promise!=="undefined"){return new Promise((function(r,s){commandExists(e,(function(t,n){if(n){r(e)}else{s(t)}}))}))}if(l){commandExistsWindows(e,s,r)}else{commandExistsUnix(e,s,r)}};e.exports.sync=function(e){var r=cleanInput(e);if(l){return commandExistsWindowsSync(e,r)}else{return commandExistsUnixSync(e,r)}}},898:(e,r,s)=>{"use strict";var t=s(81).spawn;var n=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=n._extend({},e);e=escapeSpacesInOptions(e);var s=e.platform||process.platform;var o=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(o&&!a){i.push("--chmod=ugo=rwX")}if(typeof e.host!=="undefined"||e.ssh){i.push("--rsh");var u="ssh";if(typeof e.port!=="undefined"){u+=" -p "+e.port}if(typeof e.privateKey!=="undefined"){u+=" -i "+e.privateKey}if(typeof e.sshCmdArgs!=="undefined"){u+=" "+e.sshCmdArgs.join(" ")}i.push(u)}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"&&n.isArray(e.excludeFirst)){e.excludeFirst.forEach((function(e,r){i.push("--exclude="+e)}))}if(typeof e.include!=="undefined"&&n.isArray(e.include)){e.include.forEach((function(e,r){i.push("--include="+e)}))}if(typeof e.exclude!=="undefined"&&n.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"&&n.isArray(e.args)){i=[...new Set([...i,...e.args])]}i=[...new Set(i)];var noop=function(){};var l=e.onStdout||noop;var d=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 y="";var v;if(o){v=t("cmd.exe",["/s","/c",'"'+f+'"'],{windowsVerbatimArguments:true,stdio:[process.stdin,"pipe","pipe"]})}else{v=t("/bin/sh",["-c",f])}v.stdout.on("data",(function(e){l(e);p+=e}));v.stderr.on("data",(function(e){d(e);y+=e}));v.on("exit",(function(e){var s=null;if(e!==0){s=new Error("rsync exited with code "+e);s.code=e}r(s,p,y,f)}))}catch(e){r(e,null,null,f)}}},505:(e,r,s)=>{const{existsSync:t,mkdirSync:n,writeFileSync:o}=s(147);const{GITHUB_WORKSPACE:c}=process.env;const validateDir=e=>{if(!t(e)){console.log(`[SSH] Creating ${e} dir in `,c);n(e);console.log("✅ [SSH] dir created.")}else{console.log(`[SSH] ${e} dir exist`)}};const validateFile=e=>{if(!t(e)){console.log(`[SSH] Creating ${e} file in `,c);try{o(e,"",{encoding:"utf8",mode:384});console.log("✅ [SSH] file created.")}catch(r){console.error("⚠️ [SSH] writeFileSync error",e,r.message);process.abort()}}else{console.log(`[SSH] ${e} file exist`)}};e.exports={validateDir:validateDir,validateFile:validateFile}},229:e=>{const r=["REMOTE_HOST","REMOTE_USER","REMOTE_PORT","SSH_PRIVATE_KEY","DEPLOY_KEY_NAME","SOURCE","TARGET","ARGS","EXCLUDE"];const s={GITHUB_WORKSPACE:process.env.GITHUB_WORKSPACE};r.forEach((e=>{s[e]=process.env[e]||process.env[`INPUT_${e}`]}));e.exports=s},447:(e,r,s)=>{const{sync:t}=s(569);const{exec:n,execSync:o}=s(81);const validateRsync=(e=(()=>{}))=>{const r=t("rsync");if(r){console.log("⚠️ [CLI] Rsync exists");const r=o("rsync --version",{stdio:"inherit"});return e()}console.log('⚠️ [CLI] Rsync doesn\'t exists. Start installation with "apt-get" \n');n("sudo apt-get update && sudo apt-get --no-install-recommends install rsync",((r,s,t)=>{if(r){console.log("⚠️ [CLI] Rsync installation failed. Aborting ... ",r.message);process.abort()}else{console.log("✅ [CLI] Rsync installed. \n",s,t);e()}}))};const validateInputs=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){console.error("⚠️ [INPUTS] Inputs not valid, aborting ...");process.abort()}};e.exports={validateRsync:validateRsync,validateInputs:validateInputs}},822:(e,r,s)=>{const{writeFileSync:t}=s(147);const{join:n}=s(17);const{validateDir:o,validateFile:c}=s(505);const{HOME:i}=process.env;const addSshKey=(e,r)=>{const s=n(i||__dirname,".ssh");const a=n(s,r);o(s);c(`${s}/known_hosts`);try{t(a,e,{encoding:"utf8",mode:384})}catch(e){console.error("⚠️ writeFileSync error",a,e.message);process.abort()}console.log("✅ Ssh key added to `.ssh` dir ",a);return a};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 n=r[s]={exports:{}};var o=true;try{e[s](n,n.exports,__nccwpck_require__);o=false}finally{if(o)delete r[s]}return n.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var s={};(()=>{const e=__nccwpck_require__(898);const{validateRsync:r,validateInputs:s}=__nccwpck_require__(447);const{addSshKey:t}=__nccwpck_require__(822);const{REMOTE_HOST:n,REMOTE_USER:o,REMOTE_PORT:c,SSH_PRIVATE_KEY:i,DEPLOY_KEY_NAME:a,SOURCE:u,TARGET:l,ARGS:d,EXCLUDE:f,GITHUB_WORKSPACE:p}=__nccwpck_require__(229);const y={ssh:true,sshCmdArgs:["-o StrictHostKeyChecking=no"],recursive:true};console.log("GITHUB_WORKSPACE: ",p);console.log("REMOTE_HOST: ",process.env.REMOTE_HOST);console.log("REMOTE_USER: ",process.env.REMOTE_USER);const v=(()=>{const rsync=({privateKey:r,port:s,src:t,dest:n,args:o,exclude:c})=>{console.log(`[Rsync] Starting Rsync Action: ${t} to ${n}`);if(c)console.log(`[Rsync] exluding folders ${c}`);try{e({src:t,dest:n,args:o,privateKey:r,port:s,excludeFirst:c,...y},((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);process.abort()}else{console.log("✅ [Rsync] finished.",r)}}))}catch(e){console.error("⚠️ [Rsync] command error: ",e.message,e.stack);process.abort()}};const init=({src:e,dest:s,args:n,host:o="localhost",port:c,username:i,privateKeyContent:u,exclude:l=[]})=>{r((()=>{const r=t(u,a||"deploy_key");const d=`${i}@${o}:${s}`;rsync({privateKey:r,port:c,src:e,dest:d,args:n,exclude:l})}))};return{init:init}})();const run=()=>{s({SSH_PRIVATE_KEY:i,REMOTE_HOST:n,REMOTE_USER:o});v.init({src:`${p}/${u||""}`,dest:l||`/home/${o}/`,args:d?[d]:["-rltgoDzvO"],host:n,port:c||"22",username:o,privateKeyContent:i,exclude:(f||"").split(",").map((e=>e.trim()))})};run()})();module.exports=s})(); \ No newline at end of file +(()=>{var e={126:(e,t,r)=>{var s=r(147);var o;if(process.platform==="win32"||global.TESTING_WINDOWS){o=r(1)}else{o=r(728)}e.exports=isexe;isexe.sync=sync;function isexe(e,t,r){if(typeof t==="function"){r=t;t={}}if(!r){if(typeof Promise!=="function"){throw new TypeError("callback not provided")}return new Promise((function(r,s){isexe(e,t||{},(function(e,t){if(e){s(e)}else{r(t)}}))}))}o(e,t||{},(function(e,s){if(e){if(e.code==="EACCES"||t&&t.ignoreErrors){e=null;s=false}}r(e,s)}))}function sync(e,t){try{return o.sync(e,t||{})}catch(e){if(t&&t.ignoreErrors||e.code==="EACCES"){return false}else{throw e}}}},728:(e,t,r)=>{e.exports=isexe;isexe.sync=sync;var s=r(147);function isexe(e,t,r){s.stat(e,(function(e,s){r(e,e?false:checkStat(s,t))}))}function sync(e,t){return checkStat(s.statSync(e),t)}function checkStat(e,t){return e.isFile()&&checkMode(e,t)}function checkMode(e,t){var r=e.mode;var s=e.uid;var o=e.gid;var n=t.uid!==undefined?t.uid:process.getuid&&process.getuid();var c=t.gid!==undefined?t.gid:process.getgid&&process.getgid();var i=parseInt("100",8);var a=parseInt("010",8);var u=parseInt("001",8);var l=i|a;var f=r&u||r&a&&o===c||r&i&&s===n||r&l&&n===0;return f}},1:(e,t,r)=>{e.exports=isexe;isexe.sync=sync;var s=r(147);function checkPathExt(e,t){var r=t.pathExt!==undefined?t.pathExt:process.env.PATHEXT;if(!r){return true}r=r.split(";");if(r.indexOf("")!==-1){return true}for(var s=0;s{"use strict";var s=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(t){var r=e[t];if(typeof r==="string"){e[t]=escapeSpaces(r)}else if(Array.isArray(r)===true){e[t]=r.map(escapeSpaces)}}));return e};e.exports=function(e,t){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 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 u="ssh";if(typeof e.port!=="undefined"){u+=" -p "+e.port}if(typeof e.privateKey!=="undefined"){u+=" -i "+e.privateKey}if(typeof e.sshCmdArgs!=="undefined"){u+=" "+e.sshCmdArgs.join(" ")}i.push(u)}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,t){i.push("--exclude="+e)}))}if(typeof e.include!=="undefined"&&o.isArray(e.include)){e.include.forEach((function(e,t){i.push("--include="+e)}))}if(typeof e.exclude!=="undefined"&&o.isArray(e.exclude)){e.exclude.forEach((function(e,t){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 l=e.onStdout||noop;var f=e.onStderr||noop;var d="rsync ";i.forEach((function(e){if(e.substr(0,4)==="ssh "){e='"'+e+'"'}d+=e+" "}));d=d.trim();if(e.noExec){t(null,null,null,d);return}try{var p="";var h="";var y;if(n){y=s("cmd.exe",["/s","/c",'"'+d+'"'],{windowsVerbatimArguments:true,stdio:[process.stdin,"pipe","pipe"]})}else{y=s("/bin/sh",["-c",d])}y.stdout.on("data",(function(e){l(e);p+=e}));y.stderr.on("data",(function(e){f(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}t(r,p,h,d)}))}catch(e){t(e,null,null,d)}}},143:(e,t,r)=>{const s=r(126);const{join:o,delimiter:n,sep:c,posix:i}=r(17);const a=process.platform==="win32";const u=new RegExp(`[${i.sep}${c===i.sep?"":c}]`.replace(/(\\)/g,"\\$1"));const l=new RegExp(`^\\.${u.source}`);const getNotFoundError=e=>Object.assign(new Error(`not found: ${e}`),{code:"ENOENT"});const getPathInfo=(e,{path:t=process.env.PATH,pathExt:r=process.env.PATHEXT,delimiter:s=n})=>{const o=e.match(u)?[""]:[...a?[process.cwd()]:[],...(t||"").split(s)];if(a){const t=r||[".EXE",".CMD",".BAT",".COM"].join(s);const n=t.split(s);if(e.includes(".")&&n[0]!==""){n.unshift("")}return{pathEnv:o,pathExt:n,pathExtExe:t}}return{pathEnv:o,pathExt:[""]}};const getPathPart=(e,t)=>{const r=/^".*"$/.test(e)?e.slice(1,-1):e;const s=!r&&l.test(t)?t.slice(0,2):"";return s+o(r,t)};const which=async(e,t={})=>{const{pathEnv:r,pathExt:o,pathExtExe:n}=getPathInfo(e,t);const c=[];for(const i of r){const r=getPathPart(i,e);for(const e of o){const o=r+e;const i=await s(o,{pathExt:n,ignoreErrors:true});if(i){if(!t.all){return o}c.push(o)}}}if(t.all&&c.length){return c}if(t.nothrow){return null}throw getNotFoundError(e)};const whichSync=(e,t={})=>{const{pathEnv:r,pathExt:o,pathExtExe:n}=getPathInfo(e,t);const c=[];for(const i of r){const r=getPathPart(i,e);for(const e of o){const o=r+e;const i=s.sync(o,{pathExt:n,ignoreErrors:true});if(i){if(!t.all){return o}c.push(o)}}}if(t.all&&c.length){return c}if(t.nothrow){return null}throw getNotFoundError(e)};e.exports=which;which.sync=whichSync},505:(e,t,r)=>{const{existsSync:s,mkdirSync:o,writeFileSync:n}=r(147);const{join:c}=r(17);const validateDir=e=>{if(s(e)){console.log(`[SSH] ${e} dir exist`);return}console.log(`[SSH] Creating ${e} dir in workspace root`);o(e);console.log("✅ [SSH] dir created.")};const writeToFile=({dir:e,filename:t,content:r,isRequired:o})=>{validateDir(e);const i=c(e,t);if(s(i)){console.log(`[FILE] ${i} file exist`);if(o){throw new Error(`⚠️ [FILE] ${i} Required file exist, aborting ...`)}return}try{n(i,r,{encoding:"utf8",mode:384})}catch(e){throw new Error(`⚠️[FILE] Writing to file error. filePath: ${i}, message: ${e.message}`)}};const validateRequiredInputs=e=>{const t=Object.keys(e);const r=t.filter((t=>{const r=e[t];if(!r){console.error(`⚠️ [INPUTS] ${t} is mandatory`)}return r}));if(r.length!==t.length){throw new Error("⚠️ [INPUTS] Inputs not valid, aborting ...")}};const snakeToCamel=e=>e.replace(/[^a-zA-Z0-9]+(.)/g,((e,t)=>t.toUpperCase()));e.exports={writeToFile:writeToFile,validateRequiredInputs:validateRequiredInputs,snakeToCamel:snakeToCamel}},229:(e,t,r)=>{const{snakeToCamel:s}=r(505);const o=["REMOTE_HOST","REMOTE_USER","REMOTE_PORT","SSH_PRIVATE_KEY","DEPLOY_KEY_NAME","SOURCE","TARGET","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",deployKeyName:"deploy_key"};const a={githubWorkspace:n};o.forEach((e=>{const t=s(e.toLowerCase());const r=process.env[e]||process.env[`INPUT_${e}`];const o=r===undefined?i[t]:r;let c=o;switch(t){case"source":c=`${n}/${o}`;break;case"exclude":c=o.split(",").map((e=>e.trim()));break;case"args":c=[o];break}a[t]=c}));a.sshServer=`${a.remoteUser}@${a.remoteHost}:${a.target}`;e.exports=a},976:(e,t,r)=>{const{join:s}=r(17);const{exec:o}=r(81);const{sshServer:n,githubWorkspace:c}=r(229);const{writeToFile:i}=r(505);const remoteCmd=(e,t)=>{const r=s(c,`local_ssh_script-${t}.sh`);try{i(r,e);o(`ssh ${n} 'bash -s' < ${r}`,((e,t,r)=>{if(e){console.log("⚠️ [CMD] Remote script failed. ",e.message)}else{console.log("✅ [CMD] Remote script executed. \n",t,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,t,r)=>{const{execSync:s}=r(81);const o=r(143);const n=r(898);const validateRsync=async()=>{const e=await o("rsync",{nothrow:true});s("rsync --version",{stdio:"inherit"});if(e){console.log("⚠️ [CLI] Rsync exists");s("rsync --version",{stdio:"inherit"});return}console.log('⚠️ [CLI] Rsync doesn\'t exists. Start installation with "apt-get" \n');try{s("sudo apt-get update && sudo apt-get --no-install-recommends install rsync",{stdio:"inherit"});console.log("✅ [CLI] Rsync installed. \n")}catch(e){throw new Error(`⚠️ [CLI] Rsync installation failed. Aborting ... error: ${e.message}`)}};const rsyncCli=({source:e,sshServer:t,exclude:r,remotePort:s,privateKey:o,args:c,callback:i})=>{console.log(`[Rsync] Starting Rsync Action: ${e} to ${t}`);if(r)console.log(`[Rsync] excluding folders ${r}`);const a={ssh:true,sshCmdArgs:["-o StrictHostKeyChecking=no"],recursive:true};try{n({src:e,dest:t,excludeFirst:r,port:s,privateKey:o,args:c,callback:i,...a},((e,t,r,s)=>{if(e){console.error("⚠️ [Rsync] error: ",e.message);console.log("⚠️ [Rsync] stderr: ",r);console.log("⚠️ [Rsync] stdout: ",t);console.log("⚠️ [Rsync] cmd: ",s)}else{console.log("✅ [Rsync] finished.",t)}i(e,t,r,s);if(e){process.abort()}}))}catch(e){console.error("⚠️ [Rsync] command error: ",e.message,e.stack);process.abort()}};const sshDeploy=e=>{validateRsync.then((()=>{rsyncCli(e)})).catch((e=>{throw e}))};e.exports={sshDeploy:sshDeploy}},822:(e,t,r)=>{const{join:s}=r(17);const{writeToFile:o}=r(505);const addSshKey=(e,t)=>{const{HOME:r}=process.env;const n=s(r||__dirname,".ssh");const c=s(n,t);o({dir:n,filename:"known_hosts",content:""});o({dir:n,filename:t,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 t={};function __nccwpck_require__(r){var s=t[r];if(s!==undefined){return s.exports}var o=t[r]={exports:{}};var n=true;try{e[r](o,o.exports,__nccwpck_require__);n=false}finally{if(n)delete t[r]}return o.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var r={};(()=>{const{sshDeploy:e}=__nccwpck_require__(447);const{remoteCmdBefore:t,remoteCmdAfter:r}=__nccwpck_require__(976);const{addSshKey:s}=__nccwpck_require__(822);const{validateRequiredInputs:o}=__nccwpck_require__(505);const n=__nccwpck_require__(229);const run=()=>{const{source:c,remoteUser:i,remoteHost:a,remotePort:u,deployKeyName:l,sshPrivateKey:f,args:d,exclude:p,scriptBefore:h,scriptAfter:y,sshServer:g}=n;o({sshPrivateKey:f,remoteHost:a,remoteUser:i});const x=s(f,l);if(h){t(h)}let callback=()=>{};if(y){callback=(...e)=>{r(y,e)}}e({source:c,sshServer:g,exclude:p,remotePort:u,privateKey:x,args:d,callback:callback})};run()})();module.exports=r})(); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 4026c44..36e4400 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,16 @@ { "name": "@draganfilipovic/ssh-deploy", - "version": "3.0.1", + "version": "3.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@draganfilipovic/ssh-deploy", - "version": "3.0.1", + "version": "3.1.0", "license": "MIT", "dependencies": { - "command-exists": "^1.2.9", - "rsyncwrapper": "^3.0.1" + "rsyncwrapper": "^3.0.1", + "which": "^3.0.0" }, "devDependencies": { "@vercel/ncc": "^0.36.0", @@ -301,11 +301,6 @@ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", "dev": true }, - "node_modules/command-exists": { - "version": "1.2.9", - "resolved": "https://registry.npmjs.org/command-exists/-/command-exists-1.2.9.tgz", - "integrity": "sha512-LTQ/SGc+s0Xc0Fu5WaKnR0YiygZkm9eKFvyS+fRsU7/ZWFF8ykFM6Pc9aCVf1+xasOOZpO3BAVgVrKvsqKHV7w==" - }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -332,6 +327,21 @@ "node": ">= 8" } }, + "node_modules/cross-spawn/node_modules/which": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", + "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", + "dev": true, + "dependencies": { + "isexe": "^2.0.0" + }, + "bin": { + "node-which": "bin/node-which" + }, + "engines": { + "node": ">= 8" + } + }, "node_modules/debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", @@ -1280,8 +1290,7 @@ "node_modules/isexe": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", - "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", - "dev": true + "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==" }, "node_modules/js-sdsl": { "version": "4.2.0", @@ -1923,18 +1932,17 @@ } }, "node_modules/which": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", - "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", - "dev": true, + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/which/-/which-3.0.0.tgz", + "integrity": "sha512-nla//68K9NU6yRiwDY/Q8aU6siKlSs64aEC7+IV56QoAuyQT2ovsJcgGYGyqMOmI/CGN1BOR6mM5EN0FBO+zyQ==", "dependencies": { "isexe": "^2.0.0" }, "bin": { - "node-which": "bin/node-which" + "node-which": "bin/which.js" }, "engines": { - "node": ">= 8" + "node": "^14.17.0 || ^16.13.0 || >=18.0.0" } }, "node_modules/which-boxed-primitive": { @@ -2188,11 +2196,6 @@ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", "dev": true }, - "command-exists": { - "version": "1.2.9", - "resolved": "https://registry.npmjs.org/command-exists/-/command-exists-1.2.9.tgz", - "integrity": "sha512-LTQ/SGc+s0Xc0Fu5WaKnR0YiygZkm9eKFvyS+fRsU7/ZWFF8ykFM6Pc9aCVf1+xasOOZpO3BAVgVrKvsqKHV7w==" - }, "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -2214,6 +2217,17 @@ "path-key": "^3.1.0", "shebang-command": "^2.0.0", "which": "^2.0.1" + }, + "dependencies": { + "which": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", + "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", + "dev": true, + "requires": { + "isexe": "^2.0.0" + } + } } }, "debug": { @@ -2912,8 +2926,7 @@ "isexe": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", - "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", - "dev": true + "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==" }, "js-sdsl": { "version": "4.2.0", @@ -3360,10 +3373,9 @@ } }, "which": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", - "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", - "dev": true, + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/which/-/which-3.0.0.tgz", + "integrity": "sha512-nla//68K9NU6yRiwDY/Q8aU6siKlSs64aEC7+IV56QoAuyQT2ovsJcgGYGyqMOmI/CGN1BOR6mM5EN0FBO+zyQ==", "requires": { "isexe": "^2.0.0" } diff --git a/package.json b/package.json index 756b0ee..b964f31 100644 --- a/package.json +++ b/package.json @@ -30,8 +30,8 @@ }, "homepage": "https://github.com/easingthemes/ssh-deploy#readme", "dependencies": { - "command-exists": "^1.2.9", - "rsyncwrapper": "^3.0.1" + "rsyncwrapper": "^3.0.1", + "which": "^3.0.0" }, "devDependencies": { "@vercel/ncc": "^0.36.0", diff --git a/src/helpers.js b/src/helpers.js index e9c66d4..2c1b2fa 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -1,38 +1,60 @@ const { existsSync, mkdirSync, writeFileSync } = require('fs'); - -const { - GITHUB_WORKSPACE -} = process.env; +const { join } = require('path'); const validateDir = (dir) => { - if (!existsSync(dir)) { - console.log(`[SSH] Creating ${dir} dir in `, GITHUB_WORKSPACE); - mkdirSync(dir); - console.log('✅ [SSH] dir created.'); - } else { + if (existsSync(dir)) { console.log(`[SSH] ${dir} dir exist`); + return; + } + + console.log(`[SSH] Creating ${dir} dir in workspace root`); + mkdirSync(dir); + console.log('✅ [SSH] dir created.'); +}; + +const writeToFile = ({ dir, filename, content, isRequired }) => { + validateDir(dir); + const filePath = join(dir, filename); + + if (existsSync(filePath)) { + console.log(`[FILE] ${filePath} file exist`); + if (isRequired) { + throw new Error(`⚠️ [FILE] ${filePath} Required file exist, aborting ...`); + } + return; + } + + try { + writeFileSync(filePath, content, { + encoding: 'utf8', + mode: 0o600 + }); + } catch (e) { + throw new Error(`⚠️[FILE] Writing to file error. filePath: ${filePath}, message: ${e.message}`); } }; -const validateFile = (filePath) => { - if (!existsSync(filePath)) { - console.log(`[SSH] Creating ${filePath} file in `, GITHUB_WORKSPACE); - try { - writeFileSync(filePath, '', { - encoding: 'utf8', - mode: 0o600 - }); - console.log('✅ [SSH] file created.'); - } catch (e) { - console.error('⚠️ [SSH] writeFileSync error', filePath, e.message); - process.abort(); +const validateRequiredInputs = (inputs) => { + const inputKeys = Object.keys(inputs); + const validInputs = inputKeys.filter((inputKey) => { + const inputValue = inputs[inputKey]; + + if (!inputValue) { + console.error(`⚠️ [INPUTS] ${inputKey} is mandatory`); } - } else { - console.log(`[SSH] ${filePath} file exist`); + + return inputValue; + }); + + if (validInputs.length !== inputKeys.length) { + throw new Error('⚠️ [INPUTS] Inputs not valid, aborting ...'); } }; +const snakeToCamel = (str) => str.replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase()); + module.exports = { - validateDir, - validateFile + writeToFile, + validateRequiredInputs, + snakeToCamel }; diff --git a/src/index.js b/src/index.js index 8276749..1cd13c4 100644 --- a/src/index.js +++ b/src/index.js @@ -1,78 +1,37 @@ #!/usr/bin/env node -const nodeRsync = require('rsyncwrapper'); - -const { validateRsync, validateInputs } = require('./rsyncCli'); +const { sshDeploy } = require('./rsyncCli'); +const { remoteCmdBefore, remoteCmdAfter } = require('./remoteCmd'); const { addSshKey } = require('./sshKey'); - -const { - REMOTE_HOST, REMOTE_USER, - REMOTE_PORT, SSH_PRIVATE_KEY, DEPLOY_KEY_NAME, - SOURCE, TARGET, ARGS, EXCLUDE, - GITHUB_WORKSPACE -} = require('./inputs'); - -const defaultOptions = { - ssh: true, - sshCmdArgs: ['-o StrictHostKeyChecking=no'], - recursive: true -}; - -console.log('GITHUB_WORKSPACE: ', GITHUB_WORKSPACE); -console.log('REMOTE_HOST: ', process.env.REMOTE_HOST); -console.log('REMOTE_USER: ', process.env.REMOTE_USER); - -const sshDeploy = (() => { - const rsync = ({ privateKey, port, src, dest, args, exclude }) => { - console.log(`[Rsync] Starting Rsync Action: ${src} to ${dest}`); - if (exclude) console.log(`[Rsync] exluding folders ${exclude}`); - - try { - // RSYNC COMMAND - nodeRsync({ - src, dest, args, privateKey, port, excludeFirst: exclude, ...defaultOptions - }, (error, stdout, stderr, cmd) => { - if (error) { - console.error('⚠️ [Rsync] error: ', error.message); - console.log('⚠️ [Rsync] stderr: ', stderr); - console.log('⚠️ [Rsync] stdout: ', stdout); - console.log('⚠️ [Rsync] cmd: ', cmd); - process.abort(); - } else { - console.log('✅ [Rsync] finished.', stdout); - } - }); - } catch (err) { - console.error('⚠️ [Rsync] command error: ', err.message, err.stack); - process.abort(); - } - }; - - const init = ({ src, dest, args, host = 'localhost', port, username, privateKeyContent, exclude = [] }) => { - validateRsync(() => { - const privateKey = addSshKey(privateKeyContent, DEPLOY_KEY_NAME || 'deploy_key'); - const remoteDest = `${username}@${host}:${dest}`; - - rsync({ privateKey, port, src, dest: remoteDest, args, exclude }); - }); - }; - - return { - init - }; -})(); +const { validateRequiredInputs } = require('./helpers'); +const inputs = require('./inputs'); const run = () => { - validateInputs({ SSH_PRIVATE_KEY, REMOTE_HOST, REMOTE_USER }); - - sshDeploy.init({ - src: `${GITHUB_WORKSPACE}/${SOURCE || ''}`, - dest: TARGET || `/home/${REMOTE_USER}/`, - args: ARGS ? [ARGS] : ['-rltgoDzvO'], - host: REMOTE_HOST, - port: REMOTE_PORT || '22', - username: REMOTE_USER, - privateKeyContent: SSH_PRIVATE_KEY, - exclude: (EXCLUDE || '').split(',').map((item) => item.trim()) // split by comma and trim whitespace + const { + source, remoteUser, remoteHost, remotePort, + deployKeyName, sshPrivateKey, + args, exclude, + scriptBefore, scriptAfter, + sshServer + } = inputs; + // Validate required inputs + validateRequiredInputs({ sshPrivateKey, remoteHost, remoteUser }); + // Add SSH key + const privateKey = addSshKey(sshPrivateKey, deployKeyName); + // Check Script before + if (scriptBefore) { + remoteCmdBefore(scriptBefore); + } + // Check script after + let callback = () => {}; + if (scriptAfter) { + callback = (...result) => { + remoteCmdAfter(scriptAfter, result); + }; + } + /* eslint-disable object-property-newline */ + sshDeploy({ + source, sshServer, exclude, remotePort, + privateKey, args, callback }); }; diff --git a/src/inputs.js b/src/inputs.js index 9b7638e..54460a1 100644 --- a/src/inputs.js +++ b/src/inputs.js @@ -1,11 +1,47 @@ -const inputNames = ['REMOTE_HOST', 'REMOTE_USER', 'REMOTE_PORT', 'SSH_PRIVATE_KEY', 'DEPLOY_KEY_NAME', 'SOURCE', 'TARGET', 'ARGS', 'EXCLUDE']; +const { snakeToCamel } = require('./helpers'); + +const inputNames = [ + 'REMOTE_HOST', 'REMOTE_USER', 'REMOTE_PORT', + 'SSH_PRIVATE_KEY', 'DEPLOY_KEY_NAME', + 'SOURCE', 'TARGET', 'ARGS', 'EXCLUDE', + 'SCRIPT_BEFORE', 'SCRIPT_AFTER']; + +const githubWorkspace = process.env.GITHUB_WORKSPACE; +const remoteUser = process.env.REMOTE_USER; + +const defaultInputs = { + source: '', // TODO + target: `/home/${remoteUser}/`, + exclude: '', // TODO + args: '-rltgoDzvO', // TODO + deployKeyName: 'deploy_key' +}; const inputs = { - GITHUB_WORKSPACE: process.env.GITHUB_WORKSPACE + githubWorkspace }; inputNames.forEach((input) => { - inputs[input] = process.env[input] || process.env[`INPUT_${input}`]; + const inputName = snakeToCamel(input.toLowerCase()); + const inputVal = process.env[input] || process.env[`INPUT_${input}`]; + const validVal = inputVal === undefined ? defaultInputs[inputName] : inputVal; + let extendedVal = validVal; + // eslint-disable-next-line default-case + switch (inputName) { + case 'source': + extendedVal = `${githubWorkspace}/${validVal}`; + break; + case 'exclude': + extendedVal = validVal.split(',').map((item) => item.trim()); + break; + case 'args': + extendedVal = [validVal]; + break; + } + + inputs[inputName] = extendedVal; }); +inputs.sshServer = `${inputs.remoteUser}@${inputs.remoteHost}:${inputs.target}`; + module.exports = inputs; diff --git a/src/remoteCmd.js b/src/remoteCmd.js index e69de29..81f3c19 100644 --- a/src/remoteCmd.js +++ b/src/remoteCmd.js @@ -0,0 +1,27 @@ +const { join } = require('path'); +const { exec } = require('child_process'); + +const { sshServer, githubWorkspace } = require('./inputs'); +const { writeToFile } = require('./helpers'); + +const remoteCmd = (cmd, label) => { + const localScriptPath = join(githubWorkspace, `local_ssh_script-${label}.sh`); + try { + writeToFile(localScriptPath, cmd); + + exec(`ssh ${sshServer} 'bash -s' < ${localScriptPath}`, (err, data, stderr) => { + if (err) { + console.log('⚠️ [CMD] Remote script failed. ', err.message); + } else { + console.log('✅ [CMD] Remote script executed. \n', data, stderr); + } + }); + } catch (err) { + console.log('⚠️ [CMD] Starting Remote script execution failed. ', err.message); + } +}; + +module.exports = { + remoteCmdBefore: (cmd) => remoteCmd(cmd, 'before'), + remoteCmdAfter: (cmd) => remoteCmd(cmd, 'after') +}; diff --git a/src/rsyncCli.js b/src/rsyncCli.js index 1157499..7f93dcb 100644 --- a/src/rsyncCli.js +++ b/src/rsyncCli.js @@ -1,46 +1,78 @@ -const { sync: commandExists } = require("command-exists"); -const { exec, execSync } = require("child_process"); +const { execSync } = require('child_process'); +const which = require('which'); +const nodeRsync = require('rsyncwrapper'); -const validateRsync = (callback = () => {}) => { - const rsyncCli = commandExists("rsync"); +const validateRsync = async () => { + const rsyncCli = await which('rsync', { nothrow: true }); + execSync('rsync --version', { stdio: 'inherit' }); if (rsyncCli) { console.log('⚠️ [CLI] Rsync exists'); - execSync("rsync --version", { stdio: 'inherit' }); - return callback(); + execSync('rsync --version', { stdio: 'inherit' }); + return; } console.log('⚠️ [CLI] Rsync doesn\'t exists. Start installation with "apt-get" \n'); - exec("sudo apt-get update && sudo apt-get --no-install-recommends install rsync", (err, data, stderr) => { - if (err) { - console.log("⚠️ [CLI] Rsync installation failed. Aborting ... ", err.message); - process.abort(); - } else { - console.log("✅ [CLI] Rsync installed. \n", data, stderr); - callback(); - } - }); + try { + execSync('sudo apt-get update && sudo apt-get --no-install-recommends install rsync', { stdio: 'inherit' }); + console.log('✅ [CLI] Rsync installed. \n'); + } catch (err) { + throw new Error(`⚠️ [CLI] Rsync installation failed. Aborting ... error: ${err.message}`); + } }; -const validateInputs = (inputs) => { - const inputKeys = Object.keys(inputs); - const validInputs = inputKeys.filter((inputKey) => { - const inputValue = inputs[inputKey]; +const rsyncCli = ({ + source, sshServer, exclude, remotePort, + privateKey, args, callback +}) => { + console.log(`[Rsync] Starting Rsync Action: ${source} to ${sshServer}`); + if (exclude) console.log(`[Rsync] excluding folders ${exclude}`); - if (!inputValue) { - console.error(`⚠️ [INPUTS] ${inputKey} is mandatory`); - } + const defaultOptions = { + ssh: true, + sshCmdArgs: ['-o StrictHostKeyChecking=no'], + recursive: true + }; - return inputValue; - }); + try { + // RSYNC COMMAND + /* eslint-disable object-property-newline */ + nodeRsync({ + src: source, dest: sshServer, excludeFirst: exclude, port: remotePort, + privateKey, args, callback, + ...defaultOptions + }, (error, stdout, stderr, cmd) => { + if (error) { + console.error('⚠️ [Rsync] error: ', error.message); + console.log('⚠️ [Rsync] stderr: ', stderr); + console.log('⚠️ [Rsync] stdout: ', stdout); + console.log('⚠️ [Rsync] cmd: ', cmd); + } else { + console.log('✅ [Rsync] finished.', stdout); + } - if (validInputs.length !== inputKeys.length) { - console.error("⚠️ [INPUTS] Inputs not valid, aborting ..."); + callback(error, stdout, stderr, cmd); + + if (error) { + process.abort(); + } + }); + } catch (err) { + console.error('⚠️ [Rsync] command error: ', err.message, err.stack); process.abort(); } }; -module.exports = { - validateRsync, - validateInputs, +const sshDeploy = (params) => { + validateRsync + .then(() => { + rsyncCli(params); + }) + .catch((err) => { + throw err; + }); +}; + +module.exports = { + sshDeploy }; diff --git a/src/sshKey.js b/src/sshKey.js index 72eca6a..5f5e79e 100644 --- a/src/sshKey.js +++ b/src/sshKey.js @@ -1,37 +1,20 @@ -const { writeFileSync } = require('fs'); const { join } = require('path'); -const { - validateDir, - validateFile -} = require('./helpers'); +const { writeToFile } = require('./helpers'); -const { - HOME -} = process.env; +const addSshKey = (content, filename) => { + const { HOME } = process.env; + const dir = join(HOME || __dirname, '.ssh'); + const filePath = join(dir, filename); -const addSshKey = (key, name) => { - const sshDir = join(HOME || __dirname, '.ssh'); - const filePath = join(sshDir, name); + writeToFile({ dir, filename: 'known_hosts', content: '' }); + writeToFile({ dir, filename, content, isRequired: true }); - validateDir(sshDir); - validateFile(`${sshDir}/known_hosts`); - - try { - writeFileSync(filePath, key, { - encoding: 'utf8', - mode: 0o600 - }); - } catch (e) { - console.error('⚠️ writeFileSync error', filePath, e.message); - process.abort(); - } - - console.log('✅ Ssh key added to `.ssh` dir ', filePath); + console.log('✅ Ssh key added to `.ssh` dir ', dir); return filePath; }; module.exports = { addSshKey -} +}; diff --git a/src/test.js b/src/test.js deleted file mode 100644 index 75ab1c1..0000000 --- a/src/test.js +++ /dev/null @@ -1,13 +0,0 @@ -console.log('||||||||||||||||||||||||||||||||||||||'); -console.log('EXAMPLE_REMOTE_HOST: ', process.env.EXAMPLE_REMOTE_HOST); -console.log('EXAMPLE_REMOTE_USER: ', process.env.EXAMPLE_REMOTE_USER); -console.log('EXAMPLE_SSH_PRIVATE_KEY: ', process.env.EXAMPLE_SSH_PRIVATE_KEY); -console.log('||||||||||||||||||||||||||||||||||||||'); -console.log('EXAMPLE_REMOTE_HOST1: ', process.env.EXAMPLE_REMOTE_HOST1); -console.log('EXAMPLE_REMOTE_USER1: ', process.env.EXAMPLE_REMOTE_USER1); -console.log('EXAMPLE_SSH_PRIVATE_KEY1: ', process.env.EXAMPLE_SSH_PRIVATE_KEY1); -console.log('||||||||||||||||||||||||||||||||||||||'); -console.log('REMOTE_USER: ', process.env.REMOTE_USER); -console.log('REMOTE_HOST: ', process.env.REMOTE_HOST); -console.log('SSH_PRIVATE_KEY: ', process.env.SSH_PRIVATE_KEY); -console.log('||||||||||||||||||||||||||||||||||||||');