node.go 8.4 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4
package console_mysql

import (
	"fmt"
黄孟柱 已提交
5 6 7
	"github.com/eolinker/goku-api-gateway/common/database"
	. "github.com/eolinker/goku-api-gateway/common/version"
	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 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
)

// 新增节点信息
func AddNode(clusterId int, nodeName, nodeIP, nodePort, gatewayPath string, groupID int) (bool, map[string]interface{}, error) {
	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()
	res, err := stmt.Exec(clusterId, nodeName, groupID, nodeIP, nodePort, now, now, Version, gatewayPath)
	if err != nil {
		return false, map[string]interface{}{"error": "[ERROR]Failed to insert data!"}, err
	} else {
		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": Version}, nil
	}
}

// 修改节点信息
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
	} else {
		return true, "", nil
	}
}

// 删除节点信息
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
	} else {
		return true, "", nil
	}
}

// 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
		}
		if node.Version == Version {
			// 判断节点版本号是否是最新
			node.IsUpdate = true
		}
		nodeList = append(nodeList, &node)
	}
	return true, nodeList, nil
}

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` = ? ;"

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
}

// 获取节点信息
func GetNodeInfo(nodeID int) (bool, *entity.Node, error) {

	return getNodeInfo(nodeSqlId, nodeID)
}
func GetNodeByIpPort(ip string, port int) (bool, *entity.Node, error) {

	return getNodeInfo(nodeSqlIpPort, ip, port)
}

// 节点IP查重
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
	} else {
		return true
	}
}

// 获取节点IP列表
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)
	if _, err = rows.Columns(); err != nil {
		return false, make([]map[string]interface{}, 0), err
	} else {
		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
			}
			nodeList = append(nodeList, map[string]interface{}{
				"nodeID":   nodeID,
				"nodeIP":   nodeIP,
				"nodePort": nodePort,
			})
		}
		return true, nodeList, nil
	}
}

// 从待操作节点中获取关闭节点列表
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
}

// 批量修改节点分组
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
}

// 批量修改接口分组
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
}

// 更新节点集群ID
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()
}