package controllers import ( "vamicro/config" "vamicro/system-service/models" "vamicro/system-service/service" "vamicro/system-service/vo" "basic.com/valib/bhomeclient.git" "basic.com/valib/logger.git" uuid "github.com/satori/go.uuid" ) type ClusterController struct { } // @Summary 查询当前集群状态 // @Description 查询本地集群 // @Produce json // @Tags cluster // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" // @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}" // @Router /data/api-v/cluster/status [get] func (cc ClusterController) GetClusterStat(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply { var clusterE models.Cluster var reply = bhomeclient.Reply{ Success: false, Msg: "", Data: nil, } arr, err := clusterE.FindAll() if err == nil { if arr != nil && len(arr) > 0 { // 表示已加入集群 reply.Success = true var nodeE models.Node nodes, _ := nodeE.FindNodesByClusterId(arr[0].ClusterId) //logger.Debug("查询集群节点:", nodes) for _, node := range nodes { //logger.Debug("节点:", node.NodeId, " serverId:", config.Server.AnalyServerId, " stat:", node.DriftState) if node.NodeId == config.Server.AnalyServerId { if node.DriftState == "master" { reply.Msg = "master" } else { reply.Msg = "slave" } break } } reply.Data = nodes } } return &reply } // @Summary 查询本地集群 // @Description 查询本地集群 // @Produce json // @Tags cluster // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" // @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}" // @Router /data/api-v/cluster/findCluster [get] func (cc ClusterController) FindCluster(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply { var clusterE models.Cluster arr, err := clusterE.FindAll() if err == nil { if arr != nil && len(arr) > 0 { var nodeE models.Node nodes, _ := nodeE.FindNodesByClusterId(arr[0].ClusterId) //logger.Debugf("FindCluster nodes=%v", nodes) return &bhomeclient.Reply{Success: true, Data: map[string]interface{}{ "clusterId": arr[0].ClusterId, "clusterName": arr[0].ClusterName, //"password": arr[0].Password, "nodes": nodes, "virtualIp": arr[0].VirtualIp, "localId": config.Server.AnalyServerId, }} } else { return &bhomeclient.Reply{Success: true} } } else { return &bhomeclient.Reply{Success: false, Msg: "集群查询失败"} } } // @Summary 创建集群 // @Description 创建集群 // @Accept json // @Produce json // @Tags cluster // @Param clusterArg body vo.ClusterCreateVo true "集群创建参数" // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" // @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}" // @Router /data/api-v/cluster/create [post] func (cc ClusterController) Create(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply { var clusterVo vo.ClusterCreateVo err := c.BindJSON(&clusterVo) if err != nil || len(clusterVo.Password) != 6 || clusterVo.ClusterName == "" { return &bhomeclient.Reply{Success: false, Msg: "参数有误"} } sv := service.NewClusterService(h.Bk) b, newCluterId := sv.Create(clusterVo.ClusterName, clusterVo.Password, clusterVo.VirtualIp) if b { clusterVo.ClusterId = newCluterId return &bhomeclient.Reply{Success: true, Data: clusterVo} } else { return &bhomeclient.Reply{Success: false, Msg: "创建集群失败"} } } func (cc ClusterController) Update2Master(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply { var nodeVo vo.UpdateClusterVo err := c.BindJSON(&nodeVo) if err != nil || nodeVo.NodeId == "" { return &bhomeclient.Reply{Success: false, Msg: "参数有误"} } sv := service.NewClusterService(h.Bk) b, _ := sv.UpdateDriftStateByNodeId(nodeVo.ClusterId, nodeVo.NodeId, "master") if b { return &bhomeclient.Reply{Success: true, Data: nil} } else { return &bhomeclient.Reply{Success: false, Msg: "变更失败"} } } // @Summary 搜索集群 // @Description 搜索集群 // @Accept json // @Produce json // @Tags cluster // @Param searchArg body vo.ClusterSearchVo true "集群搜索参数" // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" // @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}" // @Router /data/api-v/cluster/search [post] func (cc ClusterController) Search(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply { var csv vo.ClusterSearchVo err := c.BindJSON(&csv) if err != nil || len(csv.Password) != 6 { return &bhomeclient.Reply{Success: false, Msg: "参数有误"} } searchNum := uuid.NewV4().String() sv := service.NewClusterService(h.Bk) err = sv.SearchByPwd(csv.Password) if err != nil { return &bhomeclient.Reply{Success: true, Msg: "搜索中,请稍候..."} } else { return &bhomeclient.Reply{Success: true, Data: searchNum} } } // @Summary 调search后,通过此接口获取查到的集群节点信息 // @Description 调search后,通过此接口获取查到的集群节点信息 // @Accept json // @Produce json // @Tags cluster // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" // @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}" // @Router /data/api-v/cluster/getSearchNodes [get] func (cc ClusterController) GetSearchNodes(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply { sv := service.NewClusterService(h.Bk) nodes := sv.SearchNodes() logger.Debugf("GetSearchNodes nodes=%v, len=%v", nodes, len(nodes)) if nodes != nil && len(nodes) > 0 { var nodeArr []interface{} for _, n := range nodes { nodeArr = append(nodeArr, n) } return &bhomeclient.Reply{Success: true, Msg: "查询成功", Data: nodeArr} } else { return &bhomeclient.Reply{Success: true, Msg: "查询成功", Data: []interface{}{}} } } // @Summary 停止搜索集群 // @Description 停止搜索集群 // @Produce json // @Tags cluster // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" // @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}" // @Router /data/api-v/cluster/stopSearching [post] func (cc ClusterController) StopSearching(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply { sv := service.NewClusterService(h.Bk) if sv.StopSearching() { return &bhomeclient.Reply{Success: true} } else { return &bhomeclient.Reply{Success: false, Msg: "停止失败"} } } // @Summary 加入集群 // @Description 加入集群 // @Accept json // @Produce json // @Tags cluster // @Param clusterArg body vo.ClusterJoinVo true "集群创建参数" // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" // @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}" // @Router /data/api-v/cluster/joinCluster [post] func (cc ClusterController) JoinCluster(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply { var arg vo.ClusterJoinVo err := c.BindJSON(&arg) logger.Debugf("JoinCluster reqParam=%v, err=%v", arg, err) if err != nil { return &bhomeclient.Reply{Success: false, Msg: "参数有误"} } else if arg.ClusterId == "" { return &bhomeclient.Reply{Success: false, Msg: "集群id为空错误"} } else if len(arg.Password) != 6 { return &bhomeclient.Reply{Success: false, Msg: "集群密码错误,密码必须为6位"} } else if len(arg.NodeIps) == 0 { return &bhomeclient.Reply{Success: false, Msg: "集群节点参数为空错误"} } sv := service.NewClusterService(h.Bk) if b, err := sv.JoinCluster(&arg); b { return &bhomeclient.Reply{Success: true, Msg: "加入成功"} } else { return &bhomeclient.Reply{Success: false, Msg: err.Error()} } } // @Summary 保存集群名称 // @Description 保存集群名称 // @Produce json // @Tags cluster // @Param clusterName formData string true "集群名称" // @Param virtualIp formData string false "虚拟ip" // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" // @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}" // @Router /data/api-v/cluster/updateClusterName [post] func (cc ClusterController) UpdateClusterName(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply { clusterName := c.PostForm("clusterName") if clusterName == "" { return &bhomeclient.Reply{Success: false, Msg: "参数有误"} } virtualIp := c.PostForm("virtualIp") sv := service.NewClusterService(h.Bk) b := sv.UpdateClusterName(clusterName, virtualIp) if b { return &bhomeclient.Reply{Success: true, Msg: "更新成功"} } else { return &bhomeclient.Reply{Success: false, Msg: "更新失败"} } } type LeaveArg struct { IsDel bool `json:"isDel"` //是否删除数据 Pwd string `json:"password"` //是否删除数据 } // @Summary 退出集群 // @Description 退出集群 // @Produce json // @Tags cluster // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" // @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}" // @Router /data/api-v/cluster/leave [post] func (cc ClusterController) Leave(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply { var reqBody LeaveArg c.BindJSON(&reqBody) var clusterE models.Cluster arr, err := clusterE.FindAll() if err != nil { return &bhomeclient.Reply{Success: false, Msg: "集群查询失败"} } if arr != nil && len(arr) > 0 { if config.ClusterSet.PwdPre+reqBody.Pwd != arr[0].Password { return &bhomeclient.Reply{Success: false, Msg: "密码错误"} } } else { return &bhomeclient.Reply{Success: false, Msg: "集群查询失败"} } sv := service.NewClusterService(h.Bk) if b, err := sv.Leave(reqBody.IsDel); b { return &bhomeclient.Reply{Success: true, Msg: "退出成功"} } else { return &bhomeclient.Reply{Success: false, Msg: err.Error()} } } func (cc ClusterController) TestSyncSql(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply { sv := service.NewClusterService(h.Bk) if sv.TestSyncSql() { return &bhomeclient.Reply{Success: true, Msg: "测试成功"} } else { return &bhomeclient.Reply{Success: false, Msg: "测试失败"} } } type IpsArgs struct { NodeId string `json:"node_id"` //ip列表 } func (cc ClusterController) FindIpByNode(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply { var reqBody IpsArgs c.BindJSON(&reqBody) if reqBody.NodeId == "" { return &bhomeclient.Reply{Success: false, Msg: "NodeId不能为空"} } sv := service.NewClusterService(h.Bk) ip, err := sv.FindIpByNode(reqBody.NodeId) if err != nil { return &bhomeclient.Reply{Success: false, Msg: "ip查询失败"} } return &bhomeclient.Reply{Success: true, Msg: "IP查询成功", Data: ip} }