mirror of
https://github.com/easingthemes/ssh-deploy
synced 2025-04-22 00:02:18 +00:00
update Known Hosts
This commit is contained in:
parent
fdc1b9a24d
commit
9aabe10cb3
2
dist/index.js
vendored
2
dist/index.js
vendored
File diff suppressed because one or more lines are too long
@ -16,7 +16,7 @@ const validateDir = (dir) => {
|
|||||||
console.log('✅ [DIR] dir created.');
|
console.log('✅ [DIR] dir created.');
|
||||||
};
|
};
|
||||||
|
|
||||||
const writeToFile = ({ dir, filename, content, isRequired }) => {
|
const writeToFile = ({ dir, filename, content, isRequired, mode = '0o644' }) => {
|
||||||
validateDir(dir);
|
validateDir(dir);
|
||||||
const filePath = join(dir, filename);
|
const filePath = join(dir, filename);
|
||||||
|
|
||||||
@ -32,7 +32,7 @@ const writeToFile = ({ dir, filename, content, isRequired }) => {
|
|||||||
console.log(`[FILE] writing ${filePath} file ...`, content.length);
|
console.log(`[FILE] writing ${filePath} file ...`, content.length);
|
||||||
writeFileSync(filePath, content, {
|
writeFileSync(filePath, content, {
|
||||||
encoding: 'utf8',
|
encoding: 'utf8',
|
||||||
mode: 0o600
|
mode
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw new Error(`⚠️[FILE] Writing to file error. filePath: ${filePath}, message: ${e.message}`);
|
throw new Error(`⚠️[FILE] Writing to file error. filePath: ${filePath}, message: ${e.message}`);
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
const { sshDeploy } = require('./rsyncCli');
|
const { sshDeploy } = require('./rsyncCli');
|
||||||
const { remoteCmdBefore, remoteCmdAfter } = require('./remoteCmd');
|
const { remoteCmdBefore, remoteCmdAfter } = require('./remoteCmd');
|
||||||
const { addSshKey, getPrivateKeyPath } = require('./sshKey');
|
const { addSshKey, getPrivateKeyPath, updateKnownHosts } = require('./sshKey');
|
||||||
const { validateRequiredInputs } = require('./helpers');
|
const { validateRequiredInputs } = require('./helpers');
|
||||||
const inputs = require('./inputs');
|
const inputs = require('./inputs');
|
||||||
|
|
||||||
@ -18,17 +18,19 @@ const run = async () => {
|
|||||||
// Add SSH key
|
// Add SSH key
|
||||||
addSshKey(sshPrivateKey, deployKeyName);
|
addSshKey(sshPrivateKey, deployKeyName);
|
||||||
const { path: privateKeyPath } = getPrivateKeyPath(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
|
// Check Script before
|
||||||
if (scriptBefore) {
|
if (scriptBefore) {
|
||||||
await remoteCmdBefore(scriptBefore);
|
await remoteCmdBefore(scriptBefore);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* eslint-disable object-property-newline */
|
/* eslint-disable object-property-newline */
|
||||||
await sshDeploy({
|
await sshDeploy({
|
||||||
source, rsyncServer, exclude, remotePort,
|
source, rsyncServer, exclude, remotePort,
|
||||||
privateKeyPath, args, sshCmdArgs
|
privateKeyPath, args, sshCmdArgs
|
||||||
});
|
});
|
||||||
|
|
||||||
// Check script after
|
// Check script after
|
||||||
if (scriptAfter) {
|
if (scriptAfter) {
|
||||||
await remoteCmdAfter(scriptAfter);
|
await remoteCmdAfter(scriptAfter);
|
||||||
|
@ -17,7 +17,7 @@ const remoteCmd = async (content, label, isRequired) => new Promise((resolve, re
|
|||||||
try {
|
try {
|
||||||
writeToFile({ dir: githubWorkspace, filename, content });
|
writeToFile({ dir: githubWorkspace, filename, content });
|
||||||
|
|
||||||
exec(`ssh -i ${privateKey} ${sshServer} 'RSYNC_STDOUT=${process.env.RSYNC_STDOUT} bash -s' < ${filename}`, (err, data, stderr) => {
|
exec(`DEBIAN_FRONTEND=noninteractive ssh -i ${privateKey} ${sshServer} 'RSYNC_STDOUT=${process.env.RSYNC_STDOUT} bash -s' < ${filename}`, (err, data, stderr) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
handleError(err.message, isRequired, reject);
|
handleError(err.message, isRequired, reject);
|
||||||
} else {
|
} else {
|
||||||
|
@ -1,25 +1,43 @@
|
|||||||
const { join } = require('path');
|
const { join } = require('path');
|
||||||
|
const { execSync } = require('child_process');
|
||||||
const { writeToFile } = require('./helpers');
|
const { writeToFile } = require('./helpers');
|
||||||
|
|
||||||
const getPrivateKeyPath = (filename) => {
|
const KNOWN_HOSTS = 'known_hosts';
|
||||||
|
const getPrivateKeyPath = (filename = '') => {
|
||||||
const { HOME } = process.env;
|
const { HOME } = process.env;
|
||||||
const dir = join(HOME || __dirname, '.ssh');
|
const dir = join(HOME || __dirname, '.ssh');
|
||||||
|
const knownHostsPath = join(dir, KNOWN_HOSTS);
|
||||||
return {
|
return {
|
||||||
dir,
|
dir,
|
||||||
filename,
|
filename,
|
||||||
path: join(dir, filename)
|
path: join(dir, filename),
|
||||||
|
knownHostsPath
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const addSshKey = (content, deployKeyName) => {
|
const addSshKey = (content, deployKeyName) => {
|
||||||
const { dir, filename } = getPrivateKeyPath(deployKeyName);
|
const { dir, filename } = getPrivateKeyPath(deployKeyName);
|
||||||
writeToFile({ dir, filename: 'known_hosts', content: '' });
|
writeToFile({ dir, filename: KNOWN_HOSTS, content: '' });
|
||||||
console.log('✅ [SSH] known_hosts file ensured', dir);
|
console.log('✅ [SSH] known_hosts file ensured', dir);
|
||||||
writeToFile({ dir, filename, content, isRequired: true });
|
writeToFile({ dir, filename, content, isRequired: true });
|
||||||
console.log('✅ [SSH] key added to `.ssh` dir ', dir, filename);
|
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 {
|
||||||
|
execSync(`ssh-keyscan -H ${host} >> ${knownHostsPath}`, {
|
||||||
|
stdio: 'inherit'
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('✅ [SSH] Adding host to `known_hosts` ERROR', host, error.message);
|
||||||
|
}
|
||||||
|
console.log('✅ [SSH] Adding host to `known_hosts` DONE', host, knownHostsPath);
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getPrivateKeyPath,
|
getPrivateKeyPath,
|
||||||
|
updateKnownHosts,
|
||||||
addSshKey
|
addSshKey
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user