目录
设置config
如果要忽略一切主机校验,在/root/.ssh目录下创建config文件
const gitSshConfig = `
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
`
/**
写这个的原因:git pull/fetch/update的时候是需要在~/.ssh/下添加known_hosts的,
但是由于是容器里面,需要[手动]执行yes操作,因此可以在~/.ssh下通过config文件配置忽略主机校验
*/
func updateGitConfig() error {
if err := ioutil.WriteFile("/root/.ssh/config", []byte(gitSshConfig), 0600); err != nil {
console.Error(fmt.Sprintf("Fail to write ~/.ssh/config due to %s", err))
return err
}
return nil
}
设置known_hosts
把gitlab主机添加到known_hosts
/**
以@拆分, link喂gitlab ssh协议的url, 如:
git@code.uat.xxx.cn:C19082106271968200000005634248/simple-hello.git
*/
func getLinkDns(link string) string {
dnsName := ""
// 获取域名:code.xx.xx.cn
if strings.Contains(link, "@") {
strs := strings.Split(link, "@")
// 获取第一个冒号的索引
index := strings.Index(strs[1], ":")
dnsName = strs[1][0: index]
}
return dnsName
}
// ssh://git@gitlab.devops.xxx.cn:20022/testadm/interview.git
// git@code.uat.xxx.cn:C19082106271968200000005634248/simple-hello.git
// 看Link里面是否有端口号
func getDnsPort(link string) string {
// 最后一个冒号
lastColonIndex := strings.LastIndex(link, ":")
if lastColonIndex != -1 {
tmp := link[lastColonIndex:]
// 冒号后面的第一个/
firstObliqueIndex := strings.Index(tmp, "/")
// 获取冒号与/之间的字符串,如果该字符串是数字,那么就是端口,否则就是用户名
str := tmp[1: firstObliqueIndex]
if IsDigit(str) {
return str
}
}
return ""
}
func isSingleDigit(data string) bool {
digit := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
for _, item := range digit {
if data == item {
return true
}
}
return false
}
// 判断字符串是否是数字
func IsDigit(data string) bool {
for _, item := range data {
if isSingleDigit(string(item)) {
continue
} else {
return false
}
}
return true
}
// 把gitlab域名添加到known_hosts里面
func setKnownHosts(link string) error {
dns := getLinkDns(link)
port := getDnsPort(link)
var cmd *exec.Cmd
if port == "" {
// 说明没有端口号,类似这样:git@code.uat.xxx.cn:C19082106271968200000005634248/simple-hello.git
// ssh-keyscan -t rsa $BASE_URL > ~/.ssh/known_hosts
cmd = exec.Command("ssh-keyscan", "-t", "rsa", dns, ">", "~/.ssh/known_hosts")
}else {
// 存在端口号,类似这样:git@gitlab.devops.xxx.cn:20022/testadm/interview.git
// ssh-keyscan -p $PORT -t rsa $BASE_URL > ~/.ssh/known_hosts
cmd = exec.Command("ssh-keyscan", "-p", port, "-t", "rsa", dns, ">", "~/.ssh/known_hosts")
}
out, err := cmd.CombinedOutput()
console.Println(string(out))
return err
}
设置git config --global
当添加了known_hosts或者添加了config后,可能还会报错如下:
错误信息很明显,它需要你告诉ssh你是谁,于是设置git config --global信息
// git config --global user.name $USERNAME
// git config --global user.email $EMAIL
func setGitGlobal(name, email string) error {
nameCmd := exec.Command("git", "config", "--global", "user.name", name)
emailCmd := exec.Command("git", "config", "--global", "user.email", email)
var err error
var nameOut, emailOut []byte
nameOut, err = nameCmd.CombinedOutput()
console.Println(fmt.Sprintf("git config --global user.name:%s > %s" , name, string(nameOut)))
emailOut, err = emailCmd.CombinedOutput()
console.Println(fmt.Sprintf("git config --global user.email:%s > %s" , email, string(emailOut)))
return err
}