2018-03-14 14:12:26 +08:00
|
|
|
|
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
|
2018-03-05 17:40:56 +08:00
|
|
|
|
// RAINBOND, Application Management Platform
|
2018-03-14 14:33:31 +08:00
|
|
|
|
|
2018-03-05 17:40:56 +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-03-05 17:40:56 +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-03-05 17:40:56 +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"
|
|
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2018-11-16 18:16:55 +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/event"
|
2018-03-05 17:40:56 +08:00
|
|
|
|
"github.com/pquerna/ffjson/ffjson"
|
2018-11-16 18:16:55 +08:00
|
|
|
|
|
2018-03-05 17:40:56 +08:00
|
|
|
|
//"github.com/docker/docker/api/types"
|
|
|
|
|
|
|
|
|
|
//"github.com/docker/docker/client"
|
|
|
|
|
|
2018-04-24 16:44:59 +08:00
|
|
|
|
"github.com/goodrain/rainbond/db"
|
|
|
|
|
dbmodel "github.com/goodrain/rainbond/db/model"
|
2018-03-05 17:40:56 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//MarketSlugItem MarketSlugItem
|
|
|
|
|
type MarketSlugItem struct {
|
|
|
|
|
TenantName string `json:"tenant_name"`
|
|
|
|
|
ServiceAlias string `json:"service_alias"`
|
|
|
|
|
Logger event.Logger `json:"logger"`
|
|
|
|
|
EventID string `json:"event_id"`
|
|
|
|
|
Operator string `json:"operator"`
|
|
|
|
|
DeployVersion string `json:"deploy_version"`
|
|
|
|
|
TenantID string `json:"tenant_id"`
|
|
|
|
|
ServiceID string `json:"service_id"`
|
2018-12-04 13:43:15 +08:00
|
|
|
|
Action string `json:"action"`
|
2018-03-05 17:40:56 +08:00
|
|
|
|
TGZPath string
|
2019-03-08 19:22:22 +08:00
|
|
|
|
Configs map[string]string `json:"configs"`
|
2018-03-05 17:40:56 +08:00
|
|
|
|
SlugInfo struct {
|
|
|
|
|
SlugPath string `json:"slug_path"`
|
|
|
|
|
FTPHost string `json:"ftp_host"`
|
|
|
|
|
FTPPort string `json:"ftp_port"`
|
2018-03-08 22:36:07 +08:00
|
|
|
|
FTPUser string `json:"ftp_username"`
|
2018-03-05 17:40:56 +08:00
|
|
|
|
FTPPassword string `json:"ftp_password"`
|
|
|
|
|
} `json:"slug_info"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//NewMarketSlugItem 创建实体
|
|
|
|
|
func NewMarketSlugItem(in []byte) (*MarketSlugItem, error) {
|
|
|
|
|
var msi MarketSlugItem
|
|
|
|
|
if err := ffjson.Unmarshal(in, &msi); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
msi.Logger = event.GetManager().GetLogger(msi.EventID)
|
|
|
|
|
msi.TGZPath = fmt.Sprintf("/grdata/build/tenant/%s/slug/%s/%s.tgz", msi.TenantID, msi.ServiceID, msi.DeployVersion)
|
|
|
|
|
return &msi, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Run Run
|
|
|
|
|
func (i *MarketSlugItem) Run() error {
|
|
|
|
|
if i.SlugInfo.FTPHost != "" && i.SlugInfo.FTPPort != "" {
|
2018-03-06 10:52:07 +08:00
|
|
|
|
sFTPClient, err := sources.NewSFTPClient(i.SlugInfo.FTPUser, i.SlugInfo.FTPPassword, i.SlugInfo.FTPHost, i.SlugInfo.FTPPort)
|
|
|
|
|
if err != nil {
|
|
|
|
|
i.Logger.Error("创建FTP客户端失败", map[string]string{"step": "slug-share", "status": "failure"})
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer sFTPClient.Close()
|
|
|
|
|
if err := sFTPClient.DownloadFile(i.SlugInfo.SlugPath, i.TGZPath, i.Logger); err != nil {
|
2018-03-28 21:38:55 +08:00
|
|
|
|
i.Logger.Error("源码包远程FTP获取失败,安装失败", map[string]string{"step": "slug-share", "status": "failure"})
|
2018-03-05 17:40:56 +08:00
|
|
|
|
logrus.Errorf("copy slug file error when build service, %s", err.Error())
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-03-06 10:52:07 +08:00
|
|
|
|
} else {
|
|
|
|
|
if err := sources.CopyFileWithProgress(i.SlugInfo.SlugPath, i.TGZPath, i.Logger); err != nil {
|
2018-03-28 21:38:55 +08:00
|
|
|
|
i.Logger.Error("源码包本地获取失败,安装失败", map[string]string{"step": "slug-share", "status": "failure"})
|
2018-03-06 10:52:07 +08:00
|
|
|
|
logrus.Errorf("copy slug file error when build service, %s", err.Error())
|
2018-03-05 17:40:56 +08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-06 10:52:07 +08:00
|
|
|
|
if err := os.Chown(i.TGZPath, 200, 200); err != nil {
|
|
|
|
|
os.Remove(i.TGZPath)
|
2018-03-28 21:38:55 +08:00
|
|
|
|
i.Logger.Error("源码包本地获取失败,安装失败", map[string]string{"step": "slug-share", "status": "failure"})
|
2018-03-06 10:52:07 +08:00
|
|
|
|
logrus.Errorf("chown slug file error when build service, %s", err.Error())
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-03-05 17:40:56 +08:00
|
|
|
|
i.Logger.Info("应用构建完成", map[string]string{"step": "build-code", "status": "success"})
|
|
|
|
|
vi := &dbmodel.VersionInfo{
|
|
|
|
|
DeliveredType: "slug",
|
|
|
|
|
DeliveredPath: i.TGZPath,
|
|
|
|
|
EventID: i.EventID,
|
|
|
|
|
FinalStatus: "success",
|
|
|
|
|
}
|
|
|
|
|
if err := i.UpdateVersionInfo(vi); err != nil {
|
|
|
|
|
logrus.Errorf("update version info error: %s", err.Error())
|
2018-03-28 21:38:55 +08:00
|
|
|
|
i.Logger.Error("更新应用版本信息失败", map[string]string{"step": "slug-share", "status": "failure"})
|
2018-03-05 17:40:56 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//UpdateVersionInfo 更新任务执行结果
|
|
|
|
|
func (i *MarketSlugItem) UpdateVersionInfo(vi *dbmodel.VersionInfo) error {
|
2018-03-07 15:11:20 +08:00
|
|
|
|
version, err := db.GetManager().VersionInfoDao().GetVersionByDeployVersion(i.DeployVersion, i.ServiceID)
|
2018-03-05 17:40:56 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if vi.DeliveredType != "" {
|
|
|
|
|
version.DeliveredType = vi.DeliveredType
|
|
|
|
|
}
|
|
|
|
|
if vi.DeliveredPath != "" {
|
|
|
|
|
version.DeliveredPath = vi.DeliveredPath
|
|
|
|
|
}
|
|
|
|
|
if vi.EventID != "" {
|
|
|
|
|
version.EventID = vi.EventID
|
|
|
|
|
}
|
|
|
|
|
if vi.FinalStatus != "" {
|
|
|
|
|
version.FinalStatus = vi.FinalStatus
|
|
|
|
|
}
|
2018-11-16 18:16:55 +08:00
|
|
|
|
if vi.DeliveredType == "slug" {
|
|
|
|
|
version.ImageName = builder.RUNNERIMAGENAME
|
|
|
|
|
}
|
2018-03-05 17:40:56 +08:00
|
|
|
|
if err := db.GetManager().VersionInfoDao().UpdateModel(version); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|