Rainbond/api/handler/service_check.go

103 lines
3.2 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 handler
import (
"context"
2018-01-23 15:58:10 +08:00
"fmt"
2018-12-08 12:58:40 +08:00
"strings"
2018-12-04 18:08:51 +08:00
"time"
api_model "github.com/goodrain/rainbond/api/model"
"github.com/goodrain/rainbond/api/util"
2018-12-04 18:08:51 +08:00
"github.com/goodrain/rainbond/builder/exector"
client "github.com/goodrain/rainbond/mq/client"
tutil "github.com/goodrain/rainbond/util"
2020-01-18 21:26:46 +08:00
"github.com/pquerna/ffjson/ffjson"
2020-11-25 16:39:38 +08:00
"github.com/sirupsen/logrus"
"github.com/twinj/uuid"
)
//ServiceCheck check service build source
2018-01-22 18:52:51 +08:00
func (s *ServiceAction) ServiceCheck(scs *api_model.ServiceCheckStruct) (string, string, *util.APIHandleError) {
checkUUID := uuid.NewV4().String()
2018-01-22 18:52:51 +08:00
scs.Body.CheckUUID = checkUUID
if scs.Body.EventID == "" {
scs.Body.EventID = tutil.NewUUID()
2018-01-22 18:00:45 +08:00
}
topic := client.BuilderTopic
2018-12-08 12:58:40 +08:00
if tutil.StringArrayContains(s.conf.EnableFeature, "windows") {
if scs.Body.CheckOS == "windows" {
topic = client.WindowsBuilderTopic
}
if scs.Body.SourceType == "docker-run" || scs.Body.SourceType == "docker-compose" {
2018-12-09 14:40:28 +08:00
if maybeIsWindowsContainerImage(scs.Body.SourceBody) {
2018-12-08 12:58:40 +08:00
topic = client.WindowsBuilderTopic
}
}
}
2018-12-04 18:08:51 +08:00
err := s.MQClient.SendBuilderTopic(client.TaskStruct{
TaskType: "service_check",
2018-12-04 18:08:51 +08:00
TaskBody: scs.Body,
Topic: topic,
2018-12-04 18:08:51 +08:00
})
if err != nil {
logrus.Errorf("enqueue service check message to mq error, %v", err)
2018-01-22 18:52:51 +08:00
return "", "", util.CreateAPIHandleError(500, err)
}
2018-01-22 18:52:51 +08:00
return checkUUID, scs.Body.EventID, nil
}
2018-01-23 15:58:10 +08:00
2018-12-09 14:40:28 +08:00
var windowsKeywords = []string{"windows", "asp", "microsoft", "nanoserver"}
func maybeIsWindowsContainerImage(source string) bool {
for _, k := range windowsKeywords {
if strings.Contains(source, k) {
return true
}
}
return false
}
//GetServiceCheckInfo get application source detection information
2018-12-04 18:08:51 +08:00
func (s *ServiceAction) GetServiceCheckInfo(uuid string) (*exector.ServiceCheckResult, *util.APIHandleError) {
2018-01-23 15:58:10 +08:00
k := fmt.Sprintf("/servicecheck/%s", uuid)
2018-02-05 19:30:44 +08:00
var si exector.ServiceCheckResult
2018-12-04 18:08:51 +08:00
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
2020-01-18 21:26:46 +08:00
defer cancel()
2018-01-23 15:58:10 +08:00
resp, err := s.EtcdCli.Get(ctx, k)
if err != nil {
logrus.Errorf("get etcd k %s error, %v", k, err)
return nil, util.CreateAPIHandleError(500, err)
}
if resp.Count == 0 {
2018-02-05 19:30:44 +08:00
return &si, nil
2018-01-23 15:58:10 +08:00
}
v := resp.Kvs[0].Value
if err := ffjson.Unmarshal(v, &si); err != nil {
return nil, util.CreateAPIHandleError(500, err)
}
2018-02-06 17:41:47 +08:00
if si.CheckStatus == "" {
si.CheckStatus = "Checking"
2018-02-07 09:58:55 +08:00
logrus.Debugf("checking is %v", si)
2018-02-06 17:41:47 +08:00
}
2018-12-04 18:08:51 +08:00
return &si, nil
}