[ADD] add svn clone code

This commit is contained in:
barnettZQG 2018-07-11 18:33:19 +08:00
parent ab362344f0
commit 03171c0b8d
5 changed files with 159 additions and 8 deletions

View File

@ -35,9 +35,15 @@ import (
"github.com/Sirupsen/logrus"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"github.com/goodrain/rainbond/event"
"github.com/goodrain/rainbond/util"
netssh "golang.org/x/crypto/ssh"
sshkey "golang.org/x/crypto/ssh"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/protocol/packp/sideband"
@ -45,11 +51,6 @@ import (
"gopkg.in/src-d/go-git.v4/plumbing/transport/client"
githttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
"crypto/rsa"
"encoding/pem"
"crypto/x509"
sshkey "golang.org/x/crypto/ssh"
"crypto/rand"
)
//CodeSourceInfo 代码源信息
@ -60,7 +61,7 @@ type CodeSourceInfo struct {
User string `json:"user"`
Password string `json:"password"`
//避免项目之间冲突,代码缓存目录提高到租户
TenantID string `json:"tenant_id"`
TenantID string `json:"tenant_id"`
ServiceID string `json:"service_id"`
}
@ -521,6 +522,7 @@ func createProgress(ctx context.Context, logger event.Logger) sideband.Progress
if err.Error() != "EOF" {
fmt.Println("read git log err", err.Error())
}
return
}
if len(line) > 0 {
progess := strings.Replace(string(line), "\r", "", -1)

View File

@ -334,7 +334,7 @@ func ImageBuild(dockerCli *client.Client, contextDir string, options types.Image
if jm.Error != nil {
return jm.Error
}
logger.Debug(jm.JSONString(), map[string]string{"step": "build-progress"})
logger.Info(jm.JSONString(), map[string]string{"step": "build-progress"})
}
}
return nil

View File

@ -24,7 +24,7 @@ import (
)
func TestCreateRepostoryBuildInfo(t *testing.T) {
info, err := CreateRepostoryBuildInfo("ssh://git@gr5042d6.7804f67d.ali-sh-s1.goodrain.net:20905/root/private2018.git?dir=abc", "master", "ADSASDADAD")
info, err := CreateRepostoryBuildInfo("ssh://git@gr5042d6.7804f67d.ali-sh-s1.goodrain.net:20905/root/private2018.git?dir=abc", "master", "ADSASDADAD", "")
if err != nil {
t.Fatal(err)
}

116
builder/sources/svn.go Normal file
View File

@ -0,0 +1,116 @@
// RAINBOND, Application Management Platform
// Copyright (C) 2014-2017 Goodrain Co., Ltd.
// 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.
// 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.
// 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"
"fmt"
"io"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/Sirupsen/logrus"
"github.com/goodrain/rainbond/event"
"golang.org/x/net/context"
)
//SvnPull SvnPull
func SvnPull(dir, user, password string) error {
cmd := exec.Command(
"svn",
"update",
"--ignore-externals",
"--username",
user,
"--password",
password)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
logrus.Errorf("Failed to SVN update %s, see output below\n%sContinuing...", dir, out)
return err
}
return nil
}
//SvnClone clone code by svn
func SvnClone(dir, url, user, password string, logger event.Logger, timeout time.Duration) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
par, rep := filepath.Split(dir)
cmd := exec.Command(
"svn",
"checkout",
"--non-interactive",
"--trust-server-cert-failures=unknown-ca",
"--username",
user,
"--password",
password,
url,
rep)
cmd.Dir = par
reader, err := cmd.StdoutPipe()
if err != nil {
return "", err
}
readererr, err := cmd.StderrPipe()
if err != nil {
return "", err
}
startReadProgress(ctx, reader, logger)
startReadProgress(ctx, readererr, logger)
if err := cmd.Run(); err != nil {
return "", err
}
return "", nil
}
//startReadProgress create svn log progress
func startReadProgress(ctx context.Context, read io.ReadCloser, logger event.Logger) {
var reader = bufio.NewReader(read)
go func() {
defer read.Close()
for {
select {
case <-ctx.Done():
return
default:
line, _, err := reader.ReadLine()
if err != nil {
if err.Error() != "EOF" {
fmt.Println("read svn log err", err.Error())
}
return
}
if len(line) > 0 {
progess := strings.Replace(string(line), "\r", "", -1)
progess = strings.Replace(progess, "\n", "", -1)
progess = strings.Replace(progess, "\u0000", "", -1)
if len(progess) > 0 {
message := fmt.Sprintf(`{"progress":"%s","id":"%s"}`, progess, "SVN:")
logger.Debug(message, map[string]string{"step": "progress"})
}
}
}
}
}()
}

View File

@ -0,0 +1,33 @@
// RAINBOND, Application Management Platform
// Copyright (C) 2014-2017 Goodrain Co., Ltd.
// 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.
// 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.
// 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 (
"testing"
"time"
"github.com/goodrain/rainbond/event"
)
func TestSvnClone(t *testing.T) {
_, err := SvnClone("/tmp/svn", "https://github.com/goodrain/lb-openresty.git", "", "", event.GetTestLogger(), time.Second*600)
if err != nil {
t.Fatal(err)
}
}