node.go 8.5 KB
Newer Older
Y
Your Name 已提交
1
package consolemysql
E
eoLinker API Management 已提交
2 3 4

import (
	"fmt"
黄孟柱 已提交
5
	"github.com/eolinker/goku-api-gateway/common/database"
Y
Your Name 已提交
6
	v "github.com/eolinker/goku-api-gateway/common/version"
黄孟柱 已提交
7
	entity "github.com/eolinker/goku-api-gateway/server/entity/console-entity"
Y
Your Name 已提交
8 9 10
	"strconv"
	"strings"
	"time"
E
eoLinker API Management 已提交
11 12
)

Y
Your Name 已提交
13 14
// AddNode 新增节点信息
func AddNode(clusterID int, nodeName, nodeIP, nodePort, gatewayPath string, groupID int) (bool, map[string]interface{}, error) {
E
eoLinker API Management 已提交
15 16 17 18 19 20 21 22
	db := database.GetConnection()
	now := time.Now().Format("2006-01-02 15:04:05")
	sql := "INSERT INTO goku_node_info (`clusterID`,`nodeName`,`groupID`,`nodeIP`,`nodePort`,`updateTime`,`createTime`,`version`, `gatewayPath`,`nodeStatus`) VALUES (?,?,?,?,?,?,?,?,?,0);"
	stmt, err := db.Prepare(sql)
	if err != nil {
		return false, map[string]interface{}{"error": "[ERROR]Illegal SQL statement!"}, err
	}
	defer stmt.Close()
Y
Your Name 已提交
23
	res, err := stmt.Exec(clusterID, nodeName, groupID, nodeIP, nodePort, now, now, v.Version, gatewayPath)
E
eoLinker API Management 已提交
24 25 26
	if err != nil {
		return false, map[string]interface{}{"error": "[ERROR]Failed to insert data!"}, err
	}
Y
Your Name 已提交
27 28 29 30 31
	nodeID, err := res.LastInsertId()
	if err != nil {
		return false, map[string]interface{}{"error": "[ERROR]Failed to insert data!"}, err
	}
	return true, map[string]interface{}{"nodeID": nodeID, "version": v.Version}, nil
E
eoLinker API Management 已提交
32 33
}

Y
Your Name 已提交
34
// EditNode 修改节点信息
E
eoLinker API Management 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47
func EditNode(nodeName, nodeIP, nodePort, gatewayPath string, nodeID, groupID int) (bool, string, error) {
	db := database.GetConnection()
	now := time.Now().Format("2006-01-02 15:04:05")
	sql := "UPDATE goku_node_info SET  nodeName = ?,nodeIP = ?,nodePort = ?,updateTime = ?,groupID = ?,gatewayPath = ? WHERE nodeID = ?;"
	stmt, err := db.Prepare(sql)
	if err != nil {
		return false, "[ERROR]Illegal SQL statement!", err
	}
	defer stmt.Close()
	_, err = stmt.Exec(nodeName, nodeIP, nodePort, now, groupID, gatewayPath, nodeID)
	if err != nil {
		return false, "[ERROR]Failed to update data!", err
	}
Y
Your Name 已提交
48
	return true, "", nil
E
eoLinker API Management 已提交
49 50
}

Y
Your Name 已提交
51
// DeleteNode 删除节点信息
E
eoLinker API Management 已提交
52 53 54 55 56 57 58 59 60 61 62 63
func DeleteNode(nodeID int) (bool, string, error) {
	db := database.GetConnection()
	sql := "DELETE FROM goku_node_info WHERE nodeID = ?;"
	stmt, err := db.Prepare(sql)
	if err != nil {
		return false, "[ERROR]Illegal SQL statement!", err
	}
	defer stmt.Close()
	_, err = stmt.Exec(nodeID)
	if err != nil {
		return false, "[ERROR]Failed to delete data!", err
	}
Y
Your Name 已提交
64
	return true, "", nil
E
eoLinker API Management 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
}

