Rainbond/api/handler/service_batch_operation.go

151 lines
5.5 KiB
Go
Raw Normal View History

2019-03-08 19:22:22 +08:00
// RAINBOND, Application Management Platform
// Copyright (C) 2014-2019 Goodrain Co., Ltd.
// 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.
// 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.
// 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 handler
import (
"github.com/goodrain/rainbond/api/model"
"github.com/goodrain/rainbond/api/util"
dbmodel "github.com/goodrain/rainbond/db/model"
2019-03-08 19:22:22 +08:00
gclient "github.com/goodrain/rainbond/mq/client"
)
//BatchOperationHandler batch operation handler
type BatchOperationHandler struct {
mqCli gclient.MQClient
operationHandler *OperationHandler
}
//BatchOperationResult batch operation result
type BatchOperationResult struct {
2019-03-09 11:50:31 +08:00
BatchResult []OperationResult `json:"batche_result"`
2019-03-08 19:22:22 +08:00
}
//CreateBatchOperationHandler create batch operation handler
2019-03-09 13:51:36 +08:00
func CreateBatchOperationHandler(mqCli gclient.MQClient, operationHandler *OperationHandler) *BatchOperationHandler {
2019-03-08 19:22:22 +08:00
return &BatchOperationHandler{
2019-03-09 13:51:36 +08:00
mqCli: mqCli,
operationHandler: operationHandler,
2019-03-08 19:22:22 +08:00
}
}
func setStartupSequenceConfig(configs map[string]string) map[string]string {
if configs == nil {
configs = make(map[string]string, 1)
}
configs["startup_sequence"] = "true"
return configs
}
//Build build
func (b *BatchOperationHandler) Build(buildInfos []model.BuildInfoRequestStruct, tenantID, reqBody string) (re BatchOperationResult) {
2019-03-08 19:22:22 +08:00
var retrys []model.BuildInfoRequestStruct
2019-03-08 19:22:22 +08:00
for _, buildInfo := range buildInfos {
event, err := util.CreateEvent(dbmodel.TargetTypeService, "build-service", buildInfo.ServiceID, tenantID, reqBody, "system", dbmodel.ASYNEVENTTYPE)
if err != nil {
re.BatchResult = append(re.BatchResult, OperationResult{ErrMsg: "create event failure"})
continue
}
buildInfo.EventID = event.EventID
2019-03-08 19:22:22 +08:00
buildInfo.Configs = setStartupSequenceConfig(buildInfo.Configs)
buildre := b.operationHandler.Build(buildInfo)
if buildre.Status != "success" {
retrys = append(retrys, buildInfo)
} else {
re.BatchResult = append(re.BatchResult, buildre)
}
}
for _, retry := range retrys {
re.BatchResult = append(re.BatchResult, b.operationHandler.Build(retry))
}
return
}
//Start batch start
func (b *BatchOperationHandler) Start(startInfos []model.StartOrStopInfoRequestStruct, tenantID, reqBody string) (re BatchOperationResult) {
2019-03-08 19:22:22 +08:00
var retrys []model.StartOrStopInfoRequestStruct
for _, startInfo := range startInfos {
event, err := util.CreateEvent(dbmodel.TargetTypeService, "build-service", startInfo.ServiceID, tenantID, reqBody, "system", dbmodel.ASYNEVENTTYPE)
if err != nil {
re.BatchResult = append(re.BatchResult, OperationResult{ErrMsg: "create event failure"})
continue
}
startInfo.EventID = event.EventID
2019-03-08 19:22:22 +08:00
startInfo.Configs = setStartupSequenceConfig(startInfo.Configs)
2019-03-09 13:51:36 +08:00
startre := b.operationHandler.Start(startInfo)
2019-03-08 19:22:22 +08:00
if startre.Status != "success" {
retrys = append(retrys, startInfo)
} else {
re.BatchResult = append(re.BatchResult, startre)
}
}
for _, retry := range retrys {
2019-03-09 13:51:36 +08:00
re.BatchResult = append(re.BatchResult, b.operationHandler.Start(retry))
2019-03-08 19:22:22 +08:00
}
return
}
//Stop batch stop
func (b *BatchOperationHandler) Stop(stopInfos []model.StartOrStopInfoRequestStruct, tenantID, reqBody string) (re BatchOperationResult) {
2019-03-08 19:22:22 +08:00
var retrys []model.StartOrStopInfoRequestStruct
for _, stopInfo := range stopInfos {
event, err := util.CreateEvent(dbmodel.TargetTypeService, "build-service", stopInfo.ServiceID, tenantID, reqBody, "system", dbmodel.ASYNEVENTTYPE)
if err != nil {
re.BatchResult = append(re.BatchResult, OperationResult{ErrMsg: "create event failure"})
continue
}
stopInfo.EventID = event.EventID
2019-03-08 19:22:22 +08:00
stopInfo.Configs = setStartupSequenceConfig(stopInfo.Configs)
2019-03-09 13:51:36 +08:00
stopre := b.operationHandler.Stop(stopInfo)
2019-03-08 19:22:22 +08:00
if stopre.Status != "success" {
retrys = append(retrys, stopInfo)
} else {
re.BatchResult = append(re.BatchResult, stopre)
}
}
for _, retry := range retrys {
2019-03-09 13:51:36 +08:00
re.BatchResult = append(re.BatchResult, b.operationHandler.Stop(retry))
2019-03-08 19:22:22 +08:00
}
return
}
//Upgrade batch upgrade
func (b *BatchOperationHandler) Upgrade(upgradeInfos []model.UpgradeInfoRequestStruct, tenantID, reqbody string) (re BatchOperationResult) {
2019-03-08 19:22:22 +08:00
var retrys []model.UpgradeInfoRequestStruct
for _, upgradeInfo := range upgradeInfos {
event, err := util.CreateEvent(dbmodel.TargetTypeService, "build-service", upgradeInfo.ServiceID, tenantID, reqbody, "system", dbmodel.ASYNEVENTTYPE)
if err != nil {
re.BatchResult = append(re.BatchResult, OperationResult{ErrMsg: "create event failure"})
continue
}
upgradeInfo.EventID = event.EventID
2019-03-08 19:22:22 +08:00
upgradeInfo.Configs = setStartupSequenceConfig(upgradeInfo.Configs)
stopre := b.operationHandler.Upgrade(upgradeInfo)
if stopre.Status != "success" {
retrys = append(retrys, upgradeInfo)
} else {
re.BatchResult = append(re.BatchResult, stopre)
}
}
for _, retry := range retrys {
re.BatchResult = append(re.BatchResult, b.operationHandler.Upgrade(retry))
}
return
}