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:07:12 +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:07:12 +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:07:12 +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 (
|
|
|
|
"bufio"
|
2018-03-03 11:13:07 +08:00
|
|
|
"bytes"
|
2018-01-17 22:06:58 +08:00
|
|
|
"context"
|
|
|
|
"crypto/sha1"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2018-03-13 10:52:26 +08:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2018-01-17 22:06:58 +08:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2018-02-27 22:31:44 +08:00
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
|
2018-01-17 22:06:58 +08:00
|
|
|
"github.com/goodrain/rainbond/pkg/event"
|
|
|
|
"github.com/goodrain/rainbond/pkg/util"
|
|
|
|
git "gopkg.in/src-d/go-git.v4"
|
|
|
|
"gopkg.in/src-d/go-git.v4/plumbing"
|
2018-01-22 16:50:23 +08:00
|
|
|
"gopkg.in/src-d/go-git.v4/plumbing/protocol/packp/sideband"
|
2018-01-17 22:06:58 +08:00
|
|
|
"gopkg.in/src-d/go-git.v4/plumbing/transport"
|
2018-03-13 10:52:26 +08:00
|
|
|
"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
|
|
|
//避免项目之间冲突,代码缓存目录提高到租户
|
|
|
|
TenantID string `json:"tenant_id"`
|
2018-01-17 22:06:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//GetCodeCacheDir 获取代码缓存目录
|
|
|
|
func (c CodeSourceInfo) GetCodeCacheDir() string {
|
2018-01-25 21:25:14 +08:00
|
|
|
cacheDir := os.Getenv("CACHE_DIR")
|
|
|
|
if cacheDir == "" {
|
|
|
|
cacheDir = "/cache"
|
|
|
|
}
|
2018-01-17 22:06:58 +08:00
|
|
|
h := sha1.New()
|
|
|
|
h.Write([]byte(c.RepositoryURL))
|
|
|
|
bs := h.Sum(nil)
|
2018-02-06 14:06:23 +08:00
|
|
|
bsStr := fmt.Sprintf("%x", bs)
|
|
|
|
logrus.Debugf("git path is %s", path.Join(cacheDir, "build", c.TenantID, bsStr))
|
|
|
|
return path.Join(cacheDir, "build", c.TenantID, bsStr)
|
2018-01-17 22:06:58 +08:00
|
|
|
}
|
|
|
|
|
2018-02-01 11:18:19 +08:00
|
|
|
//GetCodeSourceDir 获取代码下载目录
|
|
|
|
func (c CodeSourceInfo) GetCodeSourceDir() string {
|
2018-03-02 10:11:57 +08:00
|
|
|
return GetCodeSourceDir(c.RepositoryURL, c.TenantID)
|
|
|
|
}
|
|
|
|
|
|
|
|
//GetCodeSourceDir 获取源码下载目录
|
|
|
|
func GetCodeSourceDir(RepositoryURL, tenantID 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
|
|
|
}
|
|
|
|
h := sha1.New()
|
2018-03-02 10:11:57 +08:00
|
|
|
h.Write([]byte(RepositoryURL))
|
2018-02-01 11:18:19 +08:00
|
|
|
bs := h.Sum(nil)
|
2018-02-27 22:31:44 +08:00
|
|
|
bsStr := fmt.Sprintf("%x", bs)
|
2018-03-02 10:11:57 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
if logger != nil {
|
|
|
|
//进度信息
|
|
|
|
logger.Info(fmt.Sprintf("开始从Git源(%s)获取代码", csi.RepositoryURL), map[string]string{"step": "clone_code"})
|
|
|
|
}
|
|
|
|
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-03-03 11:13:07 +08:00
|
|
|
progress := createProgress(ctx, logger)
|
2018-01-17 22:06:58 +08:00
|
|
|
opts := &git.CloneOptions{
|
|
|
|
URL: csi.RepositoryURL,
|
2018-03-02 10:11:57 +08:00
|
|
|
Progress: progress,
|
2018-01-17 22:06:58 +08:00
|
|
|
SingleBranch: false,
|
|
|
|
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
|
|
|
|
}
|
|
|
|
if csi.Branch != "" {
|
|
|
|
opts.ReferenceName = plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", csi.Branch))
|
|
|
|
}
|
|
|
|
var rs *git.Repository
|
|
|
|
if ep.Protocol == "ssh" {
|
|
|
|
publichFile := GetPrivateFile()
|
|
|
|
sshAuth, auerr := ssh.NewPublicKeysFromFile("git", publichFile, "")
|
|
|
|
if auerr != nil {
|
|
|
|
if logger != nil {
|
|
|
|
logger.Error(fmt.Sprintf("创建PublicKeys错误"), map[string]string{"step": "callback", "status": "failure"})
|
|
|
|
}
|
|
|
|
return nil, auerr
|
|
|
|
}
|
|
|
|
opts.Auth = sshAuth
|
|
|
|
rs, err = git.PlainCloneContext(ctx, sourceDir, false, opts)
|
|
|
|
} else {
|
2018-03-13 10:52:26 +08:00
|
|
|
// 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.Second * 10
|
|
|
|
client.InstallProtocol("https", githttp.NewClient(customClient))
|
|
|
|
defer func() {
|
|
|
|
client.InstallProtocol("https", githttp.DefaultClient)
|
|
|
|
}()
|
|
|
|
}
|
2018-03-01 14:27:22 +08:00
|
|
|
if csi.User != "" && csi.Password != "" {
|
2018-03-13 10:52:26 +08:00
|
|
|
httpAuth := &githttp.BasicAuth{
|
2018-03-01 14:27:22 +08:00
|
|
|
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": "callback", "status": "failure"})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err == transport.ErrAuthenticationRequired {
|
|
|
|
if logger != nil {
|
|
|
|
logger.Error(fmt.Sprintf("拉取代码发生错误,代码源需要授权访问。"), map[string]string{"step": "callback", "status": "failure"})
|
|
|
|
}
|
|
|
|
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": "callback", "status": "failure"})
|
|
|
|
}
|
|
|
|
return rs, err
|
|
|
|
}
|
|
|
|
if err == transport.ErrRepositoryNotFound {
|
|
|
|
if logger != nil {
|
|
|
|
logger.Error(fmt.Sprintf("拉取代码发生错误,仓库不存在。"), map[string]string{"step": "callback", "status": "failure"})
|
|
|
|
}
|
|
|
|
return rs, err
|
|
|
|
}
|
|
|
|
if err == transport.ErrEmptyRemoteRepository {
|
|
|
|
if logger != nil {
|
|
|
|
logger.Error(fmt.Sprintf("拉取代码发生错误,远程仓库为空。"), map[string]string{"step": "callback", "status": "failure"})
|
|
|
|
}
|
|
|
|
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": "callback", "status": "failure"})
|
|
|
|
}
|
|
|
|
return rs, fmt.Errorf("branch %s is not exist", csi.Branch)
|
|
|
|
}
|
|
|
|
if strings.Contains(err.Error(), "ssh: unable to authenticate") {
|
|
|
|
if logger != nil {
|
|
|
|
logger.Error(fmt.Sprintf("远程代码库需要配置SSH Key。"), map[string]string{"step": "callback", "status": "failure"})
|
|
|
|
}
|
|
|
|
return rs, err
|
|
|
|
}
|
2018-03-06 11:53:27 +08:00
|
|
|
if strings.Contains(err.Error(), "context deadline exceeded") {
|
|
|
|
if logger != nil {
|
|
|
|
logger.Error(fmt.Sprintf("获取代码超时"), map[string]string{"step": "callback", "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) {
|
|
|
|
var rs *git.Repository
|
|
|
|
|
|
|
|
return rs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//GetPrivateFile 获取私钥文件地址
|
|
|
|
func GetPrivateFile() string {
|
|
|
|
home, _ := Home()
|
2018-03-02 20:14:52 +08:00
|
|
|
if home == "" {
|
|
|
|
home = "/root"
|
|
|
|
}
|
2018-01-17 22:06:58 +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")
|
|
|
|
}
|
|
|
|
|
|
|
|
//GetPublicKey 获取公钥
|
|
|
|
func GetPublicKey() string {
|
|
|
|
home, _ := Home()
|
2018-03-02 20:14:52 +08:00
|
|
|
if home == "" {
|
|
|
|
home = "/root"
|
|
|
|
}
|
2018-03-03 15:42:47 +08:00
|
|
|
if ok, _ := util.FileExists(path.Join(home, "/.ssh/builder_rsa.pub")); ok {
|
2018-01-17 22:06:58 +08:00
|
|
|
body, _ := ioutil.ReadFile(path.Join(home, "/.ssh/builder_rsa.pub"))
|
|
|
|
return string(body)
|
|
|
|
}
|
|
|
|
body, _ := ioutil.ReadFile(path.Join(home, "/.ssh/id_rsa.pub"))
|
|
|
|
return string(body)
|
|
|
|
}
|
|
|
|
|
|
|
|
//createProgress create git log progress
|
2018-03-03 11:13:07 +08:00
|
|
|
func createProgress(ctx context.Context, logger event.Logger) sideband.Progress {
|
2018-01-17 22:06:58 +08:00
|
|
|
if logger == nil {
|
|
|
|
return os.Stdout
|
|
|
|
}
|
2018-03-08 15:36:46 +08:00
|
|
|
buffer := bytes.NewBuffer(make([]byte, 4096))
|
2018-03-08 13:32:44 +08:00
|
|
|
var reader = bufio.NewReader(buffer)
|
2018-01-17 22:06:58 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
line, _, err := reader.ReadLine()
|
|
|
|
if err != nil {
|
2018-03-03 11:13:07 +08:00
|
|
|
if err.Error() != "EOF" {
|
|
|
|
fmt.Println("read git log err", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(line) > 0 {
|
2018-03-08 10:51:29 +08:00
|
|
|
progess := strings.Replace(string(line), "\r", "", -1)
|
2018-03-08 11:11:34 +08:00
|
|
|
progess = strings.Replace(progess, "\n", "", -1)
|
2018-03-08 16:30:51 +08:00
|
|
|
progess = strings.Replace(progess, "\u0000", "", -1)
|
2018-03-08 16:04:24 +08:00
|
|
|
if len(progess) > 0 {
|
|
|
|
message := fmt.Sprintf(`{"progress":"%s","id":"%s"}`, progess, "获取源码")
|
|
|
|
logger.Debug(message, map[string]string{"step": "progress"})
|
|
|
|
}
|
2018-01-17 22:06:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2018-03-08 13:32:44 +08:00
|
|
|
return buffer
|
2018-01-17 22:06:58 +08:00
|
|
|
}
|