2017-11-07 11:40:44 +08:00
|
|
|
|
// RAINBOND, Application Management Platform
|
|
|
|
|
// Copyright (C) 2014-2017 Goodrain Co., Ltd.
|
2017-11-28 14:46:38 +08:00
|
|
|
|
|
2017-11-07 11:40:44 +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.
|
2017-11-28 14:46:38 +08:00
|
|
|
|
|
2017-11-07 11:40:44 +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.
|
2017-11-28 14:46:38 +08:00
|
|
|
|
|
2017-11-07 11:40:44 +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 exector
|
|
|
|
|
|
|
|
|
|
import (
|
2018-01-08 10:30:55 +08:00
|
|
|
|
"bufio"
|
2017-11-07 11:40:44 +08:00
|
|
|
|
"fmt"
|
2018-01-08 10:30:55 +08:00
|
|
|
|
"io"
|
2017-11-07 11:40:44 +08:00
|
|
|
|
"os"
|
2018-01-08 10:30:55 +08:00
|
|
|
|
"os/exec"
|
2017-11-07 11:40:44 +08:00
|
|
|
|
"strings"
|
2017-11-28 14:46:38 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/goodrain/rainbond/pkg/db"
|
|
|
|
|
"github.com/goodrain/rainbond/pkg/event"
|
2017-11-07 11:40:44 +08:00
|
|
|
|
|
|
|
|
|
"github.com/pquerna/ffjson/ffjson"
|
|
|
|
|
|
|
|
|
|
"github.com/goodrain/rainbond/pkg/builder/model"
|
|
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
|
"github.com/akkuman/parseConfig"
|
|
|
|
|
)
|
|
|
|
|
|
2017-11-28 14:46:38 +08:00
|
|
|
|
const (
|
|
|
|
|
cloneTimeout = 60
|
|
|
|
|
buildingTimeout = 180
|
|
|
|
|
formatSourceDir = "/cache/build/%s/source/%s"
|
|
|
|
|
)
|
2017-11-07 11:40:44 +08:00
|
|
|
|
|
|
|
|
|
func (e *exectorManager) pluginDockerfileBuild(in []byte) {
|
|
|
|
|
config := getConf(configPath)
|
|
|
|
|
var tb model.BuildPluginTaskBody
|
|
|
|
|
if err := ffjson.Unmarshal(in, &tb); err != nil {
|
|
|
|
|
logrus.Errorf("unmarshal taskbody error, %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
eventID := tb.EventID
|
|
|
|
|
logger := event.GetManager().GetLogger(eventID)
|
2017-12-13 12:25:09 +08:00
|
|
|
|
logger.Info("从dockerfile构建插件任务开始执行", map[string]string{"step": "builder-exector", "status": "starting"})
|
2017-11-07 11:40:44 +08:00
|
|
|
|
|
2017-11-28 14:46:38 +08:00
|
|
|
|
go func() {
|
|
|
|
|
time.Sleep(buildingTimeout * time.Second)
|
2017-11-28 18:58:07 +08:00
|
|
|
|
logrus.Debugf("building plugin time-out time is reach")
|
2017-11-28 14:46:38 +08:00
|
|
|
|
version, err := db.GetManager().TenantPluginBuildVersionDao().GetBuildVersionByVersionID(tb.PluginID, tb.VersionID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logrus.Errorf("get version error, %v", err)
|
|
|
|
|
}
|
|
|
|
|
if version.Status != "complete" {
|
2017-12-13 12:25:09 +08:00
|
|
|
|
version.Status = "timeout"
|
2017-11-28 14:46:38 +08:00
|
|
|
|
if err := db.GetManager().TenantPluginBuildVersionDao().UpdateModel(version); err != nil {
|
|
|
|
|
logrus.Errorf("update version error, %v", err)
|
|
|
|
|
}
|
2018-01-08 10:45:10 +08:00
|
|
|
|
logger.Error("插件构建超时,修改插件状态失败", map[string]string{"step": "last", "status": "failure"})
|
2017-11-28 14:46:38 +08:00
|
|
|
|
}
|
|
|
|
|
}()
|
2017-11-07 11:40:44 +08:00
|
|
|
|
go func() {
|
|
|
|
|
logrus.Info("start exec build plugin from image worker")
|
|
|
|
|
defer event.GetManager().ReleaseLogger(logger)
|
|
|
|
|
for retry := 0; retry < 3; retry++ {
|
|
|
|
|
err := e.runD(&tb, config, logger)
|
|
|
|
|
if err != nil {
|
2018-01-08 15:41:13 +08:00
|
|
|
|
logrus.Errorf("exec plugin build from dockerfile error:%s", err.Error())
|
|
|
|
|
logger.Info("dockerfile构建插件任务执行失败,开始重试", map[string]string{"step": "builder-exector", "status": "failure"})
|
2017-11-07 11:40:44 +08:00
|
|
|
|
} else {
|
2018-01-08 15:41:13 +08:00
|
|
|
|
return
|
2017-11-07 11:40:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-08 15:41:13 +08:00
|
|
|
|
version, err := db.GetManager().TenantPluginBuildVersionDao().GetBuildVersionByVersionID(tb.PluginID, tb.VersionID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logrus.Errorf("get version error, %v", err)
|
|
|
|
|
}
|
|
|
|
|
version.Status = "failure"
|
|
|
|
|
if err := db.GetManager().TenantPluginBuildVersionDao().UpdateModel(version); err != nil {
|
|
|
|
|
logrus.Errorf("update version error, %v", err)
|
|
|
|
|
}
|
|
|
|
|
logger.Error("dockerfile构建插件任务执行失败", map[string]string{"step": "last", "status": "failure"})
|
2017-11-07 11:40:44 +08:00
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *exectorManager) runD(t *model.BuildPluginTaskBody, c parseConfig.Config, logger event.Logger) error {
|
|
|
|
|
logger.Info("开始拉取代码", map[string]string{"step": "build-exector"})
|
2017-11-28 18:58:07 +08:00
|
|
|
|
logrus.Debugf("开始拉取代码")
|
2017-11-07 11:40:44 +08:00
|
|
|
|
sourceDir := fmt.Sprintf(formatSourceDir, t.TenantID, t.VersionID)
|
2017-11-28 14:46:38 +08:00
|
|
|
|
if t.Repo == "" {
|
|
|
|
|
t.Repo = "master"
|
|
|
|
|
}
|
2018-01-08 15:41:13 +08:00
|
|
|
|
if err := clone(t.GitURL, sourceDir, logger, t.Repo); err != nil {
|
2018-01-05 15:12:11 +08:00
|
|
|
|
logger.Error("拉取代码失败", map[string]string{"step": "builder-exector", "status": "failure"})
|
2017-11-07 11:40:44 +08:00
|
|
|
|
logrus.Errorf("拉取代码失败,%v", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if !checkDockerfile(sourceDir) {
|
2018-01-05 15:12:11 +08:00
|
|
|
|
logger.Error("代码未检测到dockerfile,暂不支持构建,任务即将退出", map[string]string{"step": "builder-exector", "status": "failure"})
|
2017-11-07 11:40:44 +08:00
|
|
|
|
logrus.Error("代码未检测到dockerfile")
|
|
|
|
|
return fmt.Errorf("have no dockerfile")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Info("代码检测为dockerfile,开始编译", map[string]string{"step": "build-exector"})
|
|
|
|
|
curImage, err := buildImage(t.VersionID, t.GitURL, sourceDir, c.Get("publish > image > curr_registry").(string), logger)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logrus.Errorf("build error, %v", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
logger.Info(fmt.Sprintf("镜像编译完成,开始推送镜像,镜像名为 %s", curImage), map[string]string{"step": "build-exector"})
|
|
|
|
|
|
|
|
|
|
if err := push(curImage, logger); err != nil {
|
2018-01-05 15:12:11 +08:00
|
|
|
|
logger.Error("推送镜像失败", map[string]string{"step": "builder-exector", "status": "failure"})
|
2017-11-07 11:40:44 +08:00
|
|
|
|
logrus.Error("推送镜像失败")
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
//TODO: 权限
|
|
|
|
|
//if err := e.DockerPush(curImage); err != nil {
|
|
|
|
|
// logger.Info("推送镜像失败", map[string]string{"step": "builder-exector", "status": "failure"})
|
|
|
|
|
// return err
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
logger.Info("推送镜像完成", map[string]string{"step": "build-exector"})
|
|
|
|
|
version, err := db.GetManager().TenantPluginBuildVersionDao().GetBuildVersionByVersionID(t.PluginID, t.VersionID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logrus.Errorf("get version error, %v", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
version.BuildLocalImage = curImage
|
|
|
|
|
version.Status = "complete"
|
|
|
|
|
if err := db.GetManager().TenantPluginBuildVersionDao().UpdateModel(version); err != nil {
|
|
|
|
|
logrus.Errorf("update version error, %v", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
2017-12-25 16:03:51 +08:00
|
|
|
|
logger.Info("从dockerfile构建插件完成", map[string]string{"step": "last", "status": "success"})
|
2017-11-07 11:40:44 +08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-28 14:46:38 +08:00
|
|
|
|
func clone(gitURL string, sourceDir string, logger event.Logger, repo string) error {
|
2018-01-04 13:51:35 +08:00
|
|
|
|
path := fmt.Sprintf("%s/.git/config", sourceDir)
|
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
logrus.Debugf("clone: %s", fmt.Sprintf("git clone -b %s %s %s", repo, gitURL, sourceDir))
|
|
|
|
|
mm := []string{"clone", "-b", repo, gitURL, sourceDir}
|
|
|
|
|
if err := ShowExec("git", mm, logger); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
logrus.Debugf("file check error: %v", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
2018-01-05 11:07:53 +08:00
|
|
|
|
} else {
|
2018-01-05 15:23:42 +08:00
|
|
|
|
logrus.Debugf("pull: %s", fmt.Sprintf("sudo -P git -C %s pull", sourceDir))
|
|
|
|
|
mm := []string{"-P", "git", "-C", sourceDir, "pull"}
|
|
|
|
|
if err := ShowExec("sudo", mm, logger); err != nil {
|
2018-01-05 11:07:53 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
2017-11-07 11:40:44 +08:00
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-08 10:30:55 +08:00
|
|
|
|
func gitclone(gitURL string, sourceDir string, logger event.Logger, repo string) error {
|
|
|
|
|
path := fmt.Sprintf("%s/.git/config", sourceDir)
|
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
logrus.Debugf("clone: %s", fmt.Sprintf("git clone -b %s %s %s", repo, gitURL, sourceDir))
|
|
|
|
|
mm := []string{"-P", "git", "clone", "-b", repo, gitURL, sourceDir}
|
|
|
|
|
cmd := exec.Command("sudo", mm...)
|
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
|
if err != nil {
|
2018-01-08 15:41:13 +08:00
|
|
|
|
logrus.Errorf(fmt.Sprintf("builder err: %v", err))
|
2018-01-08 10:30:55 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
errC := cmd.Start()
|
|
|
|
|
if errC != nil {
|
|
|
|
|
logrus.Debugf(fmt.Sprintf("builder: %v", errC))
|
|
|
|
|
logger.Error(fmt.Sprintf("builder:%v", errC), map[string]string{"step": "build-exector"})
|
|
|
|
|
return errC
|
|
|
|
|
}
|
|
|
|
|
reader := bufio.NewReader(stdout)
|
|
|
|
|
go func() {
|
|
|
|
|
for {
|
|
|
|
|
line, errL := reader.ReadString('\n')
|
|
|
|
|
if errL != nil || io.EOF == errL {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
//fmt.Print(line)
|
|
|
|
|
logrus.Debugf(fmt.Sprintf("builder: %v", line))
|
|
|
|
|
logger.Debug(fmt.Sprintf("builder:%v", line), map[string]string{"step": "build-exector"})
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
errW := cmd.Wait()
|
2018-01-08 10:45:10 +08:00
|
|
|
|
logrus.Debugf("errw is %v", errW)
|
2018-01-08 10:30:55 +08:00
|
|
|
|
if errW != nil {
|
|
|
|
|
cierr := strings.Split(errW.Error(), "\n")
|
|
|
|
|
if strings.Contains(errW.Error(), "Cloning into") && len(cierr) < 3 {
|
2018-01-08 15:41:13 +08:00
|
|
|
|
logrus.Errorf(fmt.Sprintf("builder:%v", errW))
|
2018-01-08 10:30:55 +08:00
|
|
|
|
logger.Error(fmt.Sprintf("builder:%v", errW), map[string]string{"step": "build-exector"})
|
|
|
|
|
return errW
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
logrus.Debugf("file check error: %v", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
logrus.Debugf("pull: %s", fmt.Sprintf("sudo -P git -C %s pull", sourceDir))
|
|
|
|
|
mm := []string{"-P", "git", "-C", sourceDir, "pull"}
|
|
|
|
|
if err := ShowExec("sudo", mm, logger); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2018-01-04 13:51:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-07 11:40:44 +08:00
|
|
|
|
func checkDockerfile(sourceDir string) bool {
|
|
|
|
|
if _, err := os.Stat(fmt.Sprintf("%s/Dockerfile", sourceDir)); os.IsNotExist(err) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func buildImage(version, gitURL, sourceDir, curRegistry string, logger event.Logger) (string, error) {
|
|
|
|
|
mm := strings.Split(gitURL, "/")
|
|
|
|
|
n1 := strings.Split(mm[len(mm)-1], ".")[0]
|
|
|
|
|
imageName := fmt.Sprintf("%s/%s_%s", curRegistry, n1, version)
|
|
|
|
|
//imagename must be lower
|
|
|
|
|
logrus.Debugf("image name is %v", imageName)
|
|
|
|
|
if os.Getenv("NO_CACHE") == "" {
|
|
|
|
|
mm := []string{"-P", "docker", "build", "-t", imageName, "--no-cache", sourceDir}
|
2018-01-04 11:42:34 +08:00
|
|
|
|
logrus.Debugf("build image: sudo -P docker build -t %s --no-cache %s", imageName, sourceDir)
|
2017-11-07 11:40:44 +08:00
|
|
|
|
if err := ShowExec("sudo", mm, logger); err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
mm := []string{"-P", "docker", "build", "-t", imageName, sourceDir}
|
2018-01-04 11:42:34 +08:00
|
|
|
|
logrus.Debugf("build image: sudo -P docker build -t %s %s", imageName, sourceDir)
|
2017-11-07 11:40:44 +08:00
|
|
|
|
if err := ShowExec("sudo", mm, logger); err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return imageName, nil
|
|
|
|
|
}
|