Rainbond/builder/exector/share_image.go

144 lines
5.4 KiB
Go
Raw Normal View History

2018-03-14 14:12:26 +08:00
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
2018-03-14 14:33:31 +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
// 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
// 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 (
2018-03-04 22:48:50 +08:00
"context"
2018-02-07 11:11:35 +08:00
"fmt"
2020-04-15 14:00:16 +08:00
"github.com/goodrain/rainbond/builder"
2018-03-04 22:48:50 +08:00
"github.com/coreos/etcd/clientv3"
2018-12-07 14:24:14 +08:00
"github.com/docker/docker/client"
"github.com/goodrain/rainbond/builder/sources"
"github.com/goodrain/rainbond/event"
2018-03-04 22:48:50 +08:00
"github.com/pquerna/ffjson/ffjson"
2020-11-25 16:39:38 +08:00
"github.com/sirupsen/logrus"
)
//ImageShareItem ImageShareItem
type ImageShareItem struct {
2020-04-15 14:00:16 +08:00
Namespace string `json:"namespace"`
TenantName string `json:"tenant_name"`
ServiceID string `json:"service_id"`
ServiceAlias string `json:"service_alias"`
ImageName string `json:"image_name"`
LocalImageName string `json:"local_image_name"`
LocalImageUsername string `json:"-"`
LocalImagePassword string `json:"-"`
ShareID string `json:"share_id"`
Logger event.Logger
ShareInfo struct {
2018-03-04 22:48:50 +08:00
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"`
ImageInfo struct {
HubURL string `json:"hub_url"`
HubUser string `json:"hub_user"`
HubPassword string `json:"hub_password"`
Namespace string `json:"namespace"`
IsTrust bool `json:"is_trust,omitempty"`
2018-03-04 22:48:50 +08:00
} `json:"image_info,omitempty"`
} `json:"share_info"`
DockerClient *client.Client
EtcdCli *clientv3.Client
}
//NewImageShareItem 创建实体
2018-03-04 22:48:50 +08:00
func NewImageShareItem(in []byte, DockerClient *client.Client, EtcdCli *clientv3.Client) (*ImageShareItem, error) {
var isi ImageShareItem
if err := ffjson.Unmarshal(in, &isi); err != nil {
return nil, err
2018-02-06 17:41:47 +08:00
}
2020-04-15 14:00:16 +08:00
isi.LocalImageUsername = builder.REGISTRYUSER
isi.LocalImagePassword = builder.REGISTRYPASS
2018-03-04 22:48:50 +08:00
eventID := isi.ShareInfo.EventID
isi.Logger = event.GetManager().GetLogger(eventID)
isi.DockerClient = DockerClient
isi.EtcdCli = EtcdCli
return &isi, nil
}
2018-03-04 22:48:50 +08:00
//ShareService ShareService
func (i *ImageShareItem) ShareService() error {
hubuser, hubpass := builder.GetImageUserInfoV2(i.LocalImageName, i.LocalImageUsername, i.LocalImagePassword)
2020-04-16 14:56:37 +08:00
_, err := sources.ImagePull(i.DockerClient, i.LocalImageName, hubuser, hubpass, i.Logger, 20)
2018-02-07 11:11:35 +08:00
if err != nil {
2018-03-04 22:48:50 +08:00
logrus.Errorf("pull image %s error: %s", i.LocalImageName, err.Error())
i.Logger.Error(fmt.Sprintf("拉取应用镜像: %s失败", i.LocalImageName), map[string]string{"step": "builder-exector", "status": "failure"})
2018-02-07 11:11:35 +08:00
return err
}
2018-03-04 22:48:50 +08:00
if err := sources.ImageTag(i.DockerClient, i.LocalImageName, i.ImageName, i.Logger, 1); err != nil {
2018-02-07 11:11:35 +08:00
logrus.Errorf("change image tag error: %s", err.Error())
2018-03-04 22:48:50 +08:00
i.Logger.Error(fmt.Sprintf("修改镜像tag: %s -> %s 失败", i.LocalImageName, i.ImageName), map[string]string{"step": "builder-exector", "status": "failure"})
2018-02-07 11:11:35 +08:00
return err
}
user, pass := builder.GetImageUserInfoV2(i.ImageName, i.ShareInfo.ImageInfo.HubUser, i.ShareInfo.ImageInfo.HubPassword)
if i.ShareInfo.ImageInfo.IsTrust {
2020-04-16 18:51:00 +08:00
err = sources.TrustedImagePush(i.DockerClient, i.ImageName, user, pass, i.Logger, 30)
} else {
2020-04-16 18:51:00 +08:00
err = sources.ImagePush(i.DockerClient, i.ImageName, user, pass, i.Logger, 30)
2018-03-04 22:48:50 +08:00
}
2018-02-07 11:11:35 +08:00
if err != nil {
if err.Error() == "authentication required" {
i.Logger.Error("镜像仓库授权失败", map[string]string{"step": "builder-exector", "status": "failure"})
return err
}
2018-02-07 11:11:35 +08:00
logrus.Errorf("push image into registry error: %s", err.Error())
2018-03-04 22:48:50 +08:00
i.Logger.Error("推送镜像至镜像仓库失败", map[string]string{"step": "builder-exector", "status": "failure"})
2018-02-07 11:11:35 +08:00
return err
}
return nil
}
2018-03-04 22:48:50 +08:00
//ShareStatus share status result
//ShareStatus share status result
type ShareStatus struct {
ShareID string `json:"share_id,omitempty"`
Status string `json:"status,omitempty"`
}
func (s ShareStatus) String() string {
b, _ := ffjson.Marshal(s)
return string(b)
2018-02-07 11:11:35 +08:00
}
2018-02-07 16:10:26 +08:00
//UpdateShareStatus 更新任务执行结果
func (i *ImageShareItem) UpdateShareStatus(status string) error {
2018-03-04 22:48:50 +08:00
var ss = ShareStatus{
ShareID: i.ShareID,
Status: status,
2018-02-07 16:10:26 +08:00
}
2018-03-04 22:48:50 +08:00
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, err := i.EtcdCli.Put(ctx, fmt.Sprintf("/rainbond/shareresult/%s", i.ShareID), ss.String())
if err != nil {
logrus.Errorf("put shareresult %s into etcd error, %v", i.ShareID, err)
i.Logger.Error("存储分享结果失败。", map[string]string{"step": "callback", "status": "failure"})
}
if status == "success" {
i.Logger.Info("创建分享结果成功,分享成功", map[string]string{"step": "last", "status": "success"})
} else {
i.Logger.Info("创建分享结果成功,分享失败", map[string]string{"step": "callback", "status": "failure"})
2018-02-07 16:10:26 +08:00
}
return nil
2018-03-04 22:48:50 +08:00
}