ssh-deploy/src/index.js

156 lines
4.4 KiB
JavaScript
Raw Normal View History

2019-10-03 00:52:52 +03:00
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const commandExists = require('command-exists');
const nodeCmd = require('node-cmd');
const nodeRsync = require('rsyncwrapper');
2020-03-27 00:27:22 +03:00
const vars = { REMOTE_HOST, REMOTE_USER, REMOTE_PORT, SSH_PRIVATE_KEY, DEPLOY_KEY_NAME, SOURCE, TARGET, ARGS, GITHUB_WORKSPACE, HOME } = process.env;
console.log(vars);
2019-10-03 00:52:52 +03:00
const sshDeploy = (() => {
const rsync = ({ privateKey, port, src, dest, args }) => {
2019-10-03 00:52:52 +03:00
console.log(`Starting Rsync Action: ${src} to ${dest}`);
try {
// RSYNC COMMAND
2020-03-25 05:50:26 +03:00
nodeRsync({ src, dest, args, privateKey, ssh: false, port, sshCmdArgs: ['-o StrictHostKeyChecking=no'], recursive: true }, (error, stdout, stderr, cmd) => {
2019-10-03 00:52:52 +03:00
if (error) {
console.error('⚠️ Rsync error', error.message);
process.abort();
} else {
console.log("✅ Rsync finished.", stdout);
}
});
} catch (err) {
console.error(`⚠️ An error happened:(.`, err.message, err.stack);
process.abort();
}
};
const init = ({
2020-03-25 06:28:27 +03:00
src,
dest,
args,
host = 'localhost',
username,
privateKeyContent,
port
}) => {
2019-10-03 00:52:52 +03:00
validateRsync(() => {
const privateKey = addSshKey(privateKeyContent, DEPLOY_KEY_NAME ||'deploy_key');
const remoteDest = username + '@' + host + ':' + dest;
rsync({ privateKey, port, src, dest: remoteDest, args });
2019-10-03 00:52:52 +03:00
});
};
const validateDir = (dir) => {
if (!fs.existsSync(dir)){
console.log(`Creating ${dir} dir in `, GITHUB_WORKSPACE);
fs.mkdirSync(dir);
} else {
console.log(`${dir} dir exist`);
}
};
const validateFile = (filePath) => {
if (!fs.existsSync(filePath)){
console.log(`Creating ${filePath} file in `, GITHUB_WORKSPACE);
try {
fs.writeFileSync(filePath, '', {
encoding: 'utf8',
mode: 0o600
});
} catch (e) {
console.error('⚠️ writeFileSync error', filePath, e.message);
process.abort();
}
} else {
console.log(`${filePath} file exist`);
}
};
const addSshKey = (key, name) => {
const sshDir = path.join(HOME || __dirname, '.ssh');
const filePath = path.join(sshDir, name);
validateDir(sshDir);
validateFile(sshDir + '/known_hosts');
try {
fs.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);
return filePath;
};
const validateRsync = (callback = () => {}) => {
const rsyncCli = commandExists.sync('rsync');
if (!rsyncCli) {
nodeCmd.get(
'sudo apt-get --no-install-recommends install rsync',
function(err, data, stderr){
if (err) {
console.log('⚠️ Rsync installation failed ', err.message);
process.abort();
} else {
console.log('✅ Rsync installed. \n', data, stderr);
callback();
}
}
);
} else {
callback();
}
};
return {
init
}
})();
const validateInputs = (inputs) => {
2020-03-25 06:20:29 +03:00
const validInputs = Object.keys(inputs).filter((key) => {
const input = inputs[key];
2019-10-03 00:52:52 +03:00
if (!input) {
2020-03-25 06:20:29 +03:00
console.error(`⚠️ ${key} is mandatory`);
2019-10-03 00:52:52 +03:00
}
return input;
});
2020-03-25 06:28:27 +03:00
if (validInputs.length !== Object.keys(inputs).length) {
2019-10-03 00:52:52 +03:00
process.abort();
}
};
const run = () => {
2020-03-25 06:20:29 +03:00
validateInputs({SSH_PRIVATE_KEY, REMOTE_HOST, REMOTE_USER});
2019-10-03 00:52:52 +03:00
sshDeploy.init({
src: GITHUB_WORKSPACE + '/' + SOURCE || '',
dest: TARGET || '/home/' + REMOTE_USER + '/',
args: ARGS ? [ARGS] : ['-rltgoDzvO'],
2019-10-03 00:52:52 +03:00
host: REMOTE_HOST,
port: REMOTE_PORT || '22',
2019-10-03 00:52:52 +03:00
username: REMOTE_USER,
privateKeyContent: SSH_PRIVATE_KEY,
});
};
run();