[FIX] remove git auth when no user and password

This commit is contained in:
goodrain 2018-03-01 14:27:22 +08:00
parent a95dc2997a
commit 3f149edc71
4 changed files with 33 additions and 23 deletions

View File

@ -19,41 +19,43 @@
package apiHandler
import (
"github.com/Sirupsen/logrus"
"github.com/pquerna/ffjson/ffjson"
"bytes"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"net/http"
"fmt"
"bytes"
"github.com/Sirupsen/logrus"
"github.com/goodrain/rainbond/pkg/worker/discover/model"
"github.com/pquerna/ffjson/ffjson"
)
//UpgradeService 滚动升级
func UpgradeService(tenantName, serviceAlias string ,ru *model.RollingUpgradeTaskBody) error {
func UpgradeService(tenantName, serviceAlias string, ru *model.RollingUpgradeTaskBody) error {
url := fmt.Sprintf("http://127.0.0.1:8888/v2/tenants/%s/services/%s/upgrade", tenantName, serviceAlias)
logrus.Debugf("rolling update new version: %s, url is %s", ru.NewDeployVersion, url)
raw := struct {
DeployVersion string `json:"deploy_version"`
EventID string `json:"event_id"`
EventID string `json:"event_id"`
}{
DeployVersion:ru.CurrentDeployVersion,
EventID:ru.EventID,
DeployVersion: ru.CurrentDeployVersion,
EventID: ru.EventID,
}
rawBody, err := ffjson.Marshal(raw)
if err != nil {
return err
}
return publicRequest("post", url,rawBody)
return publicRequest("post", url, rawBody)
}
func publicRequest(method, url string, body...[]byte) error {
func publicRequest(method, url string, body ...[]byte) error {
client := &http.Client{}
var rawBody *bytes.Buffer
if len(body) != 0 {
rawBody = bytes.NewBuffer(body[0])
}else {
rawBody = nil
rawBody = bytes.NewBuffer(body[0])
} else {
rawBody = nil
}
request, _ := http.NewRequest(strings.ToUpper(method), url, rawBody)
token := os.Getenv("TOKEN")
@ -61,9 +63,13 @@ func publicRequest(method, url string, body...[]byte) error {
request.Header.Set("Authorization", "Token "+token)
}
response, _ := client.Do(request)
if response.StatusCode == 200 {
//body, _ := ioutil.ReadAll(response.Body)
return nil
if response.StatusCode == 200 {
return nil
}
return fmt.Errorf("send upgrade mission error")
}
str := ""
if response != nil && response.Body != nil {
body, _ := ioutil.ReadAll(response.Body)
str = string(body)
}
return fmt.Errorf("send upgrade mission error,response body:%s", str)
}

View File

@ -110,6 +110,7 @@ func (i *ImageBuildItem) Run(timeout time.Duration) error {
if err := apiHandler.UpgradeService(i.TenantName, i.ServiceAlias, i.CreateUpgradeTaskBody()); err != nil {
i.Logger.Error("启动应用失败,请手动启动", map[string]string{"step": "callback", "status": "failure"})
logrus.Errorf("rolling update service error, %s", err.Error())
return err
}
i.Logger.Info("应用启动成功", map[string]string{"step": "build-exector"})
return nil

View File

@ -165,6 +165,7 @@ func (i *SourceCodeBuildItem) Run(timeout time.Duration) error {
if err := apiHandler.UpgradeService(i.TenantName, i.ServiceAlias, i.CreateUpgradeTaskBody()); err != nil {
i.Logger.Error("启动应用失败,请手动启动", map[string]string{"step": "callback", "status": "failure"})
logrus.Errorf("rolling update service error, %s", err.Error())
return err
}
i.Logger.Info("应用启动成功", map[string]string{"step": "build-exector"})
return nil

View File

@ -141,11 +141,13 @@ func GitClone(csi CodeSourceInfo, sourceDir string, logger event.Logger, timeout
opts.Auth = sshAuth
rs, err = git.PlainCloneContext(ctx, sourceDir, false, opts)
} else {
httpAuth := &http.BasicAuth{
Username: csi.User,
Password: csi.Password,
if csi.User != "" && csi.Password != "" {
httpAuth := &http.BasicAuth{
Username: csi.User,
Password: csi.Password,
}
opts.Auth = httpAuth
}
opts.Auth = httpAuth
rs, err = git.PlainCloneContext(ctx, sourceDir, false, opts)
}
if err != nil {