2018-03-14 14:12:26 +08:00
|
|
|
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
|
2018-01-25 22:10:34 +08:00
|
|
|
// RAINBOND, Application Management Platform
|
2018-03-14 14:33:31 +08:00
|
|
|
|
2018-01-25 22:10:34 +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
|
|
|
|
2018-01-25 22:10:34 +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
|
|
|
|
2018-01-25 22:10:34 +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-01 13:34:54 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2018-04-24 16:44:59 +08:00
|
|
|
"github.com/goodrain/rainbond/event"
|
2018-01-25 22:10:34 +08:00
|
|
|
"github.com/tidwall/gjson"
|
2018-02-07 11:36:24 +08:00
|
|
|
//"github.com/docker/docker/api/types"
|
2018-05-25 18:24:17 +08:00
|
|
|
|
2018-02-07 11:28:31 +08:00
|
|
|
//"github.com/docker/docker/client"
|
2018-04-13 11:14:32 +08:00
|
|
|
|
2018-02-07 11:28:31 +08:00
|
|
|
"github.com/docker/engine-api/client"
|
2018-04-24 16:44:59 +08:00
|
|
|
"github.com/goodrain/rainbond/builder/apiHandler"
|
|
|
|
"github.com/goodrain/rainbond/builder/sources"
|
|
|
|
"github.com/goodrain/rainbond/db"
|
|
|
|
"github.com/goodrain/rainbond/worker/discover/model"
|
2018-01-25 22:10:34 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
//ImageBuildItem ImageBuildItem
|
|
|
|
type ImageBuildItem struct {
|
2018-03-01 13:34:54 +08:00
|
|
|
Namespace string `json:"namespace"`
|
|
|
|
TenantName string `json:"tenant_name"`
|
|
|
|
ServiceAlias string `json:"service_alias"`
|
|
|
|
Image string `json:"image"`
|
|
|
|
DestImage string `json:"dest_image"`
|
|
|
|
Logger event.Logger `json:"logger"`
|
|
|
|
EventID string `json:"event_id"`
|
|
|
|
DockerClient *client.Client
|
|
|
|
TenantID string
|
|
|
|
ServiceID string
|
|
|
|
DeployVersion string
|
2018-03-05 17:40:56 +08:00
|
|
|
HubUser string
|
|
|
|
HubPassword string
|
2018-01-25 22:10:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//NewImageBuildItem 创建实体
|
|
|
|
func NewImageBuildItem(in []byte) *ImageBuildItem {
|
|
|
|
eventID := gjson.GetBytes(in, "event_id").String()
|
|
|
|
logger := event.GetManager().GetLogger(eventID)
|
|
|
|
return &ImageBuildItem{
|
2018-03-01 13:34:54 +08:00
|
|
|
Namespace: gjson.GetBytes(in, "namespace").String(),
|
|
|
|
TenantName: gjson.GetBytes(in, "tenant_name").String(),
|
|
|
|
ServiceAlias: gjson.GetBytes(in, "service_alias").String(),
|
|
|
|
Image: gjson.GetBytes(in, "image").String(),
|
2018-01-25 22:10:34 +08:00
|
|
|
DeployVersion: gjson.GetBytes(in, "deploy_version").String(),
|
2018-03-05 17:40:56 +08:00
|
|
|
HubUser: gjson.GetBytes(in, "user").String(),
|
|
|
|
HubPassword: gjson.GetBytes(in, "password").String(),
|
2018-03-01 13:34:54 +08:00
|
|
|
Logger: logger,
|
|
|
|
EventID: eventID,
|
2018-01-25 22:10:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Run Run
|
|
|
|
func (i *ImageBuildItem) Run(timeout time.Duration) error {
|
2018-05-25 18:24:17 +08:00
|
|
|
_, err := sources.ImagePull(i.DockerClient, i.Image, i.HubUser, i.HubPassword, i.Logger, 10)
|
2018-01-25 22:10:34 +08:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("pull image %s error: %s", i.Image, err.Error())
|
2018-03-01 13:34:54 +08:00
|
|
|
i.Logger.Error(fmt.Sprintf("获取指定镜像: %s失败", i.Image), map[string]string{"step": "builder-exector", "status": "failure"})
|
2018-01-25 22:10:34 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
localImageURL := i.ImageNameHandler(i.Image)
|
|
|
|
if err := sources.ImageTag(i.DockerClient, i.Image, localImageURL, i.Logger, 1); err != nil {
|
|
|
|
logrus.Errorf("change image tag error: %s", err.Error())
|
|
|
|
i.Logger.Error(fmt.Sprintf("修改镜像tag: %s -> %s 失败", i.Image, localImageURL), map[string]string{"step": "builder-exector", "status": "failure"})
|
|
|
|
return err
|
|
|
|
}
|
2018-04-03 11:54:29 +08:00
|
|
|
err = sources.ImagePush(i.DockerClient, localImageURL, "", "", i.Logger, 10)
|
2018-01-25 22:10:34 +08:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("push image into registry error: %s", err.Error())
|
2018-03-01 13:34:54 +08:00
|
|
|
i.Logger.Error("推送镜像至镜像仓库失败", map[string]string{"step": "builder-exector", "status": "failure"})
|
2018-01-25 22:10:34 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := i.StorageLocalImageURL(localImageURL); err != nil {
|
|
|
|
logrus.Errorf("storage image url error: %s", err.Error())
|
2018-03-01 13:34:54 +08:00
|
|
|
i.Logger.Error("存储镜像信息失败", map[string]string{"step": "builder-exector", "status": "failure"})
|
2018-01-25 22:10:34 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := i.StorageVersionInfo(localImageURL); err != nil {
|
|
|
|
logrus.Errorf("storage version info error, ignor it: %s", err.Error())
|
2018-03-28 21:38:55 +08:00
|
|
|
i.Logger.Error("更新应用版本信息失败", map[string]string{"step": "builder-exector", "status": "failure"})
|
2018-03-05 17:40:56 +08:00
|
|
|
return err
|
2018-01-25 22:10:34 +08:00
|
|
|
}
|
2018-01-29 11:25:49 +08:00
|
|
|
i.Logger.Info("应用同步完成,开始启动应用", map[string]string{"step": "build-exector"})
|
2018-01-30 18:02:08 +08:00
|
|
|
if err := apiHandler.UpgradeService(i.TenantName, i.ServiceAlias, i.CreateUpgradeTaskBody()); err != nil {
|
2018-03-28 21:38:55 +08:00
|
|
|
i.Logger.Error("启动应用失败,请手动启动", map[string]string{"step": "builder-exector", "status": "failure"})
|
2018-01-29 11:25:49 +08:00
|
|
|
logrus.Errorf("rolling update service error, %s", err.Error())
|
2018-03-01 14:27:22 +08:00
|
|
|
return err
|
2018-01-29 11:25:49 +08:00
|
|
|
}
|
2018-03-01 13:34:54 +08:00
|
|
|
i.Logger.Info("应用启动成功", map[string]string{"step": "build-exector"})
|
2018-01-25 22:10:34 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//ImageNameHandler 根据平台配置处理镜像名称
|
|
|
|
func (i *ImageBuildItem) ImageNameHandler(source string) string {
|
|
|
|
imageModel := sources.ImageNameHandle(source)
|
2018-03-06 18:01:00 +08:00
|
|
|
localImageURL := fmt.Sprintf("%s/%s:%s", "goodrain.me", imageModel.Name, i.DeployVersion)
|
2018-01-25 22:10:34 +08:00
|
|
|
return localImageURL
|
|
|
|
}
|
|
|
|
|
|
|
|
//StorageLocalImageURL 修改的镜像名称存库
|
|
|
|
func (i *ImageBuildItem) StorageLocalImageURL(imageURL string) error {
|
|
|
|
tenant, err := db.GetManager().TenantDao().GetTenantIDByName(i.TenantName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
service, err := db.GetManager().TenantServiceDao().GetServiceByTenantIDAndServiceAlias(tenant.UUID, i.ServiceAlias)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
service.ImageName = imageURL
|
|
|
|
if err := db.GetManager().TenantServiceDao().UpdateModel(service); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
i.TenantID = tenant.UUID
|
|
|
|
i.ServiceID = service.ServiceID
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//StorageVersionInfo 存储version信息
|
|
|
|
func (i *ImageBuildItem) StorageVersionInfo(imageURL string) error {
|
2018-03-07 15:11:20 +08:00
|
|
|
version, err := db.GetManager().VersionInfoDao().GetVersionByDeployVersion(i.DeployVersion, i.ServiceID)
|
2018-01-25 22:10:34 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
version.DeliveredType = "image"
|
|
|
|
version.DeliveredPath = imageURL
|
2018-05-23 12:17:34 +08:00
|
|
|
version.ImageName = imageURL
|
2018-01-25 22:10:34 +08:00
|
|
|
if err := db.GetManager().VersionInfoDao().UpdateModel(version); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-01 13:34:54 +08:00
|
|
|
return nil
|
2018-01-25 22:10:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//CreateUpgradeTaskBody 构造消息体
|
2018-03-01 13:34:54 +08:00
|
|
|
func (i *ImageBuildItem) CreateUpgradeTaskBody() *model.RollingUpgradeTaskBody {
|
2018-01-25 22:10:34 +08:00
|
|
|
return &model.RollingUpgradeTaskBody{
|
2018-03-01 13:34:54 +08:00
|
|
|
TenantID: i.TenantID,
|
|
|
|
ServiceID: i.ServiceID,
|
2018-01-25 22:10:34 +08:00
|
|
|
NewDeployVersion: i.DeployVersion,
|
2018-03-01 13:34:54 +08:00
|
|
|
EventID: i.EventID,
|
2018-01-25 22:10:34 +08:00
|
|
|
}
|
2018-01-29 11:25:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//UpdateVersionInfo 更新任务执行结果
|
|
|
|
func (i *ImageBuildItem) UpdateVersionInfo(status string) error {
|
2018-03-01 13:34:54 +08:00
|
|
|
version, err := db.GetManager().VersionInfoDao().GetVersionByEventID(i.EventID)
|
2018-01-29 11:25:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
version.FinalStatus = status
|
|
|
|
if err := db.GetManager().VersionInfoDao().UpdateModel(version); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2018-03-01 13:34:54 +08:00
|
|
|
}
|