├── .idea
├── .gitignore
├── misc.xml
├── modules.xml
├── sr-controller-v1.iml
└── vcs.xml
├── .sr-c42.yaml.swp
├── README.md
├── cluster
├── checkStatus
│ ├── checkBeStatus.go
│ ├── checkFeStatus.go
│ ├── deploySuccess.go
│ ├── getFeEntry.go
│ └── getNodeInfo.go
├── clusterOption
│ ├── deploy.go
│ ├── destroy.go
│ ├── display.go
│ ├── downgrade.go
│ ├── import.go
│ ├── list.go
│ ├── scaleIn.go
│ ├── scaleOut.go
│ ├── start.go
│ ├── stop.go
│ ├── test.go
│ └── upgrade.go
├── destroyCluster
│ └── destroyCluster.go
├── displayCluster
│ └── clusterStatus.go
├── downgradeCluster
│ ├── downgradeBe.go
│ └── downgradeFe.go
├── importCluster
│ ├── importBe.go
│ └── importFe.go
├── listCluster
│ └── listCluster.go
├── modifyConfig
│ └── modifyConfig.go
├── parseConfig
│ └── .getConfig.go.swp
├── prepareOption
│ ├── createDir.go
│ ├── deploySr.go
│ ├── downloadPkg.go
│ └── preCheck.go
├── scaleOut.bak
│ ├── .scaleFe.go.swp
│ └── scaleOutFe.go
├── startCluster
│ ├── .startFe.go.swp
│ ├── initBe.go
│ ├── initFe.go
│ ├── startBe.go
│ └── startFe.go
├── stopCluster
│ ├── stopBe.go
│ └── stopFe.go
└── upgradeCluster
│ ├── upgradeBe.go
│ └── upgradeFe.go
├── deploy-template.yaml
├── importTest.yaml
├── main.go
├── module
├── .yamlConfigModule.go.swp
└── yamlConfigModule.go
├── playground
├── generateConf.go
├── playground.go
├── precheck.go
├── prepareDir.go
├── startBe.go
└── startFe.go
├── repo.yaml
├── sr-c1.yaml
├── sr-test.yaml
├── sr-utl
├── dbConnect.go
├── decompress.go
├── dirOption.go
├── download.go
├── message.go
├── modifyConfig.go
├── precheck.go
├── runProcesser.go
├── runProcesser.go.bak
├── sshRun.go
└── sshRun.go.bak
└── stargo-pkg.tar.gz
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 | # Editor-based HTTP Client requests
5 | /httpRequests/
6 | # Datasource local storage ignored files
7 | /dataSources/
8 | /dataSources.local.xml
9 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
31 |
32 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/sr-controller-v1.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.sr-c42.yaml.swp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wangtianyi2004/starrocks-controller/e01b4bf75a2b210e85e4844b127485a2390b6d17/.sr-c42.yaml.swp
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # starrocks-controller
2 |
3 | ## User Guid:
4 | https://in9aoxx754.feishu.cn/docs/doccnAUw7sF3wdul5Sx7fwinDbc
5 | ## Roadmap & Functionality:
6 | https://in9aoxx754.feishu.cn/docs/doccnSn7r3EELoj2b4OsnJCIuFe
7 | ## Test Case:
8 | https://in9aoxx754.feishu.cn/docs/doccn7pX3iEFBZkRWc67LSFJEow
9 |
10 |
--------------------------------------------------------------------------------
/cluster/checkStatus/checkBeStatus.go:
--------------------------------------------------------------------------------
1 | package checkStatus
2 |
3 | import(
4 | "fmt"
5 | "strings"
6 | "strconv"
7 | "stargo/sr-utl"
8 | "stargo/module"
9 | // "database/sql"
10 | )
11 |
12 |
13 | // 哪个大聪明在 2.2 版本改了 show backends
14 | // 我真是谢谢你,艹
15 |
16 | /*
17 | type BeStatusStruct struct{
18 |
19 | BackendId int
20 | Cluster string
21 | IP string
22 | HeartbeatServicePort int
23 | BePort int
24 | HttpPort int
25 | BrpcPort int
26 | LastStartTime sql.NullString
27 | LastHeartbeat sql.NullString
28 | Alive bool
29 | SystemDecommissioned bool
30 | ClusterDecommissioned bool
31 | TabletNum int
32 | DataUsedCapacity string
33 | AvailCapacity string
34 | TotalCapacity sql.NullString
35 | UsedPct string
36 | MaxDiskUsedPct string
37 | ErrMsg sql.NullString
38 | Version sql.NullString
39 | Status sql.NullString
40 | DataTotalCapacity sql.NullString
41 | DataUsedPct sql.NullString
42 |
43 | }
44 | */
45 |
46 |
47 | //var GBeStatArr []BeStatusStruct
48 |
49 | func CheckBePortStatus(beId int) (checkPortRes bool, err error) {
50 |
51 | var infoMess string
52 |
53 | tmpUser := module.GYamlConf.Global.User
54 | tmpKeyRsa := module.GSshKeyRsa
55 | tmpBeHost := module.GYamlConf.BeServers[beId].Host
56 | tmpSshPort := module.GYamlConf.BeServers[beId].SshPort
57 | tmpHeartbeatServicePort := module.GYamlConf.BeServers[beId].HeartbeatServicePort
58 | checkCMD := fmt.Sprintf("netstat -an | grep ':%d ' | grep -v ESTABLISHED", tmpHeartbeatServicePort)
59 |
60 | output, err := utl.SshRun(tmpUser, tmpKeyRsa, tmpBeHost, tmpSshPort, checkCMD)
61 |
62 | if err != nil {
63 | infoMess = fmt.Sprintf("Error in run cmd when check BE port status [BeHost = %s, error = %v]", tmpBeHost, err)
64 | utl.Log("DEBUG", infoMess)
65 | return false, err
66 | }
67 |
68 | if strings.Contains(string(output), ":" + strconv.Itoa(tmpHeartbeatServicePort)) {
69 | infoMess = fmt.Sprintf("Check the BE query port %s:%d run successfully", tmpBeHost, tmpHeartbeatServicePort)
70 | utl.Log("DEBUG", infoMess)
71 | return true, nil
72 | }
73 |
74 | return false, err
75 | }
76 |
77 | /*
78 | func GetBeStatJDBC(beId int) (beStat BeStatusStruct, err error) {
79 |
80 | var infoMess string
81 | var tmpBeStat BeStatusStruct
82 | //GJdbcUser = "root"
83 | //GJdbcPasswd = ""
84 | //GJdbcDb = ""
85 | queryCMD := "show backends"
86 | tmpBeHost := module.GYamlConf.BeServers[beId].Host
87 | tmpHeartbeatServicePort := module.GYamlConf.BeServers[beId].HeartbeatServicePort
88 | rows, err := utl.RunSQL(module.GJdbcUser, module.GJdbcPasswd, module.GFeEntryHost, module.GFeEntryQueryPort, module.GJdbcDb, queryCMD)
89 | if err != nil{
90 | infoMess = fmt.Sprintf("Error in run sql when check BE status: [BeHost = %s, error = %v]", tmpBeHost, err)
91 | utl.Log("DEBUG", infoMess)
92 | return beStat, err
93 | }
94 |
95 |
96 | for rows.Next(){
97 | err = rows.Scan( &tmpBeStat.BackendId,
98 | &tmpBeStat.Cluster,
99 | &tmpBeStat.IP,
100 | &tmpBeStat.HeartbeatServicePort,
101 | &tmpBeStat.BePort,
102 | &tmpBeStat.HttpPort,
103 | &tmpBeStat.BrpcPort,
104 | &tmpBeStat.LastStartTime,
105 | &tmpBeStat.LastHeartbeat,
106 | &tmpBeStat.Alive,
107 | &tmpBeStat.SystemDecommissioned,
108 | &tmpBeStat.ClusterDecommissioned,
109 | &tmpBeStat.TabletNum,
110 | &tmpBeStat.DataUsedCapacity,
111 | &tmpBeStat.AvailCapacity,
112 | &tmpBeStat.TotalCapacity,
113 | &tmpBeStat.UsedPct,
114 | &tmpBeStat.MaxDiskUsedPct,
115 | &tmpBeStat.ErrMsg,
116 | &tmpBeStat.Version,
117 | &tmpBeStat.Status,
118 | &tmpBeStat.DataTotalCapacity,
119 | &tmpBeStat.DataUsedPct)
120 | if err != nil {
121 | infoMess = fmt.Sprintf("Error in scan sql result [BeHost = %s, error = %v]", tmpBeHost, err)
122 | utl.Log("DEBUG", infoMess)
123 | return beStat, err
124 | }
125 |
126 | if string(tmpBeStat.IP) == tmpBeHost && tmpBeStat.HeartbeatServicePort == tmpHeartbeatServicePort {
127 | beStat = tmpBeStat
128 | //GFeStatusArr[feId] = feStat
129 | return beStat, nil
130 | }
131 | }
132 |
133 | return beStat, err
134 | }
135 |
136 | */
137 |
138 |
139 | func GetBeStatJDBC(beId int) (beStatus map[string]string, err error) {
140 |
141 | var infoMess string
142 | //var tmpBeStat map[string]interface{}
143 | var queryCMD string
144 | var tmpBeHost string
145 | var tmpHeartbeatServicePort int
146 |
147 |
148 | queryCMD = "show backends"
149 | tmpBeHost = module.GYamlConf.BeServers[beId].Host
150 | tmpHeartbeatServicePort = module.GYamlConf.BeServers[beId].HeartbeatServicePort
151 |
152 |
153 | rows, err := utl.RunSQL(module.GJdbcUser, module.GJdbcPasswd, module.GFeEntryHost, module.GFeEntryQueryPort, module.GJdbcDb, queryCMD)
154 |
155 |
156 | if err != nil{
157 | infoMess = fmt.Sprintf("Error in run sql when check BE status: [BeHost = %s, error = %v]", tmpBeHost, err)
158 | utl.Log("DEBUG", infoMess)
159 | return beStatus, err
160 | }
161 |
162 |
163 | columns, _ := rows.Columns()
164 | columnLength := len(columns)
165 | cache := make([]interface{}, columnLength)
166 |
167 | for index, _ := range cache {
168 | var tmpVal interface{}
169 | cache[index] = &tmpVal
170 | }
171 |
172 |
173 | for rows.Next(){
174 | err = rows.Scan(cache...)
175 |
176 | if err != nil {
177 | infoMess = fmt.Sprintf("Error in scan sql result [BeHost = %s, error = %v]", tmpBeHost, err)
178 | utl.Log("DEBUG", infoMess)
179 | return beStatus, err
180 | }
181 |
182 | /*
183 | if string(tmpBeStat.IP) == tmpBeHost && tmpBeStat.HeartbeatServicePort == tmpHeartbeatServicePort {
184 | beStat = tmpBeStat
185 | //GFeStatusArr[feId] = feStat
186 | return beStat, nil
187 | }
188 | */
189 |
190 | beStatus = make(map[string]string)
191 | for i, data := range cache {
192 | beStatus[columns[i]] = fmt.Sprintf("%s", *data.(*interface{}))
193 | }
194 |
195 |
196 | hertbeatPort, _ := strconv.Atoi(beStatus["HeartbeatPort"])
197 | if beStatus["IP"] == tmpBeHost && hertbeatPort == tmpHeartbeatServicePort {
198 | return beStatus, err
199 | }
200 | //statList = append(statList, item)
201 | }
202 |
203 |
204 |
205 |
206 | return beStatus, err
207 |
208 | }
209 |
210 |
211 | func CheckBeStatus(beId int) (beStat map[string]string, err error) {
212 |
213 | var bePortRun bool
214 | bePortRun, err = CheckBePortStatus(beId)
215 |
216 | if bePortRun {
217 | beStat, err = GetBeStatJDBC(beId)
218 | }
219 |
220 | return beStat, err
221 | }
222 |
223 |
224 | func TestBeStatus() {
225 |
226 | module.InitConf("sr-c1", "")
227 | feEntryId, _ := GetFeEntry(-1)
228 | module.SetFeEntry(feEntryId)
229 |
230 | aaa, _ := CheckBeStatus(0)
231 | fmt.Println(aaa)
232 | }
233 |
--------------------------------------------------------------------------------
/cluster/checkStatus/checkFeStatus.go:
--------------------------------------------------------------------------------
1 | package checkStatus
2 |
3 | import (
4 | "fmt"
5 | "strings"
6 | "strconv"
7 | "stargo/sr-utl"
8 | "stargo/module"
9 | //"database/sql"
10 | )
11 |
12 | /*
13 | type FeStatusStruct struct {
14 |
15 | FeName string
16 | FeIp string
17 | FeEditLogPort int
18 | FeHttpPort int
19 | FeQueryPort int
20 | FeRpcPort int
21 | FeRole string
22 | FeIsMaster bool
23 | FeClusterId int
24 | FeJoin bool
25 | FeAlive bool
26 | FeReplayedJournalId int
27 | FeLastHeartbeat sql.NullString
28 | FeIsHelper bool
29 | FeErrMsg string
30 | FeStartTime string
31 | FeVersion sql.NullString
32 |
33 | }
34 | */
35 |
36 |
37 |
38 | //var GFeStatusArr []FeStatusStruct
39 |
40 |
41 | func CheckFePortStatus(feId int) (checkPortRes bool, err error) {
42 |
43 | var infoMess string
44 |
45 | tmpUser := module.GYamlConf.Global.User
46 | tmpKeyRsa := module.GSshKeyRsa
47 | tmpFeHost := module.GYamlConf.FeServers[feId].Host
48 | tmpSshPort := module.GYamlConf.FeServers[feId].SshPort
49 | tmpQueryPort := module.GYamlConf.FeServers[feId].QueryPort
50 |
51 | // check Port stat by [netstat -nltp | grep 9030]
52 | checkCMD := fmt.Sprintf("netstat -an | grep ':%d ' | grep -v ESTABLISHED", tmpQueryPort)
53 | output, err := utl.SshRun(tmpUser, tmpKeyRsa, tmpFeHost, tmpSshPort, checkCMD)
54 |
55 | if err != nil {
56 | infoMess = fmt.Sprintf("Error in run cmd when check FE port status [FeHost = %s, error = %v]", tmpFeHost, err)
57 | utl.Log("DEBUG", infoMess)
58 | return false, err
59 | }
60 |
61 | if strings.Contains(string(output), ":" + strconv.Itoa(tmpQueryPort)) {
62 | infoMess = fmt.Sprintf("Check the fe query port %s:%d run successfully", tmpFeHost, tmpQueryPort)
63 | utl.Log("DEBUG", infoMess)
64 | return true, nil
65 | }
66 |
67 | return false, err
68 |
69 | }
70 |
71 | /*
72 | func GetFeStatJDBC(feId int) (feStat FeStatusStruct, err error) {
73 |
74 | var infoMess string
75 | var tmpFeStat FeStatusStruct
76 | //GJdbcUser = "root"
77 | //GJdbcPasswd = ""
78 | //GJdbcDb = ""
79 | queryCMD := "show frontends"
80 | tmpFeHost := module.GYamlConf.FeServers[feId].Host
81 | tmpQueryPort := module.GYamlConf.FeServers[feId].QueryPort
82 |
83 | rows, err := utl.RunSQL(module.GJdbcUser, module.GJdbcPasswd, tmpFeHost, tmpQueryPort, module.GJdbcDb, queryCMD)
84 | if err != nil{
85 | infoMess = fmt.Sprintf("Error in run sql when check fe status: [FeHost = %s, error = %v]", tmpFeHost, err)
86 | utl.Log("DEBUG", infoMess)
87 | return feStat, err
88 | }
89 |
90 | for rows.Next(){
91 | err = rows.Scan( &tmpFeStat.FeName,
92 | &tmpFeStat.FeIp,
93 | &tmpFeStat.FeEditLogPort,
94 | &tmpFeStat.FeHttpPort,
95 | &tmpFeStat.FeQueryPort,
96 | &tmpFeStat.FeRpcPort,
97 | &tmpFeStat.FeRole,
98 | &tmpFeStat.FeIsMaster,
99 | &tmpFeStat.FeClusterId,
100 | &tmpFeStat.FeJoin,
101 | &tmpFeStat.FeAlive,
102 | &tmpFeStat.FeReplayedJournalId,
103 | &tmpFeStat.FeLastHeartbeat,
104 | &tmpFeStat.FeIsHelper,
105 | &tmpFeStat.FeErrMsg,
106 | &tmpFeStat.FeStartTime,
107 | &tmpFeStat.FeVersion)
108 | if err != nil {
109 | infoMess = fmt.Sprintf("Error in scan sql result [FeHost = %s, error = %v]", tmpFeHost, err)
110 | utl.Log("DEBUG", infoMess)
111 | return feStat, err
112 | }
113 |
114 | if string(tmpFeStat.FeIp) == tmpFeHost && tmpFeStat.FeQueryPort == tmpQueryPort {
115 | feStat = tmpFeStat
116 | //GFeStatusArr[feId] = feStat
117 | return feStat, nil
118 | }
119 |
120 | }
121 |
122 | return feStat, err
123 | }
124 | */
125 |
126 | func GetFeStatJDBC(feId int) (feStat map[string]string, err error) {
127 |
128 | var infoMess string
129 | var tmpFeStat map[string]string
130 | var feStatus map[string]string
131 | //GJdbcUser = "root"
132 | //GJdbcPasswd = ""
133 | //GJdbcDb = ""
134 | queryCMD := "show frontends"
135 | tmpFeHost := module.GYamlConf.FeServers[feId].Host
136 | tmpQueryPort := module.GYamlConf.FeServers[feId].QueryPort
137 |
138 | rows, err := utl.RunSQL(module.GJdbcUser, module.GJdbcPasswd, tmpFeHost, tmpQueryPort, module.GJdbcDb, queryCMD)
139 | if err != nil{
140 | infoMess = fmt.Sprintf("Error in run sql when check fe status: [FeHost = %s, error = %v]", tmpFeHost, err)
141 | utl.Log("DEBUG", infoMess)
142 | return feStat, err
143 | }
144 |
145 | columns, _ := rows.Columns()
146 | columnLength := len(columns)
147 | cache := make([]interface{}, columnLength)
148 |
149 | for index, _ := range cache {
150 | var tmpVal interface{}
151 | cache[index] = &tmpVal
152 | }
153 |
154 |
155 | for rows.Next(){
156 | err = rows.Scan(cache...)
157 |
158 | if err != nil {
159 | infoMess = fmt.Sprintf("Error in scan sql result [FeHost = %s, error = %v]", tmpFeHost, err)
160 | utl.Log("DEBUG", infoMess)
161 | return feStatus, err
162 | }
163 |
164 | feStatus = make(map[string]string)
165 | for i, data := range cache {
166 | feStatus[columns[i]] = fmt.Sprintf("%s", *data.(*interface{}))
167 | }
168 |
169 | queryPort, _ := strconv.Atoi(feStatus["QueryPort"])
170 | if feStatus["IP"] == tmpFeHost && queryPort == tmpQueryPort {
171 | feStat = tmpFeStat
172 | //GFeStatusArr[feId] = feStat
173 | return feStatus, nil
174 | }
175 |
176 | }
177 |
178 | return feStatus, err
179 | }
180 |
181 |
182 |
183 | func CheckFeStatus(feId int) (feStat map[string]string, err error) {
184 |
185 | //var infoMess string
186 | var fePortRun bool
187 | // CheckFePort
188 | fePortRun, err = CheckFePortStatus(feId)
189 |
190 | // getFeStat by JDBC
191 | if fePortRun {
192 | feStat, err = GetFeStatJDBC(feId)
193 | }
194 | return feStat, err
195 | }
196 |
197 |
198 | func TestFeStatus() {
199 |
200 | module.InitConf("sr-c1", "")
201 | feEntryId, _ := GetFeEntry(-1)
202 | module.SetFeEntry(feEntryId)
203 |
204 | aaa, _ := CheckFeStatus(0)
205 | fmt.Println(aaa)
206 |
207 | }
208 | /*
209 | func CheckFeStatus(feId int, user string, keyRsa string, sshHost string, sshPort int, feQueryPort int) (feStat FeStatusStruct, err error) {
210 |
211 | var infoMess string
212 | var tmpFeStat FeStatusStruct
213 |
214 | // check port stat by [netstat -nltp | grep 9030]
215 | portStat := CheckFePort(feId)
216 |
217 | cmd := fmt.Sprintf("netstat -an | grep ':%d ' | grep -v ESTABLISHED", feQueryPort)
218 | output, err := utl.SshRun(user, keyRsa, sshHost, sshPort, cmd)
219 |
220 | if err != nil {
221 | infoMess = fmt.Sprintf("Error in run cmd when check FE status [FeHost = %s, error = %v]", sshHost, err)
222 | utl.Log("DEBUG", infoMess)
223 | return feStat, err
224 | }
225 |
226 | if !strings.Contains(string(output), ":" + strconv.Itoa(feQueryPort)) {
227 | infoMess = fmt.Sprintf("Check the fe query port %s:%d run failed", sshHost, feQueryPort)
228 | utl.Log("DEBUG", infoMess)
229 | err = errors.New(infoMess)
230 | return feStat, err
231 | }
232 |
233 |
234 |
235 | // check fe status by jdbc (from the master fe node)
236 | //RunSQL(userName string, password string, ip string, port int, dbName string, sqlStat string)(rows *sql.Rows, err error)
237 | feMasterUserName := "root"
238 | feMasterPassword := ""
239 | feMasterIP := module.GYamlConf.FeServers[feId].Host
240 | feMasterQueryPort := module.GYamlConf.FeServers[feId].QueryPort
241 | feMasterDbName := ""
242 | sqlStat := "show frontends"
243 | rows, err := utl.RunSQL(feMasterUserName, feMasterPassword, feMasterIP, feMasterQueryPort, feMasterDbName, sqlStat)
244 | if err != nil{
245 | infoMess = fmt.Sprintf(`Error in run sql when check fe status:
246 | feUserName = %s
247 | fePassword = %s
248 | feIP = %s
249 | queryPort = %d
250 | dbName = %s
251 | sqlStat = %s]
252 | error = %v`,
253 | feMasterUserName, feMasterPassword, feMasterIP, feMasterQueryPort, feMasterDbName, sqlStat, err)
254 | utl.Log("ERROR", infoMess)
255 | return feStat, err
256 | }
257 |
258 | for rows.Next(){
259 | err = rows.Scan( &tmpFeStat.FeName,
260 | &tmpFeStat.FeIp,
261 | &tmpFeStat.FeEditLogPort,
262 | &tmpFeStat.FeHttpPort,
263 | &tmpFeStat.FeQueryPort,
264 | &tmpFeStat.FeRpcPort,
265 | &tmpFeStat.FeRole,
266 | &tmpFeStat.FeIsMaster,
267 | &tmpFeStat.FeClusterId,
268 | &tmpFeStat.FeJoin,
269 | &tmpFeStat.FeAlive,
270 | &tmpFeStat.FeReplayedJournalId,
271 | &tmpFeStat.FeLastHeartbeat,
272 | &tmpFeStat.FeIsHelper,
273 | &tmpFeStat.FeErrMsg,
274 | &tmpFeStat.FeStartTime,
275 | &tmpFeStat.FeVersion)
276 | if err != nil {
277 | infoMess = fmt.Sprintf(`Error in scan sql result:
278 | feUserName = %s
279 | fePassword = %s
280 | feIP = %s
281 | queryPort = %d
282 | dbName = %s
283 | sqlStat = %s]
284 | error = %v`,
285 | feMasterUserName, feMasterPassword, feMasterIP, feMasterQueryPort, feMasterDbName, sqlStat, err)
286 | utl.Log("ERROR", infoMess)
287 | return feStat, err
288 | }
289 | if string(tmpFeStat.FeIp) == sshHost && tmpFeStat.FeQueryPort == feQueryPort {
290 | feStat = tmpFeStat
291 | //GFeStatusArr[feId] = feStat
292 | return feStat, nil
293 | }
294 |
295 | }
296 |
297 | return feStat, err
298 |
299 | }
300 |
301 | */
302 |
303 |
--------------------------------------------------------------------------------
/cluster/checkStatus/deploySuccess.go:
--------------------------------------------------------------------------------
1 | package checkStatus
2 |
3 | import(
4 | "fmt"
5 | )
6 |
7 | func DeploySuccess() {
8 |
9 |
10 | fmt.Printf(`旅行者,当你重新踏上旅途之后,一定要记得旅途本身的意义。
11 | 提瓦特的飞鸟、诗和城邦,女皇、愚人和怪物 ... 都是你旅途的一部分。
12 | 终点并不意味着一切,在抵达终点之前,用你的眼睛,多多观察这个世界吧 ...
13 | REPEST OPENSOURCE ......\n`)
14 | }
15 |
--------------------------------------------------------------------------------
/cluster/checkStatus/getFeEntry.go:
--------------------------------------------------------------------------------
1 | package checkStatus
2 |
3 | import(
4 | "fmt"
5 | "errors"
6 | "strings"
7 | "strconv"
8 | "stargo/module"
9 | "stargo/sr-utl"
10 | )
11 |
12 | func GetFeEntry(blackFeNodeId int) (feEntryId int, err error) {
13 |
14 | // get a usable FE host & query port for checking FE/BE status by [show frontends] & [show backends] command
15 |
16 | var infoMess string
17 |
18 | for i := 0; i < len(module.GYamlConf.FeServers); i++ {
19 | if i == blackFeNodeId {
20 | continue
21 | }
22 |
23 | tmpSshHost := module.GYamlConf.FeServers[i].Host
24 | tmpSshPort := module.GYamlConf.FeServers[i].SshPort
25 | tmpQueryPort := module.GYamlConf.FeServers[i].QueryPort
26 | tmpUser := module.GYamlConf.Global.User
27 | tmpKeyRsa := module.GSshKeyRsa
28 | // check port stat by [netstat -nltp | grep 9030 | grep -v ESTABLISHED]
29 | cmd := fmt.Sprintf("netstat -an | grep ':%d ' | grep -v ESTABLISHED", tmpQueryPort)
30 |
31 | output, err := utl.SshRun(tmpUser, tmpKeyRsa, tmpSshHost, tmpSshPort, cmd)
32 | if err != nil {
33 | infoMess = fmt.Sprintf("Error in get FE entry, checking query port failed. [FeHost = %s, QueryPort = %d, error = %v]", tmpSshHost, tmpQueryPort, err)
34 | utl.Log("DEBUG", infoMess)
35 | }
36 |
37 | if strings.Contains(string(output), ":" + strconv.Itoa(tmpQueryPort)) {
38 | infoMess = fmt.Sprintf("Get a useable FE entry. [FeID = %d, FeHost = %s, QueryPort = %d]", i, tmpSshHost, tmpQueryPort)
39 | utl.Log("DEBUG", infoMess)
40 | return i, nil
41 | }
42 | }
43 |
44 | err = errors.New("There is no useable FE entry.")
45 | return -1, err
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/cluster/checkStatus/getNodeInfo.go:
--------------------------------------------------------------------------------
1 | package checkStatus
2 |
3 | import(
4 | "strings"
5 | "fmt"
6 | "os"
7 | "strconv"
8 | "stargo/module"
9 | "stargo/sr-utl"
10 | )
11 |
12 |
13 | func CheckClusterName(clusterName string) bool {
14 |
15 | clusterPath := fmt.Sprintf("%s/cluster/%s", module.GSRCtlRoot, clusterName)
16 | _, err := os.Stat(clusterPath)
17 | if err != nil {
18 | // the file doesn't exist, the cluster name can be used
19 | return true
20 | }
21 |
22 | return false
23 | }
24 |
25 |
26 | func GetNodeType(nodeId string) (nodeType string, nodeInd int) {
27 |
28 | // FEID: module.GYamlConf.FeServers[i].EditLogPort, module.GYamlConf.FeServers[i].QueryPort
29 | // BEID: module.GYamlConf.BeServers[i].Host, module.GYamlConf.BeServers[i].BePort
30 | var infoMess string
31 | tmpNodeId := strings.Split(nodeId, ":")
32 |
33 | // check FE
34 | for i := 0; i < len(module.GYamlConf.FeServers); i++ {
35 |
36 | if tmpNodeId[0] == module.GYamlConf.FeServers[i].Host &&
37 | tmpNodeId[1] == strconv.Itoa(module.GYamlConf.FeServers[i].EditLogPort) {
38 | nodeType = "FE"
39 | //ip = module.GYamlConf.FeServers[i].Host
40 | //port = tmpNodeId[1] == module.GYamlConf.FeServers[i].EditLogPort
41 | nodeInd = i
42 | break
43 | }
44 | }
45 |
46 | for i := 0; i < len(module.GYamlConf.BeServers); i++ {
47 |
48 | if tmpNodeId[0] == module.GYamlConf.BeServers[i].Host &&
49 | tmpNodeId[1] == strconv.Itoa(module.GYamlConf.BeServers[i].BePort) {
50 | nodeType = "BE"
51 | //ip = module.GYamlConf.BeServers[i].Host
52 | //port = module.GYamlConf.BeServers[i].BePort
53 | nodeInd = i
54 | break
55 | }
56 | }
57 |
58 | infoMess = fmt.Sprintf("Get the node type [nodeid = %s, nodetype = %s, nodeindex = %d]\n", nodeId, nodeType, nodeInd)
59 | utl.Log("DEBUG", infoMess)
60 | return nodeType, nodeInd
61 |
62 | }
63 |
64 |
--------------------------------------------------------------------------------
/cluster/clusterOption/deploy.go:
--------------------------------------------------------------------------------
1 | package clusterOption
2 |
3 | import (
4 |
5 | "fmt"
6 | "stargo/module"
7 | "stargo/cluster/prepareOption"
8 | "stargo/cluster/modifyConfig"
9 | "stargo/cluster/startCluster"
10 |
11 | )
12 |
13 |
14 | // sr-ctl-cluster deploy sr-c1 v2.0.1 /tmp/sr-c1.yaml
15 |
16 | func Deploy(clusterName string, clusterVersion string, metaFile string) {
17 |
18 |
19 | module.InitConf(clusterName, metaFile)
20 | module.SetGlobalVar("GSRVersion", clusterVersion)
21 |
22 | prepareOption.PreCheckSR()
23 | prepareOption.CreateDir()
24 | prepareOption.PrepareSRPkg()
25 | prepareOption.DistributeSrDir()
26 | module.WriteBackMeta(module.GYamlConf, module.GWriteBackMetaPath)
27 |
28 | //### recover.sh ##############################")
29 | modifyConfig.ModifyClusterConfig()
30 |
31 | fmt.Println("############################################# START FE CLUSTER #############################################")
32 | fmt.Println("############################################# START FE CLUSTER #############################################")
33 |
34 | startCluster.InitFeCluster(module.GYamlConf)
35 | fmt.Println("############################################# START BE CLUSTER #############################################")
36 | fmt.Println("############################################# START BE CLUSTER #############################################")
37 | startCluster.InitBeCluster(module.GYamlConf)
38 | //checkStatus.DeploySuccess()
39 |
40 |
41 |
42 | }
43 |
44 |
--------------------------------------------------------------------------------
/cluster/clusterOption/destroy.go:
--------------------------------------------------------------------------------
1 | package clusterOption
2 |
3 |
4 | import(
5 | "stargo/cluster/destroyCluster"
6 | "stargo/module"
7 | "stargo/sr-utl"
8 | "os"
9 | "stargo/cluster/checkStatus"
10 | )
11 |
12 | func Destroy(clusterName string) {
13 |
14 | var infoMess string
15 | module.InitConf(clusterName, "")
16 |
17 |
18 | if checkStatus.CheckClusterName(clusterName) {
19 | infoMess = "Don't find the Cluster " + clusterName
20 | utl.Log("ERROR", infoMess)
21 | os.Exit(1)
22 | }
23 |
24 | Stop(clusterName, module.NULLSTR, module.NULLSTR)
25 | destroyCluster.DestroyCluster(clusterName)
26 | }
27 |
28 |
--------------------------------------------------------------------------------
/cluster/clusterOption/display.go:
--------------------------------------------------------------------------------
1 | package clusterOption
2 |
3 |
4 | import(
5 | "stargo/cluster/displayCluster"
6 | "stargo/cluster/checkStatus"
7 | "stargo/module"
8 | "stargo/sr-utl"
9 | "os"
10 | )
11 |
12 | func Display(clusterName string) {
13 |
14 | var infoMess string
15 | module.InitConf(clusterName, "")
16 |
17 | if checkStatus.CheckClusterName(clusterName) {
18 | infoMess = "Don't find the Cluster " + clusterName
19 | utl.Log("ERROR", infoMess)
20 | os.Exit(1)
21 | }
22 |
23 | if checkStatus.CheckClusterName(clusterName) {
24 | infoMess = "Don't find the Cluster " + clusterName
25 | utl.Log("ERROR", infoMess)
26 | os.Exit(1)
27 | }
28 |
29 |
30 | clusterStatus.ClusterStat(clusterName)
31 | }
32 |
33 |
--------------------------------------------------------------------------------
/cluster/clusterOption/downgrade.go:
--------------------------------------------------------------------------------
1 | package clusterOption
2 |
3 | import(
4 |
5 | "fmt"
6 | "os"
7 | "stargo/module"
8 | "stargo/sr-utl"
9 | "stargo/cluster/checkStatus"
10 | "stargo/cluster/prepareOption"
11 | "stargo/cluster/downgradeCluster"
12 | )
13 |
14 | func Downgrade(clusterName string, clusterVersion string) {
15 |
16 | var infoMess string
17 | //var err error
18 |
19 |
20 | module.InitConf(clusterName, "")
21 | module.SetGlobalVar("GSRVersion", clusterVersion)
22 |
23 | if checkStatus.CheckClusterName(clusterName) {
24 | infoMess = "Don't find the Cluster " + clusterName
25 | utl.Log("ERROR", infoMess)
26 | os.Exit(1)
27 | }
28 |
29 | oldVersion := module.GYamlConf.ClusterInfo.Version
30 | newVersion := clusterVersion
31 | if !(oldVersion > newVersion) {
32 | infoMess = fmt.Sprintf("OldVersion = %s NewVersion = %s, the NewVersion is not higher than OldVersion", oldVersion, newVersion)
33 | utl.Log("ERROR", infoMess)
34 | os.Exit(1)
35 | } else {
36 | infoMess = fmt.Sprintf("Downgrade StarRocks Cluster %s, from version %s to version %s", clusterName, oldVersion, newVersion)
37 | utl.Log("OUTPUT", infoMess)
38 | }
39 |
40 | prepareOption.PrepareSRPkg()
41 | downgradeCluster.DowngradeBeCluster()
42 | downgradeCluster.DowngradeFeCluster()
43 |
44 | module.WriteBackMeta(module.GYamlConf, module.GYamlConf.ClusterInfo.MetaPath)
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/cluster/clusterOption/import.go:
--------------------------------------------------------------------------------
1 | package clusterOption
2 |
3 |
4 | import (
5 |
6 | "fmt"
7 | "time"
8 | "os"
9 | "os/user"
10 | "stargo/module"
11 | "stargo/sr-utl"
12 | "stargo/cluster/checkStatus"
13 | "stargo/cluster/importCluster"
14 |
15 | )
16 |
17 |
18 |
19 | func ImportCluster(clusterName string, metaFile string) {
20 |
21 |
22 | var infoMess string
23 |
24 | osUser, _ := user.Current()
25 | module.GSRCtlRoot = os.Getenv("SRCTLROOT")
26 | if module.GSRCtlRoot == "" {
27 | module.GSRCtlRoot = fmt.Sprintf("%s/.stargo", osUser.HomeDir)
28 | }
29 |
30 |
31 | // check cluster exist
32 | if !checkStatus.CheckClusterName(clusterName) {
33 | infoMess = fmt.Sprintf("Error in importing the cluster. The cluster exist, pls change another namne. [ClusterName = %s, MetaFile = %s]", clusterName, metaFile)
34 | utl.Log("ERROR", infoMess)
35 | os.Exit(1)
36 | }
37 |
38 | // check the metaFile exists
39 | _, err := os.Stat(metaFile)
40 | if err != nil {
41 | // the metafile doesn't exist
42 | infoMess = fmt.Sprintf("Error in importing cluster. The MetaFile doesn't exist. [ClusterName = %s, MetaFile = %s]", clusterName, metaFile)
43 | utl.Log("ERROR", infoMess)
44 | os.Exit(1)
45 | }
46 |
47 | module.InitConf(clusterName, metaFile)
48 | //fmt.Println(module.GYamlConf.FeServers)
49 | feEntryId, err := checkStatus.GetFeEntry(-1)
50 | module.SetFeEntry(feEntryId)
51 | if err != nil {
52 | infoMess = fmt.Sprintf("Error in get FE Entry ID when import cluter info.")
53 | utl.Log("ERROR", infoMess)
54 | }
55 |
56 |
57 | module.GYamlConf.ClusterInfo.User = module.GYamlConf.Global.User
58 | module.GYamlConf.ClusterInfo.CreateDate = time.Unix(time.Now().Unix(), 0,).Format("2006-01-02 15:04:05")
59 | module.GYamlConf.ClusterInfo.MetaPath = module.GWriteBackMetaPath
60 | module.GYamlConf.ClusterInfo.PrivateKey = module.GSshKeyRsa
61 |
62 |
63 | importCluster.GetFeConf()
64 | importCluster.GetBeConf()
65 |
66 | module.WriteBackMeta(module.GYamlConf, module.GYamlConf.ClusterInfo.MetaPath)
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/cluster/clusterOption/list.go:
--------------------------------------------------------------------------------
1 | package clusterOption
2 |
3 | import (
4 | "os"
5 | "os/user"
6 | "fmt"
7 | "stargo/cluster/listCluster"
8 | "stargo/module"
9 | )
10 |
11 | func List() {
12 |
13 | // get sr-ctl root dir
14 | osUser, _ := user.Current()
15 | module.GSRCtlRoot = os.Getenv("SRCTLROOT")
16 | if module.GSRCtlRoot == "" {
17 | module.GSRCtlRoot = fmt.Sprintf("%s/.stargo", osUser.HomeDir)
18 | }
19 |
20 | listCluster.ListCluster()
21 | }
22 |
--------------------------------------------------------------------------------
/cluster/clusterOption/scaleIn.go:
--------------------------------------------------------------------------------
1 | package clusterOption
2 |
3 | import (
4 | "fmt"
5 | "time"
6 | // "strings"
7 | "os"
8 | "stargo/sr-utl"
9 | "stargo/module"
10 | "stargo/cluster/checkStatus"
11 | "stargo/cluster/stopCluster"
12 | )
13 |
14 |
15 | func ScaleIn(clusterName string, nodeId string) {
16 |
17 | var feEntryId int
18 | var err error
19 | var tmpNodeType string
20 | var nid int
21 | var dropCmd string
22 | var infoMess string
23 | var user string
24 | var keyRsa string
25 | var sshHost string
26 | var sshPort int
27 | var beHeartbeatServicePort int
28 | var feDeployDir string
29 | var beDeployDir string
30 |
31 |
32 | module.InitConf(clusterName, "")
33 |
34 |
35 | if checkStatus.CheckClusterName(clusterName) {
36 | infoMess = "Don't find the Cluster " + clusterName
37 | utl.Log("ERROR", infoMess)
38 | os.Exit(1)
39 | }
40 |
41 | tmpNodeType, nid = checkStatus.GetNodeType(nodeId)
42 | feEntryId, err = checkStatus.GetFeEntry(nid)
43 |
44 | if err != nil || feEntryId == -1 {
45 | //infoMess = "All FE nodes are down, please start FE node and display the cluster status again."
46 | //utl.Log("WARN", infoMess)
47 | module.SetFeEntry(0)
48 | } else {
49 | module.SetFeEntry(feEntryId)
50 | }
51 |
52 |
53 | sqlIp := module.GFeEntryHost
54 | sqlPort := module.GFeEntryQueryPort
55 | sqlUserName := "root"
56 | sqlPassword := ""
57 | sqlDbName := ""
58 | user = module.GYamlConf.Global.User
59 | keyRsa = module.GSshKeyRsa
60 |
61 | if tmpNodeType == "FE" {
62 |
63 | // stop fe node first
64 | sshHost = module.GYamlConf.FeServers[nid].Host
65 | sshPort = module.GYamlConf.FeServers[nid].SshPort
66 | feDeployDir = module.GYamlConf.FeServers[nid].DeployDir
67 | err = stopCluster.StopFeNode(user, keyRsa, sshHost, sshPort, feDeployDir)
68 | if err != nil {
69 | infoMess = fmt.Sprintf("Error in stop FE node. [nodeId = %s, error = %v]", nodeId, err)
70 | utl.Log("ERROR", infoMess)
71 | os.Exit(1)
72 | }
73 |
74 | time.Sleep(time.Duration(10) * time.Second)
75 | // drop BE node
76 | dropCmd = fmt.Sprintf("ALTER SYSTEM DROP FOLLOWER '%s'", nodeId)
77 | _, err := utl.RunSQL(sqlUserName, sqlPassword, sqlIp, sqlPort, sqlDbName, dropCmd)
78 |
79 | if err != nil {
80 | infoMess = fmt.Sprintf("Error in scale in FE node. [clusterName = %s, nodeId = %s, error = %s]", clusterName, nodeId, err)
81 | utl.Log("ERROR", infoMess)
82 | }
83 |
84 |
85 | // remove FE dir: data, deploy, log
86 | utl.SshRun(user, keyRsa, sshHost, sshPort, "rm -rf " + module.GYamlConf.FeServers[nid].LogDir)
87 | utl.SshRun(user, keyRsa, sshHost, sshPort, "rm -rf " + module.GYamlConf.FeServers[nid].MetaDir)
88 | utl.SshRun(user, keyRsa, sshHost, sshPort, "rm -rf " + module.GYamlConf.FeServers[nid].DeployDir)
89 |
90 |
91 | if nid != len(module.GYamlConf.FeServers) - 1 {
92 | module.GYamlConf.FeServers = append(module.GYamlConf.FeServers[:nid], module.GYamlConf.FeServers[nid+1:]...)
93 | } else {
94 | module.GYamlConf.FeServers = module.GYamlConf.FeServers[:nid]
95 | }
96 | module.WriteBackMeta(module.GYamlConf, module.GYamlConf.ClusterInfo.MetaPath)
97 |
98 | infoMess = fmt.Sprintf("Scale in FE node successfully. [clusterName = %s, nodeId = %s]", clusterName, nodeId)
99 | utl.Log("OUTPUT", infoMess)
100 |
101 | } else if tmpNodeType == "BE" {
102 | // stop BE node first
103 | sshHost = module.GYamlConf.BeServers[nid].Host
104 | sshPort = module.GYamlConf.BeServers[nid].SshPort
105 | sshHost = module.GYamlConf.BeServers[nid].Host
106 | beDeployDir = module.GYamlConf.BeServers[nid].DeployDir
107 | beHeartbeatServicePort = module.GYamlConf.BeServers[nid].HeartbeatServicePort
108 | err = stopCluster.StopBeNode(user, keyRsa, sshHost, sshPort, beDeployDir)
109 |
110 | if err != nil {
111 | infoMess = fmt.Sprintf("Error in stop BE node. [nodeId = %s, error = %v]", nodeId, err)
112 | utl.Log("ERROR", infoMess)
113 | os.Exit(1)
114 | }
115 |
116 | time.Sleep(time.Duration(10) * time.Second)
117 |
118 | // drop BE node
119 | dropCmd = fmt.Sprintf("ALTER SYSTEM DROP BACKEND '%s:%d'", sshHost, beHeartbeatServicePort)
120 |
121 | _, err := utl.RunSQL(sqlUserName, sqlPassword, sqlIp, sqlPort, sqlDbName, dropCmd)
122 |
123 | if err != nil {
124 | infoMess = fmt.Sprintf("Error in scale in BE node. [clusterName = %s, nodeId = %s, error = %s]", clusterName, nodeId, err)
125 | utl.Log("ERROR", infoMess)
126 | }
127 |
128 |
129 |
130 | // remove dir: data, deploy, log
131 | utl.SshRun(user, keyRsa, sshHost, sshPort, "rm -rf " + module.GYamlConf.BeServers[nid].LogDir)
132 | utl.SshRun(user, keyRsa, sshHost, sshPort, "rm -rf " + module.GYamlConf.BeServers[nid].StorageDir)
133 | utl.SshRun(user, keyRsa, sshHost, sshPort, "rm -rf " + module.GYamlConf.BeServers[nid].DeployDir)
134 |
135 | if nid != len(module.GYamlConf.BeServers) - 1 {
136 | module.GYamlConf.BeServers = append(module.GYamlConf.BeServers[:nid], module.GYamlConf.BeServers[nid+1:]...)
137 | } else {
138 | module.GYamlConf.BeServers = module.GYamlConf.BeServers[:nid]
139 | }
140 | // module.WriteBackMeta(module.GYamlConf, module.GYamlConf.ClusterInfo.MetaPath)
141 |
142 | module.WriteBackMeta(module.GYamlConf, module.GYamlConf.ClusterInfo.MetaPath)
143 | infoMess = fmt.Sprintf("Scale in BE node successfully. [clusterName = %s, nodeId = %s]", clusterName, nodeId)
144 | utl.Log("OUTPUT", infoMess)
145 |
146 | } else {
147 | infoMess = fmt.Sprintf("Error in get Node type. Please check the nodeId. You can use 'sr-ctl-cluster display %s ' to check the node id.[NodeId = %s]", clusterName, nodeId)
148 | utl.Log("ERROR", infoMess)
149 | }
150 |
151 | }
152 |
153 |
--------------------------------------------------------------------------------
/cluster/clusterOption/scaleOut.go:
--------------------------------------------------------------------------------
1 | package clusterOption
2 |
3 | import (
4 |
5 | "fmt"
6 | "os"
7 | "stargo/module"
8 | "stargo/sr-utl"
9 | "stargo/cluster/checkStatus"
10 | "stargo/cluster/prepareOption"
11 | "stargo/cluster/modifyConfig"
12 | "stargo/cluster/startCluster"
13 |
14 | )
15 |
16 | func ScaleOut(clusterName string, scaleMetaFile string) {
17 |
18 | var clusterVersion string
19 | var infoMess string
20 | // Get the cluster version
21 | module.AppendConf(clusterName)
22 | clusterVersion = module.GYamlConfAppend.ClusterInfo.Version
23 | module.SetGlobalVar("GSRVersion", clusterVersion)
24 | module.InitConf(clusterName, scaleMetaFile)
25 |
26 |
27 | if checkStatus.CheckClusterName(clusterName) {
28 | infoMess = "Don't find the Cluster " + clusterName
29 | utl.Log("ERROR", infoMess)
30 | os.Exit(1)
31 | }
32 |
33 | module.GYamlConf.Global = module.GYamlConfAppend.Global
34 |
35 | prepareOption.PreCheckSR()
36 | prepareOption.CreateDir()
37 | prepareOption.PrepareSRPkg()
38 | prepareOption.DistributeSrDir()
39 |
40 | tmpYamlConf := module.GYamlConfAppend
41 | module.GYamlConfAppend = module.GYamlConf
42 | module.GYamlConf = tmpYamlConf
43 |
44 | tmpYamlConf.FeServers = append(module.GYamlConf.FeServers, module.GYamlConfAppend.FeServers[0:]...)
45 | tmpYamlConf.BeServers = append(module.GYamlConf.BeServers, module.GYamlConfAppend.BeServers[0:]...)
46 | //fmt.Println("DEBUG >>> tmpYamlConf", tmpYamlConf)
47 | module.WriteBackMeta(tmpYamlConf, module.GYamlConf.ClusterInfo.MetaPath)
48 |
49 | // fmt.Println("DEBUG >>> GYamlConfAppend.FeServers", module.GYamlConfAppend.FeServers)
50 | // fmt.Println("################################################")
51 | // fmt.Println("DEBUG >>> GYamlConf.FeServers", module.GYamlConf.FeServers)
52 | // fmt.Println("################################################")
53 | // fmt.Println("DEBUG >>> tmpYamlConf.FeServers", tmpYamlConf.FeServers)
54 |
55 |
56 |
57 | modifyConfig.ModifyClusterConfig()
58 | fmt.Println("############################################# SCALE OUT FE CLUSTER #############################################")
59 | fmt.Println("############################################# SCALE OUT FE CLUSTER #############################################")
60 | startCluster.InitFeCluster(module.GYamlConfAppend)
61 | fmt.Println("############################################# START BE CLUSTER #############################################")
62 | fmt.Println("############################################# START BE CLUSTER #############################################")
63 | startCluster.InitBeCluster(module.GYamlConfAppend)
64 |
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/cluster/clusterOption/start.go:
--------------------------------------------------------------------------------
1 | package clusterOption
2 |
3 | import(
4 | "stargo/cluster/startCluster"
5 | "stargo/module"
6 | "stargo/sr-utl"
7 | "stargo/cluster/checkStatus"
8 | "os"
9 | "fmt"
10 | )
11 |
12 |
13 | func Start(clusterName string, nodeId string, role string) {
14 |
15 | var infoMess string
16 | //var tmpNodeType string
17 | var tmpNodeHost string
18 | var tmpNodePort int
19 | var tmpUser string
20 | var tmpKeyRsa string
21 | var tmpSshPort int
22 | var tmpDeployDir string
23 | //var tmpNodeInd int
24 |
25 | module.InitConf(clusterName, "")
26 |
27 | tmpUser = module.GYamlConf.Global.User
28 | tmpKeyRsa = module.GSshKeyRsa
29 |
30 | if checkStatus.CheckClusterName(clusterName) {
31 | infoMess = "Don't find the Cluster " + clusterName
32 | utl.Log("ERROR", infoMess)
33 | os.Exit(1)
34 | }
35 | // start all cluster: sr-ctl-cluster start sr-c1
36 | // start 1 node: sr-ctl-cluster start sr-c1 --node 192.168.88.33:9010
37 | // start all FE node: sr-ctl-cluster start sr-c1 --role FE
38 | // start all BE node: sr-ctl-cluster start sr-c1 --role BE
39 |
40 | // start all cluster: sr-ctl-cluster start sr-c1
41 |
42 |
43 | // -----------------------------------------------------------------------------------------
44 | // | case id | node id | role | option |
45 | // -----------------------------------------------------------------------------------------
46 | // | 1 | null | null | start all cluster |
47 | // | 2 | null | !null | start FE or BE cluster |
48 | // | 3 | !null | null | start the FE/BE node (BE only) |
49 | // | 4 | !null | !null | error |
50 | // -----------------------------------------------------------------------------------------
51 | if nodeId == module.NULLSTR && role == module.NULLSTR {
52 | // case id 1: - start all cluster: sr-ctl-cluster start sr-c1
53 | startCluster.StartFeCluster()
54 | startCluster.StartBeCluster()
55 | } // end of case 1
56 |
57 | if nodeId == module.NULLSTR && role != module.NULLSTR {
58 | // case id 2: start FE or BE cluster
59 | if role == "FE" {
60 | infoMess = "Starting FE cluster ...."
61 | utl.Log("INFO", infoMess)
62 | startCluster.StartFeCluster()
63 | } else if role == "BE" {
64 | startCluster.StartBeCluster()
65 | infoMess = "Starting BE cluster ..."
66 | utl.Log("INFO", infoMess)
67 | } else {
68 | infoMess = fmt.Sprintf("Error in get Node type. Please check the nodeId. You can use 'sr-ctl-cluster display %s ' to check the node id.[NodeId = %s]", clusterName, nodeId)
69 | utl.Log("ERROR", infoMess)
70 | }
71 | } // end of case 2
72 |
73 | if nodeId != module.NULLSTR && role == module.NULLSTR {
74 | // case id 3: start the FE/BE node
75 | // get the node type
76 | tmpNodeType, i := checkStatus.GetNodeType(nodeId)
77 | if tmpNodeType == "FE" {
78 | infoMess = "Please use --role FE to start all the FE node."
79 | utl.Log("ERROR", infoMess)
80 | os.Exit(1)
81 | //tmpNodeHost = module.GYamlConf.FeServers[i].Host
82 | //tmpSshPort = module.GYamlConf.FeServers[i].SshPort
83 | //tmpNodePort = module.GYamlConf.FeServers[i].EditLogPort
84 | //tmpDeployDir = module.GYamlConf.FeServers[i].DeployDir
85 | //startCluster.StartFeNode(tmpUser, tmpKeyRsa, tmpNodeHost, tmpSshPort, tmpNodePort, tmpDeployDir)
86 | } else if tmpNodeType == "BE" {
87 | tmpNodeHost = module.GYamlConf.BeServers[i].Host
88 | tmpSshPort = module.GYamlConf.BeServers[i].SshPort
89 | tmpNodePort = module.GYamlConf.BeServers[i].HeartbeatServicePort
90 | tmpDeployDir = module.GYamlConf.BeServers[i].DeployDir
91 | infoMess = fmt.Sprintf("Start BE node. [BeHost = %s, HeartbeatServicePort = %d]", tmpNodeHost, tmpNodePort)
92 | utl.Log("INFO", infoMess)
93 | startCluster.StartBeNode(tmpUser, tmpKeyRsa, tmpNodeHost, tmpSshPort, tmpNodePort, tmpDeployDir)
94 | } else {
95 | infoMess = fmt.Sprintf("Error in get Node type. Please check the nodeId. You can use 'sr-ctl-cluster display %s ' to check the node id.[NodeId = %s]", clusterName, nodeId)
96 | utl.Log("ERROR", infoMess)
97 | }
98 | }// end of case 3
99 |
100 | if nodeId != module.NULLSTR && role != module.NULLSTR {
101 | infoMess = "Detect both --node & --role option."
102 | utl.Log("ERROR", infoMess)
103 | } // end of case 4
104 |
105 | }
106 |
107 |
108 | /*
109 | func getNodeType(nodeId string) (nodeType string, nodeInd int) {
110 |
111 | // FEID: module.GYamlConf.FeServers[i].EditLogPort, module.GYamlConf.FeServers[i].QueryPort
112 | // BEID: module.GYamlConf.BeServers[i].Host, module.GYamlConf.BeServers[i].BePort
113 |
114 | tmpNodeId := strings.Split(nodeId, ":")
115 | fmt.Println("DEBUG>>>>>>>>>>>>>>>>>>>>>>>>>", tmpNodeId)
116 |
117 | // check FE
118 | for i := 0; i < len(module.GYamlConf.FeServers); i++ {
119 |
120 | if tmpNodeId[0] == module.GYamlConf.FeServers[i].Host &&
121 | tmpNodeId[1] == strconv.Itoa(module.GYamlConf.FeServers[i].EditLogPort) {
122 | nodeType = "FE"
123 | //ip = module.GYamlConf.FeServers[i].Host
124 | //port = tmpNodeId[1] == module.GYamlConf.FeServers[i].EditLogPort
125 | nodeInd = i
126 | break
127 | }
128 | }
129 |
130 | for i := 0; i < len(module.GYamlConf.BeServers); i++ {
131 |
132 | if tmpNodeId[0] == module.GYamlConf.BeServers[i].Host &&
133 | tmpNodeId[1] == strconv.Itoa(module.GYamlConf.BeServers[i].BePort) {
134 | nodeType = "BE"
135 | //ip = module.GYamlConf.BeServers[i].Host
136 | //port = module.GYamlConf.BeServers[i].BePort
137 | nodeInd = i
138 | break
139 | }
140 | }
141 |
142 | return nodeType, nodeInd
143 |
144 | }
145 |
146 | */
147 |
148 |
149 |
--------------------------------------------------------------------------------
/cluster/clusterOption/stop.go:
--------------------------------------------------------------------------------
1 | package clusterOption
2 |
3 | import(
4 | "stargo/cluster/stopCluster"
5 | "stargo/module"
6 | "stargo/sr-utl"
7 | "stargo/cluster/checkStatus"
8 | "fmt"
9 | "os"
10 | )
11 |
12 |
13 | func Stop(clusterName string, nodeId string, role string) {
14 |
15 | var infoMess string
16 | //var tmpNodeType string
17 | var tmpNodeHost string
18 | var tmpUser string
19 | var tmpKeyRsa string
20 | var tmpSshPort int
21 | var tmpDeployDir string
22 | //var tmpNodeInd int
23 |
24 | module.InitConf(clusterName, "")
25 |
26 | tmpUser = module.GYamlConf.Global.User
27 | tmpKeyRsa = module.GSshKeyRsa
28 |
29 | if checkStatus.CheckClusterName(clusterName) {
30 | infoMess = "Don't find the Cluster " + clusterName
31 | utl.Log("ERROR", infoMess)
32 | os.Exit(1)
33 | }
34 |
35 | // stop all cluster: sr-ctl-cluster stop sr-c1
36 | // stop 1 node: sr-ctl-cluster stop sr-c1 --node 192.168.88.33:9010
37 | // stop all FE node: sr-ctl-cluster stop sr-c1 --role FE
38 | // stop all BE node: sr-ctl-cluster stop sr-c1 --role BE
39 |
40 |
41 |
42 | // -----------------------------------------------------------------------------------------
43 | // | case id | node id | role | option |
44 | // -----------------------------------------------------------------------------------------
45 | // | 1 | null | null | stop all cluster |
46 | // | 2 | null | !null | stop FE or BE cluster |
47 | // | 3 | !null | null | stop the FE/BE node (BE only) |
48 | // | 4 | !null | !null | error |
49 | // -----------------------------------------------------------------------------------------
50 | if nodeId == module.NULLSTR && role == module.NULLSTR {
51 | // case id 1: - stop all cluster: sr-ctl-cluster stop sr-c1
52 | stopCluster.StopFeCluster(clusterName)
53 | stopCluster.StopBeCluster(clusterName)
54 | } // end of case 1
55 |
56 | if nodeId == module.NULLSTR && role != module.NULLSTR {
57 | // case id 2: stop FE or BE cluster
58 | if role == "FE" {
59 | infoMess = "Stopping FE cluster ...."
60 | utl.Log("INFO", infoMess)
61 | stopCluster.StopFeCluster(clusterName)
62 | } else if role == "BE" {
63 | infoMess = "Stopping BE cluster ..."
64 | utl.Log("INFO", infoMess)
65 | stopCluster.StopBeCluster(clusterName)
66 | } else {
67 | infoMess = fmt.Sprintf("Error in get Node type. Please check the nodeId. You can use 'sr-ctl-cluster display %s ' to check the node id.[NodeId = %s]", clusterName, nodeId)
68 | utl.Log("ERROR", infoMess)
69 | }
70 | } // end of case 2
71 |
72 | if nodeId != module.NULLSTR && role == module.NULLSTR {
73 | // case id 3: stop the FE/BE node
74 | // get the node type
75 | tmpNodeType, i := checkStatus.GetNodeType(nodeId)
76 | if tmpNodeType == "FE" {
77 | tmpNodeHost = module.GYamlConf.FeServers[i].Host
78 | tmpSshPort = module.GYamlConf.FeServers[i].SshPort
79 | tmpDeployDir = module.GYamlConf.FeServers[i].DeployDir
80 | // func StopFeNode(user string, keyRsa string, sshHost string, sshPort int, feDeployDir string) (err error)
81 | // func StopBeNode(user string, keyRsa string, sshHost string, sshPort int, beDeployDir string) (err error)
82 | infoMess = fmt.Sprintf("Stopping FE node. [BeHost = %s]", tmpNodeHost)
83 | utl.Log("INFO", infoMess)
84 | stopCluster.StopFeNode(tmpUser, tmpKeyRsa, tmpNodeHost, tmpSshPort, tmpDeployDir)
85 | } else if tmpNodeType == "BE" {
86 | tmpNodeHost = module.GYamlConf.BeServers[i].Host
87 | tmpSshPort = module.GYamlConf.BeServers[i].SshPort
88 | tmpDeployDir = module.GYamlConf.BeServers[i].DeployDir
89 | infoMess = fmt.Sprintf("Stopping BE node. [BeHost = %s]", tmpNodeHost)
90 | utl.Log("INFO", infoMess)
91 | stopCluster.StopBeNode(tmpUser, tmpKeyRsa, tmpNodeHost, tmpSshPort, tmpDeployDir)
92 | } else {
93 | infoMess = fmt.Sprintf("Error in get Node type. Please check the nodeId. You can use 'sr-ctl-cluster display %s ' to check the node id.[NodeId = %s]", clusterName, nodeId)
94 | utl.Log("ERROR", infoMess)
95 | }
96 | }// end of case 3
97 |
98 | if nodeId != module.NULLSTR && role != module.NULLSTR {
99 | infoMess = "Detect both --node & --role option."
100 | utl.Log("ERROR", infoMess)
101 | } // end of case 4
102 |
103 | }
104 |
105 |
106 | /*
107 | func getNodeType(nodeId string) (nodeType string, nodeInd int) {
108 |
109 | // FEID: module.GYamlConf.FeServers[i].EditLogPort, module.GYamlConf.FeServers[i].QueryPort
110 | // BEID: module.GYamlConf.BeServers[i].Host, module.GYamlConf.BeServers[i].BePort
111 |
112 | tmpNodeId := strings.Split(nodeId, ":")
113 | fmt.Println("DEBUG>>>>>>>>>>>>>>>>>>>>>>>>>", tmpNodeId)
114 |
115 | // check FE
116 | for i := 0; i < len(module.GYamlConf.FeServers); i++ {
117 |
118 | if tmpNodeId[0] == module.GYamlConf.FeServers[i].Host &&
119 | tmpNodeId[1] == strconv.Itoa(module.GYamlConf.FeServers[i].EditLogPort) {
120 | nodeType = "FE"
121 | //ip = module.GYamlConf.FeServers[i].Host
122 | //port = tmpNodeId[1] == module.GYamlConf.FeServers[i].EditLogPort
123 | nodeInd = i
124 | break
125 | }
126 | }
127 |
128 | for i := 0; i < len(module.GYamlConf.BeServers); i++ {
129 |
130 | if tmpNodeId[0] == module.GYamlConf.BeServers[i].Host &&
131 | tmpNodeId[1] == strconv.Itoa(module.GYamlConf.BeServers[i].BePort) {
132 | nodeType = "BE"
133 | //ip = module.GYamlConf.BeServers[i].Host
134 | //port = module.GYamlConf.BeServers[i].BePort
135 | nodeInd = i
136 | break
137 | }
138 | }
139 |
140 | return nodeType, nodeInd
141 |
142 | }
143 |
144 | */
145 |
146 |
147 |
--------------------------------------------------------------------------------
/cluster/clusterOption/test.go:
--------------------------------------------------------------------------------
1 | package clusterOption
2 |
3 | import (
4 |
5 | "stargo/module"
6 | "stargo/cluster/prepareOption"
7 |
8 | )
9 |
10 |
11 | // sr-ctl-cluster deploy sr-c1 v2.0.1 /tmp/sr-c1.yaml
12 |
13 | func TestOpt() {
14 |
15 | clusterName := "test-sr"
16 | metaFile := "sr-c1.yaml"
17 | module.InitConf(clusterName, metaFile)
18 | prepareOption.PreCheckSR()
19 |
20 | }
21 |
22 |
--------------------------------------------------------------------------------
/cluster/clusterOption/upgrade.go:
--------------------------------------------------------------------------------
1 | package clusterOption
2 |
3 | import(
4 |
5 | "fmt"
6 | "os"
7 | "stargo/module"
8 | "stargo/sr-utl"
9 | "stargo/cluster/checkStatus"
10 | "stargo/cluster/prepareOption"
11 | "stargo/cluster/upgradeCluster"
12 | )
13 |
14 | func Upgrade(clusterName string, clusterVersion string) {
15 |
16 | var infoMess string
17 | //var err error
18 |
19 |
20 | module.InitConf(clusterName, "")
21 | module.SetGlobalVar("GSRVersion", clusterVersion)
22 |
23 | if checkStatus.CheckClusterName(clusterName) {
24 | infoMess = "Don't find the Cluster " + clusterName
25 | utl.Log("ERROR", infoMess)
26 | os.Exit(1)
27 | }
28 |
29 | oldVersion := module.GYamlConf.ClusterInfo.Version
30 | newVersion := clusterVersion
31 | if !(oldVersion < newVersion) {
32 | infoMess = fmt.Sprintf("OldVersion = %s NewVersion = %s, the NewVersion is not higher than OldVersion", oldVersion, newVersion)
33 | utl.Log("ERROR", infoMess)
34 | os.Exit(1)
35 | } else {
36 | infoMess = fmt.Sprintf("Upgrade StarRocks Cluster %s, from version %s to version %s", clusterName, oldVersion, newVersion)
37 | utl.Log("OUTPUT", infoMess)
38 | }
39 |
40 | prepareOption.PrepareSRPkg()
41 | upgradeCluster.UpgradeBeCluster()
42 | upgradeCluster.UpgradeFeCluster()
43 |
44 | module.WriteBackMeta(module.GYamlConf, module.GYamlConf.ClusterInfo.MetaPath)
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/cluster/destroyCluster/destroyCluster.go:
--------------------------------------------------------------------------------
1 | package destroyCluster
2 |
3 | import(
4 | "fmt"
5 | "os"
6 | "stargo/sr-utl"
7 | "stargo/module"
8 | )
9 |
10 | func DestroyCluster(clusterName string) {
11 |
12 | rmFeDir(clusterName)
13 | rmBeDir(clusterName)
14 | rmMeta(clusterName)
15 |
16 | }
17 |
18 |
19 | func rmFeDir(clusterName string) {
20 |
21 | var infoMess string
22 | var tmpFeDeployDir string
23 | var tmpFeMetaDir string
24 | var tmpUser string
25 | var tmpKeyRsa string
26 | var tmpFeHost string
27 | var tmpFeSshPort int
28 | var tmpRemoveDeployCmd string
29 | var tmpRemoveMetaCmd string
30 |
31 |
32 |
33 | for i := 0; i < len(module.GYamlConf.FeServers); i++ {
34 |
35 | tmpFeDeployDir = module.GYamlConf.FeServers[i].DeployDir
36 | tmpUser = module.GYamlConf.Global.User
37 | tmpKeyRsa = module.GSshKeyRsa
38 | tmpFeMetaDir = module.GYamlConf.FeServers[i].MetaDir
39 | tmpFeHost = module.GYamlConf.FeServers[i].Host
40 | tmpFeSshPort = module.GYamlConf.FeServers[i].SshPort
41 | tmpRemoveDeployCmd = "rm -rf " + tmpFeDeployDir
42 | tmpRemoveMetaCmd = "rm -rf " + tmpFeMetaDir
43 |
44 | // remove deploy dir
45 | infoMess = fmt.Sprintf("Waiting for remove FE deploy dir. [FeHost = %s, DeployDir = %s]", tmpFeHost, tmpRemoveDeployCmd)
46 | utl.Log("INFO", infoMess)
47 | _, _ = utl.SshRun(tmpUser, tmpKeyRsa, tmpFeHost, tmpFeSshPort, tmpRemoveDeployCmd)
48 |
49 | infoMess = fmt.Sprintf("Waiting for remove FE meta dir. [FeHost = %s, MetaDir = %s]", tmpFeHost, tmpRemoveMetaCmd)
50 | utl.Log("INFO", infoMess)
51 | _, _ = utl.SshRun(tmpUser, tmpKeyRsa, tmpFeHost, tmpFeSshPort, tmpRemoveMetaCmd)
52 |
53 | infoMess = fmt.Sprintf("Fe node removed. [FeHost = %s]", tmpFeHost)
54 | utl.Log("OUTPUT", infoMess)
55 | }
56 | }
57 |
58 | func rmBeDir(clusterName string) {
59 |
60 | var infoMess string
61 | var tmpBeDeployDir string
62 | var tmpBeStorageDir string
63 | var tmpUser string
64 | var tmpKeyRsa string
65 | var tmpBeHost string
66 | var tmpBeSshPort int
67 | var tmpRemoveDeployCmd string
68 | var tmpRemoveStorageCmd string
69 |
70 |
71 | for i := 0; i < len(module.GYamlConf.BeServers); i++ {
72 |
73 | tmpBeDeployDir = module.GYamlConf.BeServers[i].DeployDir
74 | tmpUser = module.GYamlConf.Global.User
75 | tmpKeyRsa = module.GSshKeyRsa
76 | tmpBeStorageDir = module.GYamlConf.BeServers[i].StorageDir
77 | tmpBeHost = module.GYamlConf.BeServers[i].Host
78 | tmpBeSshPort = module.GYamlConf.BeServers[i].SshPort
79 | tmpRemoveDeployCmd = "rm -rf " + tmpBeDeployDir
80 | tmpRemoveStorageCmd = "rm -rf " + tmpBeStorageDir
81 |
82 | // remove deploy dir
83 | infoMess = fmt.Sprintf("Waiting for remove BE deploy dir. [BeHost = %s, DeployDir = %s]", tmpBeHost, tmpRemoveDeployCmd)
84 | utl.Log("INFO", infoMess)
85 | _, _ = utl.SshRun(tmpUser, tmpKeyRsa, tmpBeHost, tmpBeSshPort, tmpRemoveDeployCmd)
86 |
87 | infoMess = fmt.Sprintf("Waiting for remove BE storage dir. [BeHost = %s, StorageDir = %s]", tmpBeHost, tmpRemoveStorageCmd)
88 | utl.Log("INFO", infoMess)
89 | _, _ = utl.SshRun(tmpUser, tmpKeyRsa, tmpBeHost, tmpBeSshPort, tmpRemoveStorageCmd)
90 |
91 | infoMess = fmt.Sprintf("Be node removed. [BeHost = %s]", tmpBeHost)
92 | utl.Log("OUTPUT", infoMess)
93 | }
94 | }
95 |
96 |
97 | func rmMeta(clusterName string) {
98 |
99 | var infoMess string
100 | var metaFileDir string
101 |
102 | metaFileDir = fmt.Sprintf("%s/cluster/%s", module.GSRCtlRoot, clusterName)
103 | err := os.RemoveAll(metaFileDir)
104 | if err != nil {
105 | infoMess = fmt.Sprintf("Error in remove the meta dir. [Dir = %s]", metaFileDir)
106 | utl.Log("ERROR", infoMess)
107 | } else {
108 | infoMess = fmt.Sprintf("Meta Dir removed. [Dir = %s]", metaFileDir)
109 | utl.Log("OUTPUT", infoMess)
110 | }
111 |
112 | }
113 |
--------------------------------------------------------------------------------
/cluster/displayCluster/clusterStatus.go:
--------------------------------------------------------------------------------
1 | package clusterStatus
2 |
3 | import(
4 | "fmt"
5 | // "errors"
6 | "stargo/module"
7 | "stargo/sr-utl"
8 | "stargo/cluster/checkStatus"
9 | )
10 |
11 | func ClusterStat(clusterName string) {
12 |
13 | var infoMess string
14 | fmt.Printf("clusterName = %s\n", clusterName)
15 | fmt.Printf("clusterVerison = %s\n", module.GYamlConf.ClusterInfo.Version)
16 | //metaFile := "/tmp/c1-meta.yaml"
17 | //module.InitConf(metaFile)
18 |
19 | var tmpID string
20 | var tmpRole string
21 | var tmpHost string
22 | var tmpPort string
23 | var tmpStat string
24 | var tmpDataDir string
25 | var tmpDeployDir string
26 | var feEntryId int
27 | var noFeEntry bool
28 | var err error
29 |
30 | // Get FE entry
31 | feEntryId, err = checkStatus.GetFeEntry(-1)
32 | if err != nil || feEntryId == -1 {
33 | infoMess = "All FE nodes are down, please start FE node and display the cluster status again."
34 | utl.Log("WARN", infoMess)
35 | noFeEntry = true
36 | } else {
37 | // feEntryHost = module.GYamlConf.FeServers[feEntryId].Host
38 | // feEntryQueryPort = module.GYamlConf.FeServers[feEntryId].QueryPort
39 | module.SetFeEntry(feEntryId)
40 | }
41 |
42 | tmpMinus := []byte("------------------------------------------------------------------------------------------------------")
43 | fmt.Printf("%-26s %-6s %-20s %-15s %-10s %-50s %-50s\n", "ID", "ROLE", "HOST", "PORT", "STAT", "DATADIR", "DEPLOYDIR")
44 | fmt.Printf("%-26s %-6s %-20s %-15s %-10s %-50s %-50s\n", tmpMinus[:26], tmpMinus[:6], tmpMinus[:20], tmpMinus[:15], tmpMinus[:10], tmpMinus[:50], tmpMinus[:50])
45 | // Get FE status
46 | for i := 0; i < len(module.GYamlConf.FeServers); i++ {
47 | tmpID = fmt.Sprintf("%s:%d", module.GYamlConf.FeServers[i].Host, module.GYamlConf.FeServers[i].EditLogPort)
48 | tmpRole = "FE"
49 | tmpHost = module.GYamlConf.FeServers[i].Host
50 | tmpPort = fmt.Sprintf("%d/%d", module.GYamlConf.FeServers[i].EditLogPort, module.GYamlConf.FeServers[i].QueryPort)
51 | tmpDataDir = module.GYamlConf.FeServers[i].MetaDir
52 | tmpDeployDir = module.GYamlConf.FeServers[i].DeployDir
53 |
54 |
55 | if !noFeEntry {
56 |
57 | // If we can get a FE entry(more than one FE node is running), we can use [show frontends] command by JDBC)
58 | // CheckFeStatus(feId int, user string, keyRsa string, sshHost string, sshPort int, feQueryPort int) (feStat FeStatusStruct, err error)
59 | feStatStruct, err := checkStatus.CheckFeStatus(i)
60 | if err != nil {
61 | infoMess = fmt.Sprintf("Error in checking FE status [FeHost = %s, error = %v]", tmpHost, err)
62 | utl.Log("DEBUG", infoMess)
63 | }
64 |
65 | if feStatStruct["Alive"] == "true" {
66 | if feStatStruct["IsMaster"] == "true" {
67 | tmpStat = "UP/L"
68 | } else {
69 | tmpStat = "UP"
70 | }
71 | } else {
72 | tmpStat = "DOWN"
73 | }
74 |
75 | } else {
76 |
77 | // If we cannot get a FE entry, it means no FE node is running, so we don't need to check FE status using [show frontends] command
78 | tmpStat = "DOWN"
79 | }
80 | // get the Dir output string
81 | // if the dir output string is too long, only display the tail 43 chars
82 | // for example, the tmpDeployDir is "/opt/starrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrocks/fe",
83 | // it will show the tmpDeployDir "... rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrocks/fe"
84 |
85 | fmt.Printf("%-26s %-6s %-20s %-15s %-10s %-50s %-50s\n", tmpID, tmpRole, tmpHost, tmpPort, tmpStat, tmpDeployDir, tmpDataDir)
86 |
87 | }
88 |
89 | // check BE status
90 | for i := 0; i < len(module.GYamlConf.BeServers); i++ {
91 |
92 | tmpID = fmt.Sprintf("%s:%d", module.GYamlConf.BeServers[i].Host, module.GYamlConf.BeServers[i].BePort)
93 | tmpRole = "BE"
94 | tmpHost = module.GYamlConf.BeServers[i].Host
95 | tmpPort = fmt.Sprintf("%d/%d", module.GYamlConf.BeServers[i].BePort, module.GYamlConf.BeServers[i].HeartbeatServicePort)
96 | tmpDataDir = module.GYamlConf.BeServers[i].StorageDir
97 | tmpDeployDir = module.GYamlConf.BeServers[i].DeployDir
98 |
99 |
100 | if !noFeEntry {
101 | // If we can get a FE entry(more than one FE node is running), we can use [show backends] command by JDBC
102 | // CheckBeStatus(beId int, user string, keyRsa string, sshHost string, sshPort int, heartbeatServicePort int) (beStat BeStatusStruct, err error)
103 | beStat, err := checkStatus.CheckBeStatus(i)
104 | if err != nil {
105 | infoMess = fmt.Sprintf("Error in checking BE status [BeHost = %s, error = %v]", tmpHost, err)
106 | utl.Log("DEBUG", infoMess)
107 | }
108 |
109 | if beStat["Alive"] == "true" {
110 | tmpStat = "UP"
111 | } else {
112 | tmpStat = "DOWN"
113 | }
114 | //fmt.Printf("id = %s\t role = %s\t host = %s\t tmpPort = %s\t tmpStat = %s\t tmpDataDir = %s\t tmpDeployDir = %s\n", tmpID, tmpRole, tmpHost, tmpPort, tmpStat, tmpDataDir, tmpDeployDir)
115 | } else {
116 |
117 | // If we cannot get a FE entry, it means no FE node is running, so we don't need to check BE status using [show frontends] command, the BE status is "WAITING FE"
118 | bePortRun, _ := checkStatus.CheckBePortStatus(i)
119 | if bePortRun {
120 | tmpStat = "WAITING FE"
121 | } else {
122 | tmpStat = "DOWN"
123 | }
124 | }
125 | fmt.Printf("%-26s %-6s %-20s %-15s %-10s %-50s %-50s\n", tmpID, tmpRole, tmpHost, tmpPort, tmpStat, tmpDeployDir, tmpDataDir)
126 | }
127 |
128 | }
129 |
--------------------------------------------------------------------------------
/cluster/downgradeCluster/downgradeBe.go:
--------------------------------------------------------------------------------
1 | package downgradeCluster
2 |
3 | import (
4 |
5 | "fmt"
6 | "time"
7 | "strings"
8 | //"errors"
9 | "stargo/module"
10 | "stargo/sr-utl"
11 | "stargo/cluster/stopCluster"
12 | "stargo/cluster/startCluster"
13 | "stargo/cluster/checkStatus"
14 | //"stargo/cluster/prepareOption"
15 |
16 | )
17 |
18 |
19 | func DowngradeBeCluster() { //(err error){
20 |
21 | var infoMess string
22 | var err error
23 | var beStat map[string]string
24 | var feEntryId int
25 |
26 |
27 | feEntryId, err = checkStatus.GetFeEntry(-1)
28 | if err != nil || feEntryId == -1 {
29 | //infoMess = "All FE nodes are down, please start FE node and display the cluster status again."
30 | //utl.Log("WARN", infoMess)
31 | module.SetFeEntry(0)
32 | } else {
33 | module.SetFeEntry(feEntryId)
34 | }
35 |
36 |
37 | for i := 0; i < len(module.GYamlConf.BeServers); i++ {
38 | infoMess = fmt.Sprintf("Starting downgrade BE node. [beId = %d]", i)
39 | utl.Log("OUTPUT", infoMess)
40 | err = DowngradeBeNode(i)
41 | if err != nil {
42 | infoMess = fmt.Sprintf("Error in downgrade be node. [nodeid = %d]", i)
43 | utl.Log("ERROR", infoMess)
44 | }
45 |
46 | beStat, err = checkStatus.CheckBeStatus(i)
47 |
48 |
49 | for j := 0; j < 3; j++ {
50 | infoMess = fmt.Sprintf("The %d time to check be status: %v", j, beStat["Alive"])
51 | utl.Log("DEBUG", infoMess)
52 | if beStat["Alive"] == "true" {
53 | break
54 | } else {
55 | infoMess = fmt.Sprintf("The BE node doesn't work, wait for 10s and check the status again. [beId = %d]\n", i)
56 | utl.Log("DEBUG", infoMess)
57 | time.Sleep(10 * time.Second)
58 | beStat, err = checkStatus.CheckBeStatus(i)
59 | }
60 | }
61 |
62 |
63 | if err != nil {
64 | infoMess = fmt.Sprintf("Error in get the Be status [beId = %d, error = %v]", i, err)
65 | utl.Log("DEBUG", infoMess)
66 | //return err
67 | }
68 | if beStat["Alive"] == "false" {
69 | infoMess = fmt.Sprintf("The BE node downgrade failed. The BE node doesn't work. [beId = %d]\n", i)
70 | utl.Log("ERROR", infoMess)
71 | //return errors.New(infoMess)
72 | } else if ! strings.Contains(beStat["Version"], strings.Replace(module.GSRVersion, "v", "", -1)) {
73 | infoMess = fmt.Sprintf("The BE node downgrade failed. [beId = %d, targetVersion = %s, currentVersion = v%s]", i, module.GSRVersion, beStat["Version"])
74 | utl.Log("ERROR", infoMess)
75 | //return errors.New(infoMess)
76 | } else {
77 | infoMess = fmt.Sprintf("The Be node downgrade successfully. [beId = %d, currentVersion = v%s]", i, beStat["Version"])
78 | utl.Log("OUTPUT", infoMess)
79 | }
80 | }
81 |
82 | //return nil
83 |
84 |
85 | }
86 |
87 |
88 | func DowngradeBeNode(beId int) (err error) {
89 | // step 1. backup be lib
90 | // step 2. upload new be lib
91 | // step 3. stop be node
92 | // step 4. start be node
93 |
94 | var infoMess string
95 | var user string
96 | var sourceDir string
97 | var targetDir string
98 | var sshHost string
99 | var sshPort int
100 | var beDeployDir string
101 | var beHeartBeatServicePort int
102 | var keyRsa string
103 |
104 |
105 | user = module.GYamlConf.Global.User
106 | keyRsa = module.GSshKeyRsa
107 | sshHost = module.GYamlConf.BeServers[beId].Host
108 | sshPort = module.GYamlConf.BeServers[beId].SshPort
109 | beDeployDir = module.GYamlConf.BeServers[beId].DeployDir
110 | beHeartBeatServicePort = module.GYamlConf.BeServers[beId].HeartbeatServicePort
111 |
112 |
113 |
114 |
115 | // step1. backup be lib
116 | sourceDir = fmt.Sprintf("%s/lib", beDeployDir)
117 | targetDir = fmt.Sprintf("%s/lib.bak-%s", beDeployDir, time.Now().Format("20060102150405"))
118 |
119 | err = utl.RenameDir(user, keyRsa, sshHost, sshPort, sourceDir, targetDir)
120 | if err != nil {
121 | infoMess = fmt.Sprintf("Error in rename dir when backup be lib. [host = %s, sourceDir = %s, targetDir = %s]", sshHost, sourceDir, targetDir)
122 | utl.Log("ERROR", infoMess)
123 | return err
124 | } else {
125 | infoMess = fmt.Sprintf("downgrade be node - backup be lib. [host = %s, sourceDir = %s, targetDir = %s]", sshHost, sourceDir, targetDir)
126 | utl.Log("INFO", infoMess)
127 | }
128 |
129 |
130 | // step2. upload new be lib
131 | sourceDir = fmt.Sprintf("%s/StarRocks-%s/be/lib", module.GDownloadPath, strings.Replace(module.GSRVersion, "v", "", -1))
132 | // sourceDir = fmt.Sprintf("%s/download/StarRocks-%s/be/lib", module.GSRCtlRoot, strings.Replace(module.GSRVersion, "v", "", -1))
133 | targetDir = fmt.Sprintf("%s/lib", beDeployDir)
134 | utl.UploadDir(user, keyRsa, sshHost, sshPort, sourceDir, targetDir)
135 | infoMess = fmt.Sprintf("downgrade be node - upload new be lib. [host = %s, sourceDir = %s, targetDir = %s]", sshHost, sourceDir, targetDir)
136 | utl.Log("INFO", infoMess)
137 |
138 |
139 |
140 | // step3. stop be node
141 | err = stopCluster.StopBeNode(user, keyRsa, sshHost, sshPort, beDeployDir)
142 | if err != nil {
143 | infoMess = fmt.Sprintf("Error in stop be node when downgrade be node. [host = %s, beDeployDir = %s]", sshHost, beDeployDir)
144 | utl.Log("ERROR", infoMess)
145 | return err
146 | } else {
147 | infoMess = fmt.Sprintf("downgrade be node - stop be node. [host = %s, beDeployDir = %s]", sshHost, beDeployDir)
148 | utl.Log("INFO", infoMess)
149 | }
150 |
151 | // step4. start be node
152 | startCluster.StartBeNode(user, keyRsa, sshHost, sshPort, beHeartBeatServicePort, beDeployDir)
153 | infoMess = fmt.Sprintf("downgrade be node - start be node. [host = %s, beDeployDir = %s]", sshHost, beDeployDir)
154 | utl.Log("INFO", infoMess)
155 |
156 | return nil
157 |
158 | }
159 |
160 |
161 |
162 |
163 |
--------------------------------------------------------------------------------
/cluster/downgradeCluster/downgradeFe.go:
--------------------------------------------------------------------------------
1 | package downgradeCluster
2 |
3 | import (
4 |
5 | "fmt"
6 | "time"
7 | "strings"
8 | //"errors"
9 | "stargo/module"
10 | "stargo/sr-utl"
11 | "stargo/cluster/stopCluster"
12 | "stargo/cluster/startCluster"
13 | "stargo/cluster/checkStatus"
14 | //"stargo/cluster/prepareOption"
15 |
16 | )
17 |
18 |
19 | func DowngradeFeCluster() { //(err error){
20 |
21 | var infoMess string
22 | var err error
23 | var feStat map[string]string
24 | var feEntryId int
25 |
26 |
27 | feEntryId, err = checkStatus.GetFeEntry(-1)
28 | if err != nil || feEntryId == -1 {
29 | //infoMess = "All FE nodes are down, please start FE node and display the cluster status again."
30 | //utl.Log("WARN", infoMess)
31 | module.SetFeEntry(0)
32 | } else {
33 | module.SetFeEntry(feEntryId)
34 | }
35 |
36 |
37 | for i := 0; i < len(module.GYamlConf.FeServers); i++ {
38 | infoMess = fmt.Sprintf("Starting downgrade FE node. [feId = %d]", i)
39 | utl.Log("OUTPUT", infoMess)
40 | err = DowngradeFeNode(i)
41 | if err != nil {
42 | infoMess = fmt.Sprintf("Error in downgrade FE node. [nodeid = %d]", i)
43 | utl.Log("ERROR", infoMess)
44 | }
45 |
46 | feStat, err = checkStatus.CheckFeStatus(i)
47 |
48 |
49 | for j := 0; j < 3; j++ {
50 | infoMess = fmt.Sprintf("The %d time to check FE status: %v", j, feStat["Alive"])
51 | utl.Log("DEBUG", infoMess)
52 | if feStat["Alive"] == "true"{
53 | break
54 | } else {
55 | infoMess = fmt.Sprintf("The FE node doesn't work, wait for 10s and check the status again. [feId = %d]\n", i)
56 | utl.Log("DEBUG", infoMess)
57 | time.Sleep(10 * time.Second)
58 | feStat, err = checkStatus.CheckFeStatus(i)
59 | }
60 | }
61 |
62 |
63 | if err != nil {
64 | infoMess = fmt.Sprintf("Error in get the FE status [feId = %d, error = %v]", i, err)
65 | utl.Log("DEBUG", infoMess)
66 | //return err
67 | }
68 | if feStat["Alive"] == "false" {
69 | infoMess = fmt.Sprintf("The FE node downgrade failed. The FE node doesn't work. [feId = %d]\n", i)
70 | utl.Log("ERROR", infoMess)
71 | //return errors.New(infoMess)
72 | } else if ! strings.Contains(feStat["FeVersion"], strings.Replace(module.GSRVersion, "v", "", -1)) {
73 | infoMess = fmt.Sprintf("The FE node downgrade failed. [feId = %d, targetVersion = %s, currentVersion = v%s]", i, module.GSRVersion, feStat["FeVersion"])
74 | utl.Log("ERROR", infoMess)
75 | //return errors.New(infoMess)
76 | } else {
77 | infoMess = fmt.Sprintf("The Fe node downgrade successfully. [feId = %d, currentVersion = v%s]", i, feStat["FeVersion"])
78 | utl.Log("OUTPUT", infoMess)
79 | }
80 | }
81 |
82 | //return nil
83 |
84 |
85 | }
86 |
87 |
88 | func DowngradeFeNode(feId int) (err error) {
89 | // step 1. backup fe lib
90 | // step 2. download new fe lib
91 | // step 3. stop fe node
92 | // step 4. start fe node
93 |
94 | var infoMess string
95 | var user string
96 | var sourceDir string
97 | var targetDir string
98 | var sshHost string
99 | var sshPort int
100 | var feDeployDir string
101 | var feEditLogPort int
102 | var keyRsa string
103 |
104 |
105 | user = module.GYamlConf.Global.User
106 | keyRsa = module.GSshKeyRsa
107 | sshHost = module.GYamlConf.FeServers[feId].Host
108 | sshPort = module.GYamlConf.FeServers[feId].SshPort
109 | feDeployDir = module.GYamlConf.FeServers[feId].DeployDir
110 | feEditLogPort = module.GYamlConf.FeServers[feId].EditLogPort
111 |
112 |
113 |
114 |
115 | // step1. backup fe lib
116 | sourceDir = fmt.Sprintf("%s/lib", feDeployDir)
117 | targetDir = fmt.Sprintf("%s/lib.bak-%s", feDeployDir, time.Now().Format("20060102150405"))
118 |
119 | err = utl.RenameDir(user, keyRsa, sshHost, sshPort, sourceDir, targetDir)
120 | if err != nil {
121 | infoMess = fmt.Sprintf("Error in rename dir when backup FE lib. [host = %s, sourceDir = %s, targetDir = %s]", sshHost, sourceDir, targetDir)
122 | utl.Log("ERROR", infoMess)
123 | return err
124 | } else {
125 | infoMess = fmt.Sprintf("downgrade FE node - backup FE lib. [host = %s, sourceDir = %s, targetDir = %s]", sshHost, sourceDir, targetDir)
126 | utl.Log("INFO", infoMess)
127 | }
128 |
129 |
130 | // step2. download new FE lib
131 | sourceDir = fmt.Sprintf("%s/StarRocks-%s/fe/lib", module.GDownloadPath, strings.Replace(module.GSRVersion, "v", "", -1))
132 | // sourceDir = fmt.Sprintf("%s/download/StarRocks-%s/fe/lib", module.GSRCtlRoot, strings.Replace(module.GSRVersion, "v", "", -1))
133 | targetDir = fmt.Sprintf("%s/lib", feDeployDir)
134 | utl.UploadDir(user, keyRsa, sshHost, sshPort, sourceDir, targetDir)
135 | infoMess = fmt.Sprintf("downgrade FE node - download new FE lib. [host = %s, sourceDir = %s, targetDir = %s]", sshHost, sourceDir, targetDir)
136 | utl.Log("INFO", infoMess)
137 |
138 |
139 |
140 | // step3. stop FE node
141 | err = stopCluster.StopFeNode(user, keyRsa, sshHost, sshPort, feDeployDir)
142 | if err != nil {
143 | infoMess = fmt.Sprintf("Error in stop FE node when downgrade FE node. [host = %s, feDeployDir = %s]", sshHost, feDeployDir)
144 | utl.Log("ERROR", infoMess)
145 | return err
146 | } else {
147 | infoMess = fmt.Sprintf("downgrade FE node - stop FE node. [host = %s, feDeployDir = %s]", sshHost, feDeployDir)
148 | utl.Log("INFO", infoMess)
149 | }
150 |
151 | // step4. start FE node
152 | startCluster.StartFeNode(user, keyRsa, sshHost, sshPort, feEditLogPort, feDeployDir)
153 | infoMess = fmt.Sprintf("downgrade FE node - start FE node. [host = %s, feDeployDir = %s]", sshHost, feDeployDir)
154 | utl.Log("INFO", infoMess)
155 |
156 |
157 | return nil
158 |
159 | }
160 |
161 |
162 |
163 |
164 |
--------------------------------------------------------------------------------
/cluster/importCluster/importBe.go:
--------------------------------------------------------------------------------
1 | package importCluster
2 |
3 | import (
4 |
5 | "fmt"
6 | "stargo/module"
7 | "stargo/sr-utl"
8 | "stargo/cluster/checkStatus"
9 | "io/ioutil"
10 | "net/http"
11 | "regexp"
12 | "os"
13 | "strings"
14 | "strconv"
15 |
16 | )
17 |
18 |
19 | func GetBeConf() {
20 |
21 | var infoMess string
22 | var beHttpUrl string
23 |
24 | for i := 0; i < len(module.GYamlConf.BeServers); i++ {
25 |
26 | beStat, err := checkStatus.CheckBeStatus(i)
27 | module.GYamlConf.BeServers[i].WebServerPort, _ = strconv.Atoi(beStat["HttpPort"])
28 | module.GYamlConf.BeServers[i].BrpcPort, _ = strconv.Atoi(beStat["BrpcPort"])
29 | module.GYamlConf.BeServers[i].BePort, _ = strconv.Atoi(beStat["BePort"])
30 |
31 | rootPasswd := ""
32 |
33 | beHttpUrl = fmt.Sprintf("http://root:%s@%s:%d/varz", rootPasswd, module.GYamlConf.BeServers[i].Host, module.GYamlConf.BeServers[i].WebServerPort)
34 | res, err := http.Get(beHttpUrl)
35 | defer res.Body.Close()
36 | if err != nil {
37 | infoMess = fmt.Sprintf("Error in create http get request when get BE conf. [beHttpUrl = %s, error = %v]", beHttpUrl, err)
38 | utl.Log("ERROR", infoMess)
39 | os.Exit(1)
40 | }
41 |
42 | robots, err := ioutil.ReadAll(res.Body)
43 | if err != nil{
44 | infoMess = fmt.Sprintf("Error in read body.[error = %v]", err)
45 | utl.Log("ERROR", infoMess)
46 | os.Exit(1)
47 | }
48 |
49 | //fmt.Println(string(robots))
50 | // get priority_networks
51 | r, _ := regexp.Compile("priority_networks=.*")
52 | module.GYamlConf.BeServers[i].PriorityNetworks = strings.Replace(r.FindString(string(robots)), "priority_networks=", "", -1)
53 |
54 | // get MetaDir
55 | r, _ = regexp.Compile("storage_root_path=.*")
56 | module.GYamlConf.BeServers[i].StorageDir = strings.Replace(r.FindString(string(robots)), "storage_root_path=", "", -1)
57 |
58 | // get LogDir
59 | r, _ = regexp.Compile("sys_log_dir=.*")
60 | module.GYamlConf.BeServers[i].LogDir = strings.Replace(r.FindString(string(robots)), "sys_log_dir=", "", -1)
61 |
62 |
63 | }
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/cluster/importCluster/importFe.go:
--------------------------------------------------------------------------------
1 | package importCluster
2 |
3 | import (
4 |
5 | "fmt"
6 | "stargo/module"
7 | "stargo/sr-utl"
8 | "stargo/cluster/checkStatus"
9 | "io/ioutil"
10 | "net/http"
11 | "regexp"
12 | "os"
13 | "strings"
14 | "strconv"
15 |
16 | )
17 |
18 |
19 | func GetFeConf() {
20 |
21 | var infoMess string
22 | var feHttpUrl string
23 |
24 |
25 | for i := 0; i < len(module.GYamlConf.FeServers); i++ {
26 |
27 | feStat, err := checkStatus.CheckFeStatus(i)
28 | module.GYamlConf.FeServers[i].HttpPort, _ = strconv.Atoi(feStat["HttpPort"])
29 | module.GYamlConf.FeServers[i].RpcPort, _ = strconv.Atoi(feStat["RpcPort"])
30 | module.GYamlConf.FeServers[i].EditLogPort, _ = strconv.Atoi(feStat["EditLogPort"])
31 | module.GSRVersion = "v" + strings.Split(feStat["Version"], "-")[0]
32 | rootPasswd := ""
33 |
34 | feHttpUrl = fmt.Sprintf("http://root:%s@%s:%d/variable", rootPasswd, module.GYamlConf.FeServers[i].Host, module.GYamlConf.FeServers[i].HttpPort)
35 | res, err := http.Get(feHttpUrl)
36 | defer res.Body.Close()
37 | if err != nil {
38 | infoMess = fmt.Sprintf("Error in create http get request when get FE conf. [feHttpUrl = %s, error = %v]", feHttpUrl, err)
39 | utl.Log("ERROR", infoMess)
40 | os.Exit(1)
41 | }
42 |
43 | robots, err := ioutil.ReadAll(res.Body)
44 | if err != nil {
45 | infoMess = fmt.Sprintf("Error in read body.[error = %v]", err)
46 | utl.Log("ERROR", infoMess)
47 | os.Exit(1)
48 | }
49 |
50 | //fmt.Println(string(robots))
51 | // get priority_networks
52 | r, _ := regexp.Compile("priority_networks=.*")
53 | module.GYamlConf.FeServers[i].PriorityNetworks = strings.Replace(r.FindString(string(robots)), "priority_networks=", "", -1)
54 |
55 | // get MetaDir
56 | r, _ = regexp.Compile("meta_dir=.*")
57 | module.GYamlConf.FeServers[i].MetaDir = strings.Replace(r.FindString(string(robots)), "meta_dir=", "", -1)
58 |
59 | // get LogDir
60 | r, _ = regexp.Compile("sys_log_dir=.*")
61 | module.GYamlConf.FeServers[i].LogDir = strings.Replace(r.FindString(string(robots)), "sys_log_dir=", "", -1)
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/cluster/listCluster/listCluster.go:
--------------------------------------------------------------------------------
1 | package listCluster
2 |
3 | import (
4 | "fmt"
5 | "stargo/module"
6 | "stargo/sr-utl"
7 | "io/ioutil"
8 | )
9 |
10 | func ListCluster() {
11 |
12 | // module.GSRCtlRoot
13 | var infoMess string
14 | var clusterName string
15 | var metaFile string
16 | metaPath := fmt.Sprintf("%s/cluster", module.GSRCtlRoot)
17 |
18 | dir, err := ioutil.ReadDir(metaPath)
19 | if err != nil {
20 | infoMess = fmt.Sprintf("Error in read dir [DirPath = %s]", metaPath)
21 | utl.Log("ERROR", infoMess)
22 | }
23 |
24 | tmpMinus := []byte("----------------------------------------------------------------------------------------")
25 | fmt.Printf("%-15s %-10s %-10s %-25s %-60s %-50s\n", "ClusterName", "Version", "User", "CreateDate", "MetaPath", "PrivateKey")
26 | fmt.Printf("%-15s %-10s %-10s %-25s %-60s %-50s\n", tmpMinus[:15], tmpMinus[:10], tmpMinus[:10], tmpMinus[:25], tmpMinus[:60], tmpMinus[:50])
27 |
28 | for _, info := range dir {
29 | clusterName = info.Name()
30 | metaFile = fmt.Sprintf("%s/cluster/%s/meta.yaml", module.GSRCtlRoot, clusterName)
31 |
32 | module.InitConf(clusterName, metaFile)
33 | fmt.Printf("%-15s %-10s %-10s %-25s %-60s %-50s\n", clusterName,
34 | module.GYamlConf.ClusterInfo.Version,
35 | module.GYamlConf.ClusterInfo.User,
36 | module.GYamlConf.ClusterInfo.CreateDate,
37 | module.GYamlConf.ClusterInfo.MetaPath,
38 | module.GYamlConf.ClusterInfo.PrivateKey)
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/cluster/modifyConfig/modifyConfig.go:
--------------------------------------------------------------------------------
1 | package modifyConfig
2 | import (
3 | "fmt"
4 | "strconv"
5 | "strings"
6 | "stargo/module"
7 | "stargo/sr-utl"
8 | )
9 |
10 |
11 |
12 |
13 | func ModifyTest() {
14 |
15 | /*
16 | var configMap map[string] string
17 | configMap = make(map[string] string)
18 |
19 | i := 0
20 | configMap["priority_networks"] = module.GYamlConf.FeServers[i].PriorityNetworks
21 | configMap["http_port"] = strconv.Itoa(module.GYamlConf.FeServers[i].HttpPort)
22 | configMap["rpc_port"] = strconv.Itoa(module.GYamlConf.FeServers[i].RpcPort)
23 | configMap["edit_log_port"] = strconv.Itoa(module.GYamlConf.FeServers[i].EditLogPort)
24 | fmt.Printf("%s:%d\n", "http_port", module.GYamlConf.FeServers[i].HttpPort)
25 | fmt.Printf("%s:%d\n", "rpc_port", module.GYamlConf.FeServers[i].RpcPort)
26 | fmt.Println("[TEST] #########################################################################")
27 | for key, val := range configMap {
28 | fmt.Printf("key:%s\tvalue:%s\n", key, val)
29 | }
30 | */
31 | i := 0
32 | for k, v := range module.GYamlConf.FeServers[i].Config {
33 | fmt.Printf("%s:%s\n", k, v)
34 | }
35 | //fmt.Println("[TEST]", module.GYamlConf.FeServers[i].Config)
36 |
37 | }
38 |
39 |
40 | func ModifyClusterConfig() {
41 |
42 | var infoMess string
43 | //var tmpConfigKey string
44 | //var tmpConfigValue string
45 | var tmpUser string = module.GYamlConf.Global.User
46 | var tmpKeyFile string = module.GSshKeyRsa
47 | var configMap map[string] string
48 |
49 | infoMess = "Modify configuration for FE nodes & BE nodes ..."
50 | utl.Log("OUTPUT", infoMess)
51 | // modify FE config
52 | for i := 0; i < len(module.GYamlConf.FeServers); i++ {
53 | // copy fe config file
54 | tmpFeHost := module.GYamlConf.FeServers[i].Host
55 | tmpFeQueryPort := module.GYamlConf.FeServers[i].QueryPort
56 | tmpFeSourceConfFile := fmt.Sprintf("%s/StarRocks-%s/fe/conf/fe.conf", module.GDownloadPath, strings.Replace(module.GSRVersion, "v", "", -1))
57 | // tmpFeSourceConfFile := fmt.Sprintf("%s/download/StarRocks-%s/fe/conf/fe.conf", module.GSRCtlRoot, strings.Replace(module.GSRVersion, "v", "", -1))
58 | tmpFeTargetConfFile := fmt.Sprintf("%s/tmp/fe.conf-%s-%d", module.GSRCtlRoot, tmpFeHost, tmpFeQueryPort)
59 |
60 | err := copyConfigFile(tmpFeSourceConfFile, tmpFeTargetConfFile)
61 | if err != nil {
62 | infoMess = fmt.Sprintf("Error in modifing fe cluster configuration. Copy configuration file failed [sourceFile = %s, targetFile = %s]", tmpFeSourceConfFile, tmpFeTargetConfFile)
63 | utl.Log("ERROR", infoMess)
64 | panic(err)
65 | }
66 |
67 | // append new config into tmp configuration file
68 |
69 | // add network priority config
70 | //tmpConfigKey = "priority_networks"
71 | //tmpConfigValue = module.GYamlConf.FeServers[i].PriorityNetworks
72 | //appendConfig(tmpFeTargetConfFile, tmpConfigKey, tmpConfigValue)
73 |
74 | configMap = make(map[string] string)
75 | configMap["priority_networks"] = module.GYamlConf.FeServers[i].PriorityNetworks
76 | configMap["http_port"] = strconv.Itoa(module.GYamlConf.FeServers[i].HttpPort)
77 | configMap["rpc_port"] = strconv.Itoa(module.GYamlConf.FeServers[i].RpcPort)
78 | configMap["edit_log_port"] = strconv.Itoa(module.GYamlConf.FeServers[i].EditLogPort)
79 | configMap["query_port"] = strconv.Itoa(module.GYamlConf.FeServers[i].QueryPort)
80 |
81 | for k, v := range configMap {
82 | if v != "0" {
83 | appendConfig(tmpFeTargetConfFile, k, v)
84 | }
85 | }
86 |
87 | for k, v := range module.GYamlConf.FeServers[i].Config {
88 | appendConfig(tmpFeTargetConfFile, k, v)
89 | }
90 |
91 |
92 | // distribute tmp fe configuration file
93 | tmpUser := module.GYamlConf.Global.User
94 | tmpFeSshPort := module.GYamlConf.FeServers[i].SshPort
95 | tmpTargetFeConfPath := module.GYamlConf.FeServers[i].DeployDir + "/conf/fe.conf"
96 | utl.UploadFile(tmpUser, tmpKeyFile, tmpFeHost, tmpFeSshPort, tmpFeTargetConfFile, tmpTargetFeConfPath)
97 |
98 |
99 | }
100 |
101 | // modify BE config
102 | for i := 0; i < len(module.GYamlConf.BeServers); i++ {
103 | // copy BE config file
104 | tmpBeHost := module.GYamlConf.BeServers[i].Host
105 | tmpBeHeartbeatServicePort := module.GYamlConf.BeServers[i].HeartbeatServicePort
106 | tmpBeSourceConfFile := fmt.Sprintf("%s/StarRocks-%s/be/conf/be.conf", module.GDownloadPath, strings.Replace(module.GSRVersion, "v", "", -1))
107 | // tmpBeSourceConfFile := fmt.Sprintf("%s/download/StarRocks-%s/be/conf/be.conf", module.GSRCtlRoot, strings.Replace(module.GSRVersion, "v", "", -1))
108 | tmpBeTargetConfFile := fmt.Sprintf("%s/tmp/be.conf-%s-%d", module.GSRCtlRoot, tmpBeHost, tmpBeHeartbeatServicePort)
109 |
110 | err := copyConfigFile(tmpBeSourceConfFile, tmpBeTargetConfFile)
111 | if err != nil {
112 | infoMess = fmt.Sprintf("Error in modifing BE cluster configuration. Copy configuration file failed [sourceFile = %s, targetFile = %s]", tmpBeSourceConfFile, tmpBeTargetConfFile)
113 | utl.Log("ERROR", infoMess)
114 | panic(err)
115 | }
116 |
117 | // append new config into tmp configuration file
118 | // add network priority config
119 | //tmpConfigKey = "priority_networks"
120 | //tmpConfigValue = module.GYamlConf.BeServers[i].PriorityNetworks
121 | //appendConfig(tmpBeTargetConfFile, tmpConfigKey, tmpConfigValue)
122 | configMap = make(map[string] string)
123 | configMap["priority_networks"] = module.GYamlConf.BeServers[i].PriorityNetworks
124 | configMap["be_port"] = strconv.Itoa(module.GYamlConf.BeServers[i].BePort)
125 | configMap["webserver_port"] = strconv.Itoa(module.GYamlConf.BeServers[i].WebServerPort)
126 | configMap["heartbeat_service_port"] = strconv.Itoa(module.GYamlConf.BeServers[i].HeartbeatServicePort)
127 | configMap["brpc_port"] = strconv.Itoa(module.GYamlConf.BeServers[i].BrpcPort)
128 | for k, v := range configMap {
129 | if v != "0" {
130 | appendConfig(tmpBeTargetConfFile, k, v)
131 | }
132 | }
133 |
134 | for k, v := range module.GYamlConf.BeServers[i].Config {
135 | appendConfig(tmpBeTargetConfFile, k, v)
136 | }
137 |
138 |
139 | // distribute tmp fe configuration file
140 | tmpBeSshPort := module.GYamlConf.BeServers[i].SshPort
141 | tmpTargetBeConfPath := module.GYamlConf.BeServers[i].DeployDir + "/conf/be.conf"
142 | utl.UploadFile(tmpUser, tmpKeyFile, tmpBeHost, tmpBeSshPort, tmpBeTargetConfFile, tmpTargetBeConfPath)
143 |
144 | }
145 |
146 |
147 | }
148 |
149 | func copyConfigFile(sourceFile string, targetFile string) (err error){
150 |
151 | var infoMess string
152 |
153 | fileByte, err := utl.CopyFile(sourceFile, targetFile)
154 | if err != nil || fileByte == 0 {
155 | infoMess = fmt.Sprintf("Error in copy fe config file, [sourceFile = %s, targetFile = %s, fileByte = %d, error = %v]", sourceFile, targetFile, fileByte, err)
156 | utl.Log("ERROR", infoMess)
157 | return err
158 | }
159 |
160 | return nil
161 |
162 | }
163 |
164 | func appendConfig(configFile string, configKey string, configValue string) {
165 |
166 | var infoMess string
167 |
168 | err := utl.AppendConfig(configFile, configKey, configValue)
169 | if err != nil {
170 | infoMess = fmt.Sprintf("Error in append new configuration to tmp config file [configFile = %s, configKey = %s, configValue = %s]", configFile, configKey, configValue)
171 | utl.Log("ERROR", infoMess)
172 | panic(err)
173 | }
174 | infoMess = fmt.Sprintf("Append new configuration to tmp config file [configFile = %s, configKey = %s, configValue = %s]", configFile, configKey, configValue)
175 | utl.Log("DEBUG", infoMess)
176 |
177 | }
178 |
179 |
180 |
--------------------------------------------------------------------------------
/cluster/parseConfig/.getConfig.go.swp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wangtianyi2004/starrocks-controller/e01b4bf75a2b210e85e4844b127485a2390b6d17/cluster/parseConfig/.getConfig.go.swp
--------------------------------------------------------------------------------
/cluster/prepareOption/createDir.go:
--------------------------------------------------------------------------------
1 | package prepareOption
2 |
3 | import(
4 | "stargo/sr-utl"
5 | "stargo/module"
6 | "fmt"
7 | )
8 |
9 |
10 | func CreateDir() {
11 |
12 | var infoMess string
13 |
14 | infoMess = "Create the deploy folder ..."
15 | utl.Log("OUTPUT", infoMess)
16 | CreateiSrCtlDir()
17 | CreateFeDir()
18 | CreateBeDir()
19 |
20 | }
21 |
22 | func CreateiSrCtlDir() {
23 |
24 | // create SrCtlDir
25 | // SRCTLROOT/{download,tmp}
26 |
27 | utl.MkDir(module.GSRCtlRoot+"/tmp")
28 | utl.MkDir(module.GSRCtlRoot+"/download")
29 | utl.MkDir(module.GSRCtlRoot+"/cluster")
30 |
31 | }
32 |
33 | func CreateFeDir() {
34 |
35 | var infoMess string
36 | var errMess string
37 | var cmd string
38 | var err error
39 | //var outPut []byte
40 |
41 | sshUser := module.GYamlConf.Global.User
42 | sshKeyRsaFile := module.GSshKeyRsa
43 |
44 | for i := 0; i < len(module.GYamlConf.FeServers); i++ {
45 | sshHost := module.GYamlConf.FeServers[i].Host
46 | sshPort := module.GYamlConf.FeServers[i].SshPort
47 |
48 | // create DEPLOY dir for FE nodes
49 | cmd = fmt.Sprintf("mkdir -p %s", module.GYamlConf.FeServers[i].DeployDir)
50 | infoMess = fmt.Sprintf("Create DEPLOY Folder for FE node: %s@%s:%d \"%s\"", sshUser, sshHost, sshPort, cmd)
51 | utl.Log("DEBUG", infoMess)
52 |
53 | _, err = utl.SshRun(sshUser, sshKeyRsaFile, sshHost, sshPort, cmd)
54 | if err != nil {
55 | errMess = fmt.Sprintf("ERROR in creating DEPLOY folder for FE node: %s@%s:%d \"%s\"", sshUser, sshHost, sshPort, cmd)
56 | utl.Log("ERROR", errMess)
57 | panic(err)
58 | }
59 |
60 | // create META dir for FE nodes
61 | cmd = fmt.Sprintf("mkdir -p %s", module.GYamlConf.FeServers[i].MetaDir)
62 | infoMess = fmt.Sprintf("Create META Folder for FE node: %s@%s:%d \"%s\"", sshUser, sshHost, sshPort, cmd)
63 | utl.Log("DEBUG", infoMess)
64 |
65 | _, err = utl.SshRun(sshUser, sshKeyRsaFile, sshHost, sshPort, cmd)
66 | if err != nil {
67 | errMess = fmt.Sprintf("ERROR in creating META folder for FE node: %s@%s:%d \"%s\"", sshUser, sshHost, sshPort, cmd)
68 | utl.Log("ERROR", errMess)
69 | panic(err)
70 | }
71 |
72 | if module.GYamlConf.FeServers[i].DeployDir + "/meta" != module.GYamlConf.FeServers[i].MetaDir {
73 | cmd = fmt.Sprintf("ln -s %s %s", module.GYamlConf.FeServers[i].MetaDir, module.GYamlConf.FeServers[i].DeployDir + "/meta")
74 | infoMess = fmt.Sprintf("Detect MetaDir isn't under DeployDir, Create the soft link, CMD %s", cmd)
75 | utl.Log("DEBUG", infoMess)
76 | _, err := utl.SshRun(sshUser, sshKeyRsaFile, sshHost, sshPort, cmd)
77 | if err != nil {
78 | errMess = fmt.Sprintf("Error in create soft link for MetaDir, CMD %s", cmd)
79 | utl.Log("ERROR", errMess)
80 | panic(err)
81 | }
82 | }
83 |
84 | // create LOG dir for FE nodes
85 | cmd = fmt.Sprintf("mkdir -p %s", module.GYamlConf.FeServers[i].LogDir)
86 | infoMess = fmt.Sprintf("Create LOG Folder for FE node: %s@%s:%d \"%s\"", sshUser, sshHost, sshPort, cmd)
87 | utl.Log("DEBUG", infoMess)
88 |
89 | _, err = utl.SshRun(sshUser, sshKeyRsaFile, sshHost, sshPort, cmd)
90 | if err != nil {
91 | errMess = fmt.Sprintf("ERROR in creating LOG folder for FE node: %s@%s:%d \"%s\"", sshUser, sshHost, sshPort, cmd)
92 | utl.Log("ERROR", errMess)
93 | panic(err)
94 | }
95 | }
96 | }
97 |
98 |
99 |
100 |
101 | func CreateBeDir() {
102 | var infoMess string
103 | var errMess string
104 | var cmd string
105 | var err error
106 | //var outPut []byte
107 |
108 | sshUser := module.GYamlConf.Global.User
109 | sshKeyRsaFile := module.GSshKeyRsa
110 |
111 | for i := 0; i < len(module.GYamlConf.BeServers); i++ {
112 | sshHost := module.GYamlConf.BeServers[i].Host
113 | sshPort := module.GYamlConf.BeServers[i].SshPort
114 |
115 | // create DEPLOY dir for BE nodes
116 | cmd = fmt.Sprintf("mkdir -p %s", module.GYamlConf.BeServers[i].DeployDir)
117 | infoMess = fmt.Sprintf("Create DEPLOY Folder for BE node: %s@%s:%d \"%s\"", sshUser, sshHost, sshPort, cmd)
118 | utl.Log("DEBUG", infoMess)
119 |
120 | _, err = utl.SshRun(sshUser, sshKeyRsaFile, sshHost, sshPort, cmd)
121 | if err != nil {
122 | errMess = fmt.Sprintf("ERROR in creating DEPLOY folder for BE node: %s@%s:%d \"%s\"", sshUser, sshHost, sshPort, cmd)
123 | utl.Log("ERROR", errMess)
124 | panic(err)
125 | }
126 |
127 | // create STORAGE dir for BE nodes
128 | cmd = fmt.Sprintf("mkdir -p %s", module.GYamlConf.BeServers[i].StorageDir)
129 | infoMess = fmt.Sprintf("Create Storage Folder for BE node: %s@%s:%d \"%s\"", sshUser, sshHost, sshPort, cmd)
130 | utl.Log("DEBUG", infoMess)
131 |
132 | _, err = utl.SshRun(sshUser, sshKeyRsaFile, sshHost, sshPort, cmd)
133 | if err != nil {
134 | errMess = fmt.Sprintf("ERROR in creating STORAGE folder for BE node: %s@%s:%d \"%s\"", sshUser, sshHost, sshPort, cmd)
135 | utl.Log("ERROR", errMess)
136 | panic(err)
137 | }
138 |
139 | if module.GYamlConf.BeServers[i].DeployDir + "/storage" != module.GYamlConf.BeServers[i].StorageDir {
140 | cmd = fmt.Sprintf("ln -s %s %s", module.GYamlConf.BeServers[i].StorageDir, module.GYamlConf.BeServers[i].DeployDir + "/storage")
141 | infoMess = fmt.Sprintf("Detect StorageDir isn't under DeployDir, Create the soft link, CMD %s", cmd)
142 | utl.Log("DEBUG", infoMess)
143 | _, err := utl.SshRun(sshUser, sshKeyRsaFile, sshHost, sshPort, cmd)
144 | if err != nil {
145 | errMess = fmt.Sprintf("Error in create soft link for StorageDir, CMD %s", cmd)
146 | utl.Log("ERROR", errMess)
147 | panic(err)
148 | }
149 | }
150 |
151 | // create LOG dir for BE nodes
152 | cmd = fmt.Sprintf("mkdir -p %s", module.GYamlConf.BeServers[i].LogDir)
153 | infoMess = fmt.Sprintf("Create LOG Folder for BE node: %s@%s:%d \"%s\"", sshUser, sshHost, sshPort, cmd)
154 | utl.Log("DEBUG", infoMess)
155 |
156 | _, err = utl.SshRun(sshUser, sshKeyRsaFile, sshHost, sshPort, cmd)
157 | if err != nil {
158 | errMess = fmt.Sprintf("ERROR in creating LOG folder for BE node: %s@%s:%d \"%s\"", sshUser, sshHost, sshPort, cmd)
159 | utl.Log("ERROR", errMess)
160 | panic(err)
161 | }
162 |
163 | }
164 |
165 | }
166 |
--------------------------------------------------------------------------------
/cluster/prepareOption/deploySr.go:
--------------------------------------------------------------------------------
1 | package prepareOption
2 |
3 | import(
4 | "fmt"
5 | "strings"
6 | "stargo/module"
7 | "stargo/sr-utl"
8 | )
9 |
10 |
11 | func DistributeSrDir() {
12 |
13 | var infoMess string
14 |
15 | infoMess = "Distribute FE Dir ..."
16 | utl.Log("OUTPUT", infoMess)
17 | DistributeFeDir()
18 |
19 | infoMess = "Distribute BE Dir ..."
20 | utl.Log("OUTPUT", infoMess)
21 | DistributeBeDir()
22 |
23 | //module.WriteBackMeta(module.GYamlConf, module.GWriteBackMetaPath)
24 |
25 | }
26 |
27 | func DistributeFeDir() {
28 |
29 | var infoMess string
30 | // scp -r -P 22 -i rsaKey sourceDir root@nd1:targetDir
31 | // distribute FE folder
32 | for i := 0; i < len(module.GYamlConf.FeServers); i++ {
33 |
34 | sshUser := module.GYamlConf.Global.User
35 | rsaKey := module.GSshKeyRsa
36 | sshPort := module.GYamlConf.FeServers[i].SshPort
37 | sshHost := module.GYamlConf.FeServers[i].Host
38 |
39 | //utl.UploadDir(user string, keyFile string, host string, port int, sourceDir string, targetDir string)
40 | // upload fe dir
41 | feSourceDir := fmt.Sprintf("%s/StarRocks-%s/fe", module.GDownloadPath, strings.Replace(module.GSRVersion, "v", "", -1))
42 | // feSourceDir := fmt.Sprintf("%s/download/StarRocks-%s/fe", module.GSRCtlRoot, strings.Replace(module.GSRVersion, "v", "", -1))
43 | feTargetDir := module.GYamlConf.FeServers[i].DeployDir
44 | utl.UploadDir(sshUser, rsaKey, sshHost, sshPort, feSourceDir, feTargetDir)
45 | infoMess = fmt.Sprintf("Upload dir feSourceDir = [%s] to feTargetDir = [%s] on FeHost = [%s]", feSourceDir, feTargetDir, sshHost)
46 | utl.Log("INFO", infoMess)
47 |
48 | // upload jdk dir
49 | jdkSourceDir := fmt.Sprintf("%s/jdk1.8.0_301", module.GDownloadPath)
50 | // jdkSourceDir := fmt.Sprintf("%s/download/jdk1.8.0_301", module.GSRCtlRoot)
51 | jdkTargetDir := fmt.Sprintf("%s/jdk", module.GYamlConf.FeServers[i].DeployDir)
52 | utl.UploadDir(sshUser, rsaKey, sshHost, sshPort, jdkSourceDir, jdkTargetDir)
53 | infoMess = fmt.Sprintf("Upload dir JDKSourceDir = [%s] to JDKTargetDir = [%s] on FeHost = [%s]", jdkSourceDir, jdkTargetDir, sshHost)
54 | utl.Log("INFO", infoMess)
55 |
56 |
57 | // modify JAVA_HOME
58 | startFeFilePath := fmt.Sprintf("%s/bin/start_fe.sh", module.GYamlConf.FeServers[i].DeployDir)
59 | jdkPath := fmt.Sprintf("%s/jdk", module.GYamlConf.FeServers[i].DeployDir)
60 | modifyJavaHome(sshUser, rsaKey, sshHost, sshPort, startFeFilePath, jdkPath)
61 | infoMess = fmt.Sprintf("Modify JAVA_HOME: host = [%s], filePath = [%s]", sshHost, startFeFilePath)
62 | utl.Log("INFO", infoMess)
63 |
64 | }
65 |
66 | }
67 |
68 |
69 |
70 |
71 | func DistributeBeDir() {
72 |
73 | var infoMess string
74 | // scp -r -P 22 -i rsaKey sourceDir root@nd1:targetDir
75 | // distribute FE folder
76 | for i := 0; i < len(module.GYamlConf.BeServers); i++ {
77 |
78 | sshUser := module.GYamlConf.Global.User
79 | rsaKey := module.GSshKeyRsa
80 | sshPort := module.GYamlConf.BeServers[i].SshPort
81 | sshHost := module.GYamlConf.BeServers[i].Host
82 | beSourceDir := fmt.Sprintf("%s/StarRocks-%s/be", module.GDownloadPath, strings.Replace(module.GSRVersion, "v", "", -1))
83 | // beSourceDir := fmt.Sprintf("%s/download/StarRocks-%s/be", module.GSRCtlRoot, strings.Replace(module.GSRVersion, "v", "", -1))
84 | beTargetDir := module.GYamlConf.BeServers[i].DeployDir
85 |
86 | //utl.UploadDir(user string, keyFile string, host string, port int, sourceDir string, targetDir string)
87 | utl.UploadDir(sshUser, rsaKey, sshHost, sshPort, beSourceDir, beTargetDir)
88 | infoMess = fmt.Sprintf("Upload dir BeSourceDir = [%s] to BeTargetDir = [%s] on BeHost = [%s]", beSourceDir, beTargetDir, sshHost)
89 | utl.Log("INFO", infoMess)
90 |
91 | }
92 |
93 | }
94 |
95 |
96 | func modifyJavaHome(sshUser string, rsaKey string, host string, sshPort int, startFeFilePath string, jdkFilePath string) {
97 |
98 | var infoMess string
99 | var cmd string
100 | var err error
101 |
102 | // filePath = module.GYamlConf.FeServers[i].DeployDir
103 | // sed -i 's$# java$# java\nJAVA_HOME=module.GYamlConf.FeServers[i].DeployDir/fe/jdk1.8.0\n$g' filePath
104 | cmd = fmt.Sprintf("sed -i 's$# java$# java\\nJAVA_HOME=%s\\n$g' %s", jdkFilePath, startFeFilePath)
105 |
106 | _, err = utl.SshRun(sshUser, rsaKey, host, sshPort, cmd)
107 | if err != nil {
108 | infoMess = fmt.Sprintf("Error in modify JAVA_HOME. [FeHost = %s, cmd = %s, Error = %v]", host, cmd, err)
109 | utl.Log("ERROR", infoMess)
110 | panic(err)
111 | }
112 |
113 |
114 | }
115 |
116 |
--------------------------------------------------------------------------------
/cluster/prepareOption/downloadPkg.go:
--------------------------------------------------------------------------------
1 | package prepareOption
2 |
3 |
4 | import (
5 | "stargo/sr-utl"
6 | "stargo/module"
7 | "fmt"
8 | "os"
9 | "io/ioutil"
10 | "net/http"
11 | "strings"
12 | )
13 |
14 |
15 |
16 | func PrepareSRPkg() {
17 |
18 | var infoMess string
19 |
20 | infoMess = "Download StarRocks package & jdk ..."
21 | utl.Log("OUTPUT", infoMess)
22 | DownloadSRPkg()
23 |
24 | infoMess = "Decompress StarRocks pakcage & jdk ..."
25 | utl.Log("OUTPUT", infoMess)
26 | DecompressSRPkg()
27 | }
28 |
29 |
30 | func GetDownloadUrl(srVersion string) (downloadUrl string) {
31 |
32 | var infoMess string
33 | var repoUrl string
34 |
35 | module.GetRepo()
36 |
37 | // deal with
38 | if strings.Contains(module.GRepo.Repo, "file://") {
39 | downloadUrl = ""
40 | return downloadUrl
41 | }
42 |
43 | repoUrl = module.GRepo.Repo + "/packageVersion.list"
44 | res, err := http.Get(repoUrl)
45 | defer res.Body.Close()
46 | if err != nil {
47 | infoMess = fmt.Sprintf("Error in create http get request when download the repo list. [error = %v]", err)
48 | utl.Log("ERROR", infoMess)
49 | os.Exit(1)
50 | }
51 |
52 | robots, err := ioutil.ReadAll(res.Body)
53 | if err != nil{
54 | infoMess = fmt.Sprintf("Error in read body.[error = %v]", err)
55 | utl.Log("ERROR", infoMess)
56 | os.Exit(1)
57 | }
58 |
59 |
60 | versionList := strings.Split(string(robots), "\n")
61 | vLabel := fmt.Sprintf("[%s]", srVersion)
62 |
63 | for i := 0; i < len(versionList); i++ {
64 | if strings.Contains(versionList[i], vLabel) {
65 | downloadUrl = strings.Replace(versionList[i], vLabel, "", -1)
66 | downloadUrl = strings.Replace(downloadUrl, " ", "", -1)
67 | break
68 | } else {
69 | downloadUrl = ""
70 | }
71 | }
72 |
73 | if downloadUrl == "" {
74 | infoMess = fmt.Sprintf("Error in get version %s package, pls check it again. [DownloadUrl = %s]", srVersion, downloadUrl)
75 | utl.Log("ERROR", infoMess)
76 | os.Exit(1)
77 | }
78 | return downloadUrl
79 |
80 | }
81 |
82 | func DownloadSRPkg() {
83 |
84 |
85 | // download sr & jdk union package
86 | // "http://cdn-thirdparty.starrocks.com/starrocks-2.0.1-quickstart.tar.gz?Expires=10282764349&OSSAccessKeyId=LTAI4GFYjbX9e7QmFnAAvkt8&Signature=kXpA4RHT3sg4Lz9vyRJtbnPdmqM%3D"
87 | // "http://cdn-thirdparty.starrocks.com/starrocks-2.1.3-quickstart.tar.gz?Expires=2511847820&OSSAccessKeyId=LTAI4GFYjbX9e7QmFnAAvkt8&Signature=izihf34yKm7ppk5DENn8jEO2vuw%3D"
88 | // fmt.Sprintf("http://192.168.88.89:9000/starrocks-quick-start/starrocks-%s-quickstart.tar.gz", strings.Replace(module.GSRVersion, "v", "", -1))
89 |
90 | var infoMess string
91 | pkgUrl := GetDownloadUrl(module.GSRVersion)
92 |
93 | // downloadPath := module.GSRCtlRoot + "/download"
94 | if pkgUrl != "" {
95 | downloadFile := fmt.Sprintf("starrocks-%s-quickstart.tar.gz", strings.Replace(module.GSRVersion, "v", "", -1))
96 | utl.DownloadFile(pkgUrl, module.GDownloadPath, downloadFile)
97 | infoMess = fmt.Sprintf("Download done.")
98 | utl.Log("OUTPUT", infoMess)
99 | }
100 | }
101 |
102 |
103 | func DecompressSRPkg() {
104 |
105 |
106 | var tarFileName string
107 | var destFilePath string
108 | var infoMess string
109 |
110 | // Decompress SR & JDK union pakcage
111 | tarFileName = fmt.Sprintf("%s/starrocks-%s-quickstart.tar.gz", module.GDownloadPath, strings.Replace(module.GSRVersion, "v", "", -1))
112 | //tarFileName = module.GSRCtlRoot + fmt.Sprintf("/download/starrocks-%s-quickstart.tar.gz", strings.Replace(module.GSRVersion, "v", "", -1))
113 | // destFilePath = module.GSRCtlRoot + "/download"
114 | destFilePath = module.GDownloadPath
115 | utl.UnTargz(tarFileName, destFilePath)
116 | infoMess = fmt.Sprintf("The tar file %s has been decompressed under %s", tarFileName, destFilePath)
117 | utl.Log("INFO", infoMess)
118 |
119 | // Decompress StarRocks Package
120 | tarFileName = fmt.Sprintf("%s/StarRocks-%s.tar.gz", module.GDownloadPath, strings.Replace(module.GSRVersion, "v", "", -1))
121 | // tarFileName = module.GSRCtlRoot + fmt.Sprintf("/download/StarRocks-%s.tar.gz", strings.Replace(module.GSRVersion, "v", "", -1))
122 | // destFilePath = module.GSRCtlRoot + "/download"
123 | destFilePath = module.GDownloadPath
124 | utl.UnTargz(tarFileName, destFilePath)
125 | infoMess = fmt.Sprintf("The tar file %s has been decompressed under %s", tarFileName, destFilePath)
126 | utl.Log("INFO", infoMess)
127 |
128 | // Decompress JDK Package
129 | tarFileName = module.GDownloadPath + "/jdk-8u301-linux-x64.tar.gz"
130 | // tarFileName = module.GSRCtlRoot + "/download/jdk-8u301-linux-x64.tar.gz"
131 | destFilePath = module.GDownloadPath
132 | // destFilePath = module.GSRCtlRoot + "/download"
133 | utl.UnTargz(tarFileName, destFilePath)
134 | infoMess = fmt.Sprintf("The tar file %s has been decompressed under %s", tarFileName, destFilePath)
135 | utl.Log("INFO", infoMess)
136 |
137 | }
138 |
139 |
140 |
141 |
--------------------------------------------------------------------------------
/cluster/scaleOut.bak/.scaleFe.go.swp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wangtianyi2004/starrocks-controller/e01b4bf75a2b210e85e4844b127485a2390b6d17/cluster/scaleOut.bak/.scaleFe.go.swp
--------------------------------------------------------------------------------
/cluster/scaleOut.bak/scaleOutFe.go:
--------------------------------------------------------------------------------
1 | package scaleOutCluster
2 |
3 | import (
4 | "fmt"
5 | "time"
6 | "stargo/sr-utl"
7 | "stargo/module"
8 | "stargo/cluster/startCluster"
9 | "stargo/cluster/checkStatus"
10 | )
11 |
12 |
13 |
14 | func ScaleOutFeCluster() {
15 |
16 | var infoMess string
17 | var err error
18 | var feStat checkStatus.FeStatusStruct
19 |
20 | // start Fe node one by one
21 | var tmpUser string
22 | var tmpKeyRsa string
23 | var tmpSshHost string
24 | var tmpSshPort int
25 | var tmpEditLogPort int
26 | var tmpQueryPort int
27 | var tmpFeDeployDir string
28 | var feStatusList string
29 |
30 | tmpUser = module.GYamlConf.Global.User
31 | tmpKeyRsa = module.GSshKeyRsa
32 |
33 | // GYamlConfAppend is for scale-out.yaml (node6)
34 | // GYamlConf is for sr-c1/meta.yaml (node3,4,5)
35 |
36 |
37 | for i := 0; i < len(module.GYamlConfAppend.FeServers); i++ {
38 |
39 | tmpSshHost = module.GYamlConfAppend.FeServers[i].Host
40 | tmpSshPort = module.GYamlConfAppend.FeServers[i].SshPort
41 | tmpEditLogPort = module.GYamlConfAppend.FeServers[i].EditLogPort
42 | tmpQueryPort = module.GYamlConfAppend.FeServers[i].QueryPort
43 | tmpFeDeployDir = module.GYamlConfAppend.FeServers[i].DeployDir
44 |
45 | //infoMess = fmt.Sprintf("Starting FE node [FeHost = %s, FeEditLogPort = %d]", tmpSshHost, tmpEditLogPort)
46 | //utl.Log("INFO", infoMess)
47 |
48 | for startTimeInd := 0; startTimeInd < 3; startTimeInd++ {
49 | // initFeNode(user string, keyRsa string, sshHost string, sshPort int, editLogPort int, feDeployDir string) (err error)
50 | infoMess = fmt.Sprintf("The %d time to start [%s]", (startTimeInd + 1), tmpSshHost)
51 | utl.Log("DEBUG", infoMess)
52 | err = startCluster.InitFeNode(tmpUser, tmpKeyRsa, tmpSshHost, tmpSshPort, tmpEditLogPort, tmpFeDeployDir)
53 | startWaitTime := time.Duration(20 - startTimeInd * 5)
54 | time.Sleep(startWaitTime * time.Second)
55 |
56 | feStat, err = checkStatus.CheckFeStatus(i)
57 |
58 | if err != nil {
59 | infoMess = fmt.Sprintf("Error in get the fe status [FeHost = %s, error = %v]", tmpSshHost, err)
60 | utl.Log("DEBUG", infoMess)
61 | }
62 | if feStat.FeAlive {
63 | infoMess = fmt.Sprintf("The FE node start succefully [host = %s, queryPort = %d]", tmpSshHost, tmpQueryPort)
64 | utl.Log("INFO", infoMess)
65 | break
66 | } else {
67 | infoMess = fmt.Sprintf("The FE node doesn't start, wait for 10s [FeHost = %s, FeQueryPort = %d, error = %v]", tmpSshHost, tmpQueryPort, err)
68 | utl.Log("WARN", infoMess)
69 | }
70 | } // FOR-END: 3 time to restart FE node
71 |
72 | if !feStat.FeAlive {
73 | infoMess = fmt.Sprintf("The FE node start failed [host = %s, queryPort = %d, error = %v]", tmpSshHost, tmpQueryPort, err)
74 | utl.Log("ERROR", infoMess)
75 | }
76 | feStatusList = feStatusList + " " + fmt.Sprintf("feHost = %-20sfeQueryPort = %d feStatus = true\n", tmpSshHost, tmpQueryPort)
77 | } // FOR-END: list all FE node
78 |
79 | feStatusList = "List all FE status:\n" + feStatusList
80 | utl.Log("INFO", feStatusList)
81 |
82 | }
83 |
--------------------------------------------------------------------------------
/cluster/startCluster/.startFe.go.swp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wangtianyi2004/starrocks-controller/e01b4bf75a2b210e85e4844b127485a2390b6d17/cluster/startCluster/.startFe.go.swp
--------------------------------------------------------------------------------
/cluster/startCluster/initBe.go:
--------------------------------------------------------------------------------
1 |
2 | package startCluster
3 |
4 | import(
5 | "fmt"
6 | "time"
7 | "errors"
8 | "stargo/sr-utl"
9 | "stargo/module"
10 | "stargo/cluster/checkStatus"
11 | )
12 |
13 |
14 |
15 |
16 | func InitBeCluster(yamlConf *module.ConfStruct) {
17 |
18 | var infoMess string
19 | var err error
20 | var beStat map[string]string
21 |
22 | // start Fe node one by one
23 | var tmpUser string
24 | var tmpKeyRsa string
25 | var tmpSshHost string
26 | var tmpSshPort int
27 | var tmpHeartbeatServicePort int
28 | var tmpBeDeployDir string
29 | var beStatusList string
30 | // var tmpFeEntryHost string
31 | // var tmpFeEntryPort int
32 | tmpUser = module.GYamlConf.Global.User
33 | tmpKeyRsa = module.GSshKeyRsa
34 |
35 | // get FE entry
36 | feEntryId, err := checkStatus.GetFeEntry(-1)
37 | //tmpFeEntryHost = yamlConf.FeServers[feEntryId].Host
38 | //tmpFeEntryPort = yamlConf.FeServers[feEntryId].QueryPort
39 | module.SetFeEntry(feEntryId)
40 | if err != nil || feEntryId == -1 {
41 | infoMess = "Error in get the FE entry, pls check FE status."
42 | utl.Log("ERROR", infoMess)
43 | err = errors.New(infoMess)
44 | panic(err)
45 | }
46 |
47 |
48 |
49 | for i := 0; i < len(yamlConf.BeServers); i++ {
50 |
51 | tmpSshHost = yamlConf.BeServers[i].Host
52 | tmpSshPort = yamlConf.BeServers[i].SshPort
53 | tmpHeartbeatServicePort = yamlConf.BeServers[i].HeartbeatServicePort
54 | tmpBeDeployDir = yamlConf.BeServers[i].DeployDir
55 |
56 | infoMess = fmt.Sprintf("Starting BE node [BeHost = %s HeartbeatServicePort = %d]", tmpSshHost, tmpHeartbeatServicePort)
57 | utl.Log("INFO", infoMess)
58 |
59 | for startTimeInd := 0; startTimeInd < 3; startTimeInd++ {
60 |
61 | infoMess = fmt.Sprintf("The %d time to start [%s]",(startTimeInd + 1), tmpSshHost)
62 | utl.Log("DEBUG", infoMess)
63 | // startBeNode(user string, keyRsa string, sshHost string, sshPort int, heartbeatServicePort int, beDeployDir string) (err error)
64 | err = initBeNode(tmpUser, tmpKeyRsa, tmpSshHost, tmpSshPort, tmpHeartbeatServicePort, tmpBeDeployDir)
65 |
66 | startWaitTime := time.Duration(20 - startTimeInd * 5)
67 | // the be process need 20s to startup
68 | time.Sleep(startWaitTime * time.Second)
69 |
70 | beStat, _ = checkStatus.CheckBeStatus(i)
71 | if beStat["Alive"] == "true" {
72 | infoMess = fmt.Sprintf("The BE node start succefully [host = %s, heartbeatServicePort = %d]", tmpSshHost, tmpHeartbeatServicePort)
73 | utl.Log("INFO", infoMess)
74 | break
75 | } else {
76 | infoMess = fmt.Sprintf("The BE node doesn't start, wait for 10s [BeHost = %s, HeartbeatServicePort = %d, error = %v]", tmpSshHost, tmpHeartbeatServicePort, err)
77 | utl.Log("WARN", infoMess)
78 | }
79 | } // FOR-END: 3 time to restart BE node
80 |
81 | if beStat["Alive"] == "false" {
82 | infoMess = fmt.Sprintf("The BE node start failed [BeHost = %s, HeartbeatServicePort = %d, error = %v]", tmpSshHost, tmpHeartbeatServicePort, err)
83 | }
84 |
85 | beStatusList = beStatusList + " " + fmt.Sprintf("beHost = %-20sbeHeartbeatServicePort = %d\tbeStatus = %v\n", tmpSshHost, tmpHeartbeatServicePort, beStat["Alive"])
86 | }
87 | beStatusList = "List all BE status:\n" + beStatusList
88 | utl.Log("OUTPUT", beStatusList)
89 | }
90 |
91 | func initBeNode(user string, keyRsa string, sshHost string, sshPort int, heartbeatServicePort int, beDeployDir string) (err error) {
92 |
93 | var infoMess string
94 |
95 |
96 | addBeSQL := fmt.Sprintf("alter system add backend \"%s:%d\"", sshHost, heartbeatServicePort)
97 | addBeCMD := fmt.Sprintf("%s/bin/start_be.sh --daemon", beDeployDir)
98 |
99 | //infoMess = fmt.Sprintf("Starting BE node [host = %s, heartbeatServicePort = %d]", sshHost, heartbeatServicePort)
100 | //utl.Log("INFO", infoMess)
101 |
102 | // alter system add backend "sshHost:heartbeatServicePort"
103 | sqlUserName := "root"
104 | sqlPassword := ""
105 | sqlIp := module.GFeEntryHost
106 | sqlPort := module.GFeEntryQueryPort
107 | sqlDbName := ""
108 |
109 | _, err = utl.RunSQL(sqlUserName, sqlPassword, sqlIp, sqlPort, sqlDbName, addBeSQL)
110 | if err != nil {
111 | infoMess = fmt.Sprintf(`Error in add follower BE node, [
112 | sqlUserName = %s
113 | sqlPassword = %s
114 | sqlIP = %s
115 | sqlPort = %d
116 | sqlDBName = %s
117 | addFollowerSQL =%s
118 | errMess = %v]`, sqlUserName, sqlPassword, sqlIp, sqlPort, sqlDbName, addBeSQL, err)
119 | utl.Log("ERROR", infoMess)
120 | return err
121 | }
122 |
123 | // run beDeploy/bin/start_be.sh --daemon
124 | _, err = utl.SshRun(user, keyRsa, sshHost, sshPort, addBeCMD)
125 | if err != nil {
126 | infoMess = fmt.Sprintf(`Waiting for startMastertFeNode:
127 | user = %s
128 | keyRsa = %s
129 | sshHost = %s
130 | sshPort = %d
131 | beDeployDir = %s`,
132 | user, keyRsa, sshHost, sshPort, beDeployDir)
133 | utl.Log("WARN", infoMess)
134 | return err
135 | }
136 |
137 | // time.Sleep(5 * time.Second)
138 | return nil
139 |
140 | }
141 |
--------------------------------------------------------------------------------
/cluster/startCluster/initFe.go:
--------------------------------------------------------------------------------
1 | package startCluster
2 |
3 | import (
4 | "fmt"
5 | "time"
6 | "strings"
7 | "stargo/sr-utl"
8 | "stargo/module"
9 | "stargo/cluster/checkStatus"
10 | )
11 |
12 |
13 | func InitFeCluster(yamlConf *module.ConfStruct) {
14 |
15 | var infoMess string
16 | var err error
17 | var feStat map[string]string
18 |
19 | // start Fe node one by one
20 | var tmpUser string
21 | var tmpKeyRsa string
22 | var tmpSshHost string
23 | var tmpSshPort int
24 | var tmpEditLogPort int
25 | var tmpQueryPort int
26 | var tmpFeDeployDir string
27 | var feStatusList string
28 | var feEntryId int
29 | tmpUser = yamlConf.Global.User
30 | tmpKeyRsa = module.GSshKeyRsa
31 |
32 |
33 | // get FE entry
34 | feEntryId, err = checkStatus.GetFeEntry(-1)
35 |
36 | if err != nil || feEntryId == -1 {
37 | //infoMess = "All FE nodes are down, please start FE node and display the cluster status again."
38 | //utl.Log("WARN", infoMess)
39 | module.SetFeEntry(0)
40 | } else {
41 | module.SetFeEntry(feEntryId)
42 | }
43 |
44 | for i := 0; i < len(yamlConf.FeServers); i++ {
45 | // for i := 0; i < 1; i++ { ## debug leader node
46 |
47 | tmpSshHost = yamlConf.FeServers[i].Host
48 | tmpSshPort = yamlConf.FeServers[i].SshPort
49 | tmpEditLogPort = yamlConf.FeServers[i].EditLogPort
50 | tmpQueryPort = yamlConf.FeServers[i].QueryPort
51 | tmpFeDeployDir = yamlConf.FeServers[i].DeployDir
52 |
53 | //infoMess = fmt.Sprintf("Starting FE node [FeHost = %s, FeEditLogPort = %d]", tmpSshHost, tmpEditLogPort)
54 | //utl.Log("INFO", infoMess)
55 |
56 | for startTimeInd := 0; startTimeInd < 3; startTimeInd++ {
57 | // initFeNode(user string, keyRsa string, sshHost string, sshPort int, editLogPort int, feDeployDir string) (err error)
58 | infoMess = fmt.Sprintf("The %d time to start [%s]", (startTimeInd + 1), tmpSshHost)
59 | utl.Log("DEBUG", infoMess)
60 | err = InitFeNode(tmpUser, tmpKeyRsa, tmpSshHost, tmpSshPort, tmpEditLogPort, tmpFeDeployDir)
61 | startWaitTime := time.Duration(20 - startTimeInd * 5)
62 | time.Sleep(startWaitTime * time.Second)
63 |
64 | feStat, err = checkStatus.CheckFeStatus(i)
65 |
66 | if err != nil {
67 | infoMess = fmt.Sprintf("Error in get the fe status [FeHost = %s, error = %v]", tmpSshHost, err)
68 | utl.Log("DEBUG", infoMess)
69 | }
70 | if feStat["Alive"] == "true" {
71 | infoMess = fmt.Sprintf("The FE node start succefully [host = %s, queryPort = %d]", tmpSshHost, tmpQueryPort)
72 | utl.Log("INFO", infoMess)
73 | break
74 | } else {
75 | infoMess = fmt.Sprintf("The FE node doesn't start, wait for 10s [FeHost = %s, FeQueryPort = %d, error = %v]", tmpSshHost, tmpQueryPort, err)
76 | utl.Log("WARN", infoMess)
77 | }
78 | } // FOR-END: 3 time to restart FE node
79 |
80 | if feStat["Alive"] == "false" {
81 | infoMess = fmt.Sprintf("The FE node start failed [host = %s, queryPort = %d, error = %v]", tmpSshHost, tmpQueryPort, err)
82 | utl.Log("ERROR", infoMess)
83 | }
84 | feStatusList = feStatusList + " " + fmt.Sprintf("feHost = %-20sfeQueryPort = %d feStatus = true\n", tmpSshHost, tmpQueryPort)
85 | } // FOR-END: list all FE node
86 |
87 | feStatusList = "List all FE status:\n" + feStatusList
88 | utl.Log("INFO", feStatusList)
89 |
90 | }
91 |
92 |
93 |
94 |
95 | func InitFeNode(user string, keyRsa string, sshHost string, sshPort int, editLogPort int, feDeployDir string) (err error) {
96 |
97 |
98 | var infoMess string
99 | //var isMasterFe bool
100 | var startFeCmd string
101 | // check master node
102 | if sshHost == module.GYamlConf.FeServers[0].Host && editLogPort == module.GYamlConf.FeServers[0].EditLogPort {
103 | //isMasterFe = true
104 | infoMess = fmt.Sprintf("Starting leader FE node [host = %s, editLogPort = %d]", module.GYamlConf.FeServers[0].Host, module.GYamlConf.FeServers[0].EditLogPort)
105 | utl.Log("INFO", infoMess)
106 | startFeCmd = fmt.Sprintf("%s/bin/start_fe.sh --daemon", feDeployDir)
107 | // time.Sleep(30 * time.Second)
108 | } else {
109 | infoMess = fmt.Sprintf("Starting follower FE node [host = %s, editLogPort = %d]", sshHost, editLogPort)
110 | utl.Log("INFO", infoMess)
111 |
112 | startFeCmd = fmt.Sprintf("%s/bin/start_fe.sh --helper %s:%d --daemon", feDeployDir, module.GFeEntryHost, module.GFeEntryEditLogPort)
113 |
114 | // if the start node is follower node, ALTER SYSTEM ADD FOLLOWER "host:editLogPort";
115 | // func RunSQL(userName string, password string, ip string, port int, dbName string, sqlStat string) (rows *sql.Rows, err error)
116 |
117 | sqlUserName := "root"
118 | sqlPassword := ""
119 | sqlIp := module.GFeEntryHost
120 | sqlPort := module.GFeEntryQueryPort
121 | sqlDbName := ""
122 | addFollowerSql := fmt.Sprintf("ALTER SYSTEM ADD FOLLOWER \"%s:%d\"", sshHost, editLogPort)
123 | _, err := utl.RunSQL(sqlUserName, sqlPassword, sqlIp, sqlPort, sqlDbName, addFollowerSql)
124 | if err != nil {
125 | if strings.Contains(err.Error(), "frontend already exists name") {
126 | } else {
127 | infoMess = fmt.Sprintf("Error in add follower fe node [FeHost = %s, Error = %v", sqlIp, err)
128 | utl.Log("ERROR", infoMess)
129 | return err
130 | }
131 | }
132 |
133 | }
134 |
135 |
136 | // run feDeploy/bin/start_fe.sh --daemon --helper hsot:edit_log_port
137 |
138 | _, err = utl.SshRun(user, keyRsa, sshHost, sshPort, startFeCmd)
139 | if err != nil {
140 | infoMess = fmt.Sprintf("Waiting for starting FE node [FeHost = %s]", sshHost)
141 | utl.Log("DEBUG", infoMess)
142 | return err
143 | }
144 | return nil
145 | }
146 |
147 |
148 |
149 |
150 |
--------------------------------------------------------------------------------
/cluster/startCluster/startBe.go:
--------------------------------------------------------------------------------
1 |
2 | package startCluster
3 |
4 | import(
5 | "fmt"
6 | "time"
7 | // "errors"
8 | "stargo/sr-utl"
9 | "stargo/module"
10 | "stargo/cluster/checkStatus"
11 | )
12 |
13 |
14 |
15 |
16 | func StartBeCluster() {
17 |
18 |
19 | // start Be node one by one
20 | var infoMess string
21 | var tmpUser string
22 | var tmpKeyRsa string
23 | var tmpSshHost string
24 | var tmpSshPort int
25 | var tmpHeartbeatServicePort int
26 | //var tmpQueryPort int
27 | var tmpBeDeployDir string
28 |
29 | tmpUser = module.GYamlConf.Global.User
30 | tmpKeyRsa = module.GSshKeyRsa
31 |
32 | for i := 0; i < len(module.GYamlConf.BeServers); i++ {
33 | // for i := 0; i < 1; i++ { ## debug leader node
34 |
35 | tmpSshHost = module.GYamlConf.BeServers[i].Host
36 | tmpSshPort = module.GYamlConf.BeServers[i].SshPort
37 | tmpHeartbeatServicePort = module.GYamlConf.BeServers[i].HeartbeatServicePort
38 | tmpBeDeployDir = module.GYamlConf.BeServers[i].DeployDir
39 |
40 | infoMess = fmt.Sprintf("Starting BE node [BeHost = %s, HeartbeatServicePort = %d]", tmpSshHost, tmpHeartbeatServicePort)
41 | utl.Log("INFO", infoMess)
42 |
43 | _ = StartBeNode(tmpUser, tmpKeyRsa, tmpSshHost, tmpSshPort, tmpHeartbeatServicePort, tmpBeDeployDir)
44 | for j := 0; j < 3; j++ {
45 | portStat, _ := checkStatus.CheckBePortStatus(i)
46 | if portStat {
47 | break
48 | //time.Sleep(10 * time.Second)
49 | } else {
50 | time.Sleep(10 * time.Second)
51 | }
52 | }
53 | }
54 | }
55 |
56 |
57 | func StartBeNode(user string, keyRsa string, sshHost string, sshPort int, heartbeatServicePort int, beDeployDir string) (err error) {
58 |
59 | var infoMess string
60 |
61 |
62 | startBeCMD := fmt.Sprintf("%s/bin/start_be.sh --daemon", beDeployDir)
63 |
64 | infoMess = fmt.Sprintf("Starting BE node [host = %s, heartbeatServicePort = %d]", sshHost, heartbeatServicePort)
65 | utl.Log("DEBUG", infoMess)
66 |
67 |
68 | // run beDeploy/bin/start_be.sh --daemon
69 | _, err = utl.SshRun(user, keyRsa, sshHost, sshPort, startBeCMD)
70 | if err != nil {
71 | infoMess = fmt.Sprintf("Waiting for start BE node.[BeHost = %s, Error = %v", sshHost, err)
72 | utl.Log("DEBUG", infoMess)
73 | return err
74 | }
75 |
76 | // time.Sleep(5 * time.Second)
77 | return nil
78 |
79 | }
80 |
--------------------------------------------------------------------------------
/cluster/startCluster/startFe.go:
--------------------------------------------------------------------------------
1 | package startCluster
2 |
3 | import (
4 | "fmt"
5 | "time"
6 | "stargo/sr-utl"
7 | "stargo/module"
8 | "stargo/cluster/checkStatus"
9 | )
10 |
11 |
12 |
13 | func StartFeCluster() {
14 |
15 | var infoMess string
16 | //var err error
17 | //var feStat checkStatus.FeStatusStruct
18 |
19 | // start Fe node one by one
20 | var tmpUser string
21 | var tmpKeyRsa string
22 | var tmpSshHost string
23 | var tmpSshPort int
24 | var tmpEditLogPort int
25 | //var tmpQueryPort int
26 | var tmpFeDeployDir string
27 |
28 | tmpUser = module.GYamlConf.Global.User
29 | tmpKeyRsa = module.GSshKeyRsa
30 |
31 | for i := 0; i < len(module.GYamlConf.FeServers); i++ {
32 | // for i := 0; i < 1; i++ { ## debug leader node
33 |
34 | tmpSshHost = module.GYamlConf.FeServers[i].Host
35 | tmpSshPort = module.GYamlConf.FeServers[i].SshPort
36 | tmpEditLogPort = module.GYamlConf.FeServers[i].EditLogPort
37 | //tmpQueryPort = module.GYamlConf.FeServers[i].QueryPort
38 | tmpFeDeployDir = module.GYamlConf.FeServers[i].DeployDir
39 |
40 | infoMess = fmt.Sprintf("Starting FE node [FeHost = %s, EditLogPort = %d]", tmpSshHost, tmpEditLogPort)
41 | utl.Log("INFO", infoMess)
42 |
43 | // startFeNode(user string, keyRsa string, sshHost string, sshPort int, editLogPort int, feDeployDir string) (err error)
44 | _ = StartFeNode(tmpUser, tmpKeyRsa, tmpSshHost, tmpSshPort, tmpEditLogPort, tmpFeDeployDir)
45 | for j := 0; j < 3; j++ {
46 | portStat, _ := checkStatus.CheckFePortStatus(i)
47 | if portStat {
48 | break
49 | //time.Sleep(10 * time.Second)
50 | } else {
51 | time.Sleep(10 * time.Second)
52 | }
53 | }
54 | }
55 | }
56 |
57 |
58 |
59 |
60 |
61 |
62 | func StartFeNode(user string, keyRsa string, sshHost string, sshPort int, editLogPort int, feDeployDir string) (err error) {
63 |
64 |
65 | var infoMess string
66 | //var isMasterFe bool
67 | var startFeCmd string
68 |
69 | // check master node
70 | startFeCmd = fmt.Sprintf("%s/bin/start_fe.sh --daemon", feDeployDir)
71 | infoMess = fmt.Sprintf("Run starting FE process [host = %s, editLogPort = %d]", sshHost, editLogPort)
72 | utl.Log("DEBUG", infoMess)
73 | _, err = utl.SshRun(user, keyRsa, sshHost, sshPort, startFeCmd)
74 |
75 | if err != nil {
76 | infoMess = fmt.Sprintf("Waiting for starting FE node [FeHost = %s]", sshHost)
77 | utl.Log("DEBUG", infoMess)
78 | return err
79 | }
80 | return nil
81 |
82 | }
83 |
84 |
--------------------------------------------------------------------------------
/cluster/stopCluster/stopBe.go:
--------------------------------------------------------------------------------
1 | package stopCluster
2 |
3 | import (
4 | "fmt"
5 | "stargo/sr-utl"
6 | "stargo/module"
7 | "stargo/cluster/checkStatus"
8 | )
9 |
10 | // func startFeNode(user string, keyRsa string, sshHost string, sshPort int, editLogPort int, feDeployDir string) (err error) {
11 |
12 | func StopBeNode(user string, keyRsa string, sshHost string, sshPort int, beDeployDir string) (err error){
13 |
14 | var infoMess string
15 | var stopBeCmd string
16 |
17 | // /opt/starrocks/be/bin/stop_be.sh
18 | stopBeCmd = fmt.Sprintf("%s/bin/stop_be.sh", beDeployDir)
19 |
20 | infoMess = fmt.Sprintf("Waiting for stoping BE node [BeHost = %s]", sshHost)
21 | utl.Log("INFO", infoMess)
22 | _, err = utl.SshRun(user, keyRsa, sshHost, sshPort, stopBeCmd)
23 | if err != nil {
24 | infoMess = fmt.Sprintf("Stop BE failed [BeHost = %s, error = %v]", sshHost, err)
25 | utl.Log("DEBUG", infoMess)
26 | return err
27 | }
28 | return nil
29 |
30 | }
31 |
32 | func StopBeCluster(clusterName string) {
33 |
34 | var infoMess string
35 | var err error
36 | var beStat map[string]string
37 |
38 |
39 | // Stop BE node one by one
40 | var tmpUser string
41 | var tmpKeyRsa string
42 | var tmpSshHost string
43 | var tmpSshPort int
44 | var tmpBeDeployDir string
45 | var tmpHeartbeatServicePort int
46 | //var beStatusList string
47 |
48 | tmpUser = module.GYamlConf.Global.User
49 | tmpKeyRsa = module.GSshKeyRsa
50 |
51 | infoMess = "Stop cluster " + clusterName
52 | utl.Log("OUTPUT", infoMess)
53 | for i := 0; i < len(module.GYamlConf.BeServers); i++ {
54 |
55 | tmpSshHost = module.GYamlConf.BeServers[i].Host
56 | tmpSshPort = module.GYamlConf.BeServers[i].SshPort
57 | tmpBeDeployDir = module.GYamlConf.BeServers[i].DeployDir
58 | tmpHeartbeatServicePort = module.GYamlConf.BeServers[i].HeartbeatServicePort
59 | // func StopFeNode(user string, keyRsa string, sshHost string, sshPort int, feDeployDir string) (err error)
60 | err = StopBeNode(tmpUser, tmpKeyRsa, tmpSshHost, tmpSshPort, tmpBeDeployDir)
61 | if err != nil {
62 | infoMess = fmt.Sprintf("Error in stoping BE node [BeHost = %s, HeartbeatServicePort = %d, error = %v]", tmpSshHost, tmpHeartbeatServicePort, err)
63 | utl.Log("DEBUG", infoMess)
64 | }
65 |
66 | beStat, err = checkStatus.CheckBeStatus(i)
67 |
68 | if err != nil {
69 | infoMess = fmt.Sprintf("Error in get the Be status [BeHost = %s, HeartbeatServicePort = %d, error = %v]", tmpSshHost, tmpHeartbeatServicePort, err)
70 | utl.Log("DEBUG", infoMess)
71 | }
72 | if beStat["Alive"] == "false" {
73 | infoMess = fmt.Sprintf("The BE node stop succefully [BeHost = %s, HeartbeatServicePort = %d]", tmpSshHost, tmpHeartbeatServicePort)
74 | utl.Log("INFO", infoMess)
75 | } else {
76 | infoMess = fmt.Sprintf("The BE node stop failed [BeHost = %s, HeartbeatServicePort = %d]", tmpSshHost, tmpHeartbeatServicePort)
77 | utl.Log("DEBUG", infoMess)
78 | }
79 | }
80 |
81 | }
82 |
--------------------------------------------------------------------------------
/cluster/stopCluster/stopFe.go:
--------------------------------------------------------------------------------
1 | package stopCluster
2 |
3 | import (
4 | "fmt"
5 | "stargo/sr-utl"
6 | "stargo/module"
7 | "stargo/cluster/checkStatus"
8 | )
9 |
10 | // func startFeNode(user string, keyRsa string, sshHost string, sshPort int, editLogPort int, feDeployDir string) (err error) {
11 |
12 | func StopFeNode(user string, keyRsa string, sshHost string, sshPort int, feDeployDir string) (err error){
13 |
14 | var infoMess string
15 | var stopFeCmd string
16 |
17 | // /opt/starrocks/fe/bin/stop_fe.sh
18 | stopFeCmd = fmt.Sprintf("%s/bin/stop_fe.sh", feDeployDir)
19 |
20 | infoMess = fmt.Sprintf("Waiting for stoping FE node [FeHost = %s]", sshHost)
21 | utl.Log("INFO", infoMess)
22 | _, err = utl.SshRun(user, keyRsa, sshHost, sshPort, stopFeCmd)
23 | if err != nil {
24 | infoMess = fmt.Sprintf("Stop FE failed [FeHost = %s, error = %v]", sshHost, err)
25 | utl.Log("INFO", infoMess)
26 | return err
27 | }
28 | return nil
29 |
30 | }
31 |
32 | func StopFeCluster(clusterName string) {
33 |
34 | var infoMess string
35 | var err error
36 | var feStat map[string]string
37 |
38 | // start Fe node one by one
39 | var tmpUser string
40 | var tmpKeyRsa string
41 | var tmpSshHost string
42 | var tmpSshPort int
43 | var tmpFeDeployDir string
44 | var tmpFeQueryPort int
45 | //var feStatusList string
46 |
47 | tmpUser = module.GYamlConf.Global.User
48 | tmpKeyRsa = module.GSshKeyRsa
49 |
50 | infoMess = "Stop cluster " + clusterName
51 | utl.Log("OUTPUT", infoMess)
52 | for i := 0; i < len(module.GYamlConf.FeServers); i++ {
53 |
54 | tmpSshHost = module.GYamlConf.FeServers[i].Host
55 | tmpSshPort = module.GYamlConf.FeServers[i].SshPort
56 | tmpFeQueryPort = module.GYamlConf.FeServers[i].QueryPort
57 | tmpFeDeployDir = module.GYamlConf.FeServers[i].DeployDir
58 |
59 | // func StopFeNode(user string, keyRsa string, sshHost string, sshPort int, feDeployDir string) (err error)
60 | err = StopFeNode(tmpUser, tmpKeyRsa, tmpSshHost, tmpSshPort, tmpFeDeployDir)
61 | if err != nil {
62 | infoMess = fmt.Sprintf("Error in stoing FE node [FeHost = %s]", tmpSshHost)
63 | }
64 |
65 | feStat, err = checkStatus.CheckFeStatus(i)
66 |
67 | if err != nil {
68 | infoMess = fmt.Sprintf("Error in get the fe status [FeHost = %s, error = %v]", tmpSshHost, err)
69 | utl.Log("DEBUG", infoMess)
70 | }
71 | if feStat["Alive"] == "false" {
72 | infoMess = fmt.Sprintf("The FE node stop succefully [host = %s, queryPort = %d]", tmpSshHost, tmpFeQueryPort)
73 | utl.Log("OUTPUT", infoMess)
74 | } else {
75 | infoMess = fmt.Sprintf("The FE node stop failed [host = %s, queryPort = %d]", tmpSshHost, tmpFeQueryPort)
76 | utl.Log("DEBUG", infoMess)
77 | }
78 | }
79 |
80 | }
81 |
--------------------------------------------------------------------------------
/cluster/upgradeCluster/upgradeBe.go:
--------------------------------------------------------------------------------
1 | package upgradeCluster
2 |
3 | import (
4 |
5 | "fmt"
6 | "time"
7 | "strings"
8 | //"errors"
9 | "stargo/module"
10 | "stargo/sr-utl"
11 | "stargo/cluster/stopCluster"
12 | "stargo/cluster/startCluster"
13 | "stargo/cluster/checkStatus"
14 | //"stargo/cluster/prepareOption"
15 |
16 | )
17 |
18 |
19 | func UpgradeBeCluster() { //(err error){
20 |
21 | var infoMess string
22 | var err error
23 | var feEntryId int
24 |
25 |
26 | feEntryId, err = checkStatus.GetFeEntry(-1)
27 | if err != nil || feEntryId == -1 {
28 | //infoMess = "All FE nodes are down, please start FE node and display the cluster status again."
29 | //utl.Log("WARN", infoMess)
30 | module.SetFeEntry(0)
31 | } else {
32 | module.SetFeEntry(feEntryId)
33 | }
34 |
35 |
36 | for i := 0; i < len(module.GYamlConf.BeServers); i++ {
37 | infoMess = fmt.Sprintf("Starting upgrade BE node. [beId = %d]", i)
38 | utl.Log("OUTPUT", infoMess)
39 | UpgradeBeNode(i)
40 | }
41 |
42 | }
43 |
44 |
45 | func UpgradeBeNode(beId int) {
46 | // step 1. backup be lib
47 | // step 2. upload new be lib
48 | // step 3. stop be node
49 | // step 4. start be node
50 |
51 | var infoMess string
52 | var user string
53 | var sourceDir string
54 | var targetDir string
55 | var sshHost string
56 | var sshPort int
57 | var beDeployDir string
58 | var beHeartBeatServicePort int
59 | var keyRsa string
60 | var beStat map[string]string
61 | var err error
62 |
63 |
64 | user = module.GYamlConf.Global.User
65 | keyRsa = module.GSshKeyRsa
66 | sshHost = module.GYamlConf.BeServers[beId].Host
67 | sshPort = module.GYamlConf.BeServers[beId].SshPort
68 | beDeployDir = module.GYamlConf.BeServers[beId].DeployDir
69 | beHeartBeatServicePort = module.GYamlConf.BeServers[beId].HeartbeatServicePort
70 |
71 |
72 |
73 |
74 | // step1. backup be lib
75 | sourceDir = fmt.Sprintf("%s/lib", beDeployDir)
76 | targetDir = fmt.Sprintf("%s/lib.bak-%s", beDeployDir, time.Now().Format("20060102150405"))
77 |
78 | err = utl.RenameDir(user, keyRsa, sshHost, sshPort, sourceDir, targetDir)
79 | if err != nil {
80 | infoMess = fmt.Sprintf("Error in rename dir when backup be lib. [host = %s, sourceDir = %s, targetDir = %s]", sshHost, sourceDir, targetDir)
81 | utl.Log("ERROR", infoMess)
82 | } else {
83 | infoMess = fmt.Sprintf("upgrade be node - backup be lib. [host = %s, sourceDir = %s, targetDir = %s]", sshHost, sourceDir, targetDir)
84 | utl.Log("INFO", infoMess)
85 | }
86 |
87 |
88 | // step2. upload new be lib
89 | sourceDir = fmt.Sprintf("%s/StarRocks-%s/be/lib", module.GDownloadPath, strings.Replace(module.GSRVersion, "v", "", -1))
90 | // sourceDir = fmt.Sprintf("%s/download/StarRocks-%s/be/lib", module.GSRCtlRoot, strings.Replace(module.GSRVersion, "v", "", -1))
91 | targetDir = fmt.Sprintf("%s/lib", beDeployDir)
92 | utl.UploadDir(user, keyRsa, sshHost, sshPort, sourceDir, targetDir)
93 | infoMess = fmt.Sprintf("upgrade be node - upload new be lib. [host = %s, sourceDir = %s, targetDir = %s]", sshHost, sourceDir, targetDir)
94 | utl.Log("INFO", infoMess)
95 |
96 |
97 |
98 | // step3. stop be node
99 | err = stopCluster.StopBeNode(user, keyRsa, sshHost, sshPort, beDeployDir)
100 | if err != nil {
101 | infoMess = fmt.Sprintf("Error in stop be node when upgrade be node. [host = %s, beDeployDir = %s]", sshHost, beDeployDir)
102 | utl.Log("ERROR", infoMess)
103 | } else {
104 | infoMess = fmt.Sprintf("upgrade be node - stop be node. [host = %s, beDeployDir = %s]", sshHost, beDeployDir)
105 | utl.Log("INFO", infoMess)
106 | }
107 |
108 | // step4. start be node
109 |
110 | for j := 0; j < 3; j++ {
111 | startCluster.StartBeNode(user, keyRsa, sshHost, sshPort, beHeartBeatServicePort, beDeployDir)
112 | infoMess = fmt.Sprintf("upgrade be node - start be node. [host = %s, beDeployDir = %s]", sshHost, beDeployDir)
113 | utl.Log("INFO", infoMess)
114 |
115 | beStat, err = checkStatus.CheckBeStatus(beId)
116 | if beStat["Alive"] == "true" && strings.Contains(beStat["Version"], strings.Replace(module.GSRVersion, "v", "", -1)) {
117 | break
118 | }
119 | time.Sleep(10 * time.Second)
120 | }
121 |
122 | if err != nil {
123 | infoMess = fmt.Sprintf("Error in get the Be status [beId = %d, error = %v]", beId, err)
124 | utl.Log("DEBUG", infoMess)
125 | } else if beStat["Alive"] == "false" {
126 | infoMess = fmt.Sprintf("The BE node upgrade failed. The BE node doesn't work. [beId = %d]\n", beId)
127 | utl.Log("ERROR", infoMess)
128 | } else if ! strings.Contains(beStat["Version"], strings.Replace(module.GSRVersion, "v", "", -1)) {
129 | infoMess = fmt.Sprintf("The BE node upgrade failed. [beId = %d, targetVersion = %s, currentVersion = v%s]", beId, module.GSRVersion, beStat["Version"])
130 | utl.Log("ERROR", infoMess)
131 | } else {
132 | infoMess = fmt.Sprintf("The Be node upgrade successfully. [beId = %d, currentVersion = v%s]", beId, beStat["Version"])
133 | utl.Log("OUTPUT", infoMess)
134 | }
135 |
136 | }
137 |
138 |
139 |
140 |
141 |
--------------------------------------------------------------------------------
/cluster/upgradeCluster/upgradeFe.go:
--------------------------------------------------------------------------------
1 | package upgradeCluster
2 |
3 | import (
4 |
5 | "fmt"
6 | "time"
7 | "strings"
8 | //"errors"
9 | "stargo/module"
10 | "stargo/sr-utl"
11 | "stargo/cluster/stopCluster"
12 | "stargo/cluster/startCluster"
13 | "stargo/cluster/checkStatus"
14 | //"stargo/cluster/prepareOption"
15 |
16 | )
17 |
18 |
19 | func UpgradeFeCluster() { //(err error){
20 |
21 | var infoMess string
22 | var err error
23 | var feEntryId int
24 |
25 |
26 | feEntryId, err = checkStatus.GetFeEntry(-1)
27 | if err != nil || feEntryId == -1 {
28 | //infoMess = "All FE nodes are down, please start FE node and display the cluster status again."
29 | //utl.Log("WARN", infoMess)
30 | module.SetFeEntry(0)
31 | } else {
32 | module.SetFeEntry(feEntryId)
33 | }
34 |
35 |
36 | for i := 0; i < len(module.GYamlConf.FeServers); i++ {
37 | infoMess = fmt.Sprintf("Starting upgrade FE node. [feId = %d]", i)
38 | utl.Log("OUTPUT", infoMess)
39 | UpgradeFeNode(i)
40 | }
41 |
42 |
43 |
44 | }
45 |
46 |
47 | func UpgradeFeNode(feId int) {
48 | // step 1. backup fe lib
49 | // step 2. upload new fe lib
50 | // step 3. stop fe node
51 | // step 4. start fe node
52 |
53 | var infoMess string
54 | var user string
55 | var sourceDir string
56 | var targetDir string
57 | var sshHost string
58 | var sshPort int
59 | var feDeployDir string
60 | var feEditLogPort int
61 | var keyRsa string
62 | var feStat map[string]string
63 | var err error
64 |
65 | user = module.GYamlConf.Global.User
66 | keyRsa = module.GSshKeyRsa
67 | sshHost = module.GYamlConf.FeServers[feId].Host
68 | sshPort = module.GYamlConf.FeServers[feId].SshPort
69 | feDeployDir = module.GYamlConf.FeServers[feId].DeployDir
70 | feEditLogPort = module.GYamlConf.FeServers[feId].EditLogPort
71 |
72 |
73 |
74 |
75 | // step1. backup fe lib
76 | sourceDir = fmt.Sprintf("%s/lib", feDeployDir)
77 | targetDir = fmt.Sprintf("%s/lib.bak-%s", feDeployDir, time.Now().Format("20060102150405"))
78 |
79 | err = utl.RenameDir(user, keyRsa, sshHost, sshPort, sourceDir, targetDir)
80 | if err != nil {
81 | infoMess = fmt.Sprintf("Error in rename dir when backup FE lib. [host = %s, sourceDir = %s, targetDir = %s]", sshHost, sourceDir, targetDir)
82 | utl.Log("ERROR", infoMess)
83 | } else {
84 | infoMess = fmt.Sprintf("upgrade FE node - backup FE lib. [host = %s, sourceDir = %s, targetDir = %s]", sshHost, sourceDir, targetDir)
85 | utl.Log("INFO", infoMess)
86 | }
87 |
88 |
89 | // step2. upload new FE lib
90 | sourceDir = fmt.Sprintf("%s/StarRocks-%s/fe/lib", module.GDownloadPath, strings.Replace(module.GSRVersion, "v", "", -1))
91 | // sourceDir = fmt.Sprintf("%s/download/StarRocks-%s/fe/lib", module.GSRCtlRoot, strings.Replace(module.GSRVersion, "v", "", -1))
92 | targetDir = fmt.Sprintf("%s/lib", feDeployDir)
93 | utl.UploadDir(user, keyRsa, sshHost, sshPort, sourceDir, targetDir)
94 | infoMess = fmt.Sprintf("upgrade FE node - upload new FE lib. [host = %s, sourceDir = %s, targetDir = %s]", sshHost, sourceDir, targetDir)
95 | utl.Log("INFO", infoMess)
96 |
97 |
98 |
99 | // step3. stop FE node
100 | err = stopCluster.StopFeNode(user, keyRsa, sshHost, sshPort, feDeployDir)
101 | if err != nil {
102 | infoMess = fmt.Sprintf("Error in stop FE node when upgrade FE node. [host = %s, feDeployDir = %s]", sshHost, feDeployDir)
103 | utl.Log("ERROR", infoMess)
104 | } else {
105 | infoMess = fmt.Sprintf("upgrade FE node - stop FE node. [host = %s, feDeployDir = %s]", sshHost, feDeployDir)
106 | utl.Log("INFO", infoMess)
107 | }
108 |
109 | // step4. start FE node
110 | for j := 0; j < 3; j++ {
111 | startCluster.StartFeNode(user, keyRsa, sshHost, sshPort, feEditLogPort, feDeployDir)
112 | infoMess = fmt.Sprintf("upgrade FE node - start FE node. [host = %s, feDeployDir = %s]", sshHost, feDeployDir)
113 | utl.Log("INFO", infoMess)
114 |
115 | feStat, err = checkStatus.CheckFeStatus(feId)
116 | if feStat["Alive"] == "true" {
117 | break
118 | }
119 | time.Sleep(10 * time.Second)
120 | }
121 |
122 | if err != nil {
123 | infoMess = fmt.Sprintf("Error in get the FE status [feId = %d, error = %v]", feId, err)
124 | utl.Log("DEBUG", infoMess)
125 | } else if feStat["Alive"] == "false" {
126 | infoMess = fmt.Sprintf("The FE node upgrade failed. The FE node doesn't work. [feId = %d]\n", feId)
127 | utl.Log("ERROR", infoMess)
128 | } else if ! strings.Contains(feStat["FeVersion"], strings.Replace(module.GSRVersion, "v", "", -1)) {
129 | infoMess = fmt.Sprintf("The FE node upgrade failed. [feId = %d, targetVersion = %s, currentVersion = v%s]", feId, module.GSRVersion, feStat["FeVersion"])
130 | utl.Log("ERROR", infoMess)
131 | } else {
132 | infoMess = fmt.Sprintf("The Be node upgrade successfully. [feId = %d, currentVersion = v%s]", feId, feStat["FeVersion"])
133 | utl.Log("OUTPUT", infoMess)
134 | }
135 |
136 |
137 | }
138 |
139 |
140 |
141 |
142 |
--------------------------------------------------------------------------------
/deploy-template.yaml:
--------------------------------------------------------------------------------
1 | global:
2 | user: "starrocks"
3 | ssh_port: 22
4 |
5 | fe_servers:
6 | - host: 192.168.88.81
7 | ssh_port: 22
8 | http_port: 8030
9 | rpc_port: 9020
10 | query_port: 9030
11 | edit_log_port: 9010
12 | deploy_dir: /opt/starrocks/fe
13 | meta_dir: /data/starrocks/fe/meta
14 | log_dir: /data/starrocks/fe/log
15 | priority_networks: 192.168.88.0/24
16 | config:
17 | sys_log_level: "INFO"
18 | - host: 192.168.88.82
19 | ssh_port: 22
20 | http_port: 8030
21 | rpc_port: 9020
22 | query_port: 9030
23 | edit_log_port: 9010
24 | deploy_dir: /opt/starrocks/fe
25 | meta_dir: /data/starrocks/fe/meta
26 | log_dir: /data/starrocks/fe/log
27 | priority_networks: 192.168.88.0/24
28 | config:
29 | sys_log_level: "INFO"
30 | - host: 192.168.88.83
31 | ssh_port: 22
32 | http_port: 8030
33 | rpc_port: 9020
34 | query_port: 9030
35 | edit_log_port: 9010
36 | deploy_dir: /opt/starrocks/fe
37 | meta_dir: /data/starrocks/fe/meta
38 | log_dir: /data/starrocks/fe/log
39 | priority_networks: 192.168.88.0/24
40 | config:
41 | sys_log_level: "INFO"
42 |
43 | be_servers:
44 | - host: 192.168.88.81
45 | ssh_port: 22
46 | be_port: 9060
47 | webserver_port: 8040
48 | heartbeat_service_port: 9050
49 | brpc_port: 8060
50 | deploy_dir : /opt/starrocks/be
51 | storage_dir: /data/starrocks/be/storage
52 | log_dir: /data/starrocks/be/log
53 | priority_networks: 192.168.88.0/24
54 | config:
55 | create_tablet_worker_count: 3
56 | - host: 192.168.88.82
57 | ssh_port: 22
58 | be_port: 9060
59 | webserver_port: 8040
60 | heartbeat_service_port: 9050
61 | brpc_port: 8060
62 | deploy_dir : /opt/starrocks/be
63 | storage_dir: /data/starrocks/be/storage
64 | log_dir: /data/starrocks/be/log
65 | priority_networks: 192.168.88.0/24
66 | config:
67 | create_tablet_worker_count: 3
68 | - host: 192.168.88.83
69 | ssh_port: 22
70 | be_port: 9060
71 | webserver_port: 8040
72 | heartbeat_service_port: 9050
73 | brpc_port: 8060
74 | deploy_dir : /opt/starrocks/be
75 | storage_dir: /data/starrocks/be/storage
76 | log_dir: /data/starrocks/be/log
77 | priority_networks: 192.168.88.0/24
78 | config:
79 | create_tablet_worker_count: 3
80 |
81 |
--------------------------------------------------------------------------------
/importTest.yaml:
--------------------------------------------------------------------------------
1 | global:
2 | user: starrocks
3 | fe_servers:
4 | - host: 192.168.88.83
5 | ssh_port: 22
6 | query_port: 9030
7 | deploy_dir: /opt/starrocks/fe
8 | - host: 192.168.88.84
9 | ssh_port: 22
10 | query_port: 9030
11 | deploy_dir: /opt/starrocks/fe
12 | - host: 192.168.88.85
13 | ssh_port: 22
14 | query_port: 9030
15 | deploy_dir: /opt/starrocks/fe
16 | be_servers:
17 | - host: 192.168.88.83
18 | ssh_port: 22
19 | heartbeat_service_port: 9050
20 | deploy_dir: /opt/starrocks/be
21 | - host: 192.168.88.84
22 | ssh_port: 22
23 | heartbeat_service_port: 9050
24 | deploy_dir: /opt/starrocks/be
25 | - host: 192.168.88.85
26 | ssh_port: 22
27 | heartbeat_service_port: 9050
28 | deploy_dir: /opt/starrocks/be
29 |
30 |
--------------------------------------------------------------------------------
/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 |
5 | "fmt"
6 | "os"
7 | "flag"
8 | "stargo/sr-utl"
9 | "stargo/playground"
10 | "stargo/cluster/clusterOption"
11 | //"stargo/cluster/checkStatus"
12 | // "stargo/module"
13 | //"stargo/cluster/prepareOption"
14 |
15 | )
16 | func main() {
17 |
18 | // sr-ctl-cluster deploy sr-c1 v2.0.1 /tmp/sr-c1.yaml
19 | // sr-ctl-cluster start sr-c1
20 | // sr-ctl-cluster stop sr-c1
21 | // sr-ctl-cluster display sr-c1
22 |
23 | // sr-ctl playground v2.0.1
24 |
25 |
26 | var component string
27 | var command string
28 | var clusterName string
29 | var clusterVersion string
30 | var metaFile string
31 | var infoMess string
32 | var node string
33 | var role string
34 | var firstArgWithDash int
35 |
36 | component = os.Args[1]
37 | //command = os.Args[2]
38 |
39 |
40 | switch component {
41 |
42 | case "playground":
43 | playground.RunPlayground()
44 | case "cluster":
45 | command = os.Args[2]
46 | switch command {
47 |
48 | case "deploy":
49 | clusterName = os.Args[3]
50 | clusterVersion = os.Args[4]
51 | metaFile = os.Args[5]
52 | infoMess = fmt.Sprintf("Deploy cluster [clusterName = %s, clusterVersion = %s, metaFile = %s]\n", clusterName, clusterVersion, metaFile)
53 | utl.Log("OUTPUT", infoMess)
54 | clusterOption.Deploy(clusterName, clusterVersion, metaFile)
55 |
56 | case "start":
57 | clusterName = os.Args[3]
58 | infoMess = fmt.Sprintf("Start cluster [clusterName = %s]", clusterName)
59 | utl.Log("OUTPUT", infoMess)
60 | firstArgWithDash = 1
61 | for i := 1; i < len(os.Args); i++ {
62 | firstArgWithDash = i
63 | if len(os.Args[i]) > 0 && os.Args[i][0] == '-' {
64 | break
65 | }
66 | }
67 | flag.StringVar(&node, "node", "", "The Node ID. Use display command to check the node id.")
68 | flag.StringVar(&role, "role", "", "The start component type. You can input FE or BE.")
69 | flag.CommandLine.Parse(os.Args[firstArgWithDash:])
70 | clusterOption.Start(clusterName, node, role)
71 |
72 | case "stop":
73 | clusterName = os.Args[3]
74 | infoMess = fmt.Sprintf("Stop cluster [clusterName = %s]", clusterName)
75 | utl.Log("OUTPUT", infoMess)
76 | firstArgWithDash = 1
77 | for i := 1; i < len(os.Args); i++ {
78 | firstArgWithDash = i
79 | if len(os.Args[i]) > 0 && os.Args[i][0] == '-' {
80 | break
81 | }
82 | }
83 | flag.StringVar(&node, "node", "", "The Node ID. Use display command to check the node id.")
84 | flag.StringVar(&role, "role", "", "The start component type. You can input FE or BE.")
85 | flag.CommandLine.Parse(os.Args[firstArgWithDash:])
86 | clusterOption.Stop(clusterName, node, role)
87 |
88 | case "display":
89 | clusterName = os.Args[3]
90 | infoMess = fmt.Sprintf("Display cluster [clusterName = %s]", clusterName)
91 | utl.Log("OUTPUT", infoMess)
92 | clusterOption.Display(clusterName)
93 |
94 | case "list":
95 | infoMess = fmt.Sprintf("List all clusters")
96 | utl.Log("OUTPUT", infoMess)
97 | clusterOption.List()
98 |
99 | case "destroy":
100 | clusterName = os.Args[3]
101 | infoMess = fmt.Sprintf("Destroy cluster. [ClusterName = %s]", clusterName)
102 | utl.Log("OUTPUT", infoMess)
103 | clusterOption.Destroy(clusterName)
104 |
105 | case "upgrade":
106 | clusterName = os.Args[3]
107 | clusterVersion = os.Args[4]
108 | infoMess = fmt.Sprintf("Upgrade cluster. [ClusterName = %s, TargetVersion = %s]", clusterName, clusterVersion)
109 | utl.Log("OUTPUT", infoMess)
110 | clusterOption.Upgrade(clusterName, clusterVersion)
111 |
112 | case "downgrade":
113 | clusterName = os.Args[3]
114 | clusterVersion = os.Args[4]
115 | infoMess = fmt.Sprintf("Downgrade cluster. [ClusterName = %s, TargetVersion = %s]", clusterName, clusterVersion)
116 | utl.Log("OUTPUT", infoMess)
117 | clusterOption.Downgrade(clusterName, clusterVersion)
118 |
119 | case "scale-out":
120 | clusterName = os.Args[3]
121 | //clusterVersion = os.Args[4]
122 | metaFile = os.Args[4]
123 | infoMess = fmt.Sprintf("Scale out cluster. [ClusterName = %s]", clusterName)
124 | utl.Log("OUTPUT", infoMess)
125 | clusterOption.ScaleOut(clusterName, metaFile)
126 |
127 | case "scale-in":
128 | clusterName = os.Args[3]
129 | firstArgWithDash = 1
130 | for i := 1; i < len(os.Args); i++ {
131 | firstArgWithDash = i
132 | if len(os.Args[i]) > 0 && os.Args[i][0] == '-' {
133 | break
134 | }
135 | }
136 | flag.StringVar(&node, "node", "", "The Node ID. Use display command to check the node id.")
137 | flag.CommandLine.Parse(os.Args[firstArgWithDash:])
138 | infoMess = fmt.Sprintf("Scale in cluster [clusterName = %s, nodeId = %s]", clusterName, node)
139 | utl.Log("OUTPUT", infoMess)
140 | clusterOption.ScaleIn(clusterName, node)
141 |
142 | case "import":
143 | clusterName = os.Args[3]
144 | metaFile = os.Args[4]
145 | infoMess = fmt.Sprintf("Import the cluster [clusterName = %s, metaFile = %s]", clusterName, metaFile)
146 | utl.Log("OUTPUT", infoMess)
147 | clusterOption.ImportCluster(clusterName, metaFile)
148 | case "test":
149 | utl.Log("OUTPUT", "TEST >>>>>>>>>")
150 | // checkStatus.TestFeStatus()
151 | //prepareOption.TestPreCheck()
152 | //prepareOption.PreCheckSR()
153 | //playground.DeployPlayground()
154 | default:
155 | infoMess = fmt.Sprintf("ERROR, sr-ctl-cluster don't support %s option", command)
156 | utl.Log("ERROR", infoMess)
157 | } // end of switch command, end of case cluster
158 | default:
159 | fmt.Printf("ERROR component input.\n")
160 | } // end of switch component
161 |
162 | }
163 |
--------------------------------------------------------------------------------
/module/.yamlConfigModule.go.swp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wangtianyi2004/starrocks-controller/e01b4bf75a2b210e85e4844b127485a2390b6d17/module/.yamlConfigModule.go.swp
--------------------------------------------------------------------------------
/playground/generateConf.go:
--------------------------------------------------------------------------------
1 | package playground
2 |
3 | import (
4 |
5 | "stargo/module"
6 | "time"
7 | "fmt"
8 | _ "embed"
9 | "os/user"
10 | "os"
11 |
12 | )
13 |
14 |
15 |
16 | func InitPlaygroundConf(){
17 |
18 | var tmp module.ConfStruct
19 |
20 |
21 | osUser, _ := user.Current()
22 | module.GSshKeyRsa = fmt.Sprintf("%s/.ssh/id_rsa", osUser.HomeDir)
23 | module.GSRCtlRoot = os.Getenv("SRCTLROOT")
24 | if module.GSRCtlRoot == "" {
25 | module.GSRCtlRoot = fmt.Sprintf("%s/.stargo", osUser.HomeDir)
26 | }
27 | tmpDeployDir := fmt.Sprintf("%s/playground", module.GSRCtlRoot)
28 |
29 | // ClusterInfo
30 | tmp.ClusterInfo.User = osUser.Username
31 | tmp.ClusterInfo.Version = "2.2.0"
32 | tmp.ClusterInfo.CreateDate = time.Unix(time.Now().Unix(), 0,).Format("2006-01-02 15:04:05")
33 | tmp.ClusterInfo.MetaPath = fmt.Sprintf("%s/cluster/sr-playground", module.GSRCtlRoot)
34 | tmp.ClusterInfo.PrivateKey = module.GSshKeyRsa
35 |
36 |
37 | tmp.Global.User = osUser.Username
38 | tmp.Global.SshPort = 22
39 |
40 | tmp.FeServers = append(tmp.FeServers,
41 | struct {
42 | Host string `yaml:"host"`
43 | SshPort int `yaml:"ssh_port"`
44 | HttpPort int `yaml:"http_port"`
45 | RpcPort int `yaml:"rpc_port"`
46 | QueryPort int `yaml:"query_port"`
47 | EditLogPort int `yaml:"edit_log_port"`
48 | DeployDir string `yaml:"deploy_dir"`
49 | MetaDir string `yaml:"meta_dir"`
50 | LogDir string `yaml:"log_dir"`
51 | PriorityNetworks string `yaml:"priority_networks"`
52 | Config map[string]string `yaml:"config"`
53 | } {
54 | Host: "127.0.0.1",
55 | SshPort: 22,
56 | HttpPort: 8030,
57 | RpcPort: 9020,
58 | QueryPort: 9030,
59 | EditLogPort: 9010,
60 | DeployDir: tmpDeployDir + "/fe",
61 | MetaDir: tmpDeployDir + "/fe/meta",
62 | LogDir: tmpDeployDir + "/fe/log",
63 | PriorityNetworks: "127.0.0.1/32",
64 | Config: nil,
65 | })
66 |
67 | tmp.BeServers = append(tmp.BeServers,
68 | struct {
69 | Host string `yaml:"host"`
70 | SshPort int `yaml:"ssh_port"`
71 | BePort int `yaml:"be_port"`
72 | WebServerPort int `yaml:"webserver_port"`
73 | HeartbeatServicePort int `yaml:"heartbeat_service_port"`
74 | BrpcPort int `yaml:"brpc_port"`
75 | DeployDir string `yaml:"deploy_dir"`
76 | StorageDir string `yaml:"storage_dir"`
77 | LogDir string `yaml:"log_dir"`
78 | PriorityNetworks string `yaml:"priority_networks"`
79 | Config map[string]string `yaml:"configs`
80 | } {
81 | Host: "127.0.0.1",
82 | SshPort: 22,
83 | BePort: 9060,
84 | WebServerPort: 8040,
85 | HeartbeatServicePort: 9050,
86 | BrpcPort: 8060,
87 | DeployDir: tmpDeployDir + "/be",
88 | StorageDir: tmpDeployDir + "/be/storage",
89 | LogDir: tmpDeployDir + "/be/log",
90 | PriorityNetworks: "127.0.0.1/32",
91 | Config: nil,
92 | })
93 |
94 |
95 | module.GYamlConf = &tmp
96 | module.GSRVersion = "v" + module.GYamlConf.ClusterInfo.Version
97 | }
98 |
99 |
100 |
101 |
102 |
103 |
--------------------------------------------------------------------------------
/playground/playground.go:
--------------------------------------------------------------------------------
1 | package playground
2 |
3 | import (
4 |
5 | "fmt"
6 | "stargo/sr-utl"
7 |
8 | )
9 |
10 |
11 |
12 | func StartFePlayground() bool {
13 |
14 | ModifyFEConfig()
15 | RunFEProcess()
16 | res := CheckFEStatus()
17 | return res
18 |
19 | }
20 |
21 |
22 | func StartBePlayground() bool {
23 |
24 | ModifyBEConfig()
25 | AddBENode()
26 | RunBEProcess()
27 | res := CheckBEStatus()
28 | return res
29 |
30 | }
31 |
32 |
33 | func RunPlayground() {
34 |
35 |
36 | var infoMess string
37 | InitPlaygroundConf()
38 |
39 | PrecheckPlayground()
40 | PreparePlaygroundDir()
41 | feSuccess := StartFePlayground()
42 | beSuccess := StartBePlayground()
43 |
44 | if feSuccess && beSuccess {
45 | infoMess = fmt.Sprintf("Playground run successfully. Please use bellowing command to connect StarRocks playground cluster:\nmysql -uroot -P9030 -h127.0.0.1")
46 | utl.Log("OUTPUT", infoMess)
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/playground/precheck.go:
--------------------------------------------------------------------------------
1 | package playground
2 |
3 | import (
4 | "fmt"
5 | "os"
6 | "stargo/sr-utl"
7 | "stargo/module"
8 | "strconv"
9 | "strings"
10 | )
11 |
12 |
13 | func precheckPortUsed() bool {
14 |
15 | // FE Port: 8030 9020 9030 9010
16 | // BE Port: 8040 8060 9050 9060
17 | var portNoUsed bool = true
18 |
19 | portArr := [8] string{":8060", ":8040", ":9010", ":9050", ":9020", ":8030", ":9060", ":9030"}
20 | for _, portStr := range portArr {
21 | if utl.PortUsed(portStr) {
22 | fmt.Println("Detect the port " + portStr + " used. Please stop it first.")
23 | portNoUsed = false
24 | }
25 | }
26 |
27 | return portNoUsed
28 | }
29 |
30 | func precheckOpenFiles() bool {
31 |
32 | var infoMess string
33 | var execCMD string
34 | var cmdRes string
35 | var err error
36 |
37 | execCMD = "ulimit -n"
38 | cmdRes, err = utl.RunShellScript(execCMD)
39 | if err != nil {
40 | infoMess = fmt.Sprintf("Failed to run command. [cmd = %s]", execCMD)
41 | utl.Log("ERROR", infoMess)
42 | }
43 |
44 | fileCount, err := strconv.Atoi(strings.Replace(cmdRes, "\n", "", -1))
45 | if err != nil {
46 | infoMess = fmt.Sprintf("Failed to convert string to int.[res = %s]", fileCount)
47 | utl.Log("ERROR", infoMess)
48 | }
49 |
50 |
51 | if fileCount >= 65535 {
52 | return true
53 | } else {
54 | infoMess = fmt.Sprintf("Error in check the open file count. Please use the command [ulimit -n] to check the openfile count and make sure more than 65535.")
55 | utl.Log("ERROR", infoMess)
56 | return false
57 | }
58 | }
59 |
60 |
61 | func playgroundDirExist() bool {
62 |
63 | var infoMess string
64 | var playgroundDir string
65 |
66 | playgroundDir = module.GSRCtlRoot + "/playground"
67 | _, err := os.Stat(playgroundDir)
68 |
69 | if err == nil {
70 | infoMess = fmt.Sprintf("Detect the playground dir exists. Please delete first. [playground dir = %s]", playgroundDir)
71 | utl.Log("ERROR", infoMess)
72 | return false
73 | }
74 |
75 | // dir exists
76 | return true
77 |
78 | }
79 |
80 |
81 | func PrecheckPlayground() {
82 |
83 | var playgroundDir bool
84 | var openFileCount bool
85 | var portNoUsed bool
86 | // var infoMess string
87 |
88 |
89 | playgroundDir = playgroundDirExist()
90 | openFileCount = precheckOpenFiles()
91 | portNoUsed = precheckPortUsed()
92 | if !playgroundDir || !openFileCount || !portNoUsed {
93 | os.Exit(1)
94 | }
95 |
96 | }
97 |
--------------------------------------------------------------------------------
/playground/prepareDir.go:
--------------------------------------------------------------------------------
1 | package playground
2 |
3 | import (
4 |
5 | "fmt"
6 | "os"
7 | "stargo/cluster/prepareOption"
8 | "stargo/module"
9 | )
10 |
11 |
12 |
13 | func PreparePlaygroundDir() {
14 |
15 | // mkdir sr ctl dir
16 | prepareOption.CreateiSrCtlDir()
17 | _ = os.MkdirAll(module.GSRCtlRoot+"/playground", 0751)
18 | // download & decompress sr package
19 | prepareOption.PrepareSRPkg()
20 | DistributePlaygroundBinary()
21 |
22 | }
23 |
24 |
25 |
26 |
27 | func DistributePlaygroundBinary() {
28 |
29 | var sourceDir string
30 | var targetDir string
31 | var err error
32 |
33 | // module.GDownloadPath is the folder /home/sr-dev/.stargo/download
34 |
35 | // deploy jdk folder
36 | sourceDir = module.GDownloadPath + "/jdk1.8.0_301"
37 | targetDir = module.GSRCtlRoot + "/playground/jdk1.8.0"
38 | err = os.Rename(sourceDir, targetDir)
39 | if err != nil { panic(err) }
40 |
41 | // deploy fe folder
42 | sourceDir = fmt.Sprintf("%s/StarRocks-%s/fe", module.GDownloadPath, module.GYamlConf.ClusterInfo.Version)
43 | targetDir = module.GSRCtlRoot + "/playground/fe"
44 | err = os.Rename(sourceDir, targetDir)
45 | if err != nil { panic(err) }
46 |
47 | // deploy be folder
48 | sourceDir = fmt.Sprintf("%s/StarRocks-%s/be", module.GDownloadPath, module.GYamlConf.ClusterInfo.Version)
49 | targetDir = module.GSRCtlRoot + "/playground/be"
50 | err = os.Rename(sourceDir, targetDir)
51 | if err != nil { panic(err) }
52 |
53 | }
54 |
55 |
56 |
57 |
58 |
--------------------------------------------------------------------------------
/playground/startBe.go:
--------------------------------------------------------------------------------
1 | package playground
2 |
3 | import (
4 | "stargo/sr-utl"
5 | "stargo/module"
6 | "fmt"
7 | "time"
8 | "strings"
9 | "os"
10 | )
11 |
12 | func ModifyBEConfig() {
13 |
14 | var modFile string
15 | var srcConfig string
16 | var tarConfig string
17 | var infoMess string
18 |
19 | // modify priority_networks for be.conf
20 | modFile = module.GSRCtlRoot + "/playground/be/conf/be.conf"
21 | srcConfig = "# priority_networks = 10.10.10.0/24;192.168.0.0/16"
22 | tarConfig = "# priority_networks = 10.10.10.0/24;192.168.0.0/16\npriority_networks = 127.0.0.1/32"
23 | err := utl.ModifyConfig(modFile, srcConfig, tarConfig)
24 | if err != nil {
25 | infoMess = fmt.Sprintf("Error in modifing BE config [modFile = %s, srcConfig = %s, tarConfig = %s]", modFile, srcConfig, tarConfig)
26 | utl.Log("ERROR", infoMess)
27 | panic(err)
28 | }
29 | infoMess = fmt.Sprintf("Modify BE config [modFile = %s, srcConfig = %s, tarConfig = %s]", modFile, srcConfig, tarConfig)
30 | utl.Log("DEBUG", infoMess)
31 | }
32 |
33 |
34 | func AddBENode() {
35 |
36 | var infoMess string
37 | // add be node in fe
38 | addExecCMD := "mysql -uroot -P9030 -h127.0.0.1 -e 'alter system add backend \"127.0.0.1:9050\"'"
39 | _, err := utl.RunShellScript(addExecCMD)
40 | if err != nil{
41 | infoMess = fmt.Sprintf("Error in running cmd, cmd = %s, err = %v", addExecCMD, err)
42 | utl.Log("ERROR", infoMess)
43 | }
44 |
45 | time.Sleep(time.Duration(5) * time.Second)
46 | checkExecCMD := "mysql -uroot -P9030 -h127.0.0.1 -e 'show backends \\G'"
47 | res, err := utl.RunShellScript(checkExecCMD)
48 | if err != nil{
49 | infoMess = fmt.Sprintf("Error in running cmd.[cmd = %s, err = %v]", checkExecCMD, err)
50 | utl.Log("ERROR", infoMess)
51 | }
52 |
53 | if strings.Contains(res, "127.0.0.1") {
54 | utl.Log("OUTPUT", "BE node 127.0.0.1 added successfully.")
55 | }
56 |
57 | }
58 |
59 |
60 | func RunBEProcess() {
61 |
62 | var infoMess string
63 | // mkdir /root/.stargo/playground/fe/meta
64 | storageDir := module.GSRCtlRoot + "/playground/be/storage"
65 | _, err := os.Stat(storageDir)
66 | if err == nil {
67 | infoMess = fmt.Sprintf("Detect meta folder %s exists, delete it\n", storageDir)
68 | utl.Log("ERROR", infoMess)
69 | err = os.RemoveAll(storageDir)
70 | }
71 |
72 | err = os.Mkdir(storageDir, 0751)
73 | if err != nil { panic(err) }
74 |
75 | // run start_fe.sh
76 | execCMD := module.GSRCtlRoot + "/playground/be/bin/start_be.sh --daemon"
77 | _, err = utl.RunShellScript(execCMD)
78 | if err != nil {
79 | infoMess = fmt.Sprintf("Error in running be process, cmd = %s, err = %v", execCMD, err)
80 | utl.Log("ERROR", infoMess )
81 | }
82 |
83 | time.Sleep(time.Duration(15) * time.Second)
84 |
85 | }
86 |
87 | func CheckBEStatus() bool {
88 |
89 | var res string
90 | execCMD := "mysql -uroot -h127.0.0.1 -P9030 -e 'show backends\\G' | grep Alive"
91 | for i := 0; i < 5; i++ {
92 | res, _ = utl.RunShellScript(execCMD)
93 | if strings.Contains(res, "true") {
94 | utl.Log("OUTPUT", "BE start successfully.")
95 | return true
96 | }
97 | time.Sleep(time.Duration(5) * time.Second)
98 | }
99 |
100 | if !strings.Contains(res, "true") {
101 | utl.Log("ERROR", "BE start failed.")
102 | }
103 |
104 | return false
105 |
106 |
107 | }
108 |
109 |
--------------------------------------------------------------------------------
/playground/startFe.go:
--------------------------------------------------------------------------------
1 | package playground
2 |
3 | import (
4 |
5 | "stargo/sr-utl"
6 | "stargo/module"
7 | "fmt"
8 | "os"
9 | "time"
10 | "strings"
11 |
12 | )
13 |
14 |
15 | func ModifyFEConfig() {
16 |
17 | var modFile string
18 | var srcConfig string
19 | var tarConfig string
20 | var infoMess string
21 |
22 | // modify JAVA_OPS for fe.conf
23 | modFile = module.GSRCtlRoot + "/playground/fe/conf/fe.conf"
24 | srcConfig = "-Xmx8192m"
25 | tarConfig = "-Xmx512m"
26 | err := utl.ModifyConfig(modFile, srcConfig, tarConfig)
27 | if err != nil {
28 | infoMess = fmt.Sprintf("Error in modify FE configuration [modFile = %s, srcConfig = %s, tarConfig = %s]", modFile, srcConfig, tarConfig)
29 | utl.Log("ERROR", infoMess)
30 | panic(err)
31 | }
32 | infoMess = fmt.Sprintf("Modify FE configuration [modFile = %s, srcConfig = %s, tarConfig = %s]", modFile, srcConfig, tarConfig)
33 | utl.Log("DEBUG", infoMess)
34 |
35 | // modify priority_networks for fe.conf
36 | modFile = module.GSRCtlRoot + "/playground/fe/conf/fe.conf"
37 | srcConfig = "# priority_networks = 10.10.10.0/24;192.168.0.0/16"
38 | tarConfig = "# priority_networks = 10.10.10.0/24;192.168.0.0/16\npriority_networks = 127.0.0.1/32"
39 | err = utl.ModifyConfig(modFile, srcConfig, tarConfig)
40 | if err != nil {
41 | infoMess = fmt.Sprintf("Error in modify FE configuration [modFile = %s, srcConfig = %s, tarConfig = %s]", modFile, srcConfig, tarConfig)
42 | utl.Log("ERROR", infoMess)
43 | panic(err)
44 | }
45 | infoMess = fmt.Sprintf("Modify FE configuration [modFile = %s, srcConfig = %s, tarConfig = %s]", modFile, srcConfig, tarConfig)
46 | utl.Log("DEBUG", infoMess)
47 |
48 | // modify JAVA_HOME for start_fe.sh
49 | modFile = module.GSRCtlRoot + "/playground/fe/bin/start_fe.sh"
50 | srcConfig = "# java"
51 | tarConfig = fmt.Sprintf("# java\nJAVA_HOME=%s/playground/jdk1.8.0\n", module.GSRCtlRoot)
52 | err = utl.ModifyConfig(modFile, srcConfig, tarConfig)
53 | if err != nil {
54 | infoMess = fmt.Sprintf("Error in modify FE configuration [modFile = %s, srcConfig = %s, tarConfig = %s]", modFile, srcConfig, tarConfig)
55 | utl.Log("ERROR", infoMess)
56 | panic(err)
57 | }
58 | infoMess = fmt.Sprintf("Modify FE configuration [modFile = %s, srcConfig = %s, tarConfig = %s]", modFile, srcConfig, tarConfig)
59 | utl.Log("DEBUG", infoMess)
60 |
61 | }
62 |
63 |
64 |
65 |
66 | func RunFEProcess() {
67 |
68 | var infoMess string
69 | // mkdir /root/.stargo/playground/fe/meta
70 | metaDir := module.GSRCtlRoot + "/playground/fe/meta"
71 | _, err := os.Stat(metaDir)
72 | if err == nil {
73 | fmt.Printf("Detect meta folder %s exists, delete it\n", metaDir)
74 | err = os.RemoveAll(metaDir)
75 | }
76 |
77 | err = os.Mkdir(metaDir, 0751)
78 | if err != nil { panic(err) }
79 |
80 | // run start_fe.sh
81 | execCMD := module.GSRCtlRoot + "/playground/fe/bin/start_fe.sh --daemon"
82 | _, err = utl.RunShellScript(execCMD)
83 | if err != nil {
84 | infoMess = fmt.Sprintf("Error in running cmd, cmd = %s, err = %v\n", execCMD, err)
85 | utl.Log("ERROR", infoMess)
86 |
87 | }
88 |
89 | time.Sleep(time.Duration(15) * time.Second)
90 |
91 | }
92 |
93 |
94 | func CheckFEStatus() bool {
95 |
96 | execCMD := "mysql -uroot -h127.0.0.1 -P9030 -e 'show frontends\\G' | grep Alive"
97 | for i := 0; i < 5; i++ {
98 | res, _:= utl.RunShellScript(execCMD)
99 |
100 | if strings.Contains(res, "true") {
101 | utl.Log("OUTPUT", "fe start successfully.")
102 | return true
103 | }
104 | time.Sleep(time.Duration(5) * time.Second)
105 | }
106 |
107 | return false
108 |
109 | }
110 |
111 |
112 |
--------------------------------------------------------------------------------
/repo.yaml:
--------------------------------------------------------------------------------
1 | repo: "http://192.168.88.89:9000/starrocks-quick-start"
2 | #repo: "file:///home/sr-dev/.stargo/test"
3 | #repo: "http://cdn-thirdparty.starrocks.com"
4 |
--------------------------------------------------------------------------------
/sr-c1.yaml:
--------------------------------------------------------------------------------
1 | global:
2 | user: "starrocks"
3 | ssh_port: 22
4 |
5 | fe_servers:
6 | - host: 192.168.88.83
7 | ssh_port: 22
8 | http_port: 8030
9 | rpc_port: 9020
10 | query_port: 9030
11 | edit_log_port: 9010
12 | deploy_dir: /opt/starrocks/fe
13 | meta_dir: /data/starrocks/fe/meta
14 | log_dir: /data/starrocks/fe/log
15 | priority_networks: 192.168.88.0/24
16 | config:
17 | sys_log_level: "INFO"
18 | sys_log_delete_age: "1d"
19 | - host: 192.168.88.84
20 | ssh_port: 22
21 | http_port: 8030
22 | rpc_port: 9020
23 | query_port: 9030
24 | edit_log_port: 9010
25 | deploy_dir: /opt/starrocks/fe
26 | meta_dir: /data/starrocks/fe/meta
27 | log_dir: /data/starrocks/fe/log
28 | priority_networks: 192.168.88.0/24
29 | config:
30 | sys_log_delete_age: "2d"
31 | - host: 192.168.88.85
32 | ssh_port: 22
33 | http_port: 8030
34 | rpc_port: 9020
35 | query_port: 9030
36 | edit_log_port: 9010
37 | deploy_dir: /opt/starrocks/fe
38 | meta_dir: /data/starrocks/fe/meta
39 | log_dir: /data/starrocks/fe/log
40 | priority_networks: 192.168.88.0/24
41 | config:
42 | sys_log_delete_age: "3d"
43 |
44 | be_servers:
45 | - host: 192.168.88.83
46 | ssh_port: 22
47 | be_port: 9060
48 | webserver_port: 8040
49 | heartbeat_service_port: 9050
50 | brpc_port: 8060
51 | deploy_dir : /opt/starrocks/be
52 | storage_dir: /data/starrocks/be/storage
53 | log_dir: /data/starrocks/be/log
54 | priority_networks: 192.168.88.0/24
55 | config:
56 | create_tablet_worker_count: 3
57 | - host: 192.168.88.84
58 | ssh_port: 22
59 | be_port: 9060
60 | webserver_port: 8040
61 | heartbeat_service_port: 9050
62 | brpc_port: 8060
63 | deploy_dir : /opt/starrocks/be
64 | storage_dir: /data/starrocks/be/storage
65 | log_dir: /data/starrocks/be/log
66 | priority_networks: 192.168.88.0/24
67 | config:
68 | create_tablet_worker_count: 3
69 | - host: 192.168.88.85
70 | ssh_port: 22
71 | be_port: 9060
72 | webserver_port: 8040
73 | heartbeat_service_port: 9050
74 | brpc_port: 8060
75 | deploy_dir : /opt/starrocks/be
76 | storage_dir: /data/starrocks/be/storage
77 | log_dir: /data/starrocks/be/log
78 | priority_networks: 192.168.88.0/24
79 | config:
80 | create_tablet_worker_count: 3
81 |
82 |
--------------------------------------------------------------------------------
/sr-test.yaml:
--------------------------------------------------------------------------------
1 |
2 | global:
3 | user: "starrocks"
4 | ssh_port: 22
5 |
6 | fe_servers:
7 | - host: 192.168.88.81
8 | ssh_port: 22
9 | http_port: 8030
10 | rpc_port: 9020
11 | query_port: 9030
12 | edit_log_port: 9010
13 | deploy_dir: /opt/starrocks/fe
14 | meta_dir: /opt/starrocks/fe/meta
15 | log_dir: /opt/starrocks/fe/log
16 | priority_networks: 192.168.88.0/24
17 | config:
18 | sys_log_level: "INFO"
19 | sys_log_delete_age: "1d"
20 |
21 | be_servers:
22 | - host: 192.168.88.81
23 | ssh_port: 22
24 | be_port: 9060
25 | webserver_port: 8040
26 | heartbeat_service_port: 9050
27 | brpc_port: 8060
28 | deploy_dir : /opt/starrocks/be
29 | storage_dir: /opt/starrocks/be/storage
30 | log_dir: /opt/starrocks/be/log
31 | priority_networks: 192.168.88.80/24
32 | config:
33 | create_tablet_worker_count: 3
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/sr-utl/dbConnect.go:
--------------------------------------------------------------------------------
1 | package utl
2 |
3 | import(
4 | "fmt"
5 | "database/sql"
6 | _ "github.com/go-sql-driver/mysql"
7 | )
8 |
9 | func RunSQL(userName string, password string, ip string, port int, dbName string, sqlStat string) (rows *sql.Rows, err error){
10 |
11 | var infoMess string
12 | dbPath := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", userName, password, ip, port, dbName)
13 | DB, err := sql.Open("mysql", dbPath)
14 | if err != nil{
15 | infoMess = fmt.Sprintf("Error in open db [dbPath = %s], error = %v", dbPath, err)
16 | Log("ERROR", infoMess)
17 | return nil, err
18 | }
19 | defer DB.Close()
20 |
21 |
22 | err = DB.Ping()
23 | if err != nil{
24 | infoMess = fmt.Sprintf("Error in ping db [dbPath = %s], error = %v", dbPath, err)
25 | Log("ERROR", infoMess)
26 | return nil, err
27 | }
28 |
29 | rows, err = DB.Query(sqlStat)
30 | if err != nil{
31 | infoMess = fmt.Sprintf(`Error in run sql:
32 | dbPath = %s
33 | SQL = %s
34 | error = %v`, dbPath, sqlStat, err)
35 | Log("DEBUG", infoMess)
36 | return nil, err
37 | }
38 |
39 | return rows, err
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/sr-utl/decompress.go:
--------------------------------------------------------------------------------
1 | package utl
2 |
3 | import (
4 | "archive/tar"
5 | "compress/gzip"
6 | "fmt"
7 | "io"
8 | "os"
9 | "path/filepath"
10 | "strings"
11 | )
12 |
13 |
14 | func UnTargz(tarFile string, targetPath string) (err error) {
15 |
16 | file, err := os.Open(tarFile)
17 | if err != nil { return err }
18 | gz, err := gzip.NewReader(file)
19 | if err != nil { return err }
20 | // This does not close file
21 | defer gz.Close()
22 |
23 | tarReader := tar.NewReader(gz)
24 |
25 | for {
26 | header, err := tarReader.Next()
27 | if err == io.EOF {
28 | break
29 | } else if err != nil {
30 | return err
31 | }
32 |
33 | path := filepath.Join(targetPath, header.Name)
34 |
35 | if !strings.HasPrefix(path, filepath.Clean(targetPath)+string(os.PathSeparator)) {
36 | err = fmt.Errorf("%s: illegal file path", path)
37 | return err
38 | }
39 |
40 | info := header.FileInfo()
41 | if info.IsDir() {
42 | if err = os.MkdirAll(path, info.Mode()); err != nil {
43 | return err
44 | }
45 | continue
46 | }
47 |
48 | file, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, info.Mode())
49 | if err != nil {
50 | return err
51 | }
52 |
53 | _, err = io.Copy(file, tarReader)
54 | if err != nil {
55 | file.Close()
56 | return err
57 | }
58 |
59 | err = file.Close()
60 | if err != nil {
61 | return err
62 | }
63 | }
64 |
65 | return nil
66 | }
67 |
68 |
--------------------------------------------------------------------------------
/sr-utl/dirOption.go:
--------------------------------------------------------------------------------
1 | package utl
2 |
3 | import (
4 | "os"
5 | "io"
6 | "fmt"
7 | )
8 |
9 |
10 | func MkDir(dirPath string) {
11 |
12 | // check dir exists
13 | mess := ""
14 | dir, _ := os.Stat(dirPath)
15 | if dir == nil {
16 | // dir doesn't exist, create new one
17 | e := os.MkdirAll(dirPath, 0755)
18 | if e != nil {
19 | mess = "Error in create folder [" + dirPath + "]"
20 | Log("ERROR", mess)
21 | panic(e)
22 | }
23 | } else {
24 | mess = "Detect the folder [" + dirPath + "] exists"
25 | Log("DEBUG", mess)
26 | }
27 | }
28 |
29 | func CopyFile(sourceFileName string, targetFileName string) (fileByte int64, err error) {
30 |
31 | var infoMess string
32 |
33 | sourceFileStat, err := os.Stat(sourceFileName)
34 |
35 | if err != nil {
36 | infoMess = fmt.Sprintf("Error in copy file, the source file doesn't exist [sourceFile = %, targetFile = %s]", sourceFileName, targetFileName)
37 | Log("ERROR", infoMess)
38 | return 0, err
39 | }
40 |
41 | if !sourceFileStat.Mode().IsRegular() {
42 | infoMess = fmt.Sprintf("Error in copy file, the source file isn't a regular file [sourceFile = %s, targetFile = %s]", sourceFileName, targetFileName)
43 | Log("ERROR", infoMess)
44 | return 0, err
45 | }
46 |
47 | src, err := os.Open(sourceFileName)
48 | if err != nil {
49 | infoMess = fmt.Sprintf("Error in copy file, the source file cannot be opened [sourceFile = %s, targetFile = %s]", sourceFileName, targetFileName)
50 | Log("ERROR", infoMess)
51 | return 0, err
52 | }
53 | defer src.Close()
54 |
55 | dest, err := os.Create(targetFileName)
56 | if err != nil {
57 | infoMess = fmt.Sprintf("Error in copy file, the target file cannot be created [sourceFile = %s, targetFile = %s]", sourceFileName, targetFileName)
58 | Log("ERROR", infoMess)
59 | return 0, err
60 | }
61 | defer dest.Close()
62 |
63 | fileByte, err = io.Copy(dest, src)
64 | if err != nil {
65 | infoMess = fmt.Sprintf("Error in copy file [sourceFile = %s, targetFile = %s]", sourceFileName, targetFileName)
66 | Log("ERROR", infoMess)
67 | return 0, err
68 | }
69 |
70 | return fileByte, err
71 |
72 | }
73 |
--------------------------------------------------------------------------------
/sr-utl/download.go:
--------------------------------------------------------------------------------
1 | package utl
2 |
3 | import (
4 |
5 | "io"
6 | "net/http"
7 | "os"
8 | "fmt"
9 | "strconv"
10 |
11 | )
12 |
13 |
14 | func IsFileExist(absFileName string, fileSize int64) bool {
15 |
16 | var infoMess string
17 | info, err := os.Stat(absFileName)
18 |
19 | if os.IsNotExist(err) {
20 | infoMess = fmt.Sprintf("Detect file %s doesn't exist.", absFileName)
21 | Log("DEBUG", infoMess)
22 | return false
23 | }
24 |
25 | if fileSize == info.Size() {
26 | infoMess = fmt.Sprintf("The package has already exist [fileName = %v, fileSize = %v, fileModTime = %v]", info.Name(), info.Size(), info.ModTime())
27 | Log("INFO", infoMess)
28 | return true
29 | }
30 |
31 | del := os.Remove(absFileName)
32 | if del != nil {
33 | infoMess = fmt.Sprintf("Delete file %s", absFileName)
34 | Log("WARN", infoMess)
35 | }
36 |
37 | return false
38 | }
39 |
40 |
41 | func DownloadFile(fileUrl string, localPath string, fileName string) {
42 |
43 | var infoMess string
44 | tmpFileName := localPath + "/" + fileName + ".download"
45 | absFileName := localPath + "/" + fileName
46 |
47 | client := new(http.Client)
48 | resp, err := client.Get(fileUrl)
49 | if err != nil {
50 | infoMess = fmt.Sprintf("Error in get the response for %s", fileUrl)
51 | Log("ERROR", infoMess)
52 | panic(err)
53 | }
54 |
55 | fileSize, err := strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 32)
56 | if err != nil {
57 | infoMess = fmt.Sprintf("Error in parsing the size for file %s", fileUrl)
58 | Log("ERROR", infoMess)
59 | }
60 |
61 | if IsFileExist(absFileName, fileSize) {
62 | // the file exist, it doesn't need to download the one
63 | return
64 | }
65 |
66 | tmpFile, err := os.Create(tmpFileName)
67 | if err != nil {
68 | infoMess = fmt.Sprintf("Error in create the tmp file %s", tmpFileName)
69 | Log("ERROR", infoMess)
70 | panic(err)
71 | }
72 | defer tmpFile.Close()
73 |
74 |
75 | if resp.Body == nil {
76 | Log("ERROR", "The download file Body is null.")
77 | Log("ERROR", infoMess)
78 | panic(err)
79 | }
80 |
81 | io.Copy(tmpFile, resp.Body)
82 | info, err := os.Stat(tmpFileName)
83 | if err != nil {
84 | infoMess = fmt.Sprintf("Cannot get the tmp file stat [fileName = %s]", tmpFileName)
85 | Log("ERROR", infoMess)
86 | panic(err)
87 | }
88 |
89 | if info.Size() != fileSize {
90 | infoMess = fmt.Sprintf("Error in download, pls check your network connection.")
91 | Log("ERROR", infoMess)
92 | panic(err)
93 | }
94 |
95 | if err == nil {
96 | err = os.Rename(tmpFileName, absFileName)
97 | }
98 |
99 | infoMess = fmt.Sprintf("The file %s [%d] download successfully", fileName, fileSize)
100 | Log("INFO", infoMess)
101 |
102 | }
103 |
104 |
--------------------------------------------------------------------------------
/sr-utl/message.go:
--------------------------------------------------------------------------------
1 | package utl
2 |
3 | import(
4 | "fmt"
5 | "time"
6 | "os"
7 | // "io"
8 | // "io/ioutil"
9 | "bufio"
10 | )
11 |
12 | /*
13 |
14 | LogLevel
15 | - DEBUG 10
16 | - INFO 20
17 | - WARN 30
18 | - ERROR 40
19 |
20 | */
21 | //var GLOGLEVEL string = "DEBUG"
22 | var GLOGLEVEL string = "DEBUG"
23 |
24 | func Log(logLevel string, mess string) {
25 |
26 |
27 | dt := string(time.Now().Format("20060102-150405"))
28 |
29 | debugFile := "debug.log"
30 |
31 | if GLOGLEVEL == "DEBUG" {
32 |
33 | /*
34 | file, err := os.OpenFile(debugFile, os.O_APPEND|os.O_CREATE, 0666)
35 | if err != nil {
36 | fmt.Printf("Error in open debug.log")
37 | }
38 | defer file.Close()
39 | logMess := fmt.Sprintf("[%s %s] %s", dt, logLevel, mess)
40 | _, _ = io.WriteString(file, logMess)
41 | */
42 | logMess := fmt.Sprintf("[%s %s] %s", dt, logLevel, mess)
43 | file, _ := os.OpenFile(debugFile, os.O_WRONLY|os.O_APPEND, 0666)
44 | defer file.Close()
45 | write := bufio.NewWriter(file)
46 | write.WriteString(logMess)
47 | write.Flush()
48 | }
49 |
50 | // logLevel: DEBUG INFO WARN ERROR
51 |
52 | switch logLevel {
53 | case "DEBUG":
54 | // output: DEBUG INFO WARN ERROR
55 | //fmt.Printf("[\x1b[47;30m%s\x1b[0m\x1b[43;30m%8s\x1b[0m] %s\n", dt, logLevel, mess)
56 | debugFile = "debug.log"
57 | case "INFO":
58 | if logLevel != "DEBUG" {
59 | fmt.Printf("[\x1b[47;30m%s\x1b[0m\x1b[43;30m%8s\x1b[0m] %s\n", dt, logLevel, mess)
60 | }
61 | case "WARN":
62 | if logLevel != "DEBUG" || logLevel != "INFO" {
63 | fmt.Printf("[\x1b[47;30m%s\x1b[0m\x1b[43;30m%8s\x1b[0m] %s\n", dt, logLevel, mess)
64 | }
65 | case "ERROR":
66 | if logLevel != "DEBUG" || logLevel != "INFO" || logLevel != "WARN" {
67 | fmt.Printf("[\x1b[47;30m%s\x1b[0m\x1b[43;30m%8s\x1b[0m] %s\n", dt, logLevel, mess)
68 | }
69 | default:
70 | fmt.Printf("[\x1b[47;30m%s\x1b[0m\x1b[43;30m%8s\x1b[0m] %s\n", dt, logLevel, mess)
71 | }
72 | //fmt.Printf("%s %8s %15s %s\n", dt, logLevel, process, mess)
73 | //fmt.Printf("[\x1b[47;30m%s\x1b[0m\x1b[43;30m%8s\x1b[0m] %s\n", dt, logLevel, mess)
74 |
75 | }
76 |
77 |
78 |
--------------------------------------------------------------------------------
/sr-utl/modifyConfig.go:
--------------------------------------------------------------------------------
1 | package utl
2 |
3 | import (
4 | "bytes"
5 | "fmt"
6 | "strings"
7 | "io/ioutil"
8 | "os"
9 | "regexp"
10 | )
11 |
12 |
13 | func ModifyConfig(fileName string, sourceStr string, targetStr string) (err error){
14 |
15 | var infoMess string
16 | _, err = os.Stat(fileName)
17 | if err != nil {
18 | infoMess = fmt.Sprintf("Error in modifing configuration, the configuration file doesn't exist [fileName = %s, sourceStr = %s, targetStr = %s]", fileName, sourceStr, targetStr)
19 | Log("ERROR", infoMess)
20 | return err
21 | }
22 |
23 | input, err := ioutil.ReadFile(fileName)
24 | if err != nil {
25 | infoMess = fmt.Sprintf("Error in modifing configuration, cannot read file [fileName = %s, sourceStr = %s, targetStr = %s]", fileName, sourceStr, targetStr)
26 | Log("ERROR", infoMess)
27 | return err
28 | }
29 |
30 | output := bytes.Replace(input, []byte(sourceStr), []byte(targetStr), -1)
31 |
32 | err = ioutil.WriteFile(fileName, output, 0644)
33 | if err != nil {
34 | infoMess = fmt.Sprintf("Error in modifing configuration, cannot read file [fileName = %s, output = %s]", fileName, output)
35 | Log("ERROR", infoMess)
36 | return err
37 | }
38 |
39 | return nil
40 | }
41 | /*
42 | func AppendConfig(fileName string, configStr string) (err error){
43 |
44 | var infoMess string
45 | _, err = os.Stat(fileName)
46 | if err != nil {
47 | infoMess = fmt.Sprintf("Error in appending configuration, the configuration file doesn't exist [fileName = %s, configStr = %s]", fileName, configStr)
48 | Log("ERROR", infoMess)
49 | return err
50 | }
51 |
52 | file, err := os.OpenFile(fileName, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
53 | if err != nil {
54 | infoMess = fmt.Sprintf("Error in appending configuration, cannot open file [fileName = %s, configStr = %s]", fileName, configStr)
55 | Log("ERROR", infoMess)
56 | return err
57 | }
58 | defer file.Close()
59 |
60 | // if the config exists
61 |
62 |
63 | _, err = file.WriteString(configStr + "\n")
64 | if err != nil {
65 | infoMess = fmt.Sprintf("Error in appending configuration, cannot write configStr [fileName = %s, configStr = %s]", fileName, configStr)
66 | Log("ERROR", infoMess)
67 | return err
68 | }
69 |
70 | return err
71 |
72 |
73 | }
74 | */
75 |
76 |
77 | func AppendConfig(fileName string, configKey string, configValue string) (err error){
78 |
79 | var infoMess string
80 | _, err = os.Stat(fileName)
81 | if err != nil {
82 | infoMess = fmt.Sprintf("Error in appending configuration, the configuration file doesn't exist [fileName = %s, configkey = %s, configValue = %s]", fileName, configKey, configValue)
83 | Log("ERROR", infoMess)
84 | return err
85 | }
86 |
87 |
88 | configFile, err := ioutil.ReadFile(fileName)
89 | if err != nil {
90 | infoMess = fmt.Sprintf("Error in appending configuration, read configuration file failed [fileName = %s, configKey = %s, configValue = %s]", fileName, configKey, configValue)
91 | Log("ERROR", infoMess)
92 | return err
93 | }
94 |
95 | lines := strings.Split(string(configFile), "\n")
96 |
97 | for i, line := range lines {
98 | pattern := fmt.Sprintf("^%s.*", configKey)
99 | match, _ := regexp.MatchString(pattern, line)
100 | if match {
101 | infoMess := fmt.Sprintf("Comment the default value [fileName = %s, configKey = %s, configValue = %s]", fileName, configKey, configValue)
102 | Log("DEBUG", infoMess)
103 | lines[i] = "# " + lines[i] + "\t\t\t## comment by stargo"
104 | }
105 | }
106 |
107 | configStr := fmt.Sprintf("\n%s = %s\n", configKey, configValue)
108 | output := strings.Join(lines, "\n")
109 | output = output + configStr
110 |
111 | err = ioutil.WriteFile(fileName, []byte(output), 0644)
112 | if err != nil {
113 | infoMess = fmt.Sprintf("Error in appending configuration, write the result to config file failed [fileName = %s, configStr= %s]", fileName, configStr)
114 | Log("ERROR", infoMess)
115 | }
116 |
117 | infoMess = fmt.Sprintf("Append configuration [fileName = %s, configStr= %s]", fileName, strings.Replace(configStr, "\n", "", -1))
118 | Log("DEBUG", infoMess)
119 |
120 | return nil
121 |
122 |
123 | }
124 |
125 |
--------------------------------------------------------------------------------
/sr-utl/precheck.go:
--------------------------------------------------------------------------------
1 | package utl
2 |
3 | import (
4 | "os/exec"
5 | )
6 |
7 |
8 | func PortUsed(portStr string) bool {
9 |
10 | output, _ := exec.Command("/bin/bash", "-c", "netstat -na | grep " + portStr + " | grep -v ESTABLISHED").CombinedOutput()
11 |
12 | if len(output) > 0 {
13 | return true
14 | } else {
15 | return false
16 | }
17 | }
18 |
19 |
--------------------------------------------------------------------------------
/sr-utl/runProcesser.go:
--------------------------------------------------------------------------------
1 | package utl
2 |
3 | import (
4 | "os/exec"
5 | "fmt"
6 | )
7 |
8 | // Run local shell command
9 | func RunShellScript(scriptName string) (string, error) {
10 | var errmess string
11 | cmd := exec.Command("/bin/bash", "-c", scriptName)
12 | res, err := cmd.Output()
13 | if err != nil {
14 | errmess = fmt.Sprintf("Error in run command [ %s ], err = %v", scriptName, err)
15 | Log("DEBUG", errmess)
16 | //panic(err)
17 | return "", err
18 | }
19 |
20 |
21 | return string(res), nil
22 | }
23 |
24 | // Run ssh shell command
25 |
26 |
27 |
--------------------------------------------------------------------------------
/sr-utl/runProcesser.go.bak:
--------------------------------------------------------------------------------
1 | package utl
2 |
3 | import (
4 | "os/exec"
5 | "fmt"
6 | "golang.org/x/crypto/ssh"
7 | "io/ioutil"
8 | )
9 |
10 | // Run local shell command
11 | func RunShellScript(scriptName string) string {
12 | var errmess string
13 | cmd := exec.Command("/bin/bash", "-c", scriptName)
14 | res, err := cmd.Output()
15 | if err != nil {
16 | errmess = fmt.Sprint("Error in run command [ %s ]", scriptName)
17 | Log("ERROR", errmess)
18 | panic(err)
19 | }
20 | //fmt.Println(string(res))
21 | return string(res)
22 | }
23 |
24 | // Run ssh shell command
25 |
26 |
27 | func NewConfig(keyFile string, user string)(config *ssh.ClientConfig, err error) {
28 |
29 | var errmess string
30 |
31 | key, err := ioutil.ReadFile(keyFile)
32 | if err != nil {
33 | errmess = fmt.Sprint("unable to read private key: %v", err)
34 | Log("ERROR", errmess)
35 | return
36 | }
37 |
38 | signer, err := ssh.ParsePrivateKey(key)
39 | if err != nil {
40 | errmess = fmt.Sprint("unable to parse private key: %v", err)
41 | Log("ERROR", errmess)
42 | return
43 | }
44 |
45 | config = &ssh.ClientConfig{
46 | User: user,
47 | Auth: []ssh.AuthMethod{
48 | ssh.PublicKeys(signer),
49 | },
50 | HostKeyCallback: ssh.InsecureIgnoreHostKey(),
51 | }
52 | return
53 |
54 | }
55 |
56 | func SshRun(config *ssh.ClientConfig, host string, port int, command string) (outPut []byte, err error) {
57 |
58 | var errmess string
59 | client, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", host, port), config)
60 | if err != nil {
61 | errmess = fmt.Sprint("unable to connect: %s error %v", host, err)
62 | Log("ERROR", errmess)
63 | return
64 | }
65 | defer client.Close()
66 |
67 | session, err := client.NewSession()
68 | if err != nil{
69 | errmess = fmt.Sprint("ssh new session error %v", err)
70 | Log("ERROR", errmess)
71 | return
72 | }
73 | defer session.Close()
74 |
75 | outPut, err = session.CombinedOutput(command)
76 | if err != nil{
77 | errmess = fmt.Sprint("run command %s on host %s error %v", command, host, err)
78 | Log("ERROR", errmess)
79 | }
80 |
81 | return
82 | }
83 |
--------------------------------------------------------------------------------
/sr-utl/sshRun.go:
--------------------------------------------------------------------------------
1 | package utl
2 |
3 | import (
4 | "fmt"
5 | "os"
6 | "path"
7 | "golang.org/x/crypto/ssh"
8 | "github.com/pkg/sftp"
9 | "io/ioutil"
10 | )
11 |
12 |
13 |
14 |
15 | func NewConfig(keyFile string, user string)(config *ssh.ClientConfig, err error) {
16 |
17 | var errmess string
18 |
19 | key, err := ioutil.ReadFile(keyFile)
20 | if err != nil {
21 | errmess = fmt.Sprint("unable to read private key: %v", err)
22 | Log("ERROR", errmess)
23 | return nil, err
24 | }
25 |
26 | signer, err := ssh.ParsePrivateKey(key)
27 | if err != nil {
28 | errmess = fmt.Sprint("unable to parse private key: %v", err)
29 | Log("ERROR", errmess)
30 | return nil, err
31 | }
32 |
33 | config = &ssh.ClientConfig{
34 | User: user,
35 | Auth: []ssh.AuthMethod{
36 | ssh.PublicKeys(signer),
37 | },
38 | HostKeyCallback: ssh.InsecureIgnoreHostKey(),
39 | }
40 |
41 | return config, nil
42 |
43 | }
44 |
45 | func sshRun(config *ssh.ClientConfig, host string, port int, command string) (outPut []byte, err error) {
46 |
47 | var errmess string
48 | var infoMess string
49 | client, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", host, port), config)
50 | if err != nil {
51 | errmess = fmt.Sprintf("unable to connect: %s error %v", host, err)
52 | Log("ERROR", errmess)
53 | return nil, err
54 | }
55 | defer client.Close()
56 |
57 | session, err := client.NewSession()
58 | if err != nil{
59 | errmess = fmt.Sprint("ssh new session error %v", err)
60 | Log("ERROR", errmess)
61 | return nil, err
62 | }
63 | defer session.Close()
64 |
65 | outPut, err = session.CombinedOutput(command)
66 | if err != nil {
67 | if v, ok := err.(*ssh.ExitError); ok {
68 | errmess = v.Msg()
69 | }
70 | }
71 |
72 | infoMess = fmt.Sprintf(`ssh run:
73 | host = %s
74 | cmd = %s
75 | error = %v
76 | result = %v`, host, command, errmess, string(outPut))
77 | Log("DEBUG", infoMess)
78 |
79 | return outPut, err
80 | }
81 |
82 | func SshRun(user string, keyFile string, host string, port int, command string) (outPut []byte, err error) {
83 |
84 | var infoMess string
85 |
86 | sshConfig, err := NewConfig(keyFile, user)
87 | if err != nil {
88 | infoMess = fmt.Sprintf("Failed to get the ssh config when run command [user = %s, host = %s, port = %d, cmd = %s], error = %v", user, host, port, command, err)
89 | Log("DEBUG", infoMess)
90 | }
91 |
92 | output, err := sshRun(sshConfig, host, port, command)
93 | if err != nil {
94 | infoMess = fmt.Sprintf("Failed to run command. [host = %s, cmd = %s, error = %v]", host, command, err)
95 | Log("DEBUG", infoMess)
96 | }
97 | return output, err
98 |
99 | }
100 |
101 | func sftpConnect(config *ssh.ClientConfig, host string, port int) (sfpClient *sftp.Client, err error) {
102 |
103 | var infoMess string
104 | addr := fmt.Sprintf("%s:%d", host, port)
105 |
106 | sshClient, err := ssh.Dial("tcp", addr, config)
107 | if err != nil {
108 | infoMess = fmt.Sprintf("Error in dail %s, %s", addr, config)
109 | Log("ERROR", infoMess)
110 | return nil, err
111 | }
112 |
113 | sftpClient, err := sftp.NewClient(sshClient)
114 | if err != nil {
115 | infoMess = fmt.Sprintf("Error in get sftp client")
116 | Log("ERROR", infoMess)
117 | return nil, err
118 | }
119 |
120 | return sftpClient, nil
121 |
122 | }
123 |
124 |
125 | func uploadFile(sftpClient *sftp.Client, localFileName string, remoteFileName string) (err error) {
126 |
127 | var infoMess string
128 |
129 | srcFile, err := os.Open(localFileName)
130 | if err != nil {
131 | infoMess = fmt.Sprintf("Error in open file %s", localFileName)
132 | Log("ERROR", infoMess)
133 | return err
134 | }
135 | defer srcFile.Close()
136 |
137 | dstFile, err := sftpClient.Create(remoteFileName)
138 | if err != nil {
139 | infoMess = fmt.Sprintf("sftpClient.Create error : %s, error = %v", remoteFileName, err)
140 | Log("ERROR", infoMess)
141 | return err
142 | }
143 | defer dstFile.Close()
144 |
145 | ff, err := ioutil.ReadAll(srcFile)
146 | if err != nil {
147 | infoMess = fmt.Sprintf("ReadAll error : %s", localFileName)
148 | Log("ERROR", infoMess)
149 | return err
150 | }
151 |
152 | dstFile.Write(ff)
153 | //infoMess = localFileName + " copy file to remote server finished!"
154 | //Log("DEBUG", infoMess)
155 | // Chmod remoteFile
156 | fileStat, err := os.Stat(localFileName)
157 | if err != nil {
158 | infoMess = fmt.Sprintf("Error in get file stat when upload file: [sourceFile = %s targetFile = %s]", localFileName, remoteFileName)
159 | Log("ERROR", infoMess)
160 | return err
161 | }
162 |
163 | err = sftpClient.Chmod(remoteFileName, fileStat.Mode())
164 | if err != nil {
165 | infoMess = fmt.Sprintf("Error in chmod file stat when upload file: [sourceFile = %s targetFile = %s]", localFileName, remoteFileName)
166 | Log("ERROR", infoMess)
167 | return err
168 | }
169 | //infoMess = fmt.Sprintf("chmod file [%s] to %s", remoteFileName, fileStat.Mode())
170 | //Log("DEBUG", infoMess)
171 | //Log("INFO", infoMess)
172 | return err
173 | }
174 |
175 |
176 | func uploadDirectory(sftpClient *sftp.Client, localPath string, remotePath string) (err error) {
177 |
178 | var infoMess string
179 |
180 | localFiles, err := ioutil.ReadDir(localPath)
181 | if err != nil {
182 | infoMess = "Read dir list fail."
183 | Log("ERROR", infoMess)
184 | return err
185 | }
186 |
187 | for _, backupDir := range localFiles {
188 |
189 | localFilePath := path.Join(localPath, backupDir.Name())
190 | remoteFilePath := path.Join(remotePath, backupDir.Name())
191 |
192 | if backupDir.IsDir() {
193 | sftpClient.Mkdir(remoteFilePath)
194 | err = uploadDirectory(sftpClient, localFilePath, remoteFilePath)
195 | if err != nil {
196 | infoMess = fmt.Sprintf("Error in upload dir %s\t%s\t%s", sftpClient, localFilePath, remoteFilePath)
197 | Log("ERROR", infoMess)
198 | return err
199 | }
200 | } else {
201 | localFileName := path.Join(localPath, backupDir.Name())
202 | remoteFileName := path.Join(remotePath, backupDir.Name())
203 | err = uploadFile(sftpClient, localFileName, remoteFileName)
204 | if err != nil {
205 | infoMess = fmt.Sprintf("Error in upload file %s\t%s\t%s", sftpClient, path.Join(localPath, backupDir.Name()), remotePath)
206 | Log("ERROR", infoMess)
207 | return err
208 | }
209 | }
210 |
211 | }
212 |
213 | //infoMess = localPath + " copy directory to remote server finished!"
214 | //Log("INFO", infoMess)
215 | return err
216 | }
217 |
218 |
219 |
220 | func UploadFile(user string, keyFile string, host string, port int, sourceFile string, targetFile string) {
221 |
222 | var infoMess string
223 |
224 | sshConfig, err := NewConfig(keyFile, user)
225 | if err != nil {
226 | infoMess = fmt.Sprintf("Error in upload file, fail to get ssh config [keyfile = %s, user = %s]", keyFile, user)
227 | Log("ERROR", infoMess)
228 | panic(err)
229 | }
230 |
231 | sftpClient, err := sftpConnect(sshConfig, host, port)
232 | if err != nil {
233 | infoMess = fmt.Sprintf("Error in upload file, fail to get sftp client [keyfile = %s, user = %s, host = %s, port = %d]", keyFile, user, host, port)
234 | Log("ERROR", infoMess)
235 | panic(err)
236 | }
237 |
238 | err = uploadFile(sftpClient, sourceFile, targetFile)
239 | if err != nil {
240 | infoMess = fmt.Sprintf("Error in upload file [user = %s, keyFile = %s, host = %s, port = %d, sourceFile = %s, targetFile = %s]", user, keyFile, host, port, sourceFile, targetFile)
241 | Log("ERROR", infoMess)
242 | panic(err)
243 | }
244 |
245 | }
246 |
247 |
248 |
249 | func UploadDir(user string, keyFile string, host string, port int, sourceDir string, targetDir string) {
250 |
251 | var infoMess string
252 | var err error
253 | // check the folder exist
254 | cmd := fmt.Sprintf("ls %s", targetDir)
255 | _, err = SshRun(user, keyFile, host, port, cmd)
256 | if err != nil {
257 | infoMess = fmt.Sprintf("The target dir [%s] doesn't exist on [%s:%d], create a new one", targetDir, host, port)
258 | Log("DEBUG", infoMess)
259 | cmd = fmt.Sprintf("mkdir -p %s", targetDir)
260 | _, err := SshRun(user, keyFile, host, port, cmd)
261 | if err != nil {
262 | infoMess = fmt.Sprintf("Error in create folder [%s] on [%s:%d]", targetDir, host, port)
263 | Log("ERROR", infoMess)
264 | panic(err)
265 | }
266 | infoMess = fmt.Sprintf("Create folder [%s] on [%s:%d]", targetDir, host, port)
267 | Log("DEBUG", infoMess)
268 | }
269 |
270 | sshConfig, err := NewConfig(keyFile, user)
271 | if err != nil {
272 | infoMess = fmt.Sprintf(`Error in upload dir, failed to get the ssh config :user = %s,
273 | keyFile = %s
274 | host = %s
275 | port = %d
276 | sourceDir = %s
277 | targetDir = %s
278 | error = %v`, user, keyFile, host, port, sourceDir, targetDir, err)
279 | }
280 | sftpClient, err := sftpConnect(sshConfig, host, port)
281 | if err != nil {
282 | infoMess = fmt.Sprintf(`Error in upload dir[sftp client]: user = %s
283 | keyFile = %s
284 | host = %s
285 | port = %d
286 | sourceDir = %s
287 | targetDir = %s
288 | error = %v`, user, keyFile, host, port, sourceDir, targetDir, err)
289 | Log("ERROR", infoMess)
290 | panic(err)
291 | }
292 |
293 | err = uploadDirectory(sftpClient, sourceDir, targetDir)
294 | if err != nil {
295 | infoMess = fmt.Sprintf(`Error in upload dir[upload dir]: user = %s
296 | keyFile = %s
297 | host = %s
298 | port = %d
299 | sourceDir = %s
300 | targetDir = %s
301 | error = %v`, user, keyFile, host, port, sourceDir, targetDir, err)
302 | Log("ERROR", infoMess)
303 | panic(err)
304 | }
305 |
306 | }
307 |
308 | func RenameDir(user string, keyFile string, host string, port int, sourceDir string, targetDir string) (err error){
309 |
310 | var infoMess string
311 |
312 | cmd := fmt.Sprintf("ls %s", sourceDir)
313 | _, err = SshRun(user, keyFile, host, port, cmd)
314 |
315 | if err != nil {
316 | infoMess = fmt.Sprintf("The source dir [%s] doesn't exist on [%s:%d], create a new one", sourceDir, host, port)
317 | Log("ERROR", infoMess)
318 | return err
319 | }
320 |
321 | sshConfig, err := NewConfig(keyFile, user)
322 | if err != nil {
323 | infoMess = fmt.Sprintf("Error in rename dir, failed to get the ssh config. [host = %s, sourceDir = %s, targetDir = %s, err = %v]", host, sourceDir, targetDir, err)
324 | Log("ERROR", infoMess)
325 | return err
326 | }
327 |
328 | sftpClient, err := sftpConnect(sshConfig, host, port)
329 | if err != nil {
330 | infoMess = fmt.Sprintf("Error in rename dir when create sftp client.[host = %s, sourceDir = %s, targetDir = %s, error = %v", host, sourceDir, targetDir, err)
331 | Log("ERROR", infoMess)
332 | return err
333 | }
334 | err = sftpClient.Rename(sourceDir, targetDir)
335 | if err != nil {
336 | infoMess = fmt.Sprintf("Error in rename dir.[host = %s, sourceDir = %s, targetDir = %s, error = %v", host, sourceDir, targetDir, err)
337 | Log("ERROR", infoMess)
338 | return err
339 | }
340 |
341 | return nil
342 |
343 | }
344 |
345 |
346 | func RemoveDir(user string, keyFile string, host string, port int, dirName string) (err error) {
347 |
348 | var infoMess string
349 | cmd := fmt.Sprintf("ls %s", dirName)
350 | _, err = SshRun(user, keyFile, host, port, cmd)
351 |
352 | if err != nil {
353 | infoMess = fmt.Sprintf("The dir [%s] doesn't exist on [%s:%d]", dirName, host, port)
354 | Log("DEBUG", infoMess)
355 | }
356 |
357 | sshConfig, err := NewConfig(keyFile, user)
358 | if err != nil {
359 | infoMess = fmt.Sprintf("Error in remove dir, failed to get the ssh config. [host = %s, dirName, err = %v]", host, dirName, err)
360 | Log("ERROR", infoMess)
361 | return err
362 | }
363 |
364 | sftpClient, err := sftpConnect(sshConfig, host, port)
365 | if err != nil {
366 | infoMess = fmt.Sprintf("Error in remove dir when create sftp client.[host = %s, dirName = %s, error = %v", host, dirName, err)
367 | Log("ERROR", infoMess)
368 | return err
369 | }
370 |
371 | err = sftpClient.RemoveDirectory(dirName)
372 |
373 | if err != nil {
374 | infoMess = fmt.Sprintf("Error in remove dir.[host = %s, dirName = %s, error = %v", host, dirName, err)
375 | Log("ERROR", infoMess)
376 | return err
377 | }
378 |
379 | return nil
380 |
381 | }
382 |
383 |
384 |
385 | func TestDir(dirName string) {
386 |
387 |
388 | // check targetDir exist
389 | user := "starrocks"
390 | keyFile := "/home/sr-dev/.ssh/id_rsa"
391 | host := "192.168.88.83"
392 | port := 22
393 | //dirName := "/opt/starrocks/fe"
394 | _ = RemoveDir(user, keyFile, host, port, dirName)
395 | }
396 |
397 |
398 |
--------------------------------------------------------------------------------
/sr-utl/sshRun.go.bak:
--------------------------------------------------------------------------------
1 | package utl
2 |
3 | import (
4 | "fmt"
5 | "os"
6 | "path"
7 | "golang.org/x/crypto/ssh"
8 | "github.com/pkg/sftp"
9 | "io/ioutil"
10 | )
11 |
12 |
13 |
14 |
15 | func NewConfig(keyFile string, user string)(config *ssh.ClientConfig, err error) {
16 |
17 | var errmess string
18 |
19 | key, err := ioutil.ReadFile(keyFile)
20 | if err != nil {
21 | errmess = fmt.Sprint("unable to read private key: %v", err)
22 | Log("ERROR", errmess)
23 | return nil, err
24 | }
25 |
26 | signer, err := ssh.ParsePrivateKey(key)
27 | if err != nil {
28 | errmess = fmt.Sprint("unable to parse private key: %v", err)
29 | Log("ERROR", errmess)
30 | return nil, err
31 | }
32 |
33 | config = &ssh.ClientConfig{
34 | User: user,
35 | Auth: []ssh.AuthMethod{
36 | ssh.PublicKeys(signer),
37 | },
38 | HostKeyCallback: ssh.InsecureIgnoreHostKey(),
39 | }
40 |
41 | return config, nil
42 |
43 | }
44 |
45 | func SshRun(config *ssh.ClientConfig, host string, port int, command string) (outPut []byte, err error) {
46 |
47 | var errmess string
48 | client, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", host, port), config)
49 | if err != nil {
50 | errmess = fmt.Sprint("unable to connect: %s error %v", host, err)
51 | Log("ERROR", errmess)
52 | return nil, err
53 | }
54 | defer client.Close()
55 |
56 | session, err := client.NewSession()
57 | if err != nil{
58 | errmess = fmt.Sprint("ssh new session error %v", err)
59 | Log("ERROR", errmess)
60 | return nil, err
61 | }
62 | defer session.Close()
63 |
64 | outPut, err = session.CombinedOutput(command)
65 | if err != nil{
66 | errmess = fmt.Sprintf("run command [%s] on host %s error %v", command, host, err)
67 | Log("WARN", errmess)
68 | return nil, err
69 | }
70 | return outPut, nil
71 | }
72 |
73 |
74 | func sftpConnect(config *ssh.ClientConfig, host string, port int) (sfpClient *sftp.Client, err error) {
75 |
76 | var infoMess string
77 | addr := fmt.Sprintf("%s:%d", host, port)
78 |
79 | sshClient, err := ssh.Dial("tcp", addr, config)
80 | if err != nil {
81 | infoMess = fmt.Sprintf("Error in dail %s, %s", addr, config)
82 | Log("ERROR", infoMess)
83 | return nil, err
84 | }
85 |
86 | sftpClient, err := sftp.NewClient(sshClient)
87 | if err != nil {
88 | infoMess = fmt.Sprintf("Error in get sftp client")
89 | Log("ERROR", infoMess)
90 | return nil, err
91 | }
92 |
93 | return sftpClient, nil
94 |
95 | }
96 |
97 | func uploadFile(sftpClient *sftp.Client, localFilePath string, remotePath string) (err error) {
98 |
99 | var infoMess string
100 |
101 | srcFile, err := os.Open(localFilePath)
102 | if err != nil {
103 | infoMess = fmt.Sprintf("Error in open file %s", localFilePath)
104 | Log("ERROR", infoMess)
105 | return err
106 | }
107 | defer srcFile.Close()
108 |
109 | var remoteFileName = path.Base(localFilePath)
110 | dstFile, err := sftpClient.Create(path.Join(remotePath, remoteFileName))
111 | if err != nil {
112 | infoMess = fmt.Sprintf("sftpClient.Create error : %s, error = %v", path.Join(remotePath, remoteFileName), err)
113 | Log("ERROR", infoMess)
114 | return err
115 | }
116 | defer dstFile.Close()
117 |
118 | ff, err := ioutil.ReadAll(srcFile)
119 | if err != nil {
120 | infoMess = fmt.Sprintf("ReadAll error : %s", localFilePath)
121 | Log("ERROR", infoMess)
122 | return err
123 | }
124 |
125 | dstFile.Write(ff)
126 | infoMess = localFilePath + " copy file to remote server finished!"
127 | Log("DEBUG", infoMess)
128 | // Chmod remoteFile
129 | fileStat, err := os.Stat(localFilePath)
130 | if err != nil {
131 | infoMess = fmt.Sprintf("Error in get file stat when upload file: [sourceFile = %s targetFile = %s]", localFilePath, path.Join(remotePath, remoteFileName))
132 | Log("ERROR", infoMess)
133 | return err
134 | }
135 |
136 | err = sftpClient.Chmod(path.Join(remotePath, remoteFileName), fileStat.Mode())
137 | if err != nil {
138 | infoMess = fmt.Sprintf("Error in chmod file stat when upload file: [sourceFile = %s targetFile = %s]", localFilePath, path.Join(remotePath, remoteFileName))
139 | Log("ERROR", infoMess)
140 | return err
141 | }
142 | infoMess = fmt.Sprintf("chmod file [%s] to %s", path.Join(remotePath, remoteFileName), fileStat.Mode())
143 | Log("DEBUG", infoMess)
144 | //Log("INFO", infoMess)
145 | return err
146 | }
147 |
148 |
149 | func uploadDirectory(sftpClient *sftp.Client, localPath string, remotePath string) (err error) {
150 |
151 | var infoMess string
152 |
153 | localFiles, err := ioutil.ReadDir(localPath)
154 | if err != nil {
155 | infoMess = "Read dir list fail."
156 | Log("ERROR", infoMess)
157 | return err
158 | }
159 |
160 | for _, backupDir := range localFiles {
161 |
162 | localFilePath := path.Join(localPath, backupDir.Name())
163 | remoteFilePath := path.Join(remotePath, backupDir.Name())
164 |
165 | if backupDir.IsDir() {
166 | sftpClient.Mkdir(remoteFilePath)
167 | err = uploadDirectory(sftpClient, localFilePath, remoteFilePath)
168 | if err != nil {
169 | infoMess = fmt.Sprintf("Error in upload dir %s\t%s\t%s", sftpClient, localFilePath, remoteFilePath)
170 | Log("ERROR", infoMess)
171 | return err
172 | }
173 | } else {
174 | err = uploadFile(sftpClient, path.Join(localPath, backupDir.Name()), remotePath)
175 | if err != nil {
176 | infoMess = fmt.Sprintf("Error in upload file %s\t%s\t%s", sftpClient, path.Join(localPath, backupDir.Name()), remotePath)
177 | Log("ERROR", infoMess)
178 | return err
179 | }
180 | }
181 |
182 | }
183 |
184 | //infoMess = localPath + " copy directory to remote server finished!"
185 | //Log("INFO", infoMess)
186 | return err
187 | }
188 |
189 |
190 | func UpLoadFile(user string, keyFile string, host string, port int, sourceFile string, targetDir string) {
191 |
192 | // sftpConnect(config *ssh.ClientConfig, host string, port int) (sfpClient *sftp.Client, err error)
193 | // NewConfig(keyFile string, user string)(config *ssh.ClientConfig, err error)
194 | // uploadFile(sftpClient *sftp.Client, localFilePath string, remotePath string)
195 | var infoMess string
196 | sshConfig, err := NewConfig(keyFile, user)
197 | if err != nil {
198 | infoMess = fmt.Sprintf("Error in upload file, fail to get ssh config [keyfile = %s, user = %s]", keyFile, user)
199 | Log("ERROR", infoMess)
200 | }
201 |
202 | sftpClient, err := sftpConnect(sshConfig, host, port)
203 | if err != nil {
204 | infoMess = fmt.Sprintf("Error in upload file, fail to get sftp client [keyfile = %s, user = %s, host = %s, port = %d]", keyFile, user, host, port)
205 | Log("ERROR", infoMess)
206 | }
207 |
208 | uploadFile(sftpClient, sourceFile, targetDir)
209 |
210 | }
211 |
212 | func UploadDir(user string, keyFile string, host string, port int, sourceDir string, targetDir string) {
213 |
214 | var infoMess string
215 | sshConfig, err := NewConfig(keyFile, user)
216 | if err != nil {
217 | infoMess = fmt.Sprintf(`Error in upload dir[get ssh config]: user = %s
218 | keyFile = %s
219 | host = %s
220 | port = %d
221 | sourceDir = %s
222 | targetDir = %s`,
223 | user, keyFile, host, port, sourceDir, targetDir)
224 | Log("ERROR", infoMess)
225 | panic(err)
226 | }
227 |
228 | // check the folder exist
229 | cmd := fmt.Sprintf("ls %s", targetDir)
230 | _, err = SshRun(sshConfig, host, port, cmd)
231 | if err != nil {
232 | infoMess = fmt.Sprintf("The target dir [%s] doesn't exist on [%s:%d], create a new one", targetDir, host, port)
233 | Log("WARN", infoMess)
234 | cmd = fmt.Sprintf("mkdir -p %s", targetDir)
235 | _, err := SshRun(sshConfig, host, port, cmd)
236 | if err != nil {
237 | infoMess = fmt.Sprintf("Error in create folder [%s] on [%s:%d]", targetDir, host, port)
238 | Log("ERROR", infoMess)
239 | panic(err)
240 | }
241 | infoMess = fmt.Sprintf("Create folder [%s] on [%s:%d]", targetDir, host, port)
242 | Log("WARN", infoMess)
243 | }
244 |
245 | sftpClient, err := sftpConnect(sshConfig, host, port)
246 | if err != nil {
247 | infoMess = fmt.Sprintf(`Error in upload dir[sftp client]: user = %s
248 | keyFile = %s
249 | host = %s
250 | port = %d
251 | sourceDir = %s
252 | targetDir = %s`,
253 | user, keyFile, host, port, sourceDir, targetDir)
254 | Log("ERROR", infoMess)
255 | panic(err)
256 | }
257 |
258 | err = uploadDirectory(sftpClient, sourceDir, targetDir)
259 | if err != nil {
260 | infoMess = fmt.Sprintf(`Error in upload dir[upload dir]: user = %s
261 | keyFile = %s
262 | host = %s
263 | port = %d
264 | sourceDir = %s
265 | targetDir = %s`,
266 | user, keyFile, host, port, sourceDir, targetDir)
267 | Log("ERROR", infoMess)
268 | panic(err)
269 | }
270 |
271 | }
272 |
273 |
274 |
275 | func TestUploadDir() {
276 |
277 | sshConfig, err := NewConfig("/root/.ssh/id_rsa", "root")
278 | if err != nil { panic(err) }
279 |
280 | // check targetDir exist
281 | output, err := SshRun(sshConfig, "192.168.230.41", 22, "ls /opt/starrocks/fe/jdk")
282 | fmt.Printf("The result of [ls /opt/starrocks/fe/jdk] on 192.168.230.41:22 ---- output = %s, error = %v\n", output, err)
283 |
284 | if err != nil {
285 | fmt.Println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
286 | fmt.Println("The target dir [/opt/starrocks/fe/jdk] doesn't exist on [192.168.230.41].")
287 | _, err := SshRun(sshConfig, "192.168.230.41", 22, "mkdir -p /opt/starrocks/fe/jdk")
288 |
289 | if err != nil {
290 | fmt.Println("Error in create folder [/opt/starrocks/fe/jdk] on [192.168.230.41]")
291 | panic(err)
292 | }
293 |
294 | }
295 | /*
296 | sftpClient, err := sftpConnect(sshConfig, "192.168.230.41", 22)
297 | if err != nil { panic(err) }
298 | uploadDirectory(sftpClient, "/tmp/aaaDir", "/opt/soft/tmp")
299 | */
300 | }
301 |
302 |
303 |
--------------------------------------------------------------------------------
/stargo-pkg.tar.gz:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wangtianyi2004/starrocks-controller/e01b4bf75a2b210e85e4844b127485a2390b6d17/stargo-pkg.tar.gz
--------------------------------------------------------------------------------