From f4c1ad199cdd31a454a60e50661db0de1747977a Mon Sep 17 00:00:00 2001
From: sunty <1172534965@qq.com>
Date: 星期五, 17 四月 2020 17:35:26 +0800
Subject: [PATCH] fix last

---
 controllers/swfsControllers.go |  254 ++------------------------------------------------
 1 files changed, 14 insertions(+), 240 deletions(-)

diff --git a/controllers/swfsControllers.go b/controllers/swfsControllers.go
index 61dea78..5e009e0 100644
--- a/controllers/swfsControllers.go
+++ b/controllers/swfsControllers.go
@@ -3,31 +3,14 @@
 import (
 	"fmt"
 	"github.com/gin-gonic/gin"
-	"io/ioutil"
-	"net/http"
-	"strconv"
 	"strings"
 	"swfs/code"
 	"swfs/config"
+	"swfs/tools/es"
+	"swfs/tools/middleware"
+	"swfs/tools/script"
 	"swfs/util"
-	"sync"
 	"time"
-)
-
-const (
-	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{}
@@ -37,7 +20,7 @@
 }
 
 func (sc *SeaweedfsController) UpdateSWFSServiceController(c *gin.Context) {
-	ReplaceLineContentBySearch(GetNewPeers(), Peer, config.Server.ScriptPath, StartServerScript)
+	script.ReplaceLineContentBySearch(es.GetNewPeers(), config.Peer, config.Server.ScriptPath, config.StartServerScript)
 	util.ResponseFormat(c, code.Success, config.Server.EsServerIp+"鏇存柊鎴愬姛")
 }
 
@@ -56,11 +39,11 @@
 	c.BindJSON(&body)
 	role := body.Role
 	if role == "master" {
-		AsMaster()
+		middleware.AsMaster(role)
 		util.ResponseFormat(c, code.AddSuccess, "鍔犲叆鑺傜偣鎴愬姛")
 		return
 	} else if role == "volume" {
-		status := AsVolume()
+		status := middleware.AsVolume()
 		if status == true {
 			util.ResponseFormat(c, code.AddSuccess, "鍔犲叆鑺傜偣鎴愬姛")
 			return
@@ -69,7 +52,7 @@
 			return
 		}
 	} else if role == "master+volume" {
-		AsMaVo()
+		middleware.AsMaVo(role)
 		util.ResponseFormat(c, code.AddSuccess, "鍔犲叆鑺傜偣鎴愬姛")
 	} else {
 		util.ResponseFormat(c, code.RequestParamError, "閫夋嫨鑺傜偣绫诲瀷閿欒")
@@ -78,226 +61,17 @@
 
 }
 
