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"
|
|
|
|
"strings"
|
|
|
|
|
2018-04-24 16:44:59 +08:00
|
|
|
"github.com/goodrain/rainbond/builder/sources"
|
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-09 12:19:50 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (e *exectorManager) pluginImageBuild(in []byte) {
|
|
|
|
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)
|
|
|
|
logger.Info("从镜像构建插件任务开始执行", map[string]string{"step": "builder-exector", "status": "starting"})
|
|
|
|
go func() {
|
|
|
|
logrus.Info("start exec build plugin from image worker")
|
|
|
|
defer event.GetManager().ReleaseLogger(logger)
|
2018-03-07 22:52:06 +08:00
|
|
|
for retry := 0; retry < 2; retry++ {
|
2018-03-24 16:16:47 +08:00
|
|
|
err := e.run(&tb, logger)
|
2017-11-07 11:40:44 +08:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("exec plugin build from image error:%s", err.Error())
|
2018-01-08 15:41:13 +08:00
|
|
|
logger.Info("镜像构建插件任务执行失败,开始重试", 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-05-29 12:51:10 +08:00
|
|
|
version, err := db.GetManager().TenantPluginBuildVersionDao().GetBuildVersionByDeployVersion(tb.PluginID, tb.VersionID, tb.DeployVersion)
|
2018-01-08 15:41:13 +08:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("get version error, %v", err)
|
2018-05-30 14:13:22 +08:00
|
|
|
return
|
2018-01-08 15:41:13 +08:00
|
|
|
}
|
|
|
|
version.Status = "failure"
|
|
|
|
if err := db.GetManager().TenantPluginBuildVersionDao().UpdateModel(version); err != nil {
|
|
|
|
logrus.Errorf("update version error, %v", err)
|
|
|
|
}
|
2018-07-11 15:44:29 +08:00
|
|
|
ErrorNum += 1
|
2018-01-10 11:00:37 +08:00
|
|
|
logger.Info("镜像构建插件任务执行失败", 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) run(t *model.BuildPluginTaskBody, logger event.Logger) error {
|
2018-03-07 22:52:06 +08:00
|
|
|
|
2018-06-21 18:04:02 +08:00
|
|
|
if _, err := sources.ImagePull(e.DockerClient, t.ImageURL, t.ImageInfo.HubUser, t.ImageInfo.HubPassword, logger, 10); err != nil {
|
2017-11-07 11:40:44 +08:00
|
|
|
logrus.Errorf("pull image %v error, %v", t.ImageURL, err)
|
2018-03-07 22:52:06 +08:00
|
|
|
logger.Error("拉取镜像失败", map[string]string{"step": "builder-exector", "status": "failure"})
|
2017-11-07 11:40:44 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
logger.Info("拉取镜像完成", map[string]string{"step": "build-exector", "status": "complete"})
|
2018-05-28 18:19:30 +08:00
|
|
|
newTag := createPluginImageTag(t.ImageURL, t.PluginID, t.DeployVersion)
|
2018-03-07 22:52:06 +08:00
|
|
|
err := sources.ImageTag(e.DockerClient, t.ImageURL, newTag, logger, 1)
|
2017-11-07 11:40:44 +08:00
|
|
|
if err != nil {
|
2018-03-07 22:52:06 +08:00
|
|
|
logrus.Errorf("set plugin image tag error, %v", err)
|
|
|
|
logger.Error("修改镜像tag失败", map[string]string{"step": "builder-exector", "status": "failure"})
|
2017-11-07 11:40:44 +08:00
|
|
|
return err
|
|
|
|
}
|
2018-03-07 22:52:06 +08:00
|
|
|
logger.Info("修改镜像Tag完成", map[string]string{"step": "build-exector", "status": "complete"})
|
2018-05-19 23:26:44 +08:00
|
|
|
if err := sources.ImagePush(e.DockerClient, newTag, "", "", logger, 10); err != nil {
|
2018-03-07 22:52:06 +08:00
|
|
|
logrus.Errorf("push image %s error, %v", newTag, err)
|
|
|
|
logger.Error("推送镜像失败", map[string]string{"step": "builder-exector", "status": "failure"})
|
2017-11-07 11:40:44 +08:00
|
|
|
return err
|
|
|
|
}
|
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 {
|
2018-03-07 22:52:06 +08:00
|
|
|
logger.Error("更新插件版本信息错误", map[string]string{"step": "builder-exector", "status": "failure"})
|
2017-11-07 11:40:44 +08:00
|
|
|
return err
|
|
|
|
}
|
2018-03-07 22:52:06 +08:00
|
|
|
version.BuildLocalImage = newTag
|
2017-11-07 11:40:44 +08:00
|
|
|
version.Status = "complete"
|
|
|
|
if err := db.GetManager().TenantPluginBuildVersionDao().UpdateModel(version); err != nil {
|
2018-03-07 22:52:06 +08:00
|
|
|
logger.Error("更新插件版本信息错误", map[string]string{"step": "builder-exector", "status": "failure"})
|
2017-11-07 11:40:44 +08:00
|
|
|
return err
|
|
|
|
}
|
2017-12-25 16:03:51 +08:00
|
|
|
logger.Info("从镜像构建插件完成", map[string]string{"step": "last", "status": "success"})
|
2017-11-07 11:40:44 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-28 18:19:30 +08:00
|
|
|
func createPluginImageTag(image string, pluginid, version string) string {
|
2017-11-07 11:40:44 +08:00
|
|
|
//alias is pluginID
|
|
|
|
mm := strings.Split(image, "/")
|
|
|
|
tag := "latest"
|
|
|
|
iName := ""
|
|
|
|
if strings.Contains(mm[len(mm)-1], ":") {
|
|
|
|
nn := strings.Split(mm[len(mm)-1], ":")
|
|
|
|
tag = nn[1]
|
|
|
|
iName = nn[0]
|
2017-11-09 12:19:50 +08:00
|
|
|
} else {
|
|
|
|
iName = image
|
2017-11-07 11:40:44 +08:00
|
|
|
}
|
2018-06-25 19:32:00 +08:00
|
|
|
if strings.HasPrefix(iName, "plugin") {
|
|
|
|
return fmt.Sprintf("goodrain.me/%s:%s_%s", iName, pluginid, version)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("goodrain.me/plugin_%s_%s:%s_%s", iName, pluginid, tag, version)
|
2017-11-07 11:40:44 +08:00
|
|
|
}
|