From 17172e532bed5bb9e1c6da21c5784e767d882624 Mon Sep 17 00:00:00 2001
From: sunty <1172534965@qq.com>
Date: 星期三, 15 四月 2020 11:53:14 +0800
Subject: [PATCH] fix test
---
controllers/swfsControllers.go | 144 ++++++++++++++++++++++++++++++++----------------
1 files changed, 96 insertions(+), 48 deletions(-)
diff --git a/controllers/swfsControllers.go b/controllers/swfsControllers.go
index 3e770e3..61dea78 100644
--- a/controllers/swfsControllers.go
+++ b/controllers/swfsControllers.go
@@ -3,6 +3,7 @@
import (
"fmt"
"github.com/gin-gonic/gin"
+ "io/ioutil"
"net/http"
"strconv"
"strings"
@@ -14,10 +15,19 @@
)
const (
- StartServerScript = "seaweedfs_start.sh"
- StopServerScript = "seaweedfs_stop.sh"
- StartScriptAsVolume = "option=1"
- StartScriptAsMaster = "option=2"
+ StartServerScript = "seaweedfs_start.sh"
+ StopServerScript = "seaweedfs_stop.sh"
+ //浣滀负 volume 鍚姩
+ StartScriptAsVolume = "1"
+ //浣滀负 master 鍚姩
+ StartScriptAsMaster = "2"
+ //浣滀负 master + volume 鍚姩
+ StartScriptAsMaVo = "3"
+ //鍚姩椤瑰弬鏁板ご
+ Option = "option="
+ //master鍒楄〃椤瑰弬鏁板ご
+ Peer = "peers="
+ //StartScriptAsFiler = "option=4"
)
type SeaweedfsController struct{}
@@ -27,10 +37,8 @@
}
func (sc *SeaweedfsController) UpdateSWFSServiceController(c *gin.Context) {
- oldPeers := GetOldPeers(config.Server.ScriptPath, StartServerScript)
- newPeers := GetNewPeers()
- UpdatePeers(config.Server.ScriptPath, StartServerScript, oldPeers, newPeers)
- ReplaceLineContentBySearch(StartScriptAsVolume, config.Server.ScriptPath, StartServerScript)
+ ReplaceLineContentBySearch(GetNewPeers(), Peer, config.Server.ScriptPath, StartServerScript)
+ util.ResponseFormat(c, code.Success, config.Server.EsServerIp+"鏇存柊鎴愬姛")
}
// @Security ApiKeyAuth
@@ -48,14 +56,21 @@
c.BindJSON(&body)
role := body.Role
if role == "master" {
- AddNewMasterToPeers()
AsMaster()
util.ResponseFormat(c, code.AddSuccess, "鍔犲叆鑺傜偣鎴愬姛")
return
} else if role == "volume" {
- AsVolume()
+ status := AsVolume()
+ if status == true {
+ util.ResponseFormat(c, code.AddSuccess, "鍔犲叆鑺傜偣鎴愬姛")
+ return
+ } else {
+ util.ResponseFormat(c, code.AddClusterInfoErr, "褰撳墠杩樻病鏈変富鑺傜偣")
+ return
+ }
+ } else if role == "master+volume" {
+ AsMaVo()
util.ResponseFormat(c, code.AddSuccess, "鍔犲叆鑺傜偣鎴愬姛")
- return
} else {
util.ResponseFormat(c, code.RequestParamError, "閫夋嫨鑺傜偣绫诲瀷閿欒")
return
@@ -63,14 +78,22 @@
}
-func (sc *SeaweedfsController) RoleOfVolumeToMasterController(c *gin.Context) {
+func AsMaVo() {
AsMaster()
+ ReplaceLineContentBySearch(StartScriptAsMaVo, Option, config.Server.ScriptPath, StartServerScript)
}
-func (sc *SeaweedfsController) RestartMasterController(c *gin.Context) {
+func (sc *SeaweedfsController) RoleOfVolumeToMasterController(c *gin.Context) {
+ AsMaster()
+ ReplaceLineContentBySearch(StartScriptAsMaVo, Option, config.Server.ScriptPath, StartServerScript)
+}
+
+func (sc *SeaweedfsController) RestartServerController(c *gin.Context) {
StopServer(config.Server.ScriptPath)
time.Sleep(time.Second * 1)
StartServer(config.Server.ScriptPath)
+ result := strings.Split("=", GetLocalStartupItem(config.Server.ScriptPath, StartServerScript))[1]
+ util.ResponseFormat(c, code.Success, result)
}
//鍚姩鏈嶅姟
@@ -84,9 +107,9 @@
}
//鏍规嵁鎼滅储鍐呭鏇挎崲鏁磋鍐呭
-func ReplaceLineContentBySearch(replaceContent string, scriptPath string, scriptFile string) {
- resContent := GetNowLineContent(scriptPath+"/"+scriptFile, "option=")
- replaceStr := "sed -ie 's/" + resContent + "/" + replaceContent + "/g' " + scriptPath + "/" + scriptFile
+func ReplaceLineContentBySearch(replaceContent string, searchContent string, scriptPath string, scriptFile string) {
+ resSearchContent := GetNowLineContent(scriptPath+"/"+scriptFile, searchContent)
+ replaceStr := "sed -ie 's/" + resSearchContent + "/" + replaceContent + "/g' " + scriptPath + "/" + scriptFile
util.RunScript(replaceStr)
}
@@ -106,23 +129,53 @@
RestartAllServer(nowPeers, coreBaseUnit)
}
-//閲嶅惎鍏朵粬鑺傜偣鏈嶅姟骞堕獙璇�
-func RestartServer(ip string, timeOut int) {
- url := "http://" + ip + ":7020/node/api-v/swfs/restartMaster"
- http.Get(url)
+//閲嶅惎鎵�鏈夎妭鐐规湇鍔″苟楠岃瘉
+func Restart(ip string, timeOut int) {
+ url := "http://" + ip + ":7020/node/api-v/swfs/restartServer"
+ httpRes, _ := http.Get(url)
+ defer httpRes.Body.Close()
+ body, _ := ioutil.ReadAll(httpRes.Body)
+ startupItem := ""
+ if httpRes.StatusCode == 200 {
+ startupItem = string(body)
+ }
tick := time.Tick(1 * time.Second)
for countdown := timeOut; countdown > 0; countdown-- {
- verificationMasterUrl := "http://" + ip + ":6333"
- verificationVolume1Url := "http://" + ip + ":6700"
- verificationVolume2Url := "http://" + ip + ":6701"
- _, masterErr := http.Get(verificationMasterUrl)
- _, volume1Err := http.Get(verificationVolume1Url)
- _, volume2Err := http.Get(verificationVolume2Url)
- if masterErr == nil && volume1Err == nil && volume2Err == nil {
- break
- }
+ Verification(startupItem, ip)
<-tick
}
+}
+
+func Verification(startupItem string, ip string) {
+ switch startupItem {
+ case StartScriptAsVolume:
+ verificationVolumeUrl := "http://" + ip + ":6700"
+ _, volume1Err := http.Get(verificationVolumeUrl)
+ if volume1Err == nil {
+ break
+ }
+ case StartScriptAsMaster:
+ verificationMasterUrl := "http://" + ip + ":6333"
+ _, masterErr := http.Get(verificationMasterUrl)
+ if masterErr == nil {
+ break
+ }
+ case StartScriptAsMaVo:
+ verificationMasterUrl := "http://" + ip + ":6333"
+ verificationVolumeUrl := "http://" + ip + ":6700"
+ _, masterErr := http.Get(verificationMasterUrl)
+ _, volume1Err := http.Get(verificationVolumeUrl)
+ if masterErr == nil && volume1Err == nil {
+ break
+ }
+ }
+}
+
+//鑾峰彇鏈湴鍚姩椤�
+func GetLocalStartupItem(scriptPath string, scriptFile string) string {
+ startupItem := GetNowLineContent(scriptPath+"/"+scriptFile, Option)
+ fmt.Println(startupItem)
+ return startupItem
}
//鏋勫缓閲嶅惎娴佺▼
@@ -136,7 +189,7 @@
if (i+1)%coreThread == 0 {
masterIp = append(masterIp, strings.Split(ip, ":")[0])
for i := 0; i < len(masterIp); i++ {
- go RestartServer(masterIp[i], timeOut)
+ go Restart(masterIp[i], timeOut)
waitGroup.Add(1) //姣忓垱寤轰竴涓猤oroutine锛屽氨鎶婁换鍔¢槦鍒椾腑浠诲姟鐨勬暟閲�+1
}
waitGroup.Wait() //.Wait()杩欓噷浼氬彂鐢熼樆濉烇紝鐩村埌闃熷垪涓墍鏈夌殑浠诲姟缁撴潫灏变細瑙i櫎闃诲
@@ -146,7 +199,7 @@
if len(nowPeers) == i+1 {
var waitGroup sync.WaitGroup
for i := 0; i < len(masterIp); i++ {
- go RestartServer(masterIp[i], timeOut)
+ go Restart(masterIp[i], timeOut)
waitGroup.Add(1) //姣忓垱寤轰竴涓猤oroutine锛屽氨鎶婁换鍔¢槦鍒椾腑浠诲姟鐨勬暟閲�+1
}
waitGroup.Wait() //.Wait()杩欓噷浼氬彂鐢熼樆濉烇紝鐩村埌闃熷垪涓墍鏈夌殑浠诲姟缁撴潫灏变細瑙i櫎闃诲
@@ -158,11 +211,6 @@
}
}
-//鑾峰彇鏈湴浠ヤ娇鐢ㄩ泦缇よ〃鍗�
-func GetOldPeers(scriptPath string, scriptFile string) string {
- return GetNowLineContent(scriptPath+"/"+scriptFile, "peers=")
-}
-
//鑾峰彇鏌ユ壘鍐呭褰撳墠琛屽唴瀹�
func GetNowLineContent(filePath string, searchContent string) string {
scriptStr := "cat" + filePath + "| grep " + searchContent
@@ -170,17 +218,23 @@
}
//浣滀负鏁版嵁鑺傜偣鍔犲叆
-func AsVolume() {
- UpdatePeers(config.Server.ScriptPath, StartServerScript, GetNewPeers(), GetNewPeers())
- ReplaceLineContentBySearch(StartScriptAsVolume, config.Server.ScriptPath, StartServerScript)
- ReplaceLineContentBySearch("peers=", config.Server.ScriptPath, StartServerScript)
+func AsVolume() bool {
+ nowPeers := GetNowPeersList()
+ if nowPeers == nil || len(nowPeers) == 0 {
+ return false
+ }
+ ReplaceLineContentBySearch(StartScriptAsVolume, Option, config.Server.ScriptPath, StartServerScript)
+ ReplaceLineContentBySearch(GetNewPeers(), Peer, config.Server.ScriptPath, StartServerScript)
StartServer(config.Server.ScriptPath)
+ return true
}
//浣滀负涓昏妭鐐瑰姞鍏ワ紙榛樿鍖呭惈鏁版嵁鑺傜偣锛�
-func AsMaster() {
+func AsMaster() bool {
+ AddNewMasterToPeers()
nowPeers := GetNowPeersList()
RequestMasterNodesOperation(nowPeers)
+ return true
}
//鑾峰彇褰撳墠闆嗙兢鍒楄〃
@@ -210,14 +264,8 @@
//鑾峰彇褰撳墠闆嗙兢鍒楄〃鏍煎紡鍖栦俊鎭�
func GetNewPeers() string {
peers := GetNowPeersList()
- p := "peers=" + strings.Replace(strings.Trim(fmt.Sprint(peers), "[]"), " ", ",", -1)
+ p := Peer + strings.Replace(strings.Trim(fmt.Sprint(peers), "[]"), " ", ",", -1)
return p
-}
-
-//鏇存柊鏈湴闆嗙兢鍒楄〃
-func UpdatePeers(scriptPath string, scriptFile string, oldPeers string, newPeers string) {
- str := "sed -ie 's/" + oldPeers + "/" + newPeers + "/g' " + scriptPath + "/" + scriptFile
- util.RunScript(str)
}
//鍚戦泦缇ゅ姞鍏ユ柊鐨刴aster
--
Gitblit v1.8.0