auto associate support multi cluster
This commit is contained in:
parent
f92b448333
commit
503b201eb2
|
@ -625,7 +625,7 @@ func (h *APIHandler) associateESNode(w http.ResponseWriter, req *http.Request, p
|
||||||
|
|
||||||
func (h *APIHandler) autoAssociateESNode(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
func (h *APIHandler) autoAssociateESNode(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||||
reqBody := struct {
|
reqBody := struct {
|
||||||
ClusterID string `json:"cluster_id"`
|
ClusterIDs []string `json:"cluster_ids"`
|
||||||
}{}
|
}{}
|
||||||
err := h.DecodeJSON(req, &reqBody)
|
err := h.DecodeJSON(req, &reqBody)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -633,14 +633,6 @@ func (h *APIHandler) autoAssociateESNode(w http.ResponseWriter, req *http.Reques
|
||||||
h.WriteError(w, err.Error(), http.StatusInternalServerError)
|
h.WriteError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// query cluster basicauth
|
|
||||||
cfg := elastic.GetConfig(reqBody.ClusterID)
|
|
||||||
basicAuth, err := common.GetBasicAuth(cfg)
|
|
||||||
if err != nil {
|
|
||||||
log.Error(err)
|
|
||||||
h.WriteError(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// query not associated nodes info
|
// query not associated nodes info
|
||||||
nodesM, err := getUnAssociateNodes()
|
nodesM, err := getUnAssociateNodes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -662,109 +654,119 @@ func (h *APIHandler) autoAssociateESNode(w http.ResponseWriter, req *http.Reques
|
||||||
h.WriteError(w, err.Error(), http.StatusInternalServerError)
|
h.WriteError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
taskSetting, err := getSettingsByClusterID(cfg.ID)
|
for _, clusterID := range reqBody.ClusterIDs {
|
||||||
if err != nil {
|
// query cluster basicauth
|
||||||
log.Error(err)
|
cfg := elastic.GetConfig(clusterID)
|
||||||
h.WriteError(w, err.Error(), http.StatusInternalServerError)
|
basicAuth, err := common.GetBasicAuth(cfg)
|
||||||
return
|
|
||||||
}
|
|
||||||
for agentID, nodes := range nodesM {
|
|
||||||
var (
|
|
||||||
inst *agent.Instance
|
|
||||||
ok bool
|
|
||||||
)
|
|
||||||
if inst, ok = agents[agentID]; !ok {
|
|
||||||
log.Warnf("agent [%v] was not found", agentID)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
settings, err := common2.GetAgentSettings(agentID, 0)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
continue
|
h.WriteError(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
for _, node := range nodes {
|
taskSetting, err := getSettingsByClusterID(cfg.ID)
|
||||||
host := node.PublishAddress
|
if err != nil {
|
||||||
var endpoint string
|
log.Error(err)
|
||||||
if strings.HasPrefix( host, "::"){
|
h.WriteError(w, err.Error(), http.StatusInternalServerError)
|
||||||
instURL, err := url.Parse(inst.Endpoint)
|
return
|
||||||
if err != nil {
|
}
|
||||||
log.Error(err)
|
for agentID, nodes := range nodesM {
|
||||||
continue
|
var (
|
||||||
}
|
inst *agent.Instance
|
||||||
host = instURL.Hostname()
|
ok bool
|
||||||
endpoint = fmt.Sprintf("%s://%s:%s", node.Schema, host, node.HttpPort)
|
)
|
||||||
}else{
|
if inst, ok = agents[agentID]; !ok {
|
||||||
endpoint = fmt.Sprintf("%s://%s", node.Schema, host)
|
log.Warnf("agent [%v] was not found", agentID)
|
||||||
}
|
|
||||||
escfg := elastic.ElasticsearchConfig{
|
|
||||||
Endpoint: endpoint,
|
|
||||||
BasicAuth: &basicAuth,
|
|
||||||
}
|
|
||||||
nodeInfo, err := client.GetClient().AuthESNode(context.Background(), inst.GetEndpoint(), escfg)
|
|
||||||
if err != nil {
|
|
||||||
log.Warn(err)
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
//matched
|
settings, err := common2.GetAgentSettings(agentID, 0)
|
||||||
if nodeInfo.ClusterUuid == cfg.ClusterUUID {
|
if err != nil {
|
||||||
//update node info
|
log.Error(err)
|
||||||
nodeInfo.ID = node.ID
|
continue
|
||||||
nodeInfo.AgentID = inst.ID
|
}
|
||||||
nodeInfo.ClusterID = cfg.ID
|
for _, node := range nodes {
|
||||||
err = orm.Save(nil, nodeInfo)
|
host := node.PublishAddress
|
||||||
|
var endpoint string
|
||||||
|
if strings.HasPrefix(host, "::") {
|
||||||
|
instURL, err := url.Parse(inst.Endpoint)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
host = instURL.Hostname()
|
||||||
|
endpoint = fmt.Sprintf("%s://%s:%s", node.Schema, host, node.HttpPort)
|
||||||
|
} else {
|
||||||
|
endpoint = fmt.Sprintf("%s://%s", node.Schema, host)
|
||||||
|
}
|
||||||
|
escfg := elastic.ElasticsearchConfig{
|
||||||
|
Endpoint: endpoint,
|
||||||
|
BasicAuth: &basicAuth,
|
||||||
|
}
|
||||||
|
nodeInfo, err := client.GetClient().AuthESNode(context.Background(), inst.GetEndpoint(), escfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Warn(err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
setting := pickAgentSettings(settings, node)
|
//matched
|
||||||
if setting == nil {
|
if nodeInfo.ClusterUuid == cfg.ClusterUUID {
|
||||||
tsetting := model.TaskSetting{
|
//update node info
|
||||||
NodeStats: &model.NodeStatsTask{
|
nodeInfo.ID = node.ID
|
||||||
Enabled: true,
|
nodeInfo.AgentID = inst.ID
|
||||||
},
|
nodeInfo.ClusterID = cfg.ID
|
||||||
Logs: &model.LogsTask{
|
err = orm.Save(nil, nodeInfo)
|
||||||
Enabled: true,
|
|
||||||
LogsPath: nodeInfo.Path.Logs,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
if taskSetting.IndexStats != nil {
|
|
||||||
tsetting.IndexStats = taskSetting.IndexStats
|
|
||||||
taskSetting.IndexStats = nil
|
|
||||||
}
|
|
||||||
if taskSetting.ClusterHealth != nil {
|
|
||||||
tsetting.ClusterHealth = taskSetting.ClusterHealth
|
|
||||||
taskSetting.ClusterHealth = nil
|
|
||||||
}
|
|
||||||
if taskSetting.ClusterStats != nil {
|
|
||||||
tsetting.ClusterStats = taskSetting.ClusterStats
|
|
||||||
taskSetting.ClusterStats = nil
|
|
||||||
}
|
|
||||||
setting = &agent.Setting{
|
|
||||||
Metadata: agent.SettingsMetadata{
|
|
||||||
Category: "agent",
|
|
||||||
Name: "task",
|
|
||||||
Labels: util.MapStr{
|
|
||||||
"agent_id": agentID,
|
|
||||||
"cluster_uuid": nodeInfo.ClusterUuid,
|
|
||||||
"cluster_id": nodeInfo.ClusterID,
|
|
||||||
"node_uuid": nodeInfo.NodeUUID,
|
|
||||||
"endpoint": fmt.Sprintf("%s://%s", nodeInfo.Schema, nodeInfo.PublishAddress),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Payload: util.MapStr{
|
|
||||||
"task": tsetting,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
err = orm.Create(nil, setting)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("save agent task setting error: ", err)
|
log.Error(err)
|
||||||
h.WriteError(w, err.Error(), http.StatusInternalServerError)
|
continue
|
||||||
return
|
}
|
||||||
|
setting := pickAgentSettings(settings, node)
|
||||||
|
if setting == nil {
|
||||||
|
tsetting := model.TaskSetting{
|
||||||
|
NodeStats: &model.NodeStatsTask{
|
||||||
|
Enabled: true,
|
||||||
|
},
|
||||||
|
Logs: &model.LogsTask{
|
||||||
|
Enabled: true,
|
||||||
|
LogsPath: nodeInfo.Path.Logs,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if taskSetting.IndexStats != nil {
|
||||||
|
tsetting.IndexStats = taskSetting.IndexStats
|
||||||
|
taskSetting.IndexStats = nil
|
||||||
|
}
|
||||||
|
if taskSetting.ClusterHealth != nil {
|
||||||
|
tsetting.ClusterHealth = taskSetting.ClusterHealth
|
||||||
|
taskSetting.ClusterHealth = nil
|
||||||
|
}
|
||||||
|
if taskSetting.ClusterStats != nil {
|
||||||
|
tsetting.ClusterStats = taskSetting.ClusterStats
|
||||||
|
taskSetting.ClusterStats = nil
|
||||||
|
}
|
||||||
|
setting = &agent.Setting{
|
||||||
|
Metadata: agent.SettingsMetadata{
|
||||||
|
Category: "agent",
|
||||||
|
Name: "task",
|
||||||
|
Labels: util.MapStr{
|
||||||
|
"agent_id": agentID,
|
||||||
|
"cluster_uuid": nodeInfo.ClusterUuid,
|
||||||
|
"cluster_id": nodeInfo.ClusterID,
|
||||||
|
"node_uuid": nodeInfo.NodeUUID,
|
||||||
|
"endpoint": fmt.Sprintf("%s://%s", nodeInfo.Schema, nodeInfo.PublishAddress),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Payload: util.MapStr{
|
||||||
|
"task": tsetting,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
err = orm.Create(nil, setting)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("save agent task setting error: ", err)
|
||||||
|
h.WriteError(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
h.WriteAckOKJSON(w)
|
h.WriteAckOKJSON(w)
|
||||||
}
|
}
|
||||||
|
@ -985,7 +987,7 @@ func getNodesInfoFromES(agentID string) (map[int]*agent.ESNodeInfo, error) {
|
||||||
|
|
||||||
func getUnAssociateNodes() (map[string][]agent.ESNodeInfo, error){
|
func getUnAssociateNodes() (map[string][]agent.ESNodeInfo, error){
|
||||||
query := util.MapStr{
|
query := util.MapStr{
|
||||||
"size": 1200,
|
"size": 3000,
|
||||||
"query": util.MapStr{
|
"query": util.MapStr{
|
||||||
"bool": util.MapStr{
|
"bool": util.MapStr{
|
||||||
"must_not": []util.MapStr{
|
"must_not": []util.MapStr{
|
||||||
|
|
Loading…
Reference in New Issue