mirror of
https://github.com/easingthemes/ssh-deploy
synced 2025-04-22 00:02:18 +00:00
2 lines
11 KiB
JavaScript
Executable File
2 lines
11 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
(()=>{var e={126:(e,r,t)=>{var s=t(147);var o;if(process.platform==="win32"||global.TESTING_WINDOWS){o=t(1)}else{o=t(728)}e.exports=isexe;isexe.sync=sync;function isexe(e,r,t){if(typeof r==="function"){t=r;r={}}if(!t){if(typeof Promise!=="function"){throw new TypeError("callback not provided")}return new Promise((function(t,s){isexe(e,r||{},(function(e,r){if(e){s(e)}else{t(r)}}))}))}o(e,r||{},(function(e,s){if(e){if(e.code==="EACCES"||r&&r.ignoreErrors){e=null;s=false}}t(e,s)}))}function sync(e,r){try{return o.sync(e,r||{})}catch(e){if(r&&r.ignoreErrors||e.code==="EACCES"){return false}else{throw e}}}},728:(e,r,t)=>{e.exports=isexe;isexe.sync=sync;var s=t(147);function isexe(e,r,t){s.stat(e,(function(e,s){t(e,e?false:checkStat(s,r))}))}function sync(e,r){return checkStat(s.statSync(e),r)}function checkStat(e,r){return e.isFile()&&checkMode(e,r)}function checkMode(e,r){var t=e.mode;var s=e.uid;var o=e.gid;var n=r.uid!==undefined?r.uid:process.getuid&&process.getuid();var c=r.gid!==undefined?r.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 d=t&u||t&a&&o===c||t&i&&s===n||t&l&&n===0;return d}},1:(e,r,t)=>{e.exports=isexe;isexe.sync=sync;var s=t(147);function checkPathExt(e,r){var t=r.pathExt!==undefined?r.pathExt:process.env.PATHEXT;if(!t){return true}t=t.split(";");if(t.indexOf("")!==-1){return true}for(var s=0;s<t.length;s++){var o=t[s].toLowerCase();if(o&&e.substr(-o.length).toLowerCase()===o){return true}}return false}function checkStat(e,r,t){if(!e.isSymbolicLink()&&!e.isFile()){return false}return checkPathExt(r,t)}function isexe(e,r,t){s.stat(e,(function(s,o){t(s,s?false:checkStat(o,e,r))}))}function sync(e,r){return checkStat(s.statSync(e),e,r)}},898:(e,r,t)=>{"use strict";var s=t(81).spawn;var o=t(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 t=e[r];if(typeof t==="string"){e[r]=escapeSpaces(t)}else if(Array.isArray(t)===true){e[r]=t.map(escapeSpaces)}}));return e};e.exports=function(e,r){e=e||{};e=o._extend({},e);e=escapeSpacesInOptions(e);var t=e.platform||process.platform;var n=t==="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,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 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 h="";var y;if(n){y=s("cmd.exe",["/s","/c",'"'+f+'"'],{windowsVerbatimArguments:true,stdio:[process.stdin,"pipe","pipe"]})}else{y=s("/bin/sh",["-c",f])}y.stdout.on("data",(function(e){l(e);p+=e}));y.stderr.on("data",(function(e){d(e);h+=e}));y.on("exit",(function(e){var t=null;if(e!==0){t=new Error("rsync exited with code "+e);t.code=e}r(t,p,h,f)}))}catch(e){r(e,null,null,f)}}},143:(e,r,t)=>{const s=t(126);const{join:o,delimiter:n,sep:c,posix:i}=t(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:r=process.env.PATH,pathExt:t=process.env.PATHEXT,delimiter:s=n})=>{const o=e.match(u)?[""]:[...a?[process.cwd()]:[],...(r||"").split(s)];if(a){const r=t||[".EXE",".CMD",".BAT",".COM"].join(s);const n=r.split(s);if(e.includes(".")&&n[0]!==""){n.unshift("")}return{pathEnv:o,pathExt:n,pathExtExe:r}}return{pathEnv:o,pathExt:[""]}};const getPathPart=(e,r)=>{const t=/^".*"$/.test(e)?e.slice(1,-1):e;const s=!t&&l.test(r)?r.slice(0,2):"";return s+o(t,r)};const which=async(e,r={})=>{const{pathEnv:t,pathExt:o,pathExtExe:n}=getPathInfo(e,r);const c=[];for(const i of t){const t=getPathPart(i,e);for(const e of o){const o=t+e;const i=await s(o,{pathExt:n,ignoreErrors:true});if(i){if(!r.all){return o}c.push(o)}}}if(r.all&&c.length){return c}if(r.nothrow){return null}throw getNotFoundError(e)};const whichSync=(e,r={})=>{const{pathEnv:t,pathExt:o,pathExtExe:n}=getPathInfo(e,r);const c=[];for(const i of t){const t=getPathPart(i,e);for(const e of o){const o=t+e;const i=s.sync(o,{pathExt:n,ignoreErrors:true});if(i){if(!r.all){return o}c.push(o)}}}if(r.all&&c.length){return c}if(r.nothrow){return null}throw getNotFoundError(e)};e.exports=which;which.sync=whichSync},505:(e,r,t)=>{const{existsSync:s,mkdirSync:o,writeFileSync:n}=t(147);const{join:c}=t(17);const validateDir=e=>{if(!e){console.log("[SSH] dir is not defined");return}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:r,content:t,isRequired:o})=>{validateDir(e);const i=c(e,r);if(s(i)){console.log(`[FILE] ${i} file exist`);if(o){throw new Error(`⚠️ [FILE] ${i} Required file exist, aborting ...`)}return}try{n(i,t,{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 t=r.filter((r=>{const t=e[r];if(!t){console.error(`⚠️ [INPUTS] ${r} is mandatory`)}return t}));if(t.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,t)=>{const{snakeToCamel:s}=t(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=s(e.toLowerCase());const t=process.env[e]||process.env[`INPUT_${e}`];const o=t===undefined?i[r]:t;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.target}`;e.exports=a},976:(e,r,t)=>{const{exec:s}=t(81);const{sshServer:o,githubWorkspace:n}=t(229);const{writeToFile:c}=t(505);const remoteCmd=(e,r)=>{const t=`local_ssh_script-${r}.sh`;try{c({dir:n,filename:t,content:e});s(`ssh ${o} 'bash -s' < ${t}`,((e,r,t)=>{if(e){console.log("⚠️ [CMD] Remote script failed. ",e.message)}else{console.log("✅ [CMD] Remote script executed. \n",r,t)}}))}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,t)=>{const{execSync:s}=t(81);const o=t(143);const n=t(898);const validateRsync=async()=>new Promise((async(e,r)=>{const t=await o("rsync",{nothrow:true});s("rsync --version",{stdio:"inherit"});if(t){console.log("⚠️ [CLI] Rsync exists");s("rsync --version",{stdio:"inherit"});e()}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");e()}catch(e){r(Error(`⚠️ [CLI] Rsync installation failed. Aborting ... error: ${e.message}`))}}));const rsyncCli=({source:e,sshServer:r,exclude:t,remotePort:s,privateKey:o,args:c,sshCmdArgs:i,callback:a})=>{console.log(`[Rsync] Starting Rsync Action: ${e} to ${r}`);if(t)console.log(`[Rsync] excluding folders ${t}`);const u={ssh:true,recursive:true};try{n({...u,src:e,dest:r,excludeFirst:t,port:s,privateKey:o,args:c,sshCmdArgs:i},((e,r,t,s)=>{if(e){console.error("⚠️ [Rsync] error: ",e.message);console.log("⚠️ [Rsync] stderr: ",t);console.log("⚠️ [Rsync] stdout: ",r);console.log("⚠️ [Rsync] cmd: ",s)}else{console.log("✅ [Rsync] finished.",r)}a(e,r,t,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,r,t)=>{const{join:s}=t(17);const{writeToFile:o}=t(505);const addSshKey=(e,r)=>{const{HOME:t}=process.env;const n=s(t||__dirname,".ssh");const c=s(n,r);o({dir:n,filename:"known_hosts",content:""});console.log("✅ [SSH] known_hosts file ensured",n,r,e);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__(t){var s=r[t];if(s!==undefined){return s.exports}var o=r[t]={exports:{}};var n=true;try{e[t](o,o.exports,__nccwpck_require__);n=false}finally{if(n)delete r[t]}return o.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var t={};(()=>{const{sshDeploy:e}=__nccwpck_require__(447);const{remoteCmdBefore:r,remoteCmdAfter:t}=__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:d,args:f,exclude:p,sshCmdArgs:h,scriptBefore:y,scriptAfter:g,sshServer:m}=n;o({sshPrivateKey:d,remoteHost:a,remoteUser:i});const x=s(d,l);if(y){r(y)}let callback=()=>{};if(g){callback=(...e)=>{t(g,e)}}e({source:c,sshServer:m,exclude:p,remotePort:u,privateKey:x,args:f,sshCmdArgs:h,callback:callback})};run()})();module.exports=t})(); |