Rainbond/pkg/builder/exector/share_slug.go

200 lines
6.6 KiB
Go
Raw Normal View History

// RAINBOND, Application Management Platform
// Copyright (C) 2014-2017 Goodrain Co., Ltd.
2018-03-04 22:48:50 +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-04 22:48:50 +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-04 22:48:50 +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-04 22:48:50 +08:00
"io/ioutil"
"os"
2018-02-26 12:54:32 +08:00
"os/exec"
2018-03-04 22:48:50 +08:00
"strings"
"github.com/Sirupsen/logrus"
"github.com/coreos/etcd/clientv3"
"github.com/goodrain/rainbond/pkg/builder/sources"
2018-02-07 16:10:26 +08:00
"github.com/goodrain/rainbond/pkg/db"
dbmodel "github.com/goodrain/rainbond/pkg/db/model"
2018-03-04 22:48:50 +08:00
"github.com/goodrain/rainbond/pkg/event"
"github.com/pquerna/ffjson/ffjson"
)
//SlugShareItem SlugShareItem
type SlugShareItem struct {
2018-03-04 22:48:50 +08:00
Namespace string `json:"namespace"`
TenantName string `json:"tenant_name"`
ServiceID string `json:"service_id"`
ServiceAlias string `json:"service_alias"`
SlugPath string `json:"slug_path"`
LocalSlugPath string `json:"local_slug_path"`
ShareID string `json:"share_id"`
Logger event.Logger
ShareInfo struct {
ServiceKey string `json:"service_key" `
AppVersion string `json:"app_version" `
EventID string `json:"event_id"`
ShareUser string `json:"share_user"`
ShareScope string `json:"share_scope"`
SlugInfo struct {
Namespace string `json:"namespace"`
FTPHost string `json:"ftp_host"`
FTPPort string `json:"ftp_port"`
FTPUser string `json:"ftp_user"`
FTPPassword string `json:"ftp_password"`
} `json:"slug_info,omitempty"`
} `json:"share_info"`
EtcdCli *clientv3.Client
PackageName string
}
//NewSlugShareItem 创建实体
2018-03-04 22:48:50 +08:00
func NewSlugShareItem(in []byte, etcdCli *clientv3.Client) (*SlugShareItem, error) {
var ssi SlugShareItem
if err := ffjson.Unmarshal(in, &ssi); err != nil {
return nil, err
}
eventID := ssi.ShareInfo.EventID
ssi.Logger = event.GetManager().GetLogger(eventID)
ssi.EtcdCli = etcdCli
return &ssi, nil
}
//Run Run
2018-03-04 22:48:50 +08:00
func (i *SlugShareItem) ShareService() error {
logrus.Debugf("分享应用,数据中心文件路径: %s ,分享目标路径 %s", i.LocalSlugPath, i.SlugPath)
if _, err := os.Stat(i.LocalSlugPath); err != nil {
i.Logger.Error(fmt.Sprintf("数据中心应用代码包不存在,请先构建应用"), map[string]string{"step": "slug-share", "status": "failure"})
return err
}
shareTag := true
2018-03-04 22:48:50 +08:00
if i.ShareInfo.SlugInfo.FTPHost != "" && i.ShareInfo.SlugInfo.FTPPort != "" {
//share YS
2018-03-04 22:48:50 +08:00
if err := i.ShareToFTP(); err != nil {
return err
}
shareTag = false
2018-03-04 22:48:50 +08:00
} else {
if err := i.ShareToLocal(); err != nil {
return err
}
}
return nil
}
func createMD5(packageName string) (string, error) {
2018-03-04 22:48:50 +08:00
md5Path := packageName + ".md5"
_, err := os.Stat(md5Path)
if err == nil {
//md5 file exist
return md5Path, nil
}
2018-02-26 12:54:32 +08:00
f, err := exec.Command("md5sum", packageName).Output()
if err != nil {
return "", err
2018-02-26 12:08:33 +08:00
}
2018-02-27 13:41:07 +08:00
md5In := strings.Split(string(f), "")
2018-02-26 12:54:32 +08:00
logrus.Debugf("md5 value is %s", string(f))
2018-02-27 13:41:07 +08:00
if err := ioutil.WriteFile(md5Path, []byte(md5In[0]), 0644); err != nil {
return "", err
}
return md5Path, nil
}
2018-03-04 22:48:50 +08:00
//ShareToFTP ShareToFTP
func (i *SlugShareItem) ShareToFTP() error {
file := i.LocalSlugPath
i.Logger.Info("开始分享云帮", map[string]string{"step": "slug-share"})
md5, err := createMD5(file)
if err != nil {
2018-03-04 22:48:50 +08:00
i.Logger.Error("生成md5失败", map[string]string{"step": "slug-share", "status": "failure"})
}
logrus.Debugf("md5 path is %s", md5)
2018-03-04 22:48:50 +08:00
localPath := fmt.Sprintf("%s%s/", i.FTPConf.LocalNamespace, i.ServiceKey)
_, err = exec.Command("cp", "rf", file+"*", localPath).Output()
if err != nil {
2018-03-04 22:48:50 +08:00
i.Logger.Info("分享云帮失败", map[string]string{"step": "callback", "status": "failure"})
return err
}
2018-03-04 22:48:50 +08:00
i.Logger.Info("分享云帮完成", map[string]string{"step": "slug-share", "status": "success"})
return nil
}
2018-03-04 22:48:50 +08:00
//ShareToLocal ShareToLocal
func (i *SlugShareItem) ShareToLocal() error {
file := i.LocalSlugPath
i.Logger.Info("开始分享应用到本地目录", map[string]string{"step": "slug-share"})
2018-02-06 17:41:47 +08:00
md5, err := createMD5(file)
if err != nil {
2018-03-04 22:48:50 +08:00
i.Logger.Error("生成md5失败", map[string]string{"step": "slug-share", "status": "success"})
2018-02-06 17:41:47 +08:00
}
2018-03-04 22:48:50 +08:00
if err := sources.CopyFileWithProgress(i.LocalSlugPath, i.SlugPath, i.Logger); err != nil {
os.Remove(i.SlugPath)
logrus.Errorf("copy file to share path error: %s", err.Error())
i.Logger.Error("复制文件失败", map[string]string{"step": "slug-share", "status": "failure"})
2018-02-06 17:41:47 +08:00
return err
}
2018-03-04 22:48:50 +08:00
if err := sources.CopyFileWithProgress(md5, i.SlugPath+".md5", i.Logger); err != nil {
os.Remove(i.SlugPath)
os.Remove(i.SlugPath + ".md5")
logrus.Errorf("copy file to share path error: %s", err.Error())
i.Logger.Error("复制md5文件失败", map[string]string{"step": "slug-share", "status": "failure"})
return err
}
i.Logger.Info("分享数据中心本地完成", map[string]string{"step": "slug-share", "status": "success"})
return nil
}
2018-02-06 17:41:47 +08:00
//UploadFtp UploadFt
2018-03-04 22:48:50 +08:00
func (i *SlugShareItem) UploadFtp(path, file, md5 string) error {
i.Logger.Info(fmt.Sprintf("开始上传代码包: %s", file), map[string]string{"step": "slug-share"})
ftp := sources.NewFTPConnManager(i.Logger, i.ShareInfo.SlugInfo.FTPUser, i.ShareInfo.SlugInfo.FTPPassword, i.ShareInfo.SlugInfo.FTPHost, i.ShareInfo.SlugInfo.FTPPort)
2018-02-27 12:41:01 +08:00
if err := ftp.FTPLogin(i.Logger); err != nil {
2018-02-08 10:36:22 +08:00
return err
}
2018-02-27 12:41:01 +08:00
defer ftp.FTP.Close()
curPath, err := ftp.FTPCWD(i.Logger, path)
2018-02-08 10:36:22 +08:00
if err != nil {
return err
}
2018-02-27 12:41:01 +08:00
if err := ftp.FTPUpload(i.Logger, curPath, file, md5); err != nil {
2018-02-26 10:46:37 +08:00
return err
2018-02-08 10:36:22 +08:00
}
2018-03-04 22:48:50 +08:00
i.Logger.Info("代码包上传完成", map[string]string{"step": "slug-share", "status": "success"})
return nil
2018-02-07 16:10:26 +08:00
}
//UpdateShareStatus 更新任务执行结果
func (i *SlugShareItem) UpdateShareStatus(status string) error {
result := &dbmodel.AppPublish{
ServiceKey: i.ServiceKey,
AppVersion: i.AppVersion,
2018-03-04 22:48:50 +08:00
Slug: i.PackageName,
Status: status,
2018-02-07 16:10:26 +08:00
}
if err := db.GetManager().AppPublishDao().AddModel(result); err != nil {
return err
}
return nil
2018-03-04 22:48:50 +08:00
}
//CheckMD5FileExist CheckMD5FileExist
2018-03-04 22:48:50 +08:00
func (i *SlugShareItem) CheckMD5FileExist(md5path, packageName string) bool {
return false
2018-03-04 22:48:50 +08:00
}