mirror of
https://github.com/easingthemes/ssh-deploy
synced 2025-04-23 00:42:08 +00:00
2 lines
9.0 KiB
JavaScript
Executable File
2 lines
9.0 KiB
JavaScript
Executable File
#!/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 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 d="ssh";if(typeof e.port!=="undefined"){d+=" -p "+e.port}if(typeof e.privateKey!=="undefined"){d+=" -i "+e.privateKey}if(typeof e.sshCmdArgs!=="undefined"){d+=" "+e.sshCmdArgs.join(" ")}i.push(d)}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,s){i.push("--exclude="+e)}))}if(typeof e.include!=="undefined"&&o.isArray(e.include)){e.include.forEach((function(e,s){i.push("--include="+e)}))}if(typeof e.exclude!=="undefined"&&o.isArray(e.exclude)){e.exclude.forEach((function(e,s){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 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){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){l(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:c}=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,mode:i="0o644"})=>{validateDir(e);const a=c(e,s);if(t(a)){console.log(`[FILE] ${a} file exist`);if(o){throw new Error(`⚠️ [FILE] ${a} Required file exist, aborting ...`)}return}try{console.log(`[FILE] writing ${a} file ...`,r.length);n(a,r,{encoding:"utf8",mode:i})}catch(e){throw new Error(`⚠️[FILE] Writing to file error. filePath: ${a}, 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 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 s=t(e.toLowerCase());const r=process.env[e]||process.env[`INPUT_${e}`];const o=r===undefined?i[s]:r;let c=o;switch(s){case"source":c=`${n}/${o}`;break;case"exclude":case"args":case"sshCmdArgs":c=o.split(",").map((e=>e.trim()));break}a[s]=c}));a.sshServer=`${a.remoteUser}@${a.remoteHost}`;a.rsyncServer=`${a.remoteUser}@${a.remoteHost}:${a.target}`;e.exports=a},976:(e,s,r)=>{const{exec:t}=r(81);const{privateKey:o,sshServer:n,githubWorkspace:c}=r(229);const{writeToFile:i}=r(505);const handleError=(e,s,r)=>{const t=`⚠️ [CMD] Remote script failed: ${e}`;if(s){r(new Error(t))}else{console.log(t)}};const remoteCmd=async(e,s,r)=>new Promise(((a,d)=>{const l=`local_ssh_script-${s}.sh`;try{i({dir:c,filename:l,content:e});t(`DEBIAN_FRONTEND=noninteractive ssh -i ${o} ${n} 'RSYNC_STDOUT=${process.env.RSYNC_STDOUT} bash -s' < ${l}`,((e,s,t)=>{if(e){handleError(e.message,r,d)}else{console.log("✅ [CMD] Remote script executed. \n",s,t);a(s)}}))}catch(e){handleError(e.message,r,d)}}));e.exports={remoteCmdBefore:async(e,s)=>remoteCmd(e,"before",s),remoteCmdAfter:async(e,s)=>remoteCmd(e,"after",s)}},447:(e,s,r)=>{const{execSync:t}=r(81);const o=r(898);const{writeToFile:n}=r(505);const nodeRsyncPromise=async e=>new Promise(((s,r)=>{try{o(e,((e,t,o,n)=>{if(e){console.log("⚠️ [Rsync] stderr: ",o);console.log("⚠️ [Rsync] stdout: ",t);console.log("⚠️ [Rsync] cmd: ",n);r(e)}else{s(t)}}))}catch(e){console.error("⚠️ [Rsync] command error: ",e.message,e.stack);r(e)}}));const validateRsync=async()=>{try{t("rsync --version",{stdio:"inherit"});console.log("⚠️ [CLI] Rsync exists");return}catch(e){console.log("⚠️ [CLI] Rsync doesn't exists",e)}console.log('⚠️ [CLI] Start rsync 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")}catch(e){throw new Error(`⚠️ [CLI] Rsync installation failed. Aborting ... error: ${e.message}`)}};const rsyncCli=async({source:e,rsyncServer:s,exclude:r,remotePort:t,privateKeyPath:o,args:n,sshCmdArgs:c})=>{console.log(`[Rsync] Starting Rsync Action: ${e} to ${s}`);if(r)console.log(`[Rsync] excluding folders ${r}`);const i={ssh:true,recursive:true};return nodeRsyncPromise({...i,src:e,dest:s,excludeFirst:r,port:t,privateKey:o,args:n,sshCmdArgs:c})};const sshDeploy=async e=>{await validateRsync();const s=await rsyncCli(e);console.log("✅ [Rsync] finished.",s);process.env.RSYNC_STDOUT=s;return s};e.exports={sshDeploy:sshDeploy}},822:(e,s,r)=>{const{join:t}=r(17);const{execSync:o}=r(81);const{writeToFile:n}=r(505);const c="known_hosts";const getPrivateKeyPath=(e="")=>{const{HOME:s}=process.env;const r=t(s||__dirname,".ssh");const o=t(r,c);return{dir:r,filename:e,path:t(r,e),knownHostsPath:o}};const addSshKey=(e,s)=>{const{dir:r,filename:t}=getPrivateKeyPath(s);n({dir:r,filename:c,content:""});console.log("✅ [SSH] known_hosts file ensured",r);n({dir:r,filename:t,content:e,isRequired:true});console.log("✅ [SSH] key added to `.ssh` dir ",r,t)};const updateKnownHosts=e=>{const{knownHostsPath:s}=getPrivateKeyPath();console.log("✅ [SSH] Adding host to `known_hosts` ....",e,s);try{o(`ssh-keyscan -H ${e} >> ${s}`,{stdio:"inherit"})}catch(s){console.error("✅ [SSH] Adding host to `known_hosts` ERROR",e,s.message)}console.log("✅ [SSH] Adding host to `known_hosts` DONE",e,s)};e.exports={getPrivateKeyPath:getPrivateKeyPath,updateKnownHosts:updateKnownHosts,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,getPrivateKeyPath:o,updateKnownHosts:n}=__nccwpck_require__(822);const{validateRequiredInputs:c}=__nccwpck_require__(505);const i=__nccwpck_require__(229);const run=async()=>{const{source:a,remoteUser:d,remoteHost:l,remotePort:u,deployKeyName:f,sshPrivateKey:p,args:h,exclude:y,sshCmdArgs:g,scriptBefore:m,scriptAfter:_,rsyncServer:v}=i;c({sshPrivateKey:p,remoteHost:l,remoteUser:d});t(p,f);const{path:w}=o(f);if(m||_){n(l)}if(m){await s(m)}await e({source:a,rsyncServer:v,exclude:y,remotePort:u,privateKeyPath:w,args:h,sshCmdArgs:g});if(_){await r(_)}};run().then((()=>{console.log("DONE")})).catch((()=>{console.error("ERROR")}))})();module.exports=r})(); |