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:
Dragan Filipović 2023-01-02 21:06:33 +01:00 committed by GitHub
parent a5d8edb941
commit ec9347f8c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 373 additions and 242 deletions

View file

@ -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
}
};