package controllers
|
|
import (
|
"fmt"
|
"time"
|
"vamicro/config"
|
"vamicro/system-service/sys"
|
"vamicro/system-service/util"
|
|
"basic.com/valib/bhomeclient.git"
|
"basic.com/valib/logger.git"
|
)
|
|
type WeedInfo struct {
|
ScriptPath string `json:"scriptPath"`
|
Peers []string `json:"peers"`
|
DefaultReplication string `json:"defaultReplication"`
|
}
|
|
type SyncSWFSNodeController struct{}
|
|
func (ssn *SyncSWFSNodeController) SyncSWFSNode(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
|
var err error
|
var spp WeedInfo
|
logger.Debug("SyncSWFSNode start")
|
if err = c.BindJSON(&spp); err != nil {
|
logger.Error("SyncSWFSNode param error")
|
return &bhomeclient.Reply{Msg: "param error"}
|
}
|
|
ip, _, _ := sys.GetLocalIP(config.Server.NetworkAdapter)
|
scriptPath := spp.ScriptPath
|
peers := spp.Peers
|
defaultReplication := spp.DefaultReplication
|
_, err = util.VerifyServer(ip)
|
logger.Debugf("SyncSWFSNode req=%v, ip=%v, peers=%v, err=%v", spp, ip, peers, err)
|
if err == nil {
|
sp := util.StopServer(scriptPath)
|
if sp == false {
|
logger.Errorf("SyncSWFSNode StopServer, ip=%v, peers=%v", ip, peers)
|
return &bhomeclient.Reply{Msg: "stop scriptPath 路径错误!"}
|
}
|
}
|
|
if !util.SetConfig(scriptPath, ip, peers, defaultReplication) {
|
logger.Errorf("SyncSWFSNode SetConfig, ip=%v, peers=%v", ip, peers)
|
return &bhomeclient.Reply{Msg: "scriptPath 路径错误!"}
|
}
|
if !util.StartServer(scriptPath) {
|
logger.Errorf("SyncSWFSNode StartServer, ip=%v, peers=%v", ip, peers)
|
return &bhomeclient.Reply{Msg: "scriptPath 路径错误!"}
|
}
|
|
time.Sleep(time.Second * 3)
|
_, errF := util.VerifyServer(ip)
|
logger.Debugf("SyncSWFSNode first time wait 3 sec, errF=%v, ip=%v, peers=%v", errF, ip, peers)
|
if errF != nil {
|
time.Sleep(time.Second * 3)
|
_, errS := util.VerifyServer(ip)
|
logger.Debugf("SyncSWFSNode second time wait 3 sec, errS=%v, ip=%v, peers=%v", errS, ip, peers)
|
if errS != nil {
|
logger.Errorf("SyncSWFSNode 启动超时, errS=%v, ip=%v, peers=%v", errS, ip, peers)
|
return &bhomeclient.Reply{Msg: "启动超时"}
|
}
|
}
|
logger.Debugf("SyncSWFSNode success, ip=%v, peers=%v", ip, peers)
|
return &bhomeclient.Reply{Success: true, Msg: fmt.Sprintf("%v: 节点同步成功", ip)}
|
}
|
|
func (ssn *SyncSWFSNodeController) GetSWFSPeers(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
|
var spp WeedInfo
|
c.BindJSON(&spp)
|
scriptPath := spp.ScriptPath
|
configInfo, err := util.GetConfig(scriptPath)
|
if err != nil {
|
return &bhomeclient.Reply{Msg: "配置文件路径或文件格式错误!!!"}
|
}
|
if len(configInfo.Peers) < 1 {
|
return &bhomeclient.Reply{Msg: "目标节点服务不存在"}
|
}
|
return &bhomeclient.Reply{Success: true, Data: configInfo.Peers}
|
}
|