-func AsMaVo() {
-	AsMaster()
-	ReplaceLineContentBySearch(StartScriptAsMaVo, Option, config.Server.ScriptPath, StartServerScript)
-}
-
 func (sc *SeaweedfsController) RoleOfVolumeToMasterController(c *gin.Context) {
-	AsMaster()
-	ReplaceLineContentBySearch(StartScriptAsMaVo, Option, config.Server.ScriptPath, StartServerScript)
+	middleware.AsMaster("master+volume")
+	util.ResponseFormat(c, code.Success, "瑙掕壊娣诲姞鎴愬姛")
 }
 
 func (sc *SeaweedfsController) RestartServerController(c *gin.Context) {
-	StopServer(config.Server.ScriptPath)
+	script.StopServer(config.Server.ScriptPath)
+	script.StartServer(config.Server.ScriptPath)
 	time.Sleep(time.Second * 1)
-	StartServer(config.Server.ScriptPath)
-	result := strings.Split("=", GetLocalStartupItem(config.Server.ScriptPath, StartServerScript))[1]
+	//fmt.Println("GetLocalStartupItem: ", GetLocalStartupItem(config.Server.ScriptPath, StartServerScript))
+	result := strings.Split(middleware.GetLocalStartupItem(config.Server.ScriptPath, config.StartServerScript), "=")[1]
+	fmt.Println("result: ", result)
 	util.ResponseFormat(c, code.Success, result)
-}
-
-//鍚姩鏈嶅姟
-func StartServer(scriptPath string) {
-	util.RunScript("sh " + scriptPath + StartServerScript)
-}
-
-//鍋滄鏈嶅姟
-func StopServer(scriptPath string) {
-	util.RunScript("sh " + scriptPath + StopServerScript)
-}
-
-//鏍规嵁鎼滅储鍐呭鏇挎崲鏁磋鍐呭
-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)
-}
-
-//鏇存柊鎵�鏈夎妭鐐圭殑鑴氭湰鍙傛暟
-func UpdateAllNodesScriptArgument(nowPeers []interface{}) {
-	for _, val := range nowPeers {
-		ip := val.(string)
-		url := "http://" + ip + ":7020/node/api-v/swfs/updateSWFSService"
-		http.Get(url)
-	}
-}
-
-//璇锋眰浣滀负涓昏妭鐐规搷浣滄祦绋�
-func RequestMasterNodesOperation(nowPeers []interface{}) {
-	coreBaseUnit, _ := strconv.Atoi(config.Server.CoreBaseUnit)
-	UpdateAllNodesScriptArgument(nowPeers)
-	RestartAllServer(nowPeers, coreBaseUnit)
-}
-
-//閲嶅惎鎵�鏈夎妭鐐规湇鍔″苟楠岃瘉
-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-- {
-		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
-}
-
-//鏋勫缓閲嶅惎娴佺▼
-func RestartAllServer(nowPeers []interface{}, coreBaseUnit int) {
-	coreThread := len(nowPeers)/coreBaseUnit + 1
-	masterIp := make([]string, 0)
-	timeOut, _ := strconv.Atoi(config.Server.TimeOut)
-	var waitGroup sync.WaitGroup
-	for i, val := range nowPeers {
-		ip := val.(string)
-		if (i+1)%coreThread == 0 {
-			masterIp = append(masterIp, strings.Split(ip, ":")[0])
-			for i := 0; i < len(masterIp); i++ {
-				go Restart(masterIp[i], timeOut)
-				waitGroup.Add(1) //姣忓垱寤轰竴涓猤oroutine锛屽氨鎶婁换鍔¢槦鍒椾腑浠诲姟鐨勬暟閲�+1
-			}
-			waitGroup.Wait() //.Wait()杩欓噷浼氬彂鐢熼樆濉烇紝鐩村埌闃熷垪涓墍鏈夌殑浠诲姟缁撴潫灏变細瑙i櫎闃诲
-			masterIp = make([]string, 0)
-		} else {
-			masterIp = append(masterIp, strings.Split(ip, ":")[0])
-			if len(nowPeers) == i+1 {
-				var waitGroup sync.WaitGroup
-				for i := 0; i < len(masterIp); i++ {
-					go Restart(masterIp[i], timeOut)
-					waitGroup.Add(1) //姣忓垱寤轰竴涓猤oroutine锛屽氨鎶婁换鍔¢槦鍒椾腑浠诲姟鐨勬暟閲�+1
-				}
-				waitGroup.Wait() //.Wait()杩欓噷浼氬彂鐢熼樆濉烇紝鐩村埌闃熷垪涓墍鏈夌殑浠诲姟缁撴潫灏变細瑙i櫎闃诲
-				masterIp = make([]string, 0)
-				break
-			}
-		}
-		continue
-	}
-}
-
-//鑾峰彇鏌ユ壘鍐呭褰撳墠琛屽唴瀹�
-func GetNowLineContent(filePath string, searchContent string) string {
-	scriptStr := "cat" + filePath + "| grep " + searchContent
-	return strings.Split(util.RunScript(scriptStr), "\n")[0]
-}
-
-//浣滀负鏁版嵁鑺傜偣鍔犲叆
-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() bool {
-	AddNewMasterToPeers()
-	nowPeers := GetNowPeersList()
-	RequestMasterNodesOperation(nowPeers)
-	return true
-}
-
-//鑾峰彇褰撳墠闆嗙兢鍒楄〃
-func GetNowPeersList() []interface{} {
-	getUrl := "http://" + config.Server.EsServerIp + ":" + config.Server.EsServerPort + "/" + config.BasicFS.IndexName + "/_search"
-	getJson := `{
-    "query": {
-        "bool": {
-            "filter": [
-                {
-                    "term": {
-						"application":"nodeOperation"
-					}
-                }
-            ]
-        }
-    },
-    "size": 1
-}`
-
-	buf, _ := util.EsReq("POST", getUrl, []byte(getJson))
-	source, _ := util.Sourcelist(buf)
-	peers := source[0]["peers"].([]interface{})
-	return peers
-}
-
-//鑾峰彇褰撳墠闆嗙兢鍒楄〃鏍煎紡鍖栦俊鎭�
-func GetNewPeers() string {
-	peers := GetNowPeersList()
-	p := Peer + strings.Replace(strings.Trim(fmt.Sprint(peers), "[]"), " ", ",", -1)
-	return p
-}
-
-//鍚戦泦缇ゅ姞鍏ユ柊鐨刴aster
-func AddNewMasterToPeers() (result bool) {
-	peer := config.Server.EsServerIp + ":6333"
-	addUrl := "http://" + config.Server.EsServerIp + ":" + config.Server.EsServerPort + "/" + config.BasicFS.IndexName + "/_update_by_query"
-	addJson := `{
-    "script": {
-        "lang": "painless",
-        "inline": "ctx._source.peers.add(params.newpeer)",
-        "params": {
-            "newpeer": "` + peer + `"
-        }
-    },
-    "query": {
-        "bool": {
-            "filter": [
-                {
-                    "term": {
-                        "application": "nodeOperation"
-                    }
-                }
-            ]
-        }
-    }
-}`
-	buf, _ := util.EsReq("POST", addUrl, []byte(addJson))
-	updateRes, _ := util.SourceUpdated(buf)
-	if updateRes == -1 {
-		result = false
-	} else {
-		result = true
-	}
-	return result
 }

--
Gitblit v1.8.0