Fixed SSH key issues

This commit is contained in:
Kasra Bigdeli
2019-03-10 11:34:29 -07:00
parent e1fcf0346f
commit 5852b48fc3
+31 -2
View File
@@ -5,6 +5,10 @@ import * as fs from 'fs-extra'
import * as path from 'path'
import * as git from 'simple-git/promise'
import * as uuid from 'uuid'
import Logger = require('./Logger')
import * as util from 'util'
import * as childPross from 'child_process'
const exec = util.promisify(childPross.exec)
class GitHelper {
static getLastHash(directory: string) {
@@ -27,17 +31,40 @@ class GitHelper {
// Some people put https when they are entering their git information
const REPO = Utils.removeHttpHttps(repo)
const remote = `https://${USER}:${PASS}@${REPO}`
if (!!sshKey) {
const SSH_KEY_PATH = path.join(
CaptainConstants.captainRootDirectoryTemp,
uuid.v4()
)
const indexOfSlash = REPO.indexOf('/')
const DOMAIN = REPO.substring(0, indexOfSlash)
const REPO_WITHOUT_DOMAIN = REPO.substring(
indexOfSlash + 1,
REPO.length
).replace(/\/$/, '')
const remote = `git@${DOMAIN}:${REPO_WITHOUT_DOMAIN}.git`
Logger.dev('Cloning SSH ' + remote)
return Promise.resolve() //
.then(function() {
fs.outputFile(SSH_KEY_PATH, sshKey + '')
})
.then(function() {
return exec(
`chmod 600 ${SSH_KEY_PATH}`
)
})
.then(function() {
return fs.ensureDir('/root/.ssh')
})
.then(function() {
return exec(
`ssh-keyscan -H ${DOMAIN} >> /root/.ssh/known_hosts`
)
})
.then(function() {
return git() //
.silent(true) //
@@ -55,6 +82,8 @@ class GitHelper {
return fs.remove(SSH_KEY_PATH)
})
} else {
const remote = `https://${USER}:${PASS}@${REPO}`
Logger.dev('Cloning HTTPS ' + remote)
return git() //
.silent(true) //
.raw(['clone', '--recursive', '-b', branch, remote, directory])