ssh-deploy/dist/index.js
2023-01-02 02:01:47 +01:00

2 lines
8.0 KiB
JavaScript
Executable File

#!/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 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.target}`;e.exports=a},976:(e,r,s)=>{const{exec:t}=s(81);const{sshServer:o,githubWorkspace:n}=s(229);const{writeToFile:i}=s(505);const remoteCmd=(e,r)=>{const s=`local_ssh_script-${r}.sh`;try{i({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}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 apt-get update && sudo apt-get --no-install-recommends 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,sshServer:r,exclude:s,remotePort:t,privateKey: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 addSshKey=(e,r)=>{const{HOME:s}=process.env;const n=t(s||__dirname,".ssh");const i=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 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 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: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){r(y)}let callback=()=>{};if(g){callback=(...e)=>{s(g,e)}}e({source:i,sshServer:m,exclude:p,remotePort:l,privateKey:_,args:f,sshCmdArgs:h,callback:callback})};run()})();module.exports=s})();