| | |
| | | package controllers |
| | | |
| | | import ( |
| | | "encoding/json" |
| | | "fmt" |
| | | "github.com/gin-gonic/gin" |
| | | "io/ioutil" |
| | |
| | | |
| | | func AsMaVo() { |
| | | AsMaster() |
| | | ReplaceLineContentBySearch(StartScriptAsMaVo, Option, config.Server.ScriptPath, StartServerScript) |
| | | ReplaceLineContentBySearch(Option+StartScriptAsMaVo, Option, config.Server.ScriptPath, StartServerScript) |
| | | } |
| | | |
| | | func (sc *SeaweedfsController) RoleOfVolumeToMasterController(c *gin.Context) { |
| | | AsMaster() |
| | | ReplaceLineContentBySearch(StartScriptAsMaVo, Option, config.Server.ScriptPath, StartServerScript) |
| | | ReplaceLineContentBySearch(Option+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] |
| | | //fmt.Println("GetLocalStartupItem: ", GetLocalStartupItem(config.Server.ScriptPath, StartServerScript)) |
| | | result := strings.Split(GetLocalStartupItem(config.Server.ScriptPath, StartServerScript), "=")[1] |
| | | fmt.Println("result: ", result) |
| | | util.ResponseFormat(c, code.Success, result) |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | //更新所有节点的脚本参数 |
| | | func UpdateAllNodesScriptArgument(nowPeers []interface{}) { |
| | | for _, val := range nowPeers { |
| | | ip := val.(string) |
| | | func UpdateAllNodesScriptArgument(peersIp []string) { |
| | | for _, ip := range peersIp { |
| | | fmt.Println("ip: ", ip) |
| | | url := "http://" + ip + ":7020/node/api-v/swfs/updateSWFSService" |
| | | fmt.Println("url", url) |
| | | http.Get(url) |
| | | } |
| | | } |
| | | |
| | | //请求作为主节点操作流程 |
| | | func RequestMasterNodesOperation(nowPeers []interface{}) { |
| | | peersIp := make([]string, 0) |
| | | for _, val := range nowPeers { |
| | | peersIp = append(peersIp, strings.Split(val.(string), ":")[0]) |
| | | } |
| | | coreBaseUnit, _ := strconv.Atoi(config.Server.CoreBaseUnit) |
| | | UpdateAllNodesScriptArgument(nowPeers) |
| | | RestartAllServer(nowPeers, coreBaseUnit) |
| | | UpdateAllNodesScriptArgument(peersIp) |
| | | RestartAllServer(peersIp, coreBaseUnit) |
| | | } |
| | | |
| | | //重启所有节点服务并验证 |
| | | func Restart(ip string, timeOut int) { |
| | | url := "http://" + ip + ":7020/node/api-v/swfs/restartServer" |
| | | var info interface{} |
| | | httpRes, _ := http.Get(url) |
| | | defer httpRes.Body.Close() |
| | | body, _ := ioutil.ReadAll(httpRes.Body) |
| | | startupItem := "" |
| | | json.Unmarshal(body, &info) |
| | | res, ok := info.(map[string]interface{}) |
| | | if !ok { |
| | | fmt.Println("http response interface can not change map[string]interface{}") |
| | | } |
| | | startupItem := res["data"].(string) |
| | | if httpRes.StatusCode == 200 { |
| | | startupItem = string(body) |
| | | } |
| | | tick := time.Tick(1 * time.Second) |
| | | for countdown := timeOut; countdown > 0; countdown-- { |
| | | Verification(startupItem, ip) |
| | | result := Verification(startupItem, ip) |
| | | if result == true { |
| | | break |
| | | } |
| | | <-tick |
| | | } |
| | | } |
| | | |
| | | func Verification(startupItem string, ip string) { |
| | | //验证服务状态 |
| | | func Verification(startupItem string, ip string) bool { |
| | | resStatu := false |
| | | switch startupItem { |
| | | case StartScriptAsVolume: |
| | | verificationVolumeUrl := "http://" + ip + ":6700" |
| | | _, volume1Err := http.Get(verificationVolumeUrl) |
| | | if volume1Err == nil { |
| | | break |
| | | resStatu = true |
| | | } |
| | | case StartScriptAsMaster: |
| | | verificationMasterUrl := "http://" + ip + ":6333" |
| | | _, masterErr := http.Get(verificationMasterUrl) |
| | | if masterErr == nil { |
| | | break |
| | | resStatu = true |
| | | } |
| | | case StartScriptAsMaVo: |
| | | verificationMasterUrl := "http://" + ip + ":6333" |
| | |
| | | _, masterErr := http.Get(verificationMasterUrl) |
| | | _, volume1Err := http.Get(verificationVolumeUrl) |
| | | if masterErr == nil && volume1Err == nil { |
| | | break |
| | | resStatu = true |
| | | } |
| | | } |
| | | return resStatu |
| | | } |
| | | |
| | | //获取本地启动项 |
| | | func GetLocalStartupItem(scriptPath string, scriptFile string) string { |
| | | startupItem := GetNowLineContent(scriptPath+"/"+scriptFile, Option) |
| | | fmt.Println(startupItem) |
| | | fmt.Println("startupItem: ", startupItem) |
| | | return startupItem |
| | | } |
| | | |
| | | //构建重启流程 |
| | | func RestartAllServer(nowPeers []interface{}, coreBaseUnit int) { |
| | | coreThread := len(nowPeers)/coreBaseUnit + 1 |
| | | func RestartAllServer(peersIp []string, coreBaseUnit int) { |
| | | coreThread := len(peersIp)/coreBaseUnit + 1 |
| | | masterIp := make([]string, 0) |
| | | timeOut, _ := strconv.Atoi(config.Server.TimeOut) |
| | | var waitGroup sync.WaitGroup |
| | | for i, val := range nowPeers { |
| | | ip := val.(string) |
| | | for i, ip := range peersIp { |
| | | if (i+1)%coreThread == 0 { |
| | | masterIp = append(masterIp, strings.Split(ip, ":")[0]) |
| | | for i := 0; i < len(masterIp); i++ { |
| | |
| | | masterIp = make([]string, 0) |
| | | } else { |
| | | masterIp = append(masterIp, strings.Split(ip, ":")[0]) |
| | | if len(nowPeers) == i+1 { |
| | | if len(peersIp) == i+1 { |
| | | var waitGroup sync.WaitGroup |
| | | for i := 0; i < len(masterIp); i++ { |
| | | go Restart(masterIp[i], timeOut) |
| | |
| | | |
| | | //获取查找内容当前行内容 |
| | | func GetNowLineContent(filePath string, searchContent string) string { |
| | | scriptStr := "cat" + filePath + "| grep " + searchContent |
| | | scriptStr := "cat " + filePath + "| grep " + searchContent |
| | | fmt.Println("scriptStr: ", scriptStr) |
| | | return strings.Split(util.RunScript(scriptStr), "\n")[0] |
| | | } |
| | | |
| | |
| | | if nowPeers == nil || len(nowPeers) == 0 { |
| | | return false |
| | | } |
| | | ReplaceLineContentBySearch(StartScriptAsVolume, Option, config.Server.ScriptPath, StartServerScript) |
| | | ReplaceLineContentBySearch(Option+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() |
| | | fmt.Println("nowPeers: ", nowPeers) |
| | | RequestMasterNodesOperation(nowPeers) |
| | | return true |
| | | } |
| | |
| | | //向集群加入新的master |
| | | func AddNewMasterToPeers() (result bool) { |
| | | peer := config.Server.EsServerIp + ":6333" |
| | | addUrl := "http://" + config.Server.EsServerIp + ":" + config.Server.EsServerPort + "/" + config.BasicFS.IndexName + "/_update_by_query" |
| | | addUrl := "http://" + config.Server.EsServerIp + ":" + config.Server.EsServerPort + "/" + config.BasicFS.IndexName + "/_update_by_query?refresh=true" |
| | | addJson := `{ |
| | | "script": { |
| | | "lang": "painless", |