mirror of
				https://github.com/easingthemes/ssh-deploy.git
				synced 2025-10-30 23:30:09 +08: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.'); | ||||
| }; | ||||
| 
 | ||||
| const writeToFile = ({ dir, filename, content, isRequired }) => { | ||||
| const writeToFile = ({ dir, filename, content, isRequired, mode = '0o644' }) => { | ||||
|   validateDir(dir); | ||||
|   const filePath = join(dir, filename); | ||||
| 
 | ||||
| @ -32,7 +32,7 @@ const writeToFile = ({ dir, filename, content, isRequired }) => { | ||||
|     console.log(`[FILE] writing ${filePath} file ...`, content.length); | ||||
|     writeFileSync(filePath, content, { | ||||
|       encoding: 'utf8', | ||||
|       mode: 0o600 | ||||
|       mode | ||||
|     }); | ||||
|   } catch (e) { | ||||
|     throw new Error(`⚠️[FILE] Writing to file error. filePath: ${filePath}, message:  ${e.message}`); | ||||
|  | ||||
| @ -1,7 +1,7 @@ | ||||
| #!/usr/bin/env node
 | ||||
| const { sshDeploy } = require('./rsyncCli'); | ||||
| const { remoteCmdBefore, remoteCmdAfter } = require('./remoteCmd'); | ||||
| const { addSshKey, getPrivateKeyPath } = require('./sshKey'); | ||||
| const { addSshKey, getPrivateKeyPath, updateKnownHosts } = require('./sshKey'); | ||||
| const { validateRequiredInputs } = require('./helpers'); | ||||
| const inputs = require('./inputs'); | ||||
| 
 | ||||
| @ -18,17 +18,19 @@ const run = async () => { | ||||
|   // 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); | ||||
|   } | ||||
| 
 | ||||
|   /* eslint-disable object-property-newline */ | ||||
|   await sshDeploy({ | ||||
|     source, rsyncServer, exclude, remotePort, | ||||
|     privateKeyPath, args, sshCmdArgs | ||||
|   }); | ||||
| 
 | ||||
|   // Check script after
 | ||||
|   if (scriptAfter) { | ||||
|     await remoteCmdAfter(scriptAfter); | ||||
|  | ||||
| @ -17,7 +17,7 @@ const remoteCmd = async (content, label, isRequired) => new Promise((resolve, re | ||||
|   try { | ||||
|     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) { | ||||
|         handleError(err.message, isRequired, reject); | ||||
|       } else { | ||||
|  | ||||
| @ -1,25 +1,43 @@ | ||||
| const { join } = require('path'); | ||||
| const { execSync } = require('child_process'); | ||||
| const { writeToFile } = require('./helpers'); | ||||
| 
 | ||||
| const getPrivateKeyPath = (filename) => { | ||||
| const KNOWN_HOSTS = 'known_hosts'; | ||||
| const getPrivateKeyPath = (filename = '') => { | ||||
|   const { HOME } = process.env; | ||||
|   const dir = join(HOME || __dirname, '.ssh'); | ||||
|   const knownHostsPath = join(dir, KNOWN_HOSTS); | ||||
|   return { | ||||
|     dir, | ||||
|     filename, | ||||
|     path: join(dir, filename) | ||||
|     path: join(dir, filename), | ||||
|     knownHostsPath | ||||
|   }; | ||||
| }; | ||||
| 
 | ||||
| const addSshKey = (content, 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); | ||||
|   writeToFile({ dir, filename, content, isRequired: true }); | ||||
|   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 = { | ||||
|   getPrivateKeyPath, | ||||
|   updateKnownHosts, | ||||
|   addSshKey | ||||
| }; | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Dragan Filipovic
						Dragan Filipovic