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"
|
2018-03-30 15:37:36 +08:00
|
|
|
"runtime/debug"
|
2017-11-07 11:40:44 +08:00
|
|
|
"time"
|
|
|
|
|
2018-01-10 13:35:05 +08:00
|
|
|
"github.com/Sirupsen/logrus"
|
2018-02-07 11:28:31 +08:00
|
|
|
//"github.com/docker/docker/client"
|
2018-01-23 12:13:22 +08:00
|
|
|
"github.com/coreos/etcd/clientv3"
|
2018-03-01 11:30:54 +08:00
|
|
|
"github.com/docker/engine-api/client"
|
2018-04-24 16:44:59 +08:00
|
|
|
"github.com/goodrain/rainbond/db"
|
|
|
|
"github.com/goodrain/rainbond/db/config"
|
|
|
|
dbmodel "github.com/goodrain/rainbond/db/model"
|
|
|
|
"github.com/goodrain/rainbond/event"
|
|
|
|
"github.com/goodrain/rainbond/mq/api/grpc/pb"
|
2018-05-25 18:24:17 +08:00
|
|
|
"github.com/goodrain/rainbond/util"
|
2017-11-07 11:40:44 +08:00
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
)
|
|
|
|
|
|
|
|
//Manager 任务执行管理器
|
|
|
|
type Manager interface {
|
|
|
|
AddTask(*pb.TaskMessage) error
|
|
|
|
Start() error
|
|
|
|
Stop() error
|
|
|
|
}
|
|
|
|
|
|
|
|
//NewManager new manager
|
|
|
|
func NewManager(conf config.Config) (Manager, error) {
|
|
|
|
dockerClient, err := client.NewEnvClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-23 12:13:22 +08:00
|
|
|
etcdCli, err := clientv3.New(clientv3.Config{
|
|
|
|
Endpoints: conf.EtcdEndPoints,
|
|
|
|
DialTimeout: 5 * time.Second,
|
|
|
|
})
|
2018-01-25 22:10:34 +08:00
|
|
|
err = db.CreateManager(conf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-02-02 12:30:15 +08:00
|
|
|
//defer db.CloseManager()
|
2018-01-23 12:13:22 +08:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("create etcd client v3 in service check error, %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-07 11:40:44 +08:00
|
|
|
return &exectorManager{
|
|
|
|
DockerClient: dockerClient,
|
2018-03-01 11:30:54 +08:00
|
|
|
EtcdCli: etcdCli,
|
2017-11-07 11:40:44 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type exectorManager struct {
|
|
|
|
DockerClient *client.Client
|
2018-03-01 11:30:54 +08:00
|
|
|
EtcdCli *clientv3.Client
|
2017-11-07 11:40:44 +08:00
|
|
|
}
|
|
|
|
|
2018-05-07 15:27:44 +08:00
|
|
|
//TaskWorker worker interface
|
|
|
|
type TaskWorker interface {
|
2018-05-07 15:10:12 +08:00
|
|
|
Run(timeout time.Duration) error
|
|
|
|
GetLogger() event.Logger
|
|
|
|
Name() string
|
|
|
|
Stop() error
|
2018-05-23 11:08:44 +08:00
|
|
|
//ErrorCallBack if run error will callback
|
|
|
|
ErrorCallBack(err error)
|
2018-05-07 15:10:12 +08:00
|
|
|
}
|
|
|
|
|
2018-05-28 11:14:56 +08:00
|
|
|
var workerCreaterList = make(map[string]func([]byte, *exectorManager) (TaskWorker, error))
|
2018-05-07 15:10:12 +08:00
|
|
|
|
|
|
|
//RegisterWorker register worker creater
|
2018-05-28 11:14:56 +08:00
|
|
|
func RegisterWorker(name string, fun func([]byte, *exectorManager) (TaskWorker, error)) {
|
2018-05-07 15:10:12 +08:00
|
|
|
workerCreaterList[name] = fun
|
|
|
|
}
|
|
|
|
|
2017-11-07 11:40:44 +08:00
|
|
|
//TaskType:
|
2018-03-24 16:16:47 +08:00
|
|
|
//build_from_image build app from docker image
|
|
|
|
//build_from_source_code build app from source code
|
|
|
|
//build_from_market_slug build app from app market by download slug
|
|
|
|
//service_check check service source info
|
|
|
|
//plugin_image_build build plugin from image
|
|
|
|
//plugin_dockerfile_build build plugin from dockerfile
|
|
|
|
//share-slug share app with slug
|
|
|
|
//share-image share app with image
|
2017-11-07 11:40:44 +08:00
|
|
|
func (e *exectorManager) AddTask(task *pb.TaskMessage) error {
|
|
|
|
switch task.TaskType {
|
2018-01-25 22:10:34 +08:00
|
|
|
case "build_from_image":
|
|
|
|
e.buildFromImage(task.TaskBody)
|
|
|
|
case "build_from_source_code":
|
|
|
|
e.buildFromSourceCode(task.TaskBody)
|
2018-03-05 17:40:56 +08:00
|
|
|
case "build_from_market_slug":
|
|
|
|
e.buildFromMarketSlug(task.TaskBody)
|
2018-01-10 13:35:05 +08:00
|
|
|
case "service_check":
|
2018-03-06 11:53:27 +08:00
|
|
|
go e.serviceCheck(task.TaskBody)
|
2017-11-07 11:40:44 +08:00
|
|
|
case "plugin_image_build":
|
|
|
|
e.pluginImageBuild(task.TaskBody)
|
|
|
|
case "plugin_dockerfile_build":
|
|
|
|
e.pluginDockerfileBuild(task.TaskBody)
|
2018-03-05 14:28:59 +08:00
|
|
|
case "share-slug":
|
2018-02-05 19:23:59 +08:00
|
|
|
e.slugShare(task.TaskBody)
|
2018-03-05 14:28:59 +08:00
|
|
|
case "share-image":
|
2018-02-05 19:23:59 +08:00
|
|
|
e.imageShare(task.TaskBody)
|
2017-11-07 11:40:44 +08:00
|
|
|
default:
|
2018-05-07 15:10:12 +08:00
|
|
|
return e.exec(task.TaskType, task.TaskBody)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *exectorManager) exec(workerName string, in []byte) error {
|
|
|
|
creater, ok := workerCreaterList[workerName]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("`%s` tasktype can't support", workerName)
|
2017-11-07 11:40:44 +08:00
|
|
|
}
|
2018-05-28 11:14:56 +08:00
|
|
|
worker, err := creater(in, e)
|
2018-05-23 11:08:44 +08:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("create worker for builder error.%s", err)
|
|
|
|
return err
|
|
|
|
}
|
2018-05-07 15:10:12 +08:00
|
|
|
go func() {
|
|
|
|
defer event.GetManager().ReleaseLogger(worker.GetLogger())
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
fmt.Println(r)
|
|
|
|
debug.PrintStack()
|
2018-05-25 18:24:17 +08:00
|
|
|
worker.GetLogger().Error(util.Translation("Please try again or contact customer service"), map[string]string{"step": "callback", "status": "failure"})
|
2018-05-23 12:52:37 +08:00
|
|
|
worker.ErrorCallBack(fmt.Errorf("%s", r))
|
2018-05-07 15:10:12 +08:00
|
|
|
}
|
|
|
|
}()
|
2018-05-23 11:08:44 +08:00
|
|
|
if err := worker.Run(time.Minute * 10); err != nil {
|
|
|
|
worker.ErrorCallBack(err)
|
|
|
|
}
|
2018-05-07 15:10:12 +08:00
|
|
|
}()
|
2017-11-07 11:40:44 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-11 21:38:48 +08:00
|
|
|
//buildFromImage build app from docker image
|
2018-01-25 22:10:34 +08:00
|
|
|
func (e *exectorManager) buildFromImage(in []byte) {
|
|
|
|
i := NewImageBuildItem(in)
|
|
|
|
i.DockerClient = e.DockerClient
|
2018-03-01 11:30:54 +08:00
|
|
|
i.Logger.Info("从镜像构建应用任务开始执行", map[string]string{"step": "builder-exector", "status": "starting"})
|
2018-01-29 11:25:49 +08:00
|
|
|
status := "success"
|
2018-01-25 22:10:34 +08:00
|
|
|
go func() {
|
|
|
|
logrus.Debugf("start build from image worker")
|
|
|
|
defer event.GetManager().ReleaseLogger(i.Logger)
|
2018-03-30 15:37:36 +08:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2018-04-12 18:09:13 +08:00
|
|
|
fmt.Println(r)
|
2018-03-30 15:37:36 +08:00
|
|
|
debug.PrintStack()
|
|
|
|
i.Logger.Error("后端服务开小差,请重试或联系客服", map[string]string{"step": "callback", "status": "failure"})
|
|
|
|
}
|
|
|
|
}()
|
2018-03-06 11:53:27 +08:00
|
|
|
for n := 0; n < 2; n++ {
|
2018-01-25 22:10:34 +08:00
|
|
|
err := i.Run(time.Minute * 30)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("build from image error: %s", err.Error())
|
2018-03-15 15:59:18 +08:00
|
|
|
if n < 1 {
|
2018-03-01 11:30:54 +08:00
|
|
|
i.Logger.Error("从镜像构建应用任务执行失败,开始重试", map[string]string{"step": "build-exector", "status": "failure"})
|
|
|
|
} else {
|
|
|
|
i.Logger.Error("从镜像构建应用任务执行失败", map[string]string{"step": "callback", "status": "failure"})
|
2018-01-29 11:25:49 +08:00
|
|
|
status = "failure"
|
2018-01-25 22:10:34 +08:00
|
|
|
}
|
2018-03-01 11:30:54 +08:00
|
|
|
} else {
|
2018-01-25 22:10:34 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-03-15 18:30:02 +08:00
|
|
|
if err := i.UpdateVersionInfo(status); err != nil {
|
|
|
|
logrus.Debugf("update version Info error: %s", err.Error())
|
|
|
|
}
|
2018-01-25 22:10:34 +08:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2018-04-11 21:38:48 +08:00
|
|
|
//buildFromSourceCode build app from source code
|
|
|
|
//support git repository
|
2018-03-01 11:30:54 +08:00
|
|
|
func (e *exectorManager) buildFromSourceCode(in []byte) {
|
2018-01-30 15:10:12 +08:00
|
|
|
i := NewSouceCodeBuildItem(in)
|
|
|
|
i.DockerClient = e.DockerClient
|
2018-03-01 11:30:54 +08:00
|
|
|
i.Logger.Info("从源码构建应用任务开始执行", map[string]string{"step": "builder-exector", "status": "starting"})
|
2018-01-30 15:10:12 +08:00
|
|
|
status := "success"
|
|
|
|
go func() {
|
|
|
|
logrus.Debugf("start build from source code")
|
|
|
|
defer event.GetManager().ReleaseLogger(i.Logger)
|
2018-03-30 15:37:36 +08:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2018-04-12 18:09:13 +08:00
|
|
|
fmt.Println(r)
|
2018-03-30 15:37:36 +08:00
|
|
|
debug.PrintStack()
|
|
|
|
i.Logger.Error("后端服务开小差,请重试或联系客服", map[string]string{"step": "callback", "status": "failure"})
|
|
|
|
}
|
|
|
|
}()
|
2018-03-06 11:53:27 +08:00
|
|
|
for n := 0; n < 2; n++ {
|
2018-01-30 15:10:12 +08:00
|
|
|
err := i.Run(time.Minute * 30)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("build from source code error: %s", err.Error())
|
2018-03-15 15:59:18 +08:00
|
|
|
if n < 1 {
|
2018-03-01 11:30:54 +08:00
|
|
|
i.Logger.Error("从源码构建应用任务执行失败,开始重试", map[string]string{"step": "build-exector", "status": "failure"})
|
|
|
|
} else {
|
|
|
|
i.Logger.Error("从源码构建应用任务执行失败", map[string]string{"step": "callback", "status": "failure"})
|
2018-01-30 15:10:12 +08:00
|
|
|
status = "failure"
|
|
|
|
}
|
2018-03-01 11:30:54 +08:00
|
|
|
} else {
|
2018-01-30 15:10:12 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-05-23 16:43:16 +08:00
|
|
|
if status == "failure" {
|
|
|
|
vi := &dbmodel.VersionInfo{
|
|
|
|
FinalStatus: status,
|
|
|
|
EventID: i.EventID,
|
|
|
|
CodeVersion: i.commit.Hash.String(),
|
|
|
|
CommitMsg: i.commit.Message,
|
|
|
|
Author: i.commit.Author.Name,
|
|
|
|
}
|
|
|
|
if err := i.UpdateVersionInfo(vi); err != nil {
|
|
|
|
logrus.Debugf("update version Info error: %s", err.Error())
|
|
|
|
}
|
2018-03-15 18:30:02 +08:00
|
|
|
}
|
2018-01-30 15:10:12 +08:00
|
|
|
}()
|
2018-01-25 22:10:34 +08:00
|
|
|
}
|
|
|
|
|
2018-04-11 21:38:48 +08:00
|
|
|
//buildFromMarketSlug build app from market slug
|
2018-03-05 17:40:56 +08:00
|
|
|
func (e *exectorManager) buildFromMarketSlug(in []byte) {
|
|
|
|
eventID := gjson.GetBytes(in, "event_id").String()
|
|
|
|
logger := event.GetManager().GetLogger(eventID)
|
|
|
|
logger.Info("云市应用代码包构建任务开始执行", map[string]string{"step": "builder-exector", "status": "starting"})
|
|
|
|
i, err := NewMarketSlugItem(in)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error("create build from market slug task error.", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
i.Logger.Info("开始构建应用", map[string]string{"step": "builder-exector", "status": "starting"})
|
|
|
|
go func() {
|
|
|
|
defer event.GetManager().ReleaseLogger(i.Logger)
|
2018-03-30 15:37:36 +08:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2018-04-12 18:09:13 +08:00
|
|
|
fmt.Println(r)
|
2018-03-30 15:37:36 +08:00
|
|
|
debug.PrintStack()
|
|
|
|
i.Logger.Error("后端服务开小差,请重试或联系客服", map[string]string{"step": "callback", "status": "failure"})
|
|
|
|
}
|
|
|
|
}()
|
2018-03-05 17:40:56 +08:00
|
|
|
for n := 0; n < 2; n++ {
|
|
|
|
err := i.Run()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("image share error: %s", err.Error())
|
2018-03-15 15:59:18 +08:00
|
|
|
if n < 1 {
|
2018-03-05 17:40:56 +08:00
|
|
|
i.Logger.Error("应用构建失败,开始重试", map[string]string{"step": "builder-exector", "status": "failure"})
|
|
|
|
} else {
|
2018-03-15 19:46:39 +08:00
|
|
|
i.Logger.Error("构建应用任务执行失败", map[string]string{"step": "callback", "status": "failure"})
|
2018-03-05 17:40:56 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2018-03-04 22:48:50 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-11 21:38:48 +08:00
|
|
|
//slugShare share app of slug
|
2018-03-01 11:30:54 +08:00
|
|
|
func (e *exectorManager) slugShare(in []byte) {
|
2018-03-04 22:48:50 +08:00
|
|
|
i, err := NewSlugShareItem(in, e.EtcdCli)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error("create share image task error.", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
i.Logger.Info("开始分享应用", map[string]string{"step": "builder-exector", "status": "starting"})
|
2018-02-07 16:10:26 +08:00
|
|
|
status := "success"
|
2018-03-01 11:30:54 +08:00
|
|
|
go func() {
|
2018-02-06 17:41:47 +08:00
|
|
|
defer event.GetManager().ReleaseLogger(i.Logger)
|
2018-04-12 18:09:13 +08:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
fmt.Println(r)
|
|
|
|
debug.PrintStack()
|
|
|
|
i.Logger.Error("后端服务开小差,请重试或联系客服", map[string]string{"step": "callback", "status": "failure"})
|
|
|
|
}
|
|
|
|
}()
|
2018-03-15 15:59:18 +08:00
|
|
|
for n := 0; n < 2; n++ {
|
2018-03-04 22:48:50 +08:00
|
|
|
err := i.ShareService()
|
2018-02-06 17:41:47 +08:00
|
|
|
if err != nil {
|
2018-03-04 22:48:50 +08:00
|
|
|
logrus.Errorf("image share error: %s", err.Error())
|
2018-03-15 15:59:18 +08:00
|
|
|
if n < 1 {
|
2018-03-04 22:48:50 +08:00
|
|
|
i.Logger.Error("应用分享失败,开始重试", map[string]string{"step": "builder-exector", "status": "failure"})
|
2018-03-01 11:30:54 +08:00
|
|
|
} else {
|
2018-03-04 22:48:50 +08:00
|
|
|
i.Logger.Error("分享应用任务执行失败", map[string]string{"step": "builder-exector", "status": "failure"})
|
2018-03-01 11:30:54 +08:00
|
|
|
status = "failure"
|
2018-02-06 17:41:47 +08:00
|
|
|
}
|
2018-03-01 11:30:54 +08:00
|
|
|
} else {
|
2018-03-04 22:48:50 +08:00
|
|
|
status = "success"
|
2018-02-06 17:41:47 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-03-04 22:48:50 +08:00
|
|
|
if err := i.UpdateShareStatus(status); err != nil {
|
|
|
|
logrus.Debugf("Add image share result error: %s", err.Error())
|
|
|
|
}
|
2018-02-06 17:41:47 +08:00
|
|
|
}()
|
2018-02-05 19:23:59 +08:00
|
|
|
}
|
|
|
|
|
2018-04-11 21:38:48 +08:00
|
|
|
//imageShare share app of docker image
|
2018-03-01 11:30:54 +08:00
|
|
|
func (e *exectorManager) imageShare(in []byte) {
|
2018-03-04 22:48:50 +08:00
|
|
|
i, err := NewImageShareItem(in, e.DockerClient, e.EtcdCli)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error("create share image task error.", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
i.Logger.Info("开始分享应用", map[string]string{"step": "builder-exector", "status": "starting"})
|
2018-02-07 16:10:26 +08:00
|
|
|
status := "success"
|
2018-03-01 11:30:54 +08:00
|
|
|
go func() {
|
2018-02-07 11:11:35 +08:00
|
|
|
defer event.GetManager().ReleaseLogger(i.Logger)
|
2018-04-12 18:09:13 +08:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
fmt.Println(r)
|
|
|
|
debug.PrintStack()
|
|
|
|
i.Logger.Error("后端服务开小差,请重试或联系客服", map[string]string{"step": "callback", "status": "failure"})
|
|
|
|
}
|
|
|
|
}()
|
2018-03-15 15:59:18 +08:00
|
|
|
for n := 0; n < 2; n++ {
|
2018-03-04 22:48:50 +08:00
|
|
|
err := i.ShareService()
|
2018-02-07 11:11:35 +08:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("image share error: %s", err.Error())
|
2018-03-15 15:59:18 +08:00
|
|
|
if n < 1 {
|
2018-03-04 22:48:50 +08:00
|
|
|
i.Logger.Error("应用分享失败,开始重试", map[string]string{"step": "builder-exector", "status": "failure"})
|
2018-03-01 11:30:54 +08:00
|
|
|
} else {
|
2018-03-04 22:48:50 +08:00
|
|
|
i.Logger.Error("分享应用任务执行失败", map[string]string{"step": "builder-exector", "status": "failure"})
|
2018-03-01 11:30:54 +08:00
|
|
|
status = "failure"
|
2018-02-07 11:11:35 +08:00
|
|
|
}
|
2018-03-01 11:30:54 +08:00
|
|
|
} else {
|
2018-03-04 22:48:50 +08:00
|
|
|
status = "success"
|
2018-02-07 11:11:35 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-03-04 22:48:50 +08:00
|
|
|
if err := i.UpdateShareStatus(status); err != nil {
|
|
|
|
logrus.Debugf("Add image share result error: %s", err.Error())
|
|
|
|
}
|
2018-02-07 11:11:35 +08:00
|
|
|
}()
|
2018-02-05 19:23:59 +08:00
|
|
|
}
|
|
|
|
|
2017-11-07 11:40:44 +08:00
|
|
|
func (e *exectorManager) Start() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (e *exectorManager) Stop() error {
|
|
|
|
return nil
|
|
|
|
}
|