// GetNodeList 获取节点列表
func GetNodeList(clusterID, groupID int, keyword string) (bool, []*entity.Node, error) {
	db := database.GetConnection()
	rule := make([]string, 0, 2)

	rule = append(rule, fmt.Sprintf("A.clusterID = %d", clusterID))
	if groupID > -1 {
		groupRule := fmt.Sprintf("A.groupID = %d", groupID)
		rule = append(rule, groupRule)
	}
	if keyword != "" {
		searchRule := "(A.nodeName LIKE '%" + keyword + "%' OR A.nodeIP LIKE '%" + keyword + "%' OR A.nodePort LIKE '%" + keyword + "%')"
		rule = append(rule, searchRule)
	}
	ruleStr := ""
	if len(rule) > 0 {
		ruleStr += "WHERE " + strings.Join(rule, " AND ")
	}
	sql := fmt.Sprintf("SELECT A.nodeID,A.nodeName,A.nodeIP,A.nodePort,A.updateTime,A.createTime,A.version,A.gatewayPath,A.groupID,IFNULL(G.groupName,'未分类') FROM goku_node_info A LEFT JOIN goku_node_group G ON A.groupID = G.groupID %s ORDER BY updateTime DESC;", ruleStr)
	rows, err := db.Query(sql)
	if err != nil {
		return false, nil, err
	}
	//延时关闭Rows
	defer rows.Close()
	//获取记录列
	nodeList := make([]*entity.Node, 0)
	for rows.Next() {
		node := entity.Node{}
		err = rows.Scan(&node.NodeID, &node.NodeName, &node.NodeIP, &node.NodePort, &node.UpdateTime, &node.CreateTime, &node.Version, &node.GatewayPath, &node.GroupID, &node.GroupName)
		if err != nil {
			panic(err)
			return false, nil, err
		}
Y
Your Name 已提交
101
		if node.Version == v.Version {
E
eoLinker API Management 已提交
102 103 104 105 106 107 108 109
			// 判断节点版本号是否是最新
			node.IsUpdate = true
		}
		nodeList = append(nodeList, &node)
	}
	return true, nodeList, nil
}

Y
Your Name 已提交
110 111
const nodeSQLIPPort = "SELECT  A.`nodeID`, A.`nodeName`, A.`nodeIP`, A.`nodePort`, A.`updateTime`, A.`createTime`, A.`version`,   A.`gatewayPath`, A.`groupID`, IFNULL(G.`groupName`,'') , C.`name` As cluster, C.`title` As cluster_title  FROM goku_node_info A LEFT JOIN goku_node_group G ON A.`groupID` = G.`groupID` LEFT JOIN `goku_cluster` C ON A.`clusterID` = C.`id`WHERE A.`nodeIP` = ? and A.`nodePort`=?;"
const nodeSQLID = "SELECT  A.`nodeID`, A.`nodeName`, A.`nodeIP`, A.`nodePort`, A.`updateTime`, A.`createTime`, A.`version`,   A.`gatewayPath`, A.`groupID`, IFNULL(G.`groupName`,''),  C.`name` As cluster, C.`title` As cluster_title  FROM goku_node_info A LEFT JOIN goku_node_group G ON A.`groupID` = G.`groupID` LEFT JOIN `goku_cluster` C ON A.`clusterID` = C.`id`WHERE A.`nodeID` = ? ;"
E
eoLinker API Management 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

func getNodeInfo(sql string, args ...interface{}) (bool, *entity.Node, error) {

	db := database.GetConnection()

	node := &entity.Node{}
	err := db.QueryRow(sql, args...).Scan(&node.NodeID,
		&node.NodeName,
		&node.NodeIP,
		&node.NodePort,
		&node.UpdateTime,
		&node.CreateTime,
		&node.Version,
		&node.GatewayPath,
		&node.GroupID,
		&node.GroupName,
		&node.Cluster,
		&node.ClusterTitle)
	if err != nil {
		return false, nil, err
	}
	return true, node, err
}

Y
Your Name 已提交
136
// GetNodeInfo 获取节点信息
E
eoLinker API Management 已提交
137 138
func GetNodeInfo(nodeID int) (bool, *entity.Node, error) {

Y
Your Name 已提交
139
	return getNodeInfo(nodeSQLID, nodeID)
E
eoLinker API Management 已提交
140 141
}

Y
Your Name 已提交
142 143 144 145
//GetNodeByIPPort 通过IP+端口获取节点信息
func GetNodeByIPPort(ip string, port int) (bool, *entity.Node, error) {

	return getNodeInfo(nodeSQLIPPort, ip, port)
E
eoLinker API Management 已提交
146 147
}

Y
Your Name 已提交
148
// CheckIsExistRemoteAddr 节点IP查重
E
eoLinker API Management 已提交
149 150 151 152 153 154 155 156 157 158 159
func CheckIsExistRemoteAddr(nodeID int, nodeIP, nodePort string) bool {
	db := database.GetConnection()
	sql := `SELECT nodeID FROM goku_node_info WHERE nodeIP = ? AND nodePort = ?;`
	var id int
	err := db.QueryRow(sql, nodeIP, nodePort).Scan(&id)
	if err != nil {
		return false
	}
	if id == nodeID {
		return false
	}
Y
Your Name 已提交
160
	return true
E
eoLinker API Management 已提交
161 162
}

Y
Your Name 已提交
163
// GetNodeIPList 获取节点IP列表
E
eoLinker API Management 已提交
164 165 166 167 168 169 170 171 172 173 174
func GetNodeIPList() (bool, []map[string]interface{}, error) {
	db := database.GetConnection()
	sql := `SELECT nodeID,nodeIP,nodePort FROM goku_node_info WHERE nodeStatus = 1;`
	rows, err := db.Query(sql)
	if err != nil {
		return false, make([]map[string]interface{}, 0), err
	}
	//延时关闭Rows
	defer rows.Close()
	//获取记录列
	nodeList := make([]map[string]interface{}, 0)
Y
Your Name 已提交
175 176 177 178 179 180 181

	for rows.Next() {
		var nodeID int
		var nodeIP, nodePort string
		err = rows.Scan(&nodeID, &nodeIP, &nodePort)
		if err != nil {
			return false, make([]map[string]interface{}, 0), err
E
eoLinker API Management 已提交
182
		}
Y
Your Name 已提交
183 184 185 186 187
		nodeList = append(nodeList, map[string]interface{}{
			"nodeID":   nodeID,
			"nodeIP":   nodeIP,
			"nodePort": nodePort,
		})
E
eoLinker API Management 已提交
188
	}
Y
Your Name 已提交
189
	return true, nodeList, nil
E
eoLinker API Management 已提交
190 191
}

Y
Your Name 已提交
192
// GetAvaliableNodeListFromNodeList 从待操作节点中获取关闭节点列表
E
eoLinker API Management 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
func GetAvaliableNodeListFromNodeList(nodeIDList string, nodeStatus int) (bool, string, error) {
	db := database.GetConnection()
	sql := "SELECT nodeID FROM goku_node_info WHERE nodeID IN (" + nodeIDList + ") AND nodeStatus = ?"
	rows, err := db.Query(sql, nodeStatus)
	if err != nil {
		return false, "[ERROR]Fail to excute SQL statement!", err
	}
	defer rows.Close()
	idList := make([]string, 0)
	for rows.Next() {
		var nodeID int
		err = rows.Scan(&nodeID)
		if err != nil {
			return false, err.Error(), err
		}
		idList = append(idList, strconv.Itoa(nodeID))
	}
	fmt.Println(sql, nodeStatus)
	return true, strings.Join(idList, ","), nil
}

Y
Your Name 已提交
214
// BatchEditNodeGroup 批量修改节点分组
E
eoLinker API Management 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228
func BatchEditNodeGroup(nodeIDList string, groupID int) (bool, string, error) {
	db := database.GetConnection()
	now := time.Now().Format("2006-01-02 15:04:05")
	Tx, _ := db.Begin()
	sql := "UPDATE goku_node_info SET groupID = ?,updateTime = ? WHERE nodeID IN (" + nodeIDList + ");"
	_, err := Tx.Exec(sql, groupID, now)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to excute SQL statement!", err
	}
	Tx.Commit()
	return true, "", nil
}

Y
Your Name 已提交
229
// BatchDeleteNode 批量修改接口分组
E
eoLinker API Management 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242
func BatchDeleteNode(nodeIDList string) (bool, string, error) {
	db := database.GetConnection()
	Tx, _ := db.Begin()
	sql := "DELETE FROM goku_node_info WHERE nodeID IN (" + nodeIDList + ");"
	_, err := Tx.Exec(sql)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to excute SQL statement!", err
	}
	Tx.Commit()
	return true, "", nil
}

Y
Your Name 已提交
243
// UpdateAllNodeClusterID 更新节点集群ID
E
eoLinker API Management 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
func UpdateAllNodeClusterID(clusterID int) {
	db := database.GetConnection()
	Tx, _ := db.Begin()
	sql := "UPDATE goku_node_info SET clusterID = ?;"
	_, err := Tx.Exec(sql, clusterID)
	if err != nil {
		Tx.Rollback()
	}
	sql = "UPDATE goku_node_group SET clusterID = ?;"
	_, err = Tx.Exec(sql, clusterID)
	if err != nil {
		Tx.Rollback()
	}
	Tx.Commit()
}