From fefcaf6b6de06db74213916cdfad9ae9cf7b315a Mon Sep 17 00:00:00 2001 From: liugq Date: Fri, 1 Apr 2022 17:08:47 +0800 Subject: [PATCH] add elasticsearch version check --- env_check.go | 88 ++++++++++++++++++++++++++++++++++ main.go | 5 ++ plugin/api/gateway/instance.go | 7 ++- 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 env_check.go diff --git a/env_check.go b/env_check.go new file mode 100644 index 00000000..3bcbbca6 --- /dev/null +++ b/env_check.go @@ -0,0 +1,88 @@ +/* Copyright © INFINI Ltd. All rights reserved. + * web: https://infinilabs.com + * mail: hello#infini.ltd */ + +package main + +import ( + "fmt" + "github.com/buger/jsonparser" + log "github.com/cihub/seelog" + "infini.sh/console/config" + "infini.sh/framework/core/elastic" + "infini.sh/framework/core/env" + "infini.sh/framework/core/util" + "io/ioutil" + "net/http" +) + +func bootstrapRequirementCheck() error{ + err := checkElasticsearchRequire() + if err != nil { + return err + } + return nil +} + + +func checkElasticsearchRequire() error{ + log.Trace("start to check elasticsearch requirement") + var esConfigs = []elastic.ElasticsearchConfig{} + ok, err := env.ParseConfig("elasticsearch", &esConfigs) + if err != nil { + return fmt.Errorf("parse elasticsearch config section error: %v", err) + } + if !ok { + return fmt.Errorf("elasticsearch config section not found") + } + appConfig = &config.AppConfig{ + Elasticsearch: "default", + } + ok, err = env.ParseConfig("web", appConfig) + if err != nil { + return fmt.Errorf("parse web config section error: %v", err) + } + if !ok { + return fmt.Errorf("web config section not found") + } + if appConfig.Elasticsearch == "" { + return fmt.Errorf("elasticsearch config of web section can not be empty") + } + var targetEsConfig *elastic.ElasticsearchConfig + for _, esConfig := range esConfigs { + if esConfig.Name == appConfig.Elasticsearch { + targetEsConfig = &esConfig + } + } + if targetEsConfig == nil { + return fmt.Errorf("elasticsearch config named %s not found", appConfig.Elasticsearch) + } + req, err := http.NewRequest(http.MethodGet, targetEsConfig.Endpoint, nil) + if err != nil { + return fmt.Errorf("new request error: %v", err) + } + if targetEsConfig.BasicAuth != nil { + req.SetBasicAuth(targetEsConfig.BasicAuth.Username, targetEsConfig.BasicAuth.Password) + } + res, err := http.DefaultClient.Do(req) + if err != nil { + return fmt.Errorf("check elasticsearch requirement error: %v", err) + } + defer res.Body.Close() + resBytes, err := ioutil.ReadAll(res.Body) + if err != nil { + return fmt.Errorf("check elasticsearch requirement error: %v", err) + } + versionNumber, err := jsonparser.GetString(resBytes, "version", "number") + if err != nil { + return fmt.Errorf("check elasticsearch requirement error: %v", err) + } + cr, err := util.VersionCompare(versionNumber, "7.3") + if err !=nil { + return fmt.Errorf("check elasticsearch requirement error: %v", err) + } + if cr == -1 { + return fmt.Errorf("elasticsearch cluster version of store data required to be version 7.0 and above, but got %s", versionNumber) + } + return nil +} \ No newline at end of file diff --git a/main.go b/main.go index b763f87f..de9f289e 100644 --- a/main.go +++ b/main.go @@ -57,6 +57,11 @@ func main() { api := api2.GatewayAPI{} if app.Setup(func() { + err := bootstrapRequirementCheck() + if err !=nil{ + panic(err) + } + //load core modules first module.RegisterSystemModule(&elastic2.ElasticModule{}) diff --git a/plugin/api/gateway/instance.go b/plugin/api/gateway/instance.go index 8629fc07..090b809b 100644 --- a/plugin/api/gateway/instance.go +++ b/plugin/api/gateway/instance.go @@ -229,7 +229,12 @@ func (h *GatewayAPI) getInstanceStatus(w http.ResponseWriter, req *http.Request, continue } var resMap = util.MapStr{} - util.MustFromJSONBytes(res.Body, &resMap) + err = util.FromJSONBytes(res.Body, &resMap) + if err != nil { + result[gid.(string)] = util.MapStr{} + log.Error(err) + continue + } result[gid.(string)] = resMap }