mirror of
https://github.com/easingthemes/ssh-deploy.git
synced 2025-06-21 12:02:09 +10:00
Feature/ssh cmd (#94)
* feat: Add SSH remote script support - before and after rsync * fix: remove __dirname * feat: add sshCmdArgs option * Add promise instead of callback * fix: improve logs * fix: Add simple command exists instead of a plugin * add non interactive install * feat: add onStderr and onStdout logs * Improve reject messages * feat: Add RSYNC_STDOUT env variable * emoji updates * fix: update workflow actions
This commit is contained in:
parent
a5d8edb941
commit
ec9347f8c6
17 changed files with 373 additions and 242 deletions
|
@ -1,38 +1,71 @@
|
|||
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 {
|
||||
console.log(`[SSH] ${dir} dir exist`);
|
||||
if (!dir) {
|
||||
console.warn('⚠️ [DIR] dir is not defined');
|
||||
return;
|
||||
}
|
||||
if (existsSync(dir)) {
|
||||
console.log(`✅ [DIR] ${dir} dir exist`);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[DIR] Creating ${dir} dir in workspace root`);
|
||||
mkdirSync(dir);
|
||||
console.log('✅ [DIR] dir created.');
|
||||
};
|
||||
|
||||
const handleError = (message, isRequired) => {
|
||||
if (isRequired) {
|
||||
throw new Error(message);
|
||||
}
|
||||
console.warn(message);
|
||||
};
|
||||
|
||||
const writeToFile = ({ dir, filename, content, isRequired, mode = '0644' }) => {
|
||||
validateDir(dir);
|
||||
const filePath = join(dir, filename);
|
||||
|
||||
if (existsSync(filePath)) {
|
||||
const message = `⚠️ [FILE] ${filePath} Required file exist.`;
|
||||
handleError(message, isRequired);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
console.log(`[FILE] writing ${filePath} file ...`, content.length);
|
||||
writeFileSync(filePath, content, {
|
||||
encoding: 'utf8',
|
||||
mode
|
||||
});
|
||||
} catch (error) {
|
||||
const message = `⚠️[FILE] Writing to file error. filePath: ${filePath}, message: ${error.message}`;
|
||||
handleError(message, isRequired);
|
||||
}
|
||||
};
|
||||
|
||||
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
|
||||
};
|
||||
|
|
116
src/index.js
116
src/index.js
|
@ -1,79 +1,47 @@
|
|||
#!/usr/bin/env node
|
||||
const nodeRsync = require('rsyncwrapper');
|
||||
const { sshDeploy } = require('./rsyncCli');
|
||||
const { remoteCmdBefore, remoteCmdAfter } = require('./remoteCmd');
|
||||
const { addSshKey, getPrivateKeyPath, updateKnownHosts } = require('./sshKey');
|
||||
const { validateRequiredInputs } = require('./helpers');
|
||||
const inputs = require('./inputs');
|
||||
|
||||
const { validateRsync, validateInputs } = require('./rsyncCli');
|
||||
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 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 run = async () => {
|
||||
const {
|
||||
source, remoteUser, remoteHost, remotePort,
|
||||
deployKeyName, sshPrivateKey,
|
||||
args, exclude, sshCmdArgs,
|
||||
scriptBefore, scriptAfter,
|
||||
rsyncServer
|
||||
} = inputs;
|
||||
// Validate required inputs
|
||||
validateRequiredInputs({ sshPrivateKey, remoteHost, remoteUser });
|
||||
// Add SSH key
|
||||
addSshKey(sshPrivateKey, deployKeyName);
|
||||
const { path: privateKeyPath } = getPrivateKeyPath(deployKeyName);
|
||||
// Update known hosts if ssh command is present to avoid prompt
|
||||
if (scriptBefore || scriptAfter) {
|
||||
updateKnownHosts(remoteHost);
|
||||
}
|
||||
// Check Script before
|
||||
if (scriptBefore) {
|
||||
await remoteCmdBefore(scriptBefore, privateKeyPath);
|
||||
}
|
||||
/* eslint-disable object-property-newline */
|
||||
await sshDeploy({
|
||||
source, rsyncServer, exclude, remotePort,
|
||||
privateKeyPath, args, sshCmdArgs
|
||||
});
|
||||
// Check script after
|
||||
if (scriptAfter) {
|
||||
await remoteCmdAfter(scriptAfter, privateKeyPath);
|
||||
}
|
||||
};
|
||||
|
||||
run();
|
||||
run()
|
||||
.then((data = '') => {
|
||||
console.log('✅ [DONE]', data);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('❌ [ERROR]', error.message);
|
||||
process.exit(1);
|
||||
});
|
||||
|
|
|
@ -1,11 +1,48 @@
|
|||
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', 'SSH_CMD_ARGS', 'EXCLUDE',
|
||||
'SCRIPT_BEFORE', 'SCRIPT_AFTER'];
|
||||
|
||||
const githubWorkspace = process.env.GITHUB_WORKSPACE;
|
||||
const remoteUser = process.env.REMOTE_USER;
|
||||
|
||||
const defaultInputs = {
|
||||
source: '',
|
||||
target: `/home/${remoteUser}/`,
|
||||
exclude: '',
|
||||
args: '-rltgoDzvO',
|
||||
sshCmdArgs: '-o StrictHostKeyChecking=no',
|
||||
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':
|
||||
case 'args':
|
||||
case 'sshCmdArgs':
|
||||
extendedVal = validVal.split(',').map((item) => item.trim());
|
||||
break;
|
||||
}
|
||||
|
||||
inputs[inputName] = extendedVal;
|
||||
});
|
||||
|
||||
inputs.sshServer = `${inputs.remoteUser}@${inputs.remoteHost}`;
|
||||
inputs.rsyncServer = `${inputs.remoteUser}@${inputs.remoteHost}:${inputs.target}`;
|
||||
|
||||
module.exports = inputs;
|
||||
|
|
40
src/remoteCmd.js
Normal file
40
src/remoteCmd.js
Normal file
|
@ -0,0 +1,40 @@
|
|||
const { exec } = require('child_process');
|
||||
const { sshServer, githubWorkspace } = require('./inputs');
|
||||
const { writeToFile } = require('./helpers');
|
||||
|
||||
const handleError = (message, isRequired, callback) => {
|
||||
if (isRequired) {
|
||||
callback(new Error(message));
|
||||
} else {
|
||||
console.warn(message);
|
||||
}
|
||||
};
|
||||
|
||||
// eslint-disable-next-line max-len
|
||||
const remoteCmd = async (content, privateKeyPath, isRequired, label) => new Promise((resolve, reject) => {
|
||||
const filename = `local_ssh_script-${label}.sh`;
|
||||
try {
|
||||
writeToFile({ dir: githubWorkspace, filename, content });
|
||||
console.log(`Executing remote script: ssh -i ${privateKeyPath} ${sshServer}`);
|
||||
exec(
|
||||
`DEBIAN_FRONTEND=noninteractive ssh -i ${privateKeyPath} ${sshServer} 'RSYNC_STDOUT="${process.env.RSYNC_STDOUT}" bash -s' < ${filename}`,
|
||||
(err, data, stderr) => {
|
||||
if (err) {
|
||||
const message = `⚠️ [CMD] Remote script failed: ${err.message}`;
|
||||
console.warn(`${message} \n`, data, stderr);
|
||||
handleError(message, isRequired, reject);
|
||||
} else {
|
||||
console.log('✅ [CMD] Remote script executed. \n', data, stderr);
|
||||
resolve(data);
|
||||
}
|
||||
}
|
||||
);
|
||||
} catch (err) {
|
||||
handleError(err.message, isRequired, reject);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
remoteCmdBefore: async (cmd, privateKeyPath, isRequired) => remoteCmd(cmd, privateKeyPath, isRequired, 'before'),
|
||||
remoteCmdAfter: async (cmd, privateKeyPath, isRequired) => remoteCmd(cmd, privateKeyPath, isRequired, 'after')
|
||||
};
|
100
src/rsyncCli.js
100
src/rsyncCli.js
|
@ -1,46 +1,76 @@
|
|||
const { sync: commandExists } = require("command-exists");
|
||||
const { exec, execSync } = require("child_process");
|
||||
const { execSync } = require('child_process');
|
||||
const nodeRsync = require('rsyncwrapper');
|
||||
|
||||
const validateRsync = (callback = () => {}) => {
|
||||
const rsyncCli = commandExists("rsync");
|
||||
if (rsyncCli) {
|
||||
console.log('⚠️ [CLI] Rsync exists');
|
||||
const rsyncVersion = execSync("rsync --version", { stdio: 'inherit' });
|
||||
return callback();
|
||||
const nodeRsyncPromise = async (config) => new Promise((resolve, reject) => {
|
||||
try {
|
||||
nodeRsync(config, (error, stdout, stderr, cmd) => {
|
||||
if (error) {
|
||||
console.error('❌ [Rsync] error: ');
|
||||
console.error(error);
|
||||
console.error('❌ [Rsync] stderr: ');
|
||||
console.error(stderr);
|
||||
console.error('❌️ [Rsync] stdout: ');
|
||||
console.error(stdout);
|
||||
console.error('❌ [Rsync] cmd: ', cmd);
|
||||
reject(new Error(`${error.message}\n\n${stderr}`));
|
||||
} else {
|
||||
resolve(stdout);
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('❌ [Rsync] command error: ', error.message, error.stack);
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
|
||||
const validateRsync = async () => {
|
||||
try {
|
||||
execSync('rsync --version', { stdio: 'inherit' });
|
||||
console.log('✅️ [CLI] Rsync exists');
|
||||
return;
|
||||
} catch (error) {
|
||||
console.warn('⚠️ [CLI] Rsync doesn\'t exists', error.message);
|
||||
}
|
||||
|
||||
console.log('⚠️ [CLI] Rsync doesn\'t exists. Start installation with "apt-get" \n');
|
||||
console.log('[CLI] Start rsync installation with "apt-get" \n');
|
||||
try {
|
||||
execSync('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 (error) {
|
||||
throw new Error(`⚠️ [CLI] Rsync installation failed. Aborting ... error: ${error.message}`);
|
||||
}
|
||||
};
|
||||
|
||||
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();
|
||||
}
|
||||
const rsyncCli = async ({
|
||||
source, rsyncServer, exclude, remotePort,
|
||||
privateKeyPath, args, sshCmdArgs
|
||||
}) => {
|
||||
console.log(`[Rsync] Starting Rsync Action: ${source} to ${rsyncServer}`);
|
||||
if (exclude) console.log(`[Rsync] excluding folders ${exclude}`);
|
||||
|
||||
const defaultOptions = {
|
||||
ssh: true,
|
||||
recursive: true
|
||||
};
|
||||
|
||||
// RSYNC COMMAND
|
||||
/* eslint-disable object-property-newline */
|
||||
return nodeRsyncPromise({
|
||||
...defaultOptions,
|
||||
src: source, dest: rsyncServer, excludeFirst: exclude, port: remotePort,
|
||||
privateKey: privateKeyPath, args, sshCmdArgs,
|
||||
onStdout: (data) => console.log(data), onStderr: (data) => console.error(data)
|
||||
});
|
||||
};
|
||||
|
||||
const validateInputs = (inputs) => {
|
||||
const inputKeys = Object.keys(inputs);
|
||||
const validInputs = inputKeys.filter((inputKey) => {
|
||||
const inputValue = inputs[inputKey];
|
||||
|
||||
if (!inputValue) {
|
||||
console.error(`⚠️ [INPUTS] ${inputKey} is mandatory`);
|
||||
}
|
||||
|
||||
return inputValue;
|
||||
});
|
||||
|
||||
if (validInputs.length !== inputKeys.length) {
|
||||
console.error("⚠️ [INPUTS] Inputs not valid, aborting ...");
|
||||
process.abort();
|
||||
}
|
||||
const sshDeploy = async (params) => {
|
||||
await validateRsync();
|
||||
const stdout = await rsyncCli(params);
|
||||
console.log('✅ [Rsync] finished.', stdout);
|
||||
process.env.RSYNC_STDOUT = `${stdout}`;
|
||||
return stdout;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
validateRsync,
|
||||
validateInputs,
|
||||
sshDeploy
|
||||
};
|
||||
|
|
|
@ -1,37 +1,43 @@
|
|||
const { writeFileSync } = require('fs');
|
||||
const { join } = require('path');
|
||||
const { execSync } = require('child_process');
|
||||
const { writeToFile } = require('./helpers');
|
||||
|
||||
const {
|
||||
validateDir,
|
||||
validateFile
|
||||
} = require('./helpers');
|
||||
const KNOWN_HOSTS = 'known_hosts';
|
||||
const getPrivateKeyPath = (filename = '') => {
|
||||
const { HOME } = process.env;
|
||||
const dir = join(HOME || '~', '.ssh');
|
||||
const knownHostsPath = join(dir, KNOWN_HOSTS);
|
||||
return {
|
||||
dir,
|
||||
filename,
|
||||
path: join(dir, filename),
|
||||
knownHostsPath
|
||||
};
|
||||
};
|
||||
|
||||
const {
|
||||
HOME
|
||||
} = process.env;
|
||||
|
||||
const addSshKey = (key, name) => {
|
||||
const sshDir = join(HOME || __dirname, '.ssh');
|
||||
const filePath = join(sshDir, name);
|
||||
|
||||
validateDir(sshDir);
|
||||
validateFile(`${sshDir}/known_hosts`);
|
||||
const addSshKey = (content, deployKeyName) => {
|
||||
const { dir, filename } = getPrivateKeyPath(deployKeyName);
|
||||
writeToFile({ dir, filename: KNOWN_HOSTS, content: '' });
|
||||
console.log('✅ [SSH] known_hosts file ensured', dir);
|
||||
writeToFile({ dir, filename, content, isRequired: true, mode: '0400' });
|
||||
console.log('✅ [SSH] key added to `.ssh` dir ', dir, filename);
|
||||
};
|
||||
|
||||
const updateKnownHosts = (host) => {
|
||||
const { knownHostsPath } = getPrivateKeyPath();
|
||||
console.log('[SSH] Adding host to `known_hosts` ....', host, knownHostsPath);
|
||||
try {
|
||||
writeFileSync(filePath, key, {
|
||||
encoding: 'utf8',
|
||||
mode: 0o600
|
||||
execSync(`ssh-keyscan -H ${host} >> ${knownHostsPath}`, {
|
||||
stdio: 'inherit'
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('⚠️ writeFileSync error', filePath, e.message);
|
||||
process.abort();
|
||||
} catch (error) {
|
||||
console.error('❌ [SSH] Adding host to `known_hosts` ERROR', host, error.message);
|
||||
}
|
||||
|
||||
console.log('✅ Ssh key added to `.ssh` dir ', filePath);
|
||||
|
||||
return filePath;
|
||||
console.log('✅ [SSH] Adding host to `known_hosts` DONE', host, knownHostsPath);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getPrivateKeyPath,
|
||||
updateKnownHosts,
|
||||
addSshKey
|
||||
}
|
||||
};
|
||||
|
|
13
src/test.js
13
src/test.js
|
@ -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('||||||||||||||||||||||||||||||||||||||');
|
Loading…
Add table
Add a link
Reference in a new issue