123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- package config
- import (
- "bufio"
- "bytes"
- "fmt"
- "os"
- "strings"
- "xg_auto_deploy/internal/models"
- )
- func GetClusterConfig(path string) (*models.ClusterConfig, error) {
- clusterConfig := models.ClusterConfig{}
- firstPart, secondPart, err := splitFileByNID(path)
- if err != nil {
- fmt.Println("GetClusterConfig Error:", err)
- return &clusterConfig, err
- }
- SetClusterConfig(firstPart, secondPart, &clusterConfig)
- return &clusterConfig, nil
- }
- func splitFileByNID(filename string) (string, string, error) {
- file, err := os.Open(filename)
- if err != nil {
- return "", "", err
- }
- defer file.Close()
- scanner := bufio.NewScanner(file)
- var firstPart, secondPart string
- foundNID := false
- for scanner.Scan() {
- line := scanner.Text()
- if !foundNID {
- if strings.Contains(line, "NID") && (strings.Index(line, "NID") == 0 || line[strings.Index(line, "NID")-1] == ' ') {
- foundNID = true
- } else {
- firstPart += line + "\n"
- }
- }
- if foundNID {
- secondPart += line + "\n"
- }
- }
- if err := scanner.Err(); err != nil {
- return "", "", err
- }
- return firstPart, secondPart, nil
- }
- func SetClusterConfig(firstPart string, secondPart string, clusterConfig *models.ClusterConfig) {
- var clusterfirstPart models.ClusterfirstPart
-
- parts := strings.Split(firstPart, " ")
-
- for _, part := range parts {
-
- eqIndex := strings.Index(part, "=")
- if eqIndex == -1 {
- continue
- }
-
- key := part[:eqIndex]
- value := part[eqIndex+1:]
- setFieldValue(&clusterfirstPart, key, value)
- }
- clusterConfig.FirstPart = clusterfirstPart
-
- lines := strings.Split(secondPart, "\n")
-
- for _, line := range lines {
-
- line = strings.TrimSpace(strings.TrimSuffix(line, ";"))
-
- if line == "" {
- break
- }
-
- parts := strings.Split(line, " ")
- node := models.ClustersecondPart{}
-
- for _, part := range parts {
-
- eqIndex := strings.Index(part, "=")
- if eqIndex == -1 {
- continue
- }
-
- key := part[:eqIndex]
- value := part[eqIndex+1:]
- setFieldValue(&node, key, value)
- }
- clusterConfig.SecondParts = append(clusterConfig.SecondParts, node)
- }
- }
- func setFieldValue(target interface{}, key string, value string) {
- switch t := target.(type) {
- case *models.ClusterfirstPart:
- switch key {
- case "#MAX_NODES":
- t.MaxNodes = value
- case "MASTER_GRPS":
- t.MasterGrps = value
- case "PROTOCOL":
- t.Protocol = strings.Trim(value, "'")
- case "MSG_PORT_NUM":
- t.MsgPortNum = value
- case "MAX_SEND_WIN":
- t.MaxSendWin = value
- case "MSG_HAVE_CRC":
- t.MsgHaveCRC = value
- case "MERGE_SMALL_MSG":
- t.MergeSmallMsg = value
- case "MSG_SIZE":
- t.MsgSize = value
- case "TIMEOUT":
- t.Timeout = value
- case "RPC_WINDOW":
- t.RPCWindow = value
- case "EJE_WINDOW":
- t.EJEWindow = value
- case "MAX_SHAKE_TIME":
- t.MaxShakeTime = value
- case "MY_NID":
- t.MyNID = value
- case "CHECK_RACK":
- t.CheckRack = value
- }
- case *models.ClustersecondPart:
-
- switch key {
- case "NID":
- t.NID = value
- case "RACK":
- t.RACK = value
- case "PORTS":
- t.PORTS = value
- case "ROLE":
- t.ROLE = value
- case "LPU":
- t.LPU = value
- case "STORE_WEIGHT":
- t.StoreWeight = value
- case "STATE":
- t.STATE = value
- }
- }
- }
- func SaveClusterConfigBuffer(clusterConfig *models.ClusterConfig, MyNID string) *bytes.Buffer {
- str := fmt.Sprintf(`#MAX_NODES=%s MASTER_GRPS=%s PROTOCOL='%s' MSG_PORT_NUM=%s MAX_SEND_WIN=%s
- MSG_HAVE_CRC=%s MERGE_SMALL_MSG=%s MSG_SIZE=%s TIMEOUT=%s RPC_WINDOW=%s
- EJE_WINDOW=%s MAX_SHAKE_TIME=%s MY_NID=%s CHECK_RACK=%s
- `, clusterConfig.FirstPart.MaxNodes, clusterConfig.FirstPart.MasterGrps,
- clusterConfig.FirstPart.Protocol, clusterConfig.FirstPart.MsgPortNum, clusterConfig.FirstPart.MaxSendWin,
- clusterConfig.FirstPart.MsgHaveCRC, clusterConfig.FirstPart.MergeSmallMsg, clusterConfig.FirstPart.MsgSize,
- clusterConfig.FirstPart.Timeout, clusterConfig.FirstPart.RPCWindow, clusterConfig.FirstPart.EJEWindow,
- clusterConfig.FirstPart.MaxShakeTime, MyNID, clusterConfig.FirstPart.CheckRack)
- for _, v := range clusterConfig.SecondParts {
- str += fmt.Sprintf(`NID=%s RACK=%s PORTS='%s' ROLE='%s' LPU=%s STORE_WEIGHT=%s STATE=%s;
- `, v.NID, v.RACK, v.PORTS, v.ROLE, v.LPU, v.StoreWeight, v.STATE)
- }
- buffer := bytes.NewBufferString(str)
-
- return buffer
- }
|