-
Notifications
You must be signed in to change notification settings - Fork 9
/
bootstrap_check.go
109 lines (96 loc) · 3.54 KB
/
bootstrap_check.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (C) INFINI Labs & INFINI LIMITED.
//
// The INFINI Console is offered under the GNU Affero General Public License v3.0
// and as commercial software.
//
// For commercial licensing, contact us at:
// - Website: infinilabs.com
// - Email: [email protected]
//
// Open Source licensed under AGPL V3:
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
/* 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/framework/core/elastic"
"infini.sh/framework/core/env"
"infini.sh/framework/core/global"
"infini.sh/framework/core/util"
)
func bootstrapRequirementCheck() error {
err := checkElasticsearchRequirements()
if err != nil {
return err
}
return nil
}
func checkElasticsearchRequirements() error {
log.Trace("start to check system cluster 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")
}
elasticsearchID := global.Lookup(elastic.GlobalSystemElasticsearchID)
if elasticsearchID == nil || elasticsearchID == "" {
return fmt.Errorf("cluster config in web section can not be empty")
}
esID := elasticsearchID.(string)
var targetEsConfig *elastic.ElasticsearchConfig
for _, esConfig := range esConfigs {
if esConfig.ID == esID || (esConfig.ID == "" && esConfig.Name == esID) {
targetEsConfig = &esConfig
}
}
if targetEsConfig == nil {
return fmt.Errorf("cluster config %s was not found", esID)
}
var req = util.NewGetRequest(targetEsConfig.GetAnyEndpoint(), nil)
if targetEsConfig.BasicAuth != nil {
req.SetBasicAuth(targetEsConfig.BasicAuth.Username, targetEsConfig.BasicAuth.Password.Get())
}
result, err := util.ExecuteRequest(req)
if err != nil {
return fmt.Errorf("check system cluster requirement error: %v", err)
}
if result == nil || result.Body == nil || len(result.Body) == 0 {
return fmt.Errorf("failed to retrive cluster version info")
}
versionNumber, err := jsonparser.GetString(result.Body, "version", "number")
if err != nil {
return fmt.Errorf("check system cluster requirement error: %v, got response: %s", err, string(result.Body))
}
distribution, _ := jsonparser.GetString(result.Body, "version", "distribution")
if distribution == elastic.Easysearch || distribution == elastic.Opensearch {
return nil
} else if distribution != "" {
return fmt.Errorf("unkonw cluster distribution: %v", distribution)
}
cr, err := util.VersionCompare(versionNumber, "5.3")
if err != nil {
return fmt.Errorf("check system cluster requirement error: %v", err)
}
if cr == -1 {
return fmt.Errorf("system cluster version with distribution elasticsearch required to be version 5.3 and above, but got %s", versionNumber)
}
return nil
}