Rainbond/builder/sources/git.go

517 lines
16 KiB
Go
Raw Normal View History

2018-03-14 14:12:26 +08:00
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
2018-01-17 22:06:58 +08:00
// RAINBOND, Application Management Platform
2018-03-14 14:33:31 +08:00
2018-01-17 22:06:58 +08:00
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
2018-03-14 14:33:31 +08:00
2018-01-17 22:06:58 +08:00
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
2018-03-14 14:33:31 +08:00
2018-01-17 22:06:58 +08:00
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package sources
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/url"
2018-01-17 22:06:58 +08:00
"os"
"path"
"strings"
"time"
2018-03-30 15:08:40 +08:00
"gopkg.in/src-d/go-git.v4/plumbing/object"
2020-09-06 11:09:48 +08:00
"github.com/sirupsen/logrus"
2018-02-27 22:31:44 +08:00
2018-07-11 18:33:19 +08:00
"crypto/rand"
"crypto/rsa"
2018-08-30 15:33:15 +08:00
"crypto/sha1"
2018-07-11 18:33:19 +08:00
"crypto/x509"
"encoding/pem"
"github.com/goodrain/rainbond/event"
"github.com/goodrain/rainbond/util"
netssh "golang.org/x/crypto/ssh"
2018-07-11 18:33:19 +08:00
sshkey "golang.org/x/crypto/ssh"
git "gopkg.in/src-d/go-git.v4"
2018-01-17 22:06:58 +08:00
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/transport"
"gopkg.in/src-d/go-git.v4/plumbing/transport/client"
githttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
2018-01-17 22:06:58 +08:00
"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
)
//CodeSourceInfo 代码源信息
type CodeSourceInfo struct {
ServerType string `json:"server_type"`
RepositoryURL string `json:"repository_url"`
Branch string `json:"branch"`
User string `json:"user"`
Password string `json:"password"`
2018-01-25 21:25:14 +08:00
//避免项目之间冲突,代码缓存目录提高到租户
2018-07-11 18:33:19 +08:00
TenantID string `json:"tenant_id"`
2018-07-05 15:33:58 +08:00
ServiceID string `json:"service_id"`
2018-01-17 22:06:58 +08:00
}
2018-08-30 15:33:15 +08:00
//GetCodeSourceDir get source storage directory
2018-02-01 11:18:19 +08:00
func (c CodeSourceInfo) GetCodeSourceDir() string {
2018-07-05 15:33:58 +08:00
return GetCodeSourceDir(c.RepositoryURL, c.Branch, c.TenantID, c.ServiceID)
2018-03-02 10:11:57 +08:00
}
2018-08-30 15:33:15 +08:00
//GetCodeSourceDir get source storage directory
// it changes as gitrepostory address, branch, and service id change
2018-07-05 15:33:58 +08:00
func GetCodeSourceDir(RepositoryURL, branch, tenantID string, ServiceID string) string {
2018-02-01 11:18:19 +08:00
sourceDir := os.Getenv("SOURCE_DIR")
if sourceDir == "" {
2018-03-02 10:11:57 +08:00
sourceDir = "/grdata/source"
2018-02-01 11:18:19 +08:00
}
2018-08-30 15:33:15 +08:00
h := sha1.New()
h.Write([]byte(RepositoryURL + branch + ServiceID))
bs := h.Sum(nil)
bsStr := fmt.Sprintf("%x", bs)
return path.Join(sourceDir, "build", tenantID, bsStr)
2018-02-06 14:06:23 +08:00
}
//CheckFileExist CheckFileExist
func CheckFileExist(path string) bool {
_, err := os.Stat(path)
2018-02-27 22:31:44 +08:00
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
2018-02-06 14:06:23 +08:00
}
//RemoveDir RemoveDir
func RemoveDir(path string) error {
if path == "/" {
return fmt.Errorf("remove wrong dir")
}
return os.RemoveAll(path)
2018-02-01 11:18:19 +08:00
}
func getShowURL(rurl string) string {
urlpath, _ := url.Parse(rurl)
if urlpath != nil {
showURL := fmt.Sprintf("%s://%s%s", urlpath.Scheme, urlpath.Host, urlpath.Path)
return showURL
}
return ""
}
2018-02-01 11:18:19 +08:00
2018-01-17 22:06:58 +08:00
//GitClone git clone code
func GitClone(csi CodeSourceInfo, sourceDir string, logger event.Logger, timeout int) (*git.Repository, error) {
2018-07-05 11:09:18 +08:00
GetPrivateFileParam := csi.TenantID
if !strings.HasSuffix(csi.RepositoryURL, ".git") {
csi.RepositoryURL = csi.RepositoryURL + ".git"
}
2018-07-05 11:09:18 +08:00
flag := true
2018-07-05 11:57:21 +08:00
Loop:
2018-01-17 22:06:58 +08:00
if logger != nil {
//Hide possible account key information
logger.Info(fmt.Sprintf("Start clone source code from %s", getShowURL(csi.RepositoryURL)), map[string]string{"step": "clone_code"})
2018-01-17 22:06:58 +08:00
}
ep, err := transport.NewEndpoint(csi.RepositoryURL)
if err != nil {
return nil, err
}
if timeout < 1 {
timeout = 1
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*time.Duration(timeout))
defer cancel()
2018-09-02 16:58:07 +08:00
writer := logger.GetWriter("progress", "debug")
2019-08-29 21:18:12 +08:00
writer.SetFormat(map[string]interface{}{"progress": "%s", "id": "Clone:"})
2018-01-17 22:06:58 +08:00
opts := &git.CloneOptions{
URL: csi.RepositoryURL,
2018-09-02 16:58:07 +08:00
Progress: writer,
SingleBranch: true,
2018-04-23 15:07:37 +08:00
Tags: git.NoTags,
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
2018-04-08 16:28:41 +08:00
Depth: 1,
2018-01-17 22:06:58 +08:00
}
if csi.Branch != "" {
2018-08-23 14:36:29 +08:00
opts.ReferenceName = getBranch(csi.Branch)
2018-01-17 22:06:58 +08:00
}
var rs *git.Repository
if ep.Protocol == "ssh" {
2018-07-05 11:09:18 +08:00
publichFile := GetPrivateFile(GetPrivateFileParam)
2018-01-17 22:06:58 +08:00
sshAuth, auerr := ssh.NewPublicKeysFromFile("git", publichFile, "")
if auerr != nil {
if logger != nil {
2018-08-27 18:37:41 +08:00
logger.Error(fmt.Sprintf("Create PublicKeys failure"), map[string]string{"step": "clone-code", "status": "failure"})
2018-01-17 22:06:58 +08:00
}
return nil, auerr
}
sshAuth.HostKeyCallbackHelper.HostKeyCallback = netssh.InsecureIgnoreHostKey()
2018-01-17 22:06:58 +08:00
opts.Auth = sshAuth
rs, err = git.PlainCloneContext(ctx, sourceDir, false, opts)
} else {
// only proxy github
// but when setting, other request will be proxyed
if strings.Contains(csi.RepositoryURL, "github.com") && os.Getenv("GITHUB_PROXY") != "" {
2018-03-31 12:53:50 +08:00
proxyURL, err := url.Parse(os.Getenv("GITHUB_PROXY"))
if err == nil {
customClient := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}}
customClient.Timeout = time.Minute * time.Duration(timeout)
client.InstallProtocol("https", githttp.NewClient(customClient))
defer func() {
client.InstallProtocol("https", githttp.DefaultClient)
}()
} else {
logrus.Error(err)
}
}
if csi.User != "" && csi.Password != "" {
httpAuth := &githttp.BasicAuth{
Username: csi.User,
Password: csi.Password,
}
opts.Auth = httpAuth
2018-01-17 22:06:58 +08:00
}
rs, err = git.PlainCloneContext(ctx, sourceDir, false, opts)
}
if err != nil {
if reerr := os.RemoveAll(sourceDir); reerr != nil {
if logger != nil {
logger.Error(fmt.Sprintf("拉取代码发生错误删除代码目录失败。"), map[string]string{"step": "clone-code", "status": "failure"})
2018-01-17 22:06:58 +08:00
}
}
if err == transport.ErrAuthenticationRequired {
if logger != nil {
logger.Error(fmt.Sprintf("拉取代码发生错误,代码源需要授权访问。"), map[string]string{"step": "clone-code", "status": "failure"})
2018-01-17 22:06:58 +08:00
}
return rs, err
}
2018-03-02 10:11:57 +08:00
if err == transport.ErrAuthorizationFailed {
if logger != nil {
logger.Error(fmt.Sprintf("拉取代码发生错误,代码源鉴权失败。"), map[string]string{"step": "clone-code", "status": "failure"})
2018-03-02 10:11:57 +08:00
}
return rs, err
}
if err == transport.ErrRepositoryNotFound {
if logger != nil {
logger.Error(fmt.Sprintf("拉取代码发生错误,仓库不存在。"), map[string]string{"step": "clone-code", "status": "failure"})
2018-03-02 10:11:57 +08:00
}
return rs, err
}
if err == transport.ErrEmptyRemoteRepository {
if logger != nil {
logger.Error(fmt.Sprintf("拉取代码发生错误,远程仓库为空。"), map[string]string{"step": "clone-code", "status": "failure"})
2018-03-02 10:11:57 +08:00
}
return rs, err
}
2018-01-17 22:06:58 +08:00
if err == plumbing.ErrReferenceNotFound {
if logger != nil {
logger.Error(fmt.Sprintf("代码分支(%s)不存在。", csi.Branch), map[string]string{"step": "clone-code", "status": "failure"})
2018-01-17 22:06:58 +08:00
}
return rs, fmt.Errorf("branch %s is not exist", csi.Branch)
}
if strings.Contains(err.Error(), "ssh: unable to authenticate") {
2018-07-05 12:47:23 +08:00
if flag {
GetPrivateFileParam = "builder_rsa"
flag = false
goto Loop
}
2018-01-17 22:06:58 +08:00
if logger != nil {
logger.Error(fmt.Sprintf("远程代码库需要配置SSH Key。"), map[string]string{"step": "clone-code", "status": "failure"})
2018-01-17 22:06:58 +08:00
}
return rs, err
}
if strings.Contains(err.Error(), "context deadline exceeded") {
if logger != nil {
logger.Error(fmt.Sprintf("获取代码超时"), map[string]string{"step": "clone-code", "status": "failure"})
}
return rs, err
}
2018-01-17 22:06:58 +08:00
}
return rs, err
}
func retryAuth(ep *transport.Endpoint, csi CodeSourceInfo) (transport.AuthMethod, error) {
switch ep.Protocol {
case "ssh":
home, _ := Home()
sshAuth, err := ssh.NewPublicKeysFromFile("git", path.Join(home, "/.ssh/id_rsa"), "")
if err != nil {
return nil, err
}
return sshAuth, nil
case "http", "https":
//return http.NewBasicAuth(csi.User, csi.Password), nil
}
return nil, nil
}
//GitPull git pull code
func GitPull(csi CodeSourceInfo, sourceDir string, logger event.Logger, timeout int) (*git.Repository, error) {
2018-07-05 11:09:18 +08:00
GetPrivateFileParam := csi.TenantID
flag := true
2018-07-05 11:57:21 +08:00
Loop:
if logger != nil {
2018-08-27 18:57:54 +08:00
logger.Info(fmt.Sprintf("Start pull source code from %s", csi.RepositoryURL), map[string]string{"step": "clone_code"})
}
if timeout < 1 {
timeout = 1
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*time.Duration(timeout))
defer cancel()
2018-09-02 16:58:07 +08:00
writer := logger.GetWriter("progress", "debug")
2019-08-29 21:18:12 +08:00
writer.SetFormat(map[string]interface{}{"progress": "%s", "id": "Pull:"})
opts := &git.PullOptions{
2018-09-02 16:58:07 +08:00
Progress: writer,
SingleBranch: true,
2018-04-23 15:07:37 +08:00
Depth: 1,
}
if csi.Branch != "" {
2018-09-02 23:06:05 +08:00
opts.ReferenceName = getBranch(csi.Branch)
}
ep, err := transport.NewEndpoint(csi.RepositoryURL)
if err != nil {
return nil, err
}
if ep.Protocol == "ssh" {
2018-07-05 11:09:18 +08:00
publichFile := GetPrivateFile(GetPrivateFileParam)
sshAuth, auerr := ssh.NewPublicKeysFromFile("git", publichFile, "")
if auerr != nil {
if logger != nil {
logger.Error(fmt.Sprintf("创建PublicKeys错误"), map[string]string{"step": "pull-code", "status": "failure"})
}
return nil, auerr
}
sshAuth.HostKeyCallbackHelper.HostKeyCallback = netssh.InsecureIgnoreHostKey()
opts.Auth = sshAuth
} else {
// only proxy github
// but when setting, other request will be proxyed
if strings.Contains(csi.RepositoryURL, "github.com") && os.Getenv("GITHUB_PROXY") != "" {
proxyURL, _ := url.Parse(os.Getenv("GITHUB_PROXY"))
customClient := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}}
customClient.Timeout = time.Minute * time.Duration(timeout)
client.InstallProtocol("https", githttp.NewClient(customClient))
defer func() {
client.InstallProtocol("https", githttp.DefaultClient)
}()
}
if csi.User != "" && csi.Password != "" {
httpAuth := &githttp.BasicAuth{
Username: csi.User,
Password: csi.Password,
}
opts.Auth = httpAuth
}
}
rs, err := git.PlainOpen(sourceDir)
if err != nil {
return nil, err
}
tree, err := rs.Worktree()
if err != nil {
return nil, err
}
err = tree.PullContext(ctx, opts)
if err != nil {
if err == transport.ErrAuthenticationRequired {
if logger != nil {
logger.Error(fmt.Sprintf("更新代码发生错误,代码源需要授权访问。"), map[string]string{"step": "pull-code", "status": "failure"})
}
return rs, err
}
if err == transport.ErrAuthorizationFailed {
2018-07-05 11:09:18 +08:00
if logger != nil {
logger.Error(fmt.Sprintf("更新代码发生错误,代码源鉴权失败。"), map[string]string{"step": "pull-code", "status": "failure"})
}
return rs, err
}
if err == transport.ErrRepositoryNotFound {
if logger != nil {
logger.Error(fmt.Sprintf("更新代码发生错误,仓库不存在。"), map[string]string{"step": "pull-code", "status": "failure"})
}
return rs, err
}
if err == transport.ErrEmptyRemoteRepository {
if logger != nil {
logger.Error(fmt.Sprintf("更新代码发生错误,远程仓库为空。"), map[string]string{"step": "pull-code", "status": "failure"})
}
return rs, err
}
if err == plumbing.ErrReferenceNotFound {
if logger != nil {
logger.Error(fmt.Sprintf("代码分支(%s)不存在。", csi.Branch), map[string]string{"step": "pull-code", "status": "failure"})
}
return rs, fmt.Errorf("branch %s is not exist", csi.Branch)
}
if strings.Contains(err.Error(), "ssh: unable to authenticate") {
2018-07-05 12:47:23 +08:00
if flag {
GetPrivateFileParam = "builder_rsa"
flag = false
goto Loop
}
if logger != nil {
logger.Error(fmt.Sprintf("远程代码库需要配置SSH Key。"), map[string]string{"step": "pull-code", "status": "failure"})
}
return rs, err
}
if strings.Contains(err.Error(), "context deadline exceeded") {
if logger != nil {
logger.Error(fmt.Sprintf("更新代码超时"), map[string]string{"step": "pull-code", "status": "failure"})
}
return rs, err
}
if err == git.NoErrAlreadyUpToDate {
return rs, nil
}
}
return rs, err
}
2018-01-17 22:06:58 +08:00
//GitCloneOrPull if code exist in local,use git pull.
func GitCloneOrPull(csi CodeSourceInfo, sourceDir string, logger event.Logger, timeout int) (*git.Repository, error) {
if ok, err := util.FileExists(path.Join(sourceDir, ".git")); err == nil && ok && !strings.HasPrefix(csi.Branch, "tag:") {
re, err := GitPull(csi, sourceDir, logger, timeout)
if err == nil && re != nil {
return re, nil
}
logrus.Error("git pull source code error,", err.Error())
}
// empty the sourceDir
if reerr := os.RemoveAll(sourceDir); reerr != nil {
logrus.Error("empty the source code dir error,", reerr.Error())
if logger != nil {
logger.Error(fmt.Sprintf("清空代码目录失败。"), map[string]string{"step": "clone-code", "status": "failure"})
}
}
return GitClone(csi, sourceDir, logger, timeout)
2018-01-17 22:06:58 +08:00
}
2018-08-23 14:36:29 +08:00
//GitCheckout checkout the specified branch
func GitCheckout(sourceDir, branch string) error {
// option := git.CheckoutOptions{
// Branch: getBranch(branch),
// }
return nil
}
func getBranch(branch string) plumbing.ReferenceName {
if strings.HasPrefix(branch, "tag:") {
return plumbing.ReferenceName(fmt.Sprintf("refs/tags/%s", branch[4:]))
}
return plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", branch))
}
2018-03-30 15:08:40 +08:00
//GetLastCommit get last commit info
//get commit by head reference
func GetLastCommit(re *git.Repository) (*object.Commit, error) {
ref, err := re.Head()
if err != nil {
return nil, err
2018-03-30 15:08:40 +08:00
}
return re.CommitObject(ref.Hash())
2018-03-30 15:08:40 +08:00
}
2018-01-17 22:06:58 +08:00
//GetPrivateFile 获取私钥文件地址
2018-08-23 14:36:29 +08:00
func GetPrivateFile(tenantID string) string {
2018-01-17 22:06:58 +08:00
home, _ := Home()
2018-03-02 20:14:52 +08:00
if home == "" {
home = "/root"
}
2018-10-11 21:51:56 +08:00
if ok, _ := util.FileExists(path.Join(home, "/.ssh/"+tenantID)); ok {
return path.Join(home, "/.ssh/"+tenantID)
} else {
2018-07-05 11:57:21 +08:00
if ok, _ := util.FileExists(path.Join(home, "/.ssh/builder_rsa")); ok {
return path.Join(home, "/.ssh/builder_rsa")
}
return path.Join(home, "/.ssh/id_rsa")
2018-01-17 22:06:58 +08:00
}
2018-07-04 15:49:32 +08:00
2018-01-17 22:06:58 +08:00
}
//GetPublicKey 获取公钥
2018-08-23 14:36:29 +08:00
func GetPublicKey(tenantID string) string {
2018-01-17 22:06:58 +08:00
home, _ := Home()
2018-03-02 20:14:52 +08:00
if home == "" {
home = "/root"
}
2018-08-23 14:36:29 +08:00
PublicKey := tenantID + ".pub"
PrivateKey := tenantID
2018-07-04 15:49:32 +08:00
if ok, _ := util.FileExists(path.Join(home, "/.ssh/"+PublicKey)); ok {
body, _ := ioutil.ReadFile(path.Join(home, "/.ssh/"+PublicKey))
2018-01-17 22:06:58 +08:00
return string(body)
}
Private, Public, err := MakeSSHKeyPair()
if err != nil {
logrus.Error("MakeSSHKeyPairError:", err)
2018-07-04 15:49:32 +08:00
}
PrivateKeyFile, err := os.Create(path.Join(home, "/.ssh/"+PrivateKey))
if err != nil {
2018-07-04 15:49:32 +08:00
fmt.Println(err)
} else {
PrivateKeyFile.WriteString(Private)
2018-07-04 15:49:32 +08:00
}
PublicKeyFile, err2 := os.Create(path.Join(home, "/.ssh/"+PublicKey))
if err2 != nil {
2018-07-04 15:49:32 +08:00
fmt.Println(err)
} else {
PublicKeyFile.WriteString(Public)
2018-07-04 15:49:32 +08:00
}
body, _ := ioutil.ReadFile(path.Join(home, "/.ssh/"+PublicKey))
2018-01-17 22:06:58 +08:00
return string(body)
2018-07-04 15:49:32 +08:00
}
2018-08-23 14:36:29 +08:00
//GenerateKey GenerateKey
2018-07-04 15:49:32 +08:00
func GenerateKey(bits int) (*rsa.PrivateKey, *rsa.PublicKey, error) {
private, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return nil, nil, err
}
return private, &private.PublicKey, nil
}
2018-08-23 14:36:29 +08:00
//EncodePrivateKey EncodePrivateKey
2018-07-04 15:49:32 +08:00
func EncodePrivateKey(private *rsa.PrivateKey) []byte {
return pem.EncodeToMemory(&pem.Block{
Bytes: x509.MarshalPKCS1PrivateKey(private),
Type: "RSA PRIVATE KEY",
})
}
2018-08-23 14:36:29 +08:00
//EncodeSSHKey EncodeSSHKey
2018-07-04 15:49:32 +08:00
func EncodeSSHKey(public *rsa.PublicKey) ([]byte, error) {
publicKey, err := sshkey.NewPublicKey(public)
if err != nil {
return nil, err
}
return sshkey.MarshalAuthorizedKey(publicKey), nil
}
2018-08-23 14:36:29 +08:00
//MakeSSHKeyPair make ssh key
2018-07-04 15:49:32 +08:00
func MakeSSHKeyPair() (string, string, error) {
pkey, pubkey, err := GenerateKey(2048)
if err != nil {
return "", "", err
}
pub, err := EncodeSSHKey(pubkey)
if err != nil {
return "", "", err
}
return string(EncodePrivateKey(pkey)), string(pub), nil
2018-01-17 22:06:58 +08:00
}