2018-03-14 14:12:26 +08:00
|
|
|
|
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
|
2017-11-07 11:40:44 +08:00
|
|
|
|
// RAINBOND, Application Management Platform
|
2018-03-14 14:33:31 +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.
|
2018-03-14 14:33:31 +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.
|
2018-03-14 14:33:31 +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 (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
2017-11-28 14:46:38 +08:00
|
|
|
|
|
2018-09-02 22:44:38 +08:00
|
|
|
|
"github.com/goodrain/rainbond/builder"
|
2018-04-24 16:44:59 +08:00
|
|
|
|
"github.com/goodrain/rainbond/builder/sources"
|
|
|
|
|
"github.com/goodrain/rainbond/util"
|
2018-03-07 22:52:06 +08:00
|
|
|
|
|
2018-04-24 16:44:59 +08:00
|
|
|
|
"github.com/goodrain/rainbond/db"
|
|
|
|
|
"github.com/goodrain/rainbond/event"
|
2017-11-07 11:40:44 +08:00
|
|
|
|
|
|
|
|
|
"github.com/pquerna/ffjson/ffjson"
|
|
|
|
|
|
2018-04-24 16:44:59 +08:00
|
|
|
|
"github.com/goodrain/rainbond/builder/model"
|
2017-11-07 11:40:44 +08:00
|
|
|
|
|
2018-10-18 18:39:21 +08:00
|
|
|
|
"github.com/goodrain/rainbond/mq/api/grpc/pb"
|
2020-11-25 16:39:38 +08:00
|
|
|
|
"github.com/sirupsen/logrus"
|
2017-11-07 11:40:44 +08:00
|
|
|
|
)
|
|
|
|
|
|
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
|
|
|
|
|
2018-10-18 18:39:21 +08:00
|
|
|
|
func (e *exectorManager) pluginDockerfileBuild(task *pb.TaskMessage) {
|
2017-11-07 11:40:44 +08:00
|
|
|
|
var tb model.BuildPluginTaskBody
|
2018-10-18 18:39:21 +08:00
|
|
|
|
if err := ffjson.Unmarshal(task.TaskBody, &tb); err != nil {
|
2017-11-07 11:40:44 +08:00
|
|
|
|
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"})
|
2019-06-29 15:20:11 +08:00
|
|
|
|
logrus.Info("start exec build plugin from image worker")
|
|
|
|
|
defer event.GetManager().ReleaseLogger(logger)
|
|
|
|
|
for retry := 0; retry < 2; retry++ {
|
|
|
|
|
err := e.runD(&tb, logger)
|
2018-01-08 15:41:13 +08:00
|
|
|
|
if err != nil {
|
2019-06-29 15:20:11 +08:00
|
|
|
|
logrus.Errorf("exec plugin build from dockerfile error:%s", err.Error())
|
|
|
|
|
logger.Info("dockerfile构建插件任务执行失败,开始重试", map[string]string{"step": "builder-exector", "status": "failure"})
|
|
|
|
|
} else {
|
2018-05-30 14:13:22 +08:00
|
|
|
|
return
|
2018-01-08 15:41:13 +08:00
|
|
|
|
}
|
2019-06-29 15:20:11 +08:00
|
|
|
|
}
|
|
|
|
|
version, err := db.GetManager().TenantPluginBuildVersionDao().GetBuildVersionByDeployVersion(tb.PluginID, tb.VersionID, tb.DeployVersion)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logrus.Errorf("get version error, %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
version.Status = "failure"
|
|
|
|
|
if err := db.GetManager().TenantPluginBuildVersionDao().UpdateModel(version); err != nil {
|
|
|
|
|
logrus.Errorf("update version error, %v", err)
|
|
|
|
|
}
|
2019-07-12 14:42:24 +08:00
|
|
|
|
MetricErrorTaskNum++
|
2019-06-29 15:20:11 +08:00
|
|
|
|
logger.Error("dockerfile构建插件任务执行失败", map[string]string{"step": "callback", "status": "failure"})
|
2017-11-07 11:40:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-24 16:16:47 +08:00
|
|
|
|
func (e *exectorManager) runD(t *model.BuildPluginTaskBody, logger event.Logger) error {
|
2017-11-07 11:40:44 +08:00
|
|
|
|
logger.Info("开始拉取代码", map[string]string{"step": "build-exector"})
|
|
|
|
|
sourceDir := fmt.Sprintf(formatSourceDir, t.TenantID, t.VersionID)
|
2017-11-28 14:46:38 +08:00
|
|
|
|
if t.Repo == "" {
|
|
|
|
|
t.Repo = "master"
|
|
|
|
|
}
|
2018-03-07 23:01:32 +08:00
|
|
|
|
if !util.DirIsEmpty(sourceDir) {
|
|
|
|
|
os.RemoveAll(sourceDir)
|
|
|
|
|
}
|
|
|
|
|
if err := util.CheckAndCreateDir(sourceDir); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2020-04-23 14:57:57 +08:00
|
|
|
|
if _, err := sources.GitClone(sources.CodeSourceInfo{RepositoryURL: t.GitURL, Branch: t.Repo, User: t.GitUsername, Password: t.GitPassword}, sourceDir, logger, 4); err != nil {
|
2018-01-05 15:12:11 +08:00
|
|
|
|
logger.Error("拉取代码失败", map[string]string{"step": "builder-exector", "status": "failure"})
|
2018-03-07 22:52:06 +08:00
|
|
|
|
logrus.Errorf("[plugin]git clone code error %v", err)
|
2017-11-07 11:40:44 +08:00
|
|
|
|
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"})
|
2018-03-07 22:52:06 +08:00
|
|
|
|
mm := strings.Split(t.GitURL, "/")
|
|
|
|
|
n1 := strings.Split(mm[len(mm)-1], ".")[0]
|
2019-03-29 18:28:01 +08:00
|
|
|
|
buildImageName := fmt.Sprintf(builder.REGISTRYDOMAIN+"/plugin_%s_%s:%s", n1, t.PluginID, t.DeployVersion)
|
2019-12-02 16:12:22 +08:00
|
|
|
|
logger.Info("start build image", map[string]string{"step": "builder-exector"})
|
2022-11-07 22:55:15 +08:00
|
|
|
|
err := sources.ImageBuild(sourceDir, "rbd-system", t.PluginID, t.DeployVersion, logger, "plug-build", buildImageName, e.KanikoImage)
|
2017-11-07 11:40:44 +08:00
|
|
|
|
if err != nil {
|
2019-12-02 16:12:22 +08:00
|
|
|
|
logger.Error(fmt.Sprintf("build image %s failure,find log in rbd-chaos", buildImageName), map[string]string{"step": "builder-exector", "status": "failure"})
|
2018-03-07 22:52:06 +08:00
|
|
|
|
logrus.Errorf("[plugin]build image error: %s", err.Error())
|
2017-11-07 11:40:44 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
2019-12-02 16:12:22 +08:00
|
|
|
|
logger.Info("build image success, start to push image to local image registry", map[string]string{"step": "builder-exector"})
|
|
|
|
|
logger.Info("push image success", map[string]string{"step": "build-exector"})
|
2018-05-29 12:51:10 +08:00
|
|
|
|
version, err := db.GetManager().TenantPluginBuildVersionDao().GetBuildVersionByDeployVersion(t.PluginID, t.VersionID, t.DeployVersion)
|
2017-11-07 11:40:44 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
logrus.Errorf("get version error, %v", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
2018-03-07 22:52:06 +08:00
|
|
|
|
version.BuildLocalImage = buildImageName
|
2017-11-07 11:40:44 +08:00
|
|
|
|
version.Status = "complete"
|
|
|
|
|
if err := db.GetManager().TenantPluginBuildVersionDao().UpdateModel(version); err != nil {
|
|
|
|
|
logrus.Errorf("update version error, %v", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
2019-12-02 16:12:22 +08:00
|
|
|
|
logger.Info("build plugin version by dockerfile success", map[string]string{"step": "last", "status": "success"})
|
2017-11-07 11:40:44 +08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func checkDockerfile(sourceDir string) bool {
|
|
|
|
|
if _, err := os.Stat(fmt.Sprintf("%s/Dockerfile", sourceDir)); os.IsNotExist(err) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|