From bf1f33e68363e4315ebad5d483a9c23b4a84e1cf Mon Sep 17 00:00:00 2001 From: kompotkot Date: Mon, 11 Jul 2022 13:53:03 +0000 Subject: [PATCH 1/9] Updated default config generation --- nodes/node_balancer/cmd/cli.go | 36 +++++++---- nodes/node_balancer/configs/settings.go | 79 +++++++++++++++++++------ 2 files changed, 85 insertions(+), 30 deletions(-) diff --git a/nodes/node_balancer/cmd/cli.go b/nodes/node_balancer/cmd/cli.go index 878f28e8..bd9fbe80 100644 --- a/nodes/node_balancer/cmd/cli.go +++ b/nodes/node_balancer/cmd/cli.go @@ -33,11 +33,12 @@ func (i *flagSlice) Set(value string) error { // Command Line Interface state type StateCLI struct { - addAccessCmd *flag.FlagSet - deleteAccessCmd *flag.FlagSet - serverCmd *flag.FlagSet - usersCmd *flag.FlagSet - versionCmd *flag.FlagSet + addAccessCmd *flag.FlagSet + generateConfigCmd *flag.FlagSet + deleteAccessCmd *flag.FlagSet + serverCmd *flag.FlagSet + usersCmd *flag.FlagSet + versionCmd *flag.FlagSet // Common flags configPathFlag string @@ -63,15 +64,15 @@ type StateCLI struct { } func (s *StateCLI) usage() { - fmt.Printf(`usage: nodebalancer [-h] {%[1]s,%[2]s,%[3]s,%[4]s,%[5]s} ... + fmt.Printf(`usage: nodebalancer [-h] {%[1]s,%[2]s,%[3]s,%[4]s,%[5]s,%[6]s} ... Moonstream node balancer CLI optional arguments: -h, --help show this help message and exit subcommands: - {%[1]s,%[2]s,%[3]s,%[4]s,%[5]s} -`, s.addAccessCmd.Name(), s.deleteAccessCmd.Name(), s.serverCmd.Name(), s.usersCmd.Name(), s.versionCmd.Name()) + {%[1]s,%[2]s,%[3]s,%[4]s,%[5]s,%[6]s} +`, s.addAccessCmd.Name(), s.generateConfigCmd.Name(), s.deleteAccessCmd.Name(), s.serverCmd.Name(), s.usersCmd.Name(), s.versionCmd.Name()) } // Check if required flags are set @@ -82,6 +83,10 @@ func (s *StateCLI) checkRequirements() { fmt.Printf("Add new user access token\n\n") s.addAccessCmd.PrintDefaults() os.Exit(0) + case s.generateConfigCmd.Parsed(): + fmt.Printf("Generate new configuration\n\n") + s.generateConfigCmd.PrintDefaults() + os.Exit(0) case s.deleteAccessCmd.Parsed(): fmt.Printf("Delete user access token\n\n") s.deleteAccessCmd.PrintDefaults() @@ -133,22 +138,25 @@ func (s *StateCLI) checkRequirements() { } } - if s.configPathFlag == "" { - configPath := configs.GenerateDefaultConfig() - s.configPathFlag = configPath + config := configs.GetConfigPath(s.configPathFlag) + fmt.Println(config) + if !configs.CheckPathExists(config.ConfigPath) { + configs.GenerateDefaultConfig(config) } + s.configPathFlag = config.ConfigPath } func (s *StateCLI) populateCLI() { // Subcommands setup s.addAccessCmd = flag.NewFlagSet("add-access", flag.ExitOnError) + s.generateConfigCmd = flag.NewFlagSet("generate-config", flag.ExitOnError) s.deleteAccessCmd = flag.NewFlagSet("delete-access", flag.ExitOnError) s.serverCmd = flag.NewFlagSet("server", flag.ExitOnError) s.usersCmd = flag.NewFlagSet("users", flag.ExitOnError) s.versionCmd = flag.NewFlagSet("version", flag.ExitOnError) // Common flag pointers - for _, fs := range []*flag.FlagSet{s.addAccessCmd, s.deleteAccessCmd, s.serverCmd, s.usersCmd, s.versionCmd} { + for _, fs := range []*flag.FlagSet{s.addAccessCmd, s.generateConfigCmd, s.deleteAccessCmd, s.serverCmd, s.usersCmd, s.versionCmd} { fs.BoolVar(&s.helpFlag, "help", false, "Show help message") fs.StringVar(&s.configPathFlag, "config", "", "Path to configuration file (default: ~/.nodebalancer/config.txt)") } @@ -228,6 +236,10 @@ func CLI() { } fmt.Println(string(resource_data)) + case "generate-config": + stateCLI.generateConfigCmd.Parse(os.Args[2:]) + stateCLI.checkRequirements() + case "delete-access": stateCLI.deleteAccessCmd.Parse(os.Args[2:]) stateCLI.checkRequirements() diff --git a/nodes/node_balancer/configs/settings.go b/nodes/node_balancer/configs/settings.go index 6bb13b40..03b39d72 100644 --- a/nodes/node_balancer/configs/settings.go +++ b/nodes/node_balancer/configs/settings.go @@ -7,6 +7,8 @@ import ( "fmt" "log" "os" + "path/filepath" + "strings" "time" ) @@ -57,32 +59,73 @@ func CheckEnvVarSet() { } } -func GenerateDefaultConfig() string { - homeDir, err := os.UserHomeDir() +type Config struct { + ConfigDirPath string + ConfigDirExists bool + + ConfigPath string + ConfigExists bool +} + +func CheckPathExists(path string) bool { + var exists = true + _, err := os.Stat(path) if err != nil { - fmt.Printf("Unable to find user home directory, %v", err) - os.Exit(1) + if os.IsNotExist(err) { + exists = false + } else { + fmt.Println(err) + os.Exit(1) + } } - configDirPath := fmt.Sprintf("%s/.nodebalancer", homeDir) - configPath := fmt.Sprintf("%s/config.txt", configDirPath) + return exists +} - err = os.MkdirAll(configDirPath, os.ModePerm) - if err != nil { - fmt.Printf("Unable to create directory, %v", err) - os.Exit(1) - } - - _, err = os.Stat(configPath) - if err != nil { - tempConfigB := []byte("ethereum,127.0.0.1,8545") - err = os.WriteFile(configPath, tempConfigB, 0644) +func GetConfigPath(providedPath string) *Config { + var configDirPath, configPath string + if providedPath == "" { + homeDir, err := os.UserHomeDir() if err != nil { + fmt.Printf("Unable to find user home directory, %v", err) + os.Exit(1) + } + configDirPath = fmt.Sprintf("%s/.nodebalancer", homeDir) + configPath = fmt.Sprintf("%s/config.txt", configDirPath) + } else { + configPath = strings.TrimSuffix(providedPath, "/") + configDirPath = filepath.Dir(configPath) + } + + defaultConfig := &Config{ + ConfigDirPath: configDirPath, + ConfigDirExists: CheckPathExists(configDirPath), + + ConfigPath: configPath, + ConfigExists: CheckPathExists(configPath), + } + + return defaultConfig +} + +func GenerateDefaultConfig(config *Config) string { + if !config.ConfigDirExists { + if err := os.MkdirAll(config.ConfigDirPath, os.ModePerm); err != nil { fmt.Printf("Unable to create directory, %v", err) os.Exit(1) } - log.Printf("Config directory were not found, created default configuration at %s", configPath) + log.Printf("Config directory created at: %s", config.ConfigDirPath) } - return configPath + if !config.ConfigExists { + tempConfigB := []byte("ethereum,127.0.0.1,8545") + err := os.WriteFile(config.ConfigPath, tempConfigB, 0644) + if err != nil { + fmt.Printf("Unable to create temp config file, %v", err) + os.Exit(1) + } + log.Printf("Created default configuration at %s", config.ConfigPath) + } + + return config.ConfigPath } From cc0b25bf03f2b349ff062d388341e3d9b80d94d1 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Mon, 11 Jul 2022 14:01:10 +0000 Subject: [PATCH 2/9] Updated structure --- nodes/.gitignore | 1 - nodes/deploy/deploy.bash | 2 +- nodes/node_balancer/cmd/{ => nodebalancer}/balancer.go | 2 +- nodes/node_balancer/cmd/{ => nodebalancer}/blockchain.go | 2 +- nodes/node_balancer/cmd/{ => nodebalancer}/cli.go | 3 +-- nodes/node_balancer/cmd/{ => nodebalancer}/clients.go | 4 ++-- .../node_balancer/cmd/{ => nodebalancer}/clients_test.go | 2 +- nodes/node_balancer/cmd/{ => nodebalancer}/db.go | 2 +- nodes/node_balancer/cmd/nodebalancer/main.go | 5 +++++ nodes/node_balancer/cmd/{ => nodebalancer}/middleware.go | 2 +- nodes/node_balancer/cmd/{ => nodebalancer}/routes.go | 2 +- nodes/node_balancer/cmd/{ => nodebalancer}/server.go | 2 +- nodes/node_balancer/dev.sh | 2 +- nodes/node_balancer/main.go | 9 --------- 14 files changed, 17 insertions(+), 23 deletions(-) rename nodes/node_balancer/cmd/{ => nodebalancer}/balancer.go (99%) rename nodes/node_balancer/cmd/{ => nodebalancer}/blockchain.go (99%) rename nodes/node_balancer/cmd/{ => nodebalancer}/cli.go (99%) rename nodes/node_balancer/cmd/{ => nodebalancer}/clients.go (99%) rename nodes/node_balancer/cmd/{ => nodebalancer}/clients_test.go (99%) rename nodes/node_balancer/cmd/{ => nodebalancer}/db.go (99%) create mode 100644 nodes/node_balancer/cmd/nodebalancer/main.go rename nodes/node_balancer/cmd/{ => nodebalancer}/middleware.go (99%) rename nodes/node_balancer/cmd/{ => nodebalancer}/routes.go (99%) rename nodes/node_balancer/cmd/{ => nodebalancer}/server.go (99%) delete mode 100644 nodes/node_balancer/main.go diff --git a/nodes/.gitignore b/nodes/.gitignore index 70a1c8af..342fca88 100644 --- a/nodes/.gitignore +++ b/nodes/.gitignore @@ -61,5 +61,4 @@ go.work dev.env prod.env test.env -nodebalancer diff --git a/nodes/deploy/deploy.bash b/nodes/deploy/deploy.bash index 8f21a619..a81524e4 100755 --- a/nodes/deploy/deploy.bash +++ b/nodes/deploy/deploy.bash @@ -58,7 +58,7 @@ echo echo -e "${PREFIX_INFO} Building executable load balancer for nodes script with Go" EXEC_DIR=$(pwd) cd "${APP_NODES_DIR}/node_balancer" -HOME=/root /usr/local/go/bin/go build -o "${APP_NODES_DIR}/node_balancer/nodebalancer" "${APP_NODES_DIR}/node_balancer/main.go" +HOME=/root /usr/local/go/bin/go build -o "${APP_NODES_DIR}/node_balancer/nodebalancer" "${APP_NODES_DIR}/node_balancer/cmd/nodebalancer/*.go" cd "${EXEC_DIR}" echo diff --git a/nodes/node_balancer/cmd/balancer.go b/nodes/node_balancer/cmd/nodebalancer/balancer.go similarity index 99% rename from nodes/node_balancer/cmd/balancer.go rename to nodes/node_balancer/cmd/nodebalancer/balancer.go index f384d7d2..17b6daf9 100644 --- a/nodes/node_balancer/cmd/balancer.go +++ b/nodes/node_balancer/cmd/nodebalancer/balancer.go @@ -1,7 +1,7 @@ /* Load balancer, based on https://github.com/kasvith/simplelb/ */ -package cmd +package main import ( "encoding/json" diff --git a/nodes/node_balancer/cmd/blockchain.go b/nodes/node_balancer/cmd/nodebalancer/blockchain.go similarity index 99% rename from nodes/node_balancer/cmd/blockchain.go rename to nodes/node_balancer/cmd/nodebalancer/blockchain.go index f64f3e7b..4711ca28 100644 --- a/nodes/node_balancer/cmd/blockchain.go +++ b/nodes/node_balancer/cmd/nodebalancer/blockchain.go @@ -1,4 +1,4 @@ -package cmd +package main import ( "io/ioutil" diff --git a/nodes/node_balancer/cmd/cli.go b/nodes/node_balancer/cmd/nodebalancer/cli.go similarity index 99% rename from nodes/node_balancer/cmd/cli.go rename to nodes/node_balancer/cmd/nodebalancer/cli.go index bd9fbe80..2a5bfe4b 100644 --- a/nodes/node_balancer/cmd/cli.go +++ b/nodes/node_balancer/cmd/nodebalancer/cli.go @@ -1,4 +1,4 @@ -package cmd +package main import ( "encoding/json" @@ -139,7 +139,6 @@ func (s *StateCLI) checkRequirements() { } config := configs.GetConfigPath(s.configPathFlag) - fmt.Println(config) if !configs.CheckPathExists(config.ConfigPath) { configs.GenerateDefaultConfig(config) } diff --git a/nodes/node_balancer/cmd/clients.go b/nodes/node_balancer/cmd/nodebalancer/clients.go similarity index 99% rename from nodes/node_balancer/cmd/clients.go rename to nodes/node_balancer/cmd/nodebalancer/clients.go index 5195cdc7..94ea3331 100644 --- a/nodes/node_balancer/cmd/clients.go +++ b/nodes/node_balancer/cmd/nodebalancer/clients.go @@ -1,4 +1,4 @@ -package cmd +package main import ( "errors" @@ -23,7 +23,7 @@ type ClientResourceData struct { Description string `json:"description"` BlockchainAccess bool `json:"blockchain_access"` ExtendedMethods bool `json:"extended_methods"` - + LastAccessTs int64 `json:"last_access_ts"` dataSource string diff --git a/nodes/node_balancer/cmd/clients_test.go b/nodes/node_balancer/cmd/nodebalancer/clients_test.go similarity index 99% rename from nodes/node_balancer/cmd/clients_test.go rename to nodes/node_balancer/cmd/nodebalancer/clients_test.go index bcdb0947..41a4d52d 100644 --- a/nodes/node_balancer/cmd/clients_test.go +++ b/nodes/node_balancer/cmd/nodebalancer/clients_test.go @@ -1,4 +1,4 @@ -package cmd +package main import ( "reflect" diff --git a/nodes/node_balancer/cmd/db.go b/nodes/node_balancer/cmd/nodebalancer/db.go similarity index 99% rename from nodes/node_balancer/cmd/db.go rename to nodes/node_balancer/cmd/nodebalancer/db.go index e31b9d24..4ce99884 100644 --- a/nodes/node_balancer/cmd/db.go +++ b/nodes/node_balancer/cmd/nodebalancer/db.go @@ -1,4 +1,4 @@ -package cmd +package main import ( "database/sql" diff --git a/nodes/node_balancer/cmd/nodebalancer/main.go b/nodes/node_balancer/cmd/nodebalancer/main.go new file mode 100644 index 00000000..f39b62eb --- /dev/null +++ b/nodes/node_balancer/cmd/nodebalancer/main.go @@ -0,0 +1,5 @@ +package main + +func main() { + CLI() +} diff --git a/nodes/node_balancer/cmd/middleware.go b/nodes/node_balancer/cmd/nodebalancer/middleware.go similarity index 99% rename from nodes/node_balancer/cmd/middleware.go rename to nodes/node_balancer/cmd/nodebalancer/middleware.go index 5cffc9c1..5e33bade 100644 --- a/nodes/node_balancer/cmd/middleware.go +++ b/nodes/node_balancer/cmd/nodebalancer/middleware.go @@ -1,7 +1,7 @@ /* Server API middleware. */ -package cmd +package main import ( "bytes" diff --git a/nodes/node_balancer/cmd/routes.go b/nodes/node_balancer/cmd/nodebalancer/routes.go similarity index 99% rename from nodes/node_balancer/cmd/routes.go rename to nodes/node_balancer/cmd/nodebalancer/routes.go index 36559915..c1deb560 100644 --- a/nodes/node_balancer/cmd/routes.go +++ b/nodes/node_balancer/cmd/nodebalancer/routes.go @@ -1,7 +1,7 @@ /* Handle routes for load balancer API. */ -package cmd +package main import ( "bytes" diff --git a/nodes/node_balancer/cmd/server.go b/nodes/node_balancer/cmd/nodebalancer/server.go similarity index 99% rename from nodes/node_balancer/cmd/server.go rename to nodes/node_balancer/cmd/nodebalancer/server.go index d265ce6b..9869d72a 100644 --- a/nodes/node_balancer/cmd/server.go +++ b/nodes/node_balancer/cmd/nodebalancer/server.go @@ -1,7 +1,7 @@ /* Node load balancer API server initialization. */ -package cmd +package main import ( "context" diff --git a/nodes/node_balancer/dev.sh b/nodes/node_balancer/dev.sh index c8719f22..25a1a896 100755 --- a/nodes/node_balancer/dev.sh +++ b/nodes/node_balancer/dev.sh @@ -5,6 +5,6 @@ set -e PROGRAM_NAME="nodebalancer" -go build -o "$PROGRAM_NAME" . +go build -o "$PROGRAM_NAME" cmd/nodebalancer/*.go ./"$PROGRAM_NAME" "$@" diff --git a/nodes/node_balancer/main.go b/nodes/node_balancer/main.go deleted file mode 100644 index 91a1764c..00000000 --- a/nodes/node_balancer/main.go +++ /dev/null @@ -1,9 +0,0 @@ -package main - -import ( - "github.com/bugout-dev/moonstream/nodes/node_balancer/cmd" -) - -func main() { - cmd.CLI() -} From 1b2f60d97c04a49dd1b4df9816508c745506c86e Mon Sep 17 00:00:00 2001 From: kompotkot Date: Tue, 12 Jul 2022 18:48:44 +0000 Subject: [PATCH 3/9] Removed status url, config is json, instead of status call latest --- .../cmd/nodebalancer/balancer.go | 76 ++++++++++------ .../cmd/nodebalancer/blockchain.go | 52 ----------- nodes/node_balancer/cmd/nodebalancer/cli.go | 68 ++++++++------ .../node_balancer/cmd/nodebalancer/clients.go | 6 +- .../cmd/nodebalancer/clients_test.go | 10 +-- .../nodebalancer/configs.go} | 90 ++++++++++++------- nodes/node_balancer/cmd/nodebalancer/db.go | 8 +- nodes/node_balancer/cmd/nodebalancer/main.go | 2 +- .../cmd/nodebalancer/middleware.go | 18 ++-- .../node_balancer/cmd/nodebalancer/routes.go | 8 +- .../node_balancer/cmd/nodebalancer/server.go | 81 ++++++++--------- .../node_balancer/cmd/nodebalancer/version.go | 3 + nodes/node_balancer/configs/version.go | 3 - nodes/node_balancer/sample.env | 1 - 14 files changed, 210 insertions(+), 216 deletions(-) rename nodes/node_balancer/{configs/settings.go => cmd/nodebalancer/configs.go} (59%) create mode 100644 nodes/node_balancer/cmd/nodebalancer/version.go delete mode 100644 nodes/node_balancer/configs/version.go diff --git a/nodes/node_balancer/cmd/nodebalancer/balancer.go b/nodes/node_balancer/cmd/nodebalancer/balancer.go index 17b6daf9..e1d1a30c 100644 --- a/nodes/node_balancer/cmd/nodebalancer/balancer.go +++ b/nodes/node_balancer/cmd/nodebalancer/balancer.go @@ -4,17 +4,17 @@ Load balancer, based on https://github.com/kasvith/simplelb/ package main import ( + "bytes" "encoding/json" - "fmt" "io/ioutil" "log" "net/http" "net/http/httputil" "net/url" + "strconv" + "strings" "sync" "sync/atomic" - - configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" ) // Main variable of pool of blockchains which contains pool of nodes @@ -23,10 +23,9 @@ var blockchainPool BlockchainPool // Node structure with // StatusURL for status server at node endpoint -// GethURL for geth/bor/etc node http.server endpoint +// Endpoint for geth/bor/etc node http.server endpoint type Node struct { - StatusURL *url.URL - GethURL *url.URL + Endpoint *url.URL Alive bool CurrentBlock uint64 @@ -49,8 +48,13 @@ type BlockchainPool struct { Blockchains []*NodePool } +// Node status response struct for HealthCheck +type NodeStatusResultResponse struct { + Number string `json:"number"` +} + type NodeStatusResponse struct { - CurrentBlock uint64 `json:"current_block"` + Result NodeStatusResultResponse `json:"result"` } // AddNode to the nodes pool @@ -153,11 +157,11 @@ func (bpool *BlockchainPool) GetNextNode(blockchain string) *Node { return nil } -// SetNodeStatus changes a status of a node by StatusURL or GethURL +// SetNodeStatus modify status of the node func (bpool *BlockchainPool) SetNodeStatus(url *url.URL, alive bool) { for _, b := range bpool.Blockchains { for _, n := range b.Nodes { - if n.StatusURL.String() == url.String() || n.GethURL.String() == url.String() { + if n.Endpoint.String() == url.String() { n.SetAlive(alive) break } @@ -165,55 +169,77 @@ func (bpool *BlockchainPool) SetNodeStatus(url *url.URL, alive bool) { } } -// StatusLog logs nodes statuses +// StatusLog logs node status // TODO(kompotkot): Print list of alive and dead nodes func (bpool *BlockchainPool) StatusLog() { for _, b := range bpool.Blockchains { for _, n := range b.Nodes { log.Printf( "Blockchain %s node %s is alive %t. Blockchain called %d times", - b.Blockchain, n.StatusURL, n.Alive, b.Current, + b.Blockchain, n.Endpoint.Host, n.Alive, b.Current, ) } } } -// HealthCheck fetch the node status and current block server +// HealthCheck fetch the node latest block func (bpool *BlockchainPool) HealthCheck() { for _, b := range bpool.Blockchains { for _, n := range b.Nodes { - n.SetAlive(false) - n.SetCurrentBlock(0) - - // Get response from node /ping endpoint - httpClient := http.Client{Timeout: configs.NB_HEALTH_CHECK_CALL_TIMEOUT} - resp, err := httpClient.Get(fmt.Sprintf("%s/status", n.StatusURL)) + httpClient := http.Client{Timeout: NB_HEALTH_CHECK_CALL_TIMEOUT} + resp, err := httpClient.Post( + n.Endpoint.String(), + "application/json", + bytes.NewBuffer([]byte(`{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", false],"id":1}`)), + ) if err != nil { - log.Printf("Unable to reach node: %s\n", n.StatusURL) + n.SetAlive(false) + n.SetCurrentBlock(0) + log.Printf("Unable to reach node: %s", n.Endpoint.Host) continue } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { - log.Printf("Unable to parse response from node: %s\n", n.StatusURL) + n.SetAlive(false) + n.SetCurrentBlock(0) + log.Printf("Unable to parse response from %s node, err %v", n.Endpoint.Host, err) continue } var statusResponse NodeStatusResponse err = json.Unmarshal(body, &statusResponse) if err != nil { - log.Printf("Unable to read json response from node: %s\n", n.StatusURL) + n.SetAlive(false) + n.SetCurrentBlock(0) + log.Printf("Unable to read json response from %s node, err: %v", n.Endpoint.Host, err) + continue + } + + blockNumberHex := strings.Replace(statusResponse.Result.Number, "0x", "", -1) + blockNumber, err := strconv.ParseUint(blockNumberHex, 16, 64) + if err != nil { + n.SetAlive(false) + n.SetCurrentBlock(0) + log.Printf("Unable to parse block number from hex to string, err: %v", err) continue } // Mark node in list of nodes as alive or not and update current block - n.SetAlive(true) - if statusResponse.CurrentBlock != 0 { - n.SetCurrentBlock(statusResponse.CurrentBlock) + var alive bool + if blockNumber != 0 { + alive = true + } else { + alive = false } + n.SetAlive(alive) + n.SetCurrentBlock(blockNumber) - log.Printf("Node %s is alive: %t with current block: %d blockchain called: %d times\n", n.StatusURL, true, statusResponse.CurrentBlock, b.Current) + log.Printf( + "Node %s is alive: %t with current block: %d blockchain called: %d times", + n.Endpoint.Host, alive, blockNumber, b.Current, + ) } } } diff --git a/nodes/node_balancer/cmd/nodebalancer/blockchain.go b/nodes/node_balancer/cmd/nodebalancer/blockchain.go index 4711ca28..40ef984c 100644 --- a/nodes/node_balancer/cmd/nodebalancer/blockchain.go +++ b/nodes/node_balancer/cmd/nodebalancer/blockchain.go @@ -1,17 +1,6 @@ package main -import ( - "io/ioutil" - "log" - "strconv" - "strings" - - configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" -) - var ( - nodeConfigs NodeConfigs - ALLOWED_METHODS = map[string]bool{ "eth_blockNumber": true, "eth_call": true, @@ -57,49 +46,8 @@ type JSONRPCRequest struct { ID uint64 `json:"id"` } -// Node conf type BlockchainConfig struct { Blockchain string IPs []string Port string } - -type NodeConfig struct { - Blockchain string - Addr string - Port uint16 -} - -type NodeConfigs struct { - NodeConfigs []NodeConfig -} - -// Return list of NodeConfig structures -func (nc *NodeConfigs) InitNodeConfigList(configPath string) { - configs.CheckEnvVarSet() - - rawBytes, err := ioutil.ReadFile(configPath) - if err != nil { - log.Fatalf("Unable to read config file, %v", err) - } - text := string(rawBytes) - lines := strings.Split(text, "\n") - - // Define available blockchain nodes - for _, line := range lines { - fields := strings.Split(line, ",") - if len(fields) == 3 { - port, err := strconv.ParseInt(fields[2], 0, 16) - if err != nil { - log.Printf("Unable to parse port number, %v", err) - continue - } - - nc.NodeConfigs = append(nc.NodeConfigs, NodeConfig{ - Blockchain: fields[0], - Addr: fields[1], - Port: uint16(port), - }) - } - } -} diff --git a/nodes/node_balancer/cmd/nodebalancer/cli.go b/nodes/node_balancer/cmd/nodebalancer/cli.go index 2a5bfe4b..23ccf9fc 100644 --- a/nodes/node_balancer/cmd/nodebalancer/cli.go +++ b/nodes/node_balancer/cmd/nodebalancer/cli.go @@ -4,13 +4,12 @@ import ( "encoding/json" "flag" "fmt" + "log" "os" "strings" bugout "github.com/bugout-dev/bugout-go/pkg" "github.com/google/uuid" - - "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" ) var ( @@ -138,9 +137,20 @@ func (s *StateCLI) checkRequirements() { } } - config := configs.GetConfigPath(s.configPathFlag) - if !configs.CheckPathExists(config.ConfigPath) { - configs.GenerateDefaultConfig(config) + // Load configuration + config, err := GetConfigPath(s.configPathFlag) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + if !config.ConfigExists { + if err := GenerateDefaultConfig(config); err != nil { + fmt.Println(err) + os.Exit(1) + } + } else { + log.Printf("Loaded configuration from %s", config.ConfigPath) } s.configPathFlag = config.ConfigPath } @@ -183,7 +193,7 @@ func (s *StateCLI) populateCLI() { s.usersCmd.IntVar(&s.offsetFlag, "offset", 0, "Result output offset") } -func CLI() { +func cli() { stateCLI.populateCLI() if len(os.Args) < 2 { stateCLI.usage() @@ -193,7 +203,7 @@ func CLI() { // Init bugout client bc, err := bugout.ClientFromEnv() if err != nil { - fmt.Printf("Unable to initialize bugout client %v", err) + fmt.Printf("Unable to initialize bugout client, err: %v\n", err) os.Exit(1) } bugoutClient = bc @@ -213,24 +223,24 @@ func CLI() { ExtendedMethods: stateCLI.extendedMethodsFlag, } _, err := bugoutClient.Brood.FindUser( - configs.NB_CONTROLLER_TOKEN, + NB_CONTROLLER_TOKEN, map[string]string{ "user_id": proposedUserAccess.UserID, - "application_id": configs.NB_APPLICATION_ID, + "application_id": NB_APPLICATION_ID, }, ) if err != nil { - fmt.Printf("User does not exists %v\n", err) + fmt.Printf("User does not exists, err: %v\n", err) os.Exit(1) } - resource, err := bugoutClient.Brood.CreateResource(configs.NB_CONTROLLER_TOKEN, configs.NB_APPLICATION_ID, proposedUserAccess) + resource, err := bugoutClient.Brood.CreateResource(NB_CONTROLLER_TOKEN, NB_APPLICATION_ID, proposedUserAccess) if err != nil { - fmt.Printf("Unable to create user access %v\n", err) + fmt.Printf("Unable to create user access, err: %v\n", err) os.Exit(1) } resource_data, err := json.Marshal(resource.ResourceData) if err != nil { - fmt.Printf("Unable to encode resource %s data interface to json %v", resource.Id, err) + fmt.Printf("Unable to encode resource %s data interface to json, err: %v", resource.Id, err) os.Exit(1) } fmt.Println(string(resource_data)) @@ -251,31 +261,31 @@ func CLI() { queryParameters["access_id"] = stateCLI.accessIDFlag } resources, err := bugoutClient.Brood.GetResources( - configs.NB_CONTROLLER_TOKEN, - configs.NB_APPLICATION_ID, + NB_CONTROLLER_TOKEN, + NB_APPLICATION_ID, queryParameters, ) if err != nil { - fmt.Printf("Unable to get Bugout resources %v\n", err) + fmt.Printf("Unable to get Bugout resources, err: %v\n", err) os.Exit(1) } var userAccesses []ClientResourceData for _, resource := range resources.Resources { - deletedResource, err := bugoutClient.Brood.DeleteResource(configs.NB_CONTROLLER_TOKEN, resource.Id) + deletedResource, err := bugoutClient.Brood.DeleteResource(NB_CONTROLLER_TOKEN, resource.Id) if err != nil { - fmt.Printf("Unable to delete resource %s %v\n", resource.Id, err) + fmt.Printf("Unable to delete resource %s, err: %v\n", resource.Id, err) continue } resource_data, err := json.Marshal(deletedResource.ResourceData) if err != nil { - fmt.Printf("Unable to encode resource %s data interface to json %v", resource.Id, err) + fmt.Printf("Unable to encode resource %s data interface to json, err: %v\n", resource.Id, err) continue } var userAccess ClientResourceData err = json.Unmarshal(resource_data, &userAccess) if err != nil { - fmt.Printf("Unable to decode resource %s data json to structure %v", resource.Id, err) + fmt.Printf("Unable to decode resource %s data json to structure, err: %v\n", resource.Id, err) continue } userAccesses = append(userAccesses, userAccess) @@ -283,7 +293,7 @@ func CLI() { userAccessesJson, err := json.Marshal(userAccesses) if err != nil { - fmt.Printf("Unable to marshal user access struct %v\n", err) + fmt.Printf("Unable to marshal user access struct, err: %v\n", err) os.Exit(1) } fmt.Println(string(userAccessesJson)) @@ -292,7 +302,7 @@ func CLI() { stateCLI.serverCmd.Parse(os.Args[2:]) stateCLI.checkRequirements() - configs.CheckEnvVarSet() + CheckEnvVarSet() Server() @@ -308,12 +318,12 @@ func CLI() { queryParameters["access_id"] = stateCLI.accessIDFlag } resources, err := bugoutClient.Brood.GetResources( - configs.NB_CONTROLLER_TOKEN, - configs.NB_APPLICATION_ID, + NB_CONTROLLER_TOKEN, + NB_APPLICATION_ID, queryParameters, ) if err != nil { - fmt.Printf("Unable to get Bugout resources %v\n", err) + fmt.Printf("Unable to get Bugout resources, err: %v\n", err) os.Exit(1) } @@ -331,20 +341,20 @@ func CLI() { for _, resource := range resources.Resources[offset:limit] { resource_data, err := json.Marshal(resource.ResourceData) if err != nil { - fmt.Printf("Unable to encode resource %s data interface to json %v", resource.Id, err) + fmt.Printf("Unable to encode resource %s data interface to json, err: %v\n", resource.Id, err) continue } var userAccess ClientResourceData err = json.Unmarshal(resource_data, &userAccess) if err != nil { - fmt.Printf("Unable to decode resource %s data json to structure %v", resource.Id, err) + fmt.Printf("Unable to decode resource %s data json to structure, err: %v\n", resource.Id, err) continue } userAccesses = append(userAccesses, userAccess) } userAccessesJson, err := json.Marshal(userAccesses) if err != nil { - fmt.Printf("Unable to marshal user accesses struct %v\n", err) + fmt.Printf("Unable to marshal user accesses struct, err: %v\n", err) os.Exit(1) } fmt.Println(string(userAccessesJson)) @@ -353,7 +363,7 @@ func CLI() { stateCLI.versionCmd.Parse(os.Args[2:]) stateCLI.checkRequirements() - fmt.Printf("v%s\n", configs.NB_VERSION) + fmt.Printf("v%s\n", NB_VERSION) default: stateCLI.usage() diff --git a/nodes/node_balancer/cmd/nodebalancer/clients.go b/nodes/node_balancer/cmd/nodebalancer/clients.go index 94ea3331..3eaffd08 100644 --- a/nodes/node_balancer/cmd/nodebalancer/clients.go +++ b/nodes/node_balancer/cmd/nodebalancer/clients.go @@ -5,8 +5,6 @@ import ( "reflect" "sync" "time" - - configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" ) var ( @@ -104,7 +102,7 @@ func (cpool *ClientPool) AddClientNode(id string, node *Node) { func (cpool *ClientPool) GetClientNode(id string) *Node { if cpool.Client[id] != nil { lastCallTs := cpool.Client[id].GetClientLastCallDiff() - if lastCallTs < configs.NB_CLIENT_NODE_KEEP_ALIVE { + if lastCallTs < NB_CLIENT_NODE_KEEP_ALIVE { cpool.Client[id].UpdateClientLastCall() return cpool.Client[id].Node } @@ -119,7 +117,7 @@ func (cpool *ClientPool) CleanInactiveClientNodes() int { cnt := 0 for id, client := range cpool.Client { lastCallTs := client.GetClientLastCallDiff() - if lastCallTs >= configs.NB_CLIENT_NODE_KEEP_ALIVE { + if lastCallTs >= NB_CLIENT_NODE_KEEP_ALIVE { delete(cpool.Client, id) } else { cnt += 1 diff --git a/nodes/node_balancer/cmd/nodebalancer/clients_test.go b/nodes/node_balancer/cmd/nodebalancer/clients_test.go index 41a4d52d..c485ebf4 100644 --- a/nodes/node_balancer/cmd/nodebalancer/clients_test.go +++ b/nodes/node_balancer/cmd/nodebalancer/clients_test.go @@ -4,8 +4,6 @@ import ( "reflect" "testing" "time" - - configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" ) func TestAddClientNode(t *testing.T) { @@ -40,7 +38,7 @@ func TestGetClientNode(t *testing.T) { {map[string]*Client{}, "1", nil}, {map[string]*Client{"1": {LastCallTs: ts, Node: &Node{Alive: true}}}, "1", &Node{Alive: true}}, {map[string]*Client{"2": {LastCallTs: ts, Node: &Node{Alive: true}}}, "1", nil}, - {map[string]*Client{"1": {LastCallTs: ts - configs.NB_CLIENT_NODE_KEEP_ALIVE, Node: &Node{Alive: true}}}, "1", nil}, + {map[string]*Client{"1": {LastCallTs: ts - NB_CLIENT_NODE_KEEP_ALIVE, Node: &Node{Alive: true}}}, "1", nil}, } for _, c := range cases { CreateClientPools() @@ -63,11 +61,11 @@ func TestCleanInactiveClientNodes(t *testing.T) { clients map[string]*Client expected string }{ - {map[string]*Client{"1": {LastCallTs: ts - configs.NB_CLIENT_NODE_KEEP_ALIVE}}, ""}, + {map[string]*Client{"1": {LastCallTs: ts - NB_CLIENT_NODE_KEEP_ALIVE}}, ""}, {map[string]*Client{"1": {LastCallTs: ts}}, "1"}, {map[string]*Client{ - "1": {LastCallTs: ts - configs.NB_CLIENT_NODE_KEEP_ALIVE}, - "2": {LastCallTs: ts - configs.NB_CLIENT_NODE_KEEP_ALIVE - 10}, + "1": {LastCallTs: ts - NB_CLIENT_NODE_KEEP_ALIVE}, + "2": {LastCallTs: ts - NB_CLIENT_NODE_KEEP_ALIVE - 10}, "3": {LastCallTs: ts}, }, "3"}, } diff --git a/nodes/node_balancer/configs/settings.go b/nodes/node_balancer/cmd/nodebalancer/configs.go similarity index 59% rename from nodes/node_balancer/configs/settings.go rename to nodes/node_balancer/cmd/nodebalancer/configs.go index 03b39d72..dc2fdebb 100644 --- a/nodes/node_balancer/configs/settings.go +++ b/nodes/node_balancer/cmd/nodebalancer/configs.go @@ -1,10 +1,12 @@ /* Configurations for load balancer server. */ -package configs +package main import ( + "encoding/json" "fmt" + "io/ioutil" "log" "os" "path/filepath" @@ -43,8 +45,6 @@ var ( MOONSTREAM_DB_CONN_MAX_LIFETIME = 30 * time.Minute ) -var MOONSTREAM_NODES_SERVER_PORT = os.Getenv("MOONSTREAM_NODES_SERVER_PORT") - func CheckEnvVarSet() { if NB_ACCESS_ID_HEADER == "" { NB_ACCESS_ID_HEADER = "x-node-balancer-access-id" @@ -52,14 +52,31 @@ func CheckEnvVarSet() { if NB_DATA_SOURCE_HEADER == "" { NB_DATA_SOURCE_HEADER = "x-node-balancer-data-source" } - - if MOONSTREAM_NODES_SERVER_PORT == "" { - fmt.Println("Environment variable MOONSTREAM_NODES_SERVER_PORT not set") - os.Exit(1) - } } -type Config struct { +// Nodes configuration +type NodeConfig struct { + Blockchain string `json:"blockchain"` + Endpoint string `json:"endpoint"` + + Internal bool `json:"internal"` +} + +func LoadConfig(configPath string) (*[]NodeConfig, error) { + rawBytes, err := ioutil.ReadFile(configPath) + if err != nil { + return nil, err + } + nodeConfigs := &[]NodeConfig{} + err = json.Unmarshal(rawBytes, nodeConfigs) + if err != nil { + return nil, err + } + + return nodeConfigs, nil +} + +type ConfigPlacement struct { ConfigDirPath string ConfigDirExists bool @@ -67,28 +84,26 @@ type Config struct { ConfigExists bool } -func CheckPathExists(path string) bool { +func CheckPathExists(path string) (bool, error) { var exists = true _, err := os.Stat(path) if err != nil { if os.IsNotExist(err) { exists = false } else { - fmt.Println(err) - os.Exit(1) + return exists, fmt.Errorf("Error due checking file path exists, err: %v", err) } } - return exists + return exists, nil } -func GetConfigPath(providedPath string) *Config { +func GetConfigPath(providedPath string) (*ConfigPlacement, error) { var configDirPath, configPath string if providedPath == "" { homeDir, err := os.UserHomeDir() if err != nil { - fmt.Printf("Unable to find user home directory, %v", err) - os.Exit(1) + return nil, fmt.Errorf("Unable to find user home directory, %v", err) } configDirPath = fmt.Sprintf("%s/.nodebalancer", homeDir) configPath = fmt.Sprintf("%s/config.txt", configDirPath) @@ -97,35 +112,48 @@ func GetConfigPath(providedPath string) *Config { configDirPath = filepath.Dir(configPath) } - defaultConfig := &Config{ - ConfigDirPath: configDirPath, - ConfigDirExists: CheckPathExists(configDirPath), - - ConfigPath: configPath, - ConfigExists: CheckPathExists(configPath), + configDirPathExists, err := CheckPathExists(configDirPath) + if err != nil { + return nil, err + } + configPathExists, err := CheckPathExists(configPath) + if err != nil { + return nil, err } - return defaultConfig + config := &ConfigPlacement{ + ConfigDirPath: configDirPath, + ConfigDirExists: configDirPathExists, + + ConfigPath: configPath, + ConfigExists: configPathExists, + } + + return config, nil } -func GenerateDefaultConfig(config *Config) string { +func GenerateDefaultConfig(config *ConfigPlacement) error { if !config.ConfigDirExists { if err := os.MkdirAll(config.ConfigDirPath, os.ModePerm); err != nil { - fmt.Printf("Unable to create directory, %v", err) - os.Exit(1) + return fmt.Errorf("Unable to create directory, %v", err) } log.Printf("Config directory created at: %s", config.ConfigDirPath) } if !config.ConfigExists { - tempConfigB := []byte("ethereum,127.0.0.1,8545") - err := os.WriteFile(config.ConfigPath, tempConfigB, 0644) + tempConfig := []NodeConfig{ + {Blockchain: "ethereum", Endpoint: "http://127.0.0.1:8545", Internal: true}, + } + tempConfigJson, err := json.Marshal(tempConfig) if err != nil { - fmt.Printf("Unable to create temp config file, %v", err) - os.Exit(1) + return fmt.Errorf("Unable to marshal configuration data, err: %v", err) + } + err = ioutil.WriteFile(config.ConfigPath, tempConfigJson, os.ModePerm) + if err != nil { + return fmt.Errorf("Unable to write default config to file %s, err: %v", config.ConfigPath, err) } log.Printf("Created default configuration at %s", config.ConfigPath) } - return config.ConfigPath + return nil } diff --git a/nodes/node_balancer/cmd/nodebalancer/db.go b/nodes/node_balancer/cmd/nodebalancer/db.go index 4ce99884..28bf3fc8 100644 --- a/nodes/node_balancer/cmd/nodebalancer/db.go +++ b/nodes/node_balancer/cmd/nodebalancer/db.go @@ -4,8 +4,6 @@ import ( "database/sql" "fmt" - configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" - _ "github.com/lib/pq" ) @@ -19,18 +17,18 @@ type DatabaseClient struct { // Establish connection with database func InitDatabaseClient() error { - db, err := sql.Open("postgres", configs.MOONSTREAM_DB_URI_READ_ONLY) + db, err := sql.Open("postgres", MOONSTREAM_DB_URI_READ_ONLY) if err != nil { return fmt.Errorf("DSN parse error or another database initialization error: %v", err) } // Set the maximum number of concurrently idle connections, // by default sql.DB allows a maximum of 2 idle connections. - db.SetMaxIdleConns(configs.MOONSTREAM_DB_MAX_IDLE_CONNS) + db.SetMaxIdleConns(MOONSTREAM_DB_MAX_IDLE_CONNS) // Set the maximum lifetime of a connection. // Longer lifetime increase memory usage. - db.SetConnMaxLifetime(configs.MOONSTREAM_DB_CONN_MAX_LIFETIME) + db.SetConnMaxLifetime(MOONSTREAM_DB_CONN_MAX_LIFETIME) databaseClient = DatabaseClient{ Client: db, diff --git a/nodes/node_balancer/cmd/nodebalancer/main.go b/nodes/node_balancer/cmd/nodebalancer/main.go index f39b62eb..cee85c6c 100644 --- a/nodes/node_balancer/cmd/nodebalancer/main.go +++ b/nodes/node_balancer/cmd/nodebalancer/main.go @@ -1,5 +1,5 @@ package main func main() { - CLI() + cli() } diff --git a/nodes/node_balancer/cmd/nodebalancer/middleware.go b/nodes/node_balancer/cmd/nodebalancer/middleware.go index 5e33bade..da93a166 100644 --- a/nodes/node_balancer/cmd/nodebalancer/middleware.go +++ b/nodes/node_balancer/cmd/nodebalancer/middleware.go @@ -16,8 +16,6 @@ import ( "sync" "time" - "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" - humbug "github.com/bugout-dev/humbug/go/pkg" ) @@ -89,7 +87,7 @@ func (ac *AccessCache) Cleanup() (int64, int64) { tsNow := time.Now().Unix() ac.mux.Lock() for aId, aData := range ac.accessIds { - if tsNow-aData.LastAccessTs > configs.NB_CACHE_ACCESS_ID_LIFETIME { + if tsNow-aData.LastAccessTs > NB_CACHE_ACCESS_ID_LIFETIME { delete(ac.accessIds, aId) removedAccessIds++ } else { @@ -101,7 +99,7 @@ func (ac *AccessCache) Cleanup() (int64, int64) { } func initCacheCleaning(debug bool) { - t := time.NewTicker(configs.NB_CACHE_CLEANING_INTERVAL) + t := time.NewTicker(NB_CACHE_CLEANING_INTERVAL) for { select { case <-t.C: @@ -118,7 +116,7 @@ func initCacheCleaning(debug bool) { func extractAccessID(r *http.Request) string { var accessID string - accessIDHeaders := r.Header[strings.Title(configs.NB_ACCESS_ID_HEADER)] + accessIDHeaders := r.Header[strings.Title(NB_ACCESS_ID_HEADER)] for _, h := range accessIDHeaders { accessID = h } @@ -137,7 +135,7 @@ func extractAccessID(r *http.Request) string { func extractDataSource(r *http.Request) string { dataSource := "database" - dataSources := r.Header[strings.Title(configs.NB_DATA_SOURCE_HEADER)] + dataSources := r.Header[strings.Title(NB_DATA_SOURCE_HEADER)] for _, h := range dataSources { dataSource = h } @@ -203,7 +201,7 @@ func logMiddleware(next http.Handler) http.Handler { var jsonrpcRequest JSONRPCRequest err = json.Unmarshal(body, &jsonrpcRequest) if err != nil { - log.Printf("Unable to parse body %v", err) + log.Printf("Unable to parse body, err: %v", err) } logStr += fmt.Sprintf(" %s", jsonrpcRequest.Method) } @@ -236,7 +234,7 @@ func accessMiddleware(next http.Handler) http.Handler { } // If access id does not belong to internal crawlers, then check cache or find it in Bugout resources - if accessID == configs.NB_CONTROLLER_ACCESS_ID { + if accessID == NB_CONTROLLER_ACCESS_ID { if stateCLI.enableDebugFlag { log.Printf("Access id belongs to internal crawlers") } @@ -254,8 +252,8 @@ func accessMiddleware(next http.Handler) http.Handler { log.Printf("New access id, looking at Brood resources") } resources, err := bugoutClient.Brood.GetResources( - configs.NB_CONTROLLER_TOKEN, - configs.NB_APPLICATION_ID, + NB_CONTROLLER_TOKEN, + NB_APPLICATION_ID, map[string]string{"access_id": accessID}, ) if err != nil { diff --git a/nodes/node_balancer/cmd/nodebalancer/routes.go b/nodes/node_balancer/cmd/nodebalancer/routes.go index c1deb560..4722533e 100644 --- a/nodes/node_balancer/cmd/nodebalancer/routes.go +++ b/nodes/node_balancer/cmd/nodebalancer/routes.go @@ -12,8 +12,6 @@ import ( "net/http" "strconv" "strings" - - configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" ) type PingResponse struct { @@ -42,8 +40,8 @@ func lbHandler(w http.ResponseWriter, r *http.Request) { } attempts := GetAttemptsFromContext(r) - if attempts > configs.NB_CONNECTION_RETRIES { - log.Printf("Max attempts reached from %s %s, terminating\n", r.RemoteAddr, r.URL.Path) + if attempts > NB_CONNECTION_RETRIES { + log.Printf("Max attempts reached from %s %s, terminating", r.RemoteAddr, r.URL.Path) http.Error(w, "Service not available", http.StatusServiceUnavailable) return } @@ -147,7 +145,7 @@ func lbDatabaseHandler(w http.ResponseWriter, r *http.Request, blockchain string block, err := databaseClient.GetBlock(blockchain, blockNumber) if err != nil { - fmt.Printf("Unable to get block from database %v", err) + log.Printf("Unable to get block from database, err: %v", err) http.Error(w, fmt.Sprintf("no such block %v", blockNumber), http.StatusBadRequest) return } diff --git a/nodes/node_balancer/cmd/nodebalancer/server.go b/nodes/node_balancer/cmd/nodebalancer/server.go index 9869d72a..50230413 100644 --- a/nodes/node_balancer/cmd/nodebalancer/server.go +++ b/nodes/node_balancer/cmd/nodebalancer/server.go @@ -14,8 +14,6 @@ import ( "os" "time" - configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" - humbug "github.com/bugout-dev/humbug/go/pkg" "github.com/google/uuid" ) @@ -29,7 +27,7 @@ var ( // initHealthCheck runs a routine for check status of the nodes every 5 seconds func initHealthCheck(debug bool) { - t := time.NewTicker(configs.NB_HEALTH_CHECK_INTERVAL) + t := time.NewTicker(NB_HEALTH_CHECK_INTERVAL) for { select { case <-t.C: @@ -37,7 +35,7 @@ func initHealthCheck(debug bool) { ethereumClients := ethereumClientPool.CleanInactiveClientNodes() polygonClients := polygonClientPool.CleanInactiveClientNodes() xdaiClients := xdaiClientPool.CleanInactiveClientNodes() - log.Printf("Active etehereum clients: %d, polygon clients: %d, xdai clients: %d\n", ethereumClients, polygonClients, xdaiClients) + log.Printf("Active ethereum clients: %d, polygon clients: %d, xdai clients: %d", ethereumClients, polygonClients, xdaiClients) if debug { blockchainPool.StatusLog() } @@ -71,13 +69,13 @@ func GetRetryFromContext(r *http.Request) int { func proxyErrorHandler(proxy *httputil.ReverseProxy, url *url.URL) { proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, e error) { retries := GetRetryFromContext(r) - if retries < configs.NB_CONNECTION_RETRIES { + if retries < NB_CONNECTION_RETRIES { log.Printf( - "An error occurred while proxying to %s, number of retries: %d/%d. Error: %s\n", - url, retries+1, configs.NB_CONNECTION_RETRIES, e.Error(), + "An error occurred while proxying to %s, number of retries: %d/%d, err: %v", + url, retries+1, NB_CONNECTION_RETRIES, e.Error(), ) select { - case <-time.After(configs.NB_CONNECTION_RETRIES_INTERVAL): + case <-time.After(NB_CONNECTION_RETRIES_INTERVAL): ctx := context.WithValue(r.Context(), Retry, retries+1) proxy.ServeHTTP(w, r.WithContext(ctx)) } @@ -94,7 +92,7 @@ func proxyErrorHandler(proxy *httputil.ReverseProxy, url *url.URL) { // If the same request routing for few attempts with different nodes, increase the count // of attempts and send request to next peer attempts := GetAttemptsFromContext(r) - log.Printf("Attempting number: %d to fetch node %s\n", attempts, url) + log.Printf("Attempting number: %d to fetch node %s", attempts, url) ctx := context.WithValue(r.Context(), Attempts, attempts+1) lbHandler(w, r.WithContext(ctx)) } @@ -111,36 +109,36 @@ func Server() { var err error sessionID := uuid.New().String() consent := humbug.CreateHumbugConsent(humbug.True) - reporter, err = humbug.CreateHumbugReporter(consent, "moonstream-node-balancer", sessionID, configs.HUMBUG_REPORTER_NB_TOKEN) + reporter, err = humbug.CreateHumbugReporter(consent, "moonstream-node-balancer", sessionID, HUMBUG_REPORTER_NB_TOKEN) if err != nil { - fmt.Printf("Invalid Humbug Crash configuration: %v", err) + fmt.Printf("Invalid Humbug Crash configuration, err: %v\n", err) os.Exit(1) } // Record system information reporter.Publish(humbug.SystemReport()) resources, err := bugoutClient.Brood.GetResources( - configs.NB_CONTROLLER_TOKEN, - configs.NB_APPLICATION_ID, - map[string]string{"access_id": configs.NB_CONTROLLER_ACCESS_ID}, + NB_CONTROLLER_TOKEN, + NB_APPLICATION_ID, + map[string]string{"access_id": NB_CONTROLLER_ACCESS_ID}, ) if err != nil { - fmt.Printf("Unable to get user with provided access identifier %v", err) + fmt.Printf("Unable to get user with provided access identifier, err: %v\n", err) os.Exit(1) } if len(resources.Resources) != 1 { - fmt.Printf("User with provided access identifier has wrong number of resources %v", err) + fmt.Printf("User with provided access identifier has wrong number of resources, err: %v\n", err) os.Exit(1) } resource_data, err := json.Marshal(resources.Resources[0].ResourceData) if err != nil { - fmt.Printf("Unable to encode resource data interface to json %v", err) + fmt.Printf("Unable to encode resource data interface to json, err: %v\n", err) os.Exit(1) } var clientAccess ClientResourceData err = json.Unmarshal(resource_data, &clientAccess) if err != nil { - fmt.Printf("Unable to decode resource data json to structure %v", err) + fmt.Printf("Unable to decode resource data json to structure, err: %v\n", err) os.Exit(1) } internalCrawlersAccess = ClientResourceData{ @@ -158,43 +156,38 @@ func Server() { err = InitDatabaseClient() if err != nil { - log.Printf("Unable to initialize database connection %v\n", err) + log.Printf("Unable to initialize database connection, err: %v", err) } else { - log.Printf("Connection with database established\n") + log.Printf("Connection with database established") } // Fill NodeConfigList with initial nodes from environment variables - nodeConfigs.InitNodeConfigList(stateCLI.configPathFlag) + nodeConfig, err := LoadConfig(stateCLI.configPathFlag) + if err != nil { + fmt.Println(err) + os.Exit(1) + } // Parse nodes and set list of proxies - for i, nodeConfig := range nodeConfigs.NodeConfigs { - gethUrl, err := url.Parse(fmt.Sprintf("http://%s:%d", nodeConfig.Addr, nodeConfig.Port)) + for i, nodeConfig := range *nodeConfig { + + endpoint, err := url.Parse(nodeConfig.Endpoint) if err != nil { - fmt.Printf("Unable to parse gethUrl with addr: %s and port: %d\n", nodeConfig.Addr, nodeConfig.Port) - continue - } - statusUrl, err := url.Parse(fmt.Sprintf("http://%s:%s", nodeConfig.Addr, configs.MOONSTREAM_NODES_SERVER_PORT)) - if err != nil { - fmt.Printf("Unable to parse statusUrl with addr: %s and port: %s\n", nodeConfig.Addr, configs.MOONSTREAM_NODES_SERVER_PORT) - continue + fmt.Println(err) + os.Exit(1) } - proxyToStatus := httputil.NewSingleHostReverseProxy(statusUrl) - proxyToGeth := httputil.NewSingleHostReverseProxy(gethUrl) - - proxyErrorHandler(proxyToStatus, statusUrl) - proxyErrorHandler(proxyToGeth, gethUrl) + proxyToEndpoint := httputil.NewSingleHostReverseProxy(endpoint) + proxyErrorHandler(proxyToEndpoint, endpoint) blockchainPool.AddNode(&Node{ - StatusURL: statusUrl, - GethURL: gethUrl, - Alive: true, - StatusReverseProxy: proxyToStatus, - GethReverseProxy: proxyToGeth, + Endpoint: endpoint, + Alive: true, + GethReverseProxy: proxyToEndpoint, }, nodeConfig.Blockchain) log.Printf( - "Added new %s proxy blockchain under index %d from config file with geth url: %s and status url: %s\n", - nodeConfig.Blockchain, i, gethUrl, statusUrl) + "Added new %s proxy blockchain under index %d from config file with geth url: %s://%s", + nodeConfig.Blockchain, i, endpoint.Scheme, endpoint.Host) } serveMux := http.NewServeMux() @@ -225,10 +218,10 @@ func Server() { // Start access id cache cleaning go initCacheCleaning(stateCLI.enableDebugFlag) - log.Printf("Starting node load balancer HTTP server at %s:%s\n", stateCLI.listeningAddrFlag, stateCLI.listeningPortFlag) + log.Printf("Starting node load balancer HTTP server at %s:%s", stateCLI.listeningAddrFlag, stateCLI.listeningPortFlag) err = server.ListenAndServe() if err != nil { - fmt.Printf("Failed to start server listener %v", err) + fmt.Printf("Failed to start server listener, err: %v\n", err) os.Exit(1) } } diff --git a/nodes/node_balancer/cmd/nodebalancer/version.go b/nodes/node_balancer/cmd/nodebalancer/version.go new file mode 100644 index 00000000..50254033 --- /dev/null +++ b/nodes/node_balancer/cmd/nodebalancer/version.go @@ -0,0 +1,3 @@ +package main + +var NB_VERSION = "0.1.1" diff --git a/nodes/node_balancer/configs/version.go b/nodes/node_balancer/configs/version.go deleted file mode 100644 index 477524d8..00000000 --- a/nodes/node_balancer/configs/version.go +++ /dev/null @@ -1,3 +0,0 @@ -package configs - -var NB_VERSION = "0.1.0" diff --git a/nodes/node_balancer/sample.env b/nodes/node_balancer/sample.env index ce99f7ca..d1aea3b6 100644 --- a/nodes/node_balancer/sample.env +++ b/nodes/node_balancer/sample.env @@ -4,7 +4,6 @@ export NB_APPLICATION_ID="" export NB_CONTROLLER_TOKEN="" export NB_CONTROLLER_ACCESS_ID="" export MOONSTREAM_DB_URI="postgresql://:@:/" -export MOONSTREAM_NODES_SERVER_PORT="" # Error humbug reporter export HUMBUG_REPORTER_NODE_BALANCER_TOKEN="" From fcb3671fde713ef9d39240f8b2e11375af761503 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Tue, 12 Jul 2022 20:16:24 +0000 Subject: [PATCH 4/9] Short version of TLS bypass, also removed access id leak --- nodes/node_balancer/cmd/nodebalancer/balancer.go | 3 +-- nodes/node_balancer/cmd/nodebalancer/configs.go | 4 +--- nodes/node_balancer/cmd/nodebalancer/routes.go | 7 +------ nodes/node_balancer/cmd/nodebalancer/server.go | 14 +++++++++++++- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/nodes/node_balancer/cmd/nodebalancer/balancer.go b/nodes/node_balancer/cmd/nodebalancer/balancer.go index e1d1a30c..ff01b8c7 100644 --- a/nodes/node_balancer/cmd/nodebalancer/balancer.go +++ b/nodes/node_balancer/cmd/nodebalancer/balancer.go @@ -32,8 +32,7 @@ type Node struct { mux sync.RWMutex - StatusReverseProxy *httputil.ReverseProxy - GethReverseProxy *httputil.ReverseProxy + GethReverseProxy *httputil.ReverseProxy } type NodePool struct { diff --git a/nodes/node_balancer/cmd/nodebalancer/configs.go b/nodes/node_balancer/cmd/nodebalancer/configs.go index dc2fdebb..88b343e7 100644 --- a/nodes/node_balancer/cmd/nodebalancer/configs.go +++ b/nodes/node_balancer/cmd/nodebalancer/configs.go @@ -58,8 +58,6 @@ func CheckEnvVarSet() { type NodeConfig struct { Blockchain string `json:"blockchain"` Endpoint string `json:"endpoint"` - - Internal bool `json:"internal"` } func LoadConfig(configPath string) (*[]NodeConfig, error) { @@ -142,7 +140,7 @@ func GenerateDefaultConfig(config *ConfigPlacement) error { if !config.ConfigExists { tempConfig := []NodeConfig{ - {Blockchain: "ethereum", Endpoint: "http://127.0.0.1:8545", Internal: true}, + {Blockchain: "ethereum", Endpoint: "http://127.0.0.1:8545"}, } tempConfigJson, err := json.Marshal(tempConfig) if err != nil { diff --git a/nodes/node_balancer/cmd/nodebalancer/routes.go b/nodes/node_balancer/cmd/nodebalancer/routes.go index 4722533e..fe0dd710 100644 --- a/nodes/node_balancer/cmd/nodebalancer/routes.go +++ b/nodes/node_balancer/cmd/nodebalancer/routes.go @@ -80,10 +80,6 @@ func lbHandler(w http.ResponseWriter, r *http.Request) { r.Header.Add("X-Origin-Path", r.URL.Path) switch { - case strings.HasPrefix(r.URL.Path, fmt.Sprintf("/nb/%s/ping", blockchain)): - r.URL.Path = "/ping" - node.StatusReverseProxy.ServeHTTP(w, r) - return case strings.HasPrefix(r.URL.Path, fmt.Sprintf("/nb/%s/jsonrpc", blockchain)): lbJSONRPCHandler(w, r, blockchain, node, currentClientAccess) return @@ -122,9 +118,8 @@ func lbJSONRPCHandler(w http.ResponseWriter, r *http.Request, blockchain string, } } + // Overwrite Path so response will be returned to correct place r.URL.Path = "/" - // If required detailed timeout configuration, define node.GethReverseProxy.Transport = &http.Transport{} - // as modified structure of DefaultTransport net/http/transport/DefaultTransport node.GethReverseProxy.ServeHTTP(w, r) return case currentClientAccess.dataSource == "database": diff --git a/nodes/node_balancer/cmd/nodebalancer/server.go b/nodes/node_balancer/cmd/nodebalancer/server.go index 50230413..9ae6ad8a 100644 --- a/nodes/node_balancer/cmd/nodebalancer/server.go +++ b/nodes/node_balancer/cmd/nodebalancer/server.go @@ -12,6 +12,7 @@ import ( "net/http/httputil" "net/url" "os" + "strings" "time" humbug "github.com/bugout-dev/humbug/go/pkg" @@ -170,7 +171,6 @@ func Server() { // Parse nodes and set list of proxies for i, nodeConfig := range *nodeConfig { - endpoint, err := url.Parse(nodeConfig.Endpoint) if err != nil { fmt.Println(err) @@ -178,6 +178,18 @@ func Server() { } proxyToEndpoint := httputil.NewSingleHostReverseProxy(endpoint) + // If required detailed timeout configuration, define node.GethReverseProxy.Transport = &http.Transport{} + // as modified structure of DefaultTransport net/http/transport/DefaultTransport + director := proxyToEndpoint.Director + proxyToEndpoint.Director = func(r *http.Request) { + director(r) + // Overwrite Query and Headers to not bypass nodebalancer Query and Headers + r.URL.RawQuery = "" + r.Header.Del(strings.Title(NB_ACCESS_ID_HEADER)) + r.Header.Del(strings.Title(NB_DATA_SOURCE_HEADER)) + // Change r.Host from nodebalancer's to end host so TLS check will be passed + r.Host = r.URL.Host + } proxyErrorHandler(proxyToEndpoint, endpoint) blockchainPool.AddNode(&Node{ From ce439f0f515fe7f57b998c4089f7cc96a6332efd Mon Sep 17 00:00:00 2001 From: kompotkot Date: Tue, 12 Jul 2022 20:41:07 +0000 Subject: [PATCH 5/9] Small fix to prevent logging additional response --- nodes/node_balancer/cmd/nodebalancer/middleware.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nodes/node_balancer/cmd/nodebalancer/middleware.go b/nodes/node_balancer/cmd/nodebalancer/middleware.go index da93a166..7f9e4a0e 100644 --- a/nodes/node_balancer/cmd/nodebalancer/middleware.go +++ b/nodes/node_balancer/cmd/nodebalancer/middleware.go @@ -201,7 +201,7 @@ func logMiddleware(next http.Handler) http.Handler { var jsonrpcRequest JSONRPCRequest err = json.Unmarshal(body, &jsonrpcRequest) if err != nil { - log.Printf("Unable to parse body, err: %v", err) + log.Printf("Unable to parse body at logging middleware, err: %v", err) } logStr += fmt.Sprintf(" %s", jsonrpcRequest.Method) } From f753e7d1370c44e2d1fff8aa87bb0246711c8b0f Mon Sep 17 00:00:00 2001 From: kompotkot Date: Tue, 12 Jul 2022 21:04:25 +0000 Subject: [PATCH 6/9] Optimised mux lock during healthcheck --- .../cmd/nodebalancer/balancer.go | 48 +++++++++---------- .../node_balancer/cmd/nodebalancer/routes.go | 2 + 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/nodes/node_balancer/cmd/nodebalancer/balancer.go b/nodes/node_balancer/cmd/nodebalancer/balancer.go index ff01b8c7..5ea2104e 100644 --- a/nodes/node_balancer/cmd/nodebalancer/balancer.go +++ b/nodes/node_balancer/cmd/nodebalancer/balancer.go @@ -29,6 +29,7 @@ type Node struct { Alive bool CurrentBlock uint64 + CallCounter uint64 mux sync.RWMutex @@ -92,19 +93,23 @@ func (node *Node) IsAlive() (alive bool) { return alive } -// SetCurrentBlock with mutex for exact node -func (node *Node) SetCurrentBlock(currentBlock uint64) { +// UpdateNodeState updates block number and live status, +// also it returns number of time node appeal +func (node *Node) UpdateNodeState(currentBlock uint64, alive bool) (callCounter uint64) { node.mux.Lock() node.CurrentBlock = currentBlock + node.Alive = alive + + callCounter = node.CallCounter node.mux.Unlock() + return callCounter } -// GetCurrentBlock returns block number -func (node *Node) GetCurrentBlock() (currentBlock uint64) { - node.mux.RLock() - currentBlock = node.CurrentBlock - node.mux.RUnlock() - return currentBlock +// IncreaseCallCounter increased to 1 each time node called +func (node *Node) IncreaseCallCounter() { + node.mux.Lock() + node.CallCounter++ + node.mux.Unlock() } // GetNextNode returns next active peer to take a connection @@ -125,7 +130,7 @@ func (bpool *BlockchainPool) GetNextNode(blockchain string) *Node { } } - // Increase Current value with 1 to be able to track node appeals + // Increase Current value with 1 currentInc := atomic.AddUint64(&np.Current, uint64(1)) // next is an Atomic incrementer, value always in range from 0 to slice length, @@ -185,6 +190,8 @@ func (bpool *BlockchainPool) StatusLog() { func (bpool *BlockchainPool) HealthCheck() { for _, b := range bpool.Blockchains { for _, n := range b.Nodes { + alive := false + httpClient := http.Client{Timeout: NB_HEALTH_CHECK_CALL_TIMEOUT} resp, err := httpClient.Post( n.Endpoint.String(), @@ -192,8 +199,7 @@ func (bpool *BlockchainPool) HealthCheck() { bytes.NewBuffer([]byte(`{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", false],"id":1}`)), ) if err != nil { - n.SetAlive(false) - n.SetCurrentBlock(0) + n.UpdateNodeState(0, alive) log.Printf("Unable to reach node: %s", n.Endpoint.Host) continue } @@ -201,8 +207,7 @@ func (bpool *BlockchainPool) HealthCheck() { body, err := ioutil.ReadAll(resp.Body) if err != nil { - n.SetAlive(false) - n.SetCurrentBlock(0) + n.UpdateNodeState(0, alive) log.Printf("Unable to parse response from %s node, err %v", n.Endpoint.Host, err) continue } @@ -210,8 +215,7 @@ func (bpool *BlockchainPool) HealthCheck() { var statusResponse NodeStatusResponse err = json.Unmarshal(body, &statusResponse) if err != nil { - n.SetAlive(false) - n.SetCurrentBlock(0) + n.UpdateNodeState(0, alive) log.Printf("Unable to read json response from %s node, err: %v", n.Endpoint.Host, err) continue } @@ -219,25 +223,19 @@ func (bpool *BlockchainPool) HealthCheck() { blockNumberHex := strings.Replace(statusResponse.Result.Number, "0x", "", -1) blockNumber, err := strconv.ParseUint(blockNumberHex, 16, 64) if err != nil { - n.SetAlive(false) - n.SetCurrentBlock(0) + n.UpdateNodeState(0, alive) log.Printf("Unable to parse block number from hex to string, err: %v", err) continue } - // Mark node in list of nodes as alive or not and update current block - var alive bool + // Mark node in list of pool as alive and update current block if blockNumber != 0 { alive = true - } else { - alive = false } - n.SetAlive(alive) - n.SetCurrentBlock(blockNumber) + callCounter := n.UpdateNodeState(blockNumber, alive) log.Printf( - "Node %s is alive: %t with current block: %d blockchain called: %d times", - n.Endpoint.Host, alive, blockNumber, b.Current, + "Node %s is alive: %t with current block: %d called: %d times", n.Endpoint.Host, alive, blockNumber, callCounter, ) } } diff --git a/nodes/node_balancer/cmd/nodebalancer/routes.go b/nodes/node_balancer/cmd/nodebalancer/routes.go index fe0dd710..0d390c39 100644 --- a/nodes/node_balancer/cmd/nodebalancer/routes.go +++ b/nodes/node_balancer/cmd/nodebalancer/routes.go @@ -118,6 +118,8 @@ func lbJSONRPCHandler(w http.ResponseWriter, r *http.Request, blockchain string, } } + node.IncreaseCallCounter() + // Overwrite Path so response will be returned to correct place r.URL.Path = "/" node.GethReverseProxy.ServeHTTP(w, r) From 5949ddf3eb404281b69bba7ba8ea233c367a6d73 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Wed, 13 Jul 2022 13:15:14 +0000 Subject: [PATCH 7/9] Config exec from txt to json --- nodes/deploy/node-balancer.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nodes/deploy/node-balancer.service b/nodes/deploy/node-balancer.service index e73de2cf..f9ecdc9a 100644 --- a/nodes/deploy/node-balancer.service +++ b/nodes/deploy/node-balancer.service @@ -15,7 +15,7 @@ ExecStart=/home/ubuntu/moonstream/nodes/node_balancer/nodebalancer server \ -host "${AWS_LOCAL_IPV4}" \ -port 8544 \ -healthcheck \ - -config /home/ubuntu/.nodebalancer/config.txt + -config /home/ubuntu/.nodebalancer/config.json SyslogIdentifier=node-balancer [Install] From 3ba9c8927aa768409349e9230cf0e393ac164ca8 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Thu, 14 Jul 2022 14:15:01 +0000 Subject: [PATCH 8/9] Nodebalancer deployment fix to work as user --- nodes/deploy/deploy.bash | 24 +++++++++---------- nodes/deploy/node-balancer-config.txt | 5 ---- ...-balancer.service => nodebalancer.service} | 6 ++--- 3 files changed, 14 insertions(+), 21 deletions(-) delete mode 100644 nodes/deploy/node-balancer-config.txt rename nodes/deploy/{node-balancer.service => nodebalancer.service} (87%) diff --git a/nodes/deploy/deploy.bash b/nodes/deploy/deploy.bash index a81524e4..6d065d74 100755 --- a/nodes/deploy/deploy.bash +++ b/nodes/deploy/deploy.bash @@ -24,11 +24,10 @@ PARAMETERS_ENV_PATH="${SECRETS_DIR}/app.env" AWS_SSM_PARAMETER_PATH="${AWS_SSM_PARAMETER_PATH:-/moonstream/prod}" SCRIPT_DIR="$(realpath $(dirname $0))" PARAMETERS_SCRIPT="${SCRIPT_DIR}/parameters.py" -NODE_BALANCER_CONFIG_PATH="${NODE_BALANCER_CONFIG_PATH:-/home/ubuntu/.nodebalancer}" -NODE_BALANCER_CONFIG_SOURCE_FILE="node-balancer-config.txt" +NODE_BALANCER_CONFIG_PATH="${NODE_BALANCER_CONFIG_PATH:-/home/ubuntu/.nodebalancer/config.json}" # Service file -NODE_BALANCER_SERVICE_FILE="node-balancer.service" +NODE_BALANCER_SERVICE_FILE="nodebalancer.service" set -eu @@ -41,12 +40,12 @@ AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION}" "${PYTHON}" "${PARAMETERS_SCRIPT}" ex echo echo echo -e "${PREFIX_INFO} Install checkenv" -HOME=/root /usr/local/go/bin/go install github.com/bugout-dev/checkenv@latest +HOME=/home/ubuntu /usr/local/go/bin/go install github.com/bugout-dev/checkenv@latest echo echo echo -e "${PREFIX_INFO} Retrieving addition deployment parameters" -AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION}" /root/go/bin/checkenv show aws_ssm+Product:moonstream >> "${PARAMETERS_ENV_PATH}" +AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION}" /home/ubuntu/go/bin/checkenv show aws_ssm+Product:moonstream >> "${PARAMETERS_ENV_PATH}" echo echo @@ -58,22 +57,21 @@ echo echo -e "${PREFIX_INFO} Building executable load balancer for nodes script with Go" EXEC_DIR=$(pwd) cd "${APP_NODES_DIR}/node_balancer" -HOME=/root /usr/local/go/bin/go build -o "${APP_NODES_DIR}/node_balancer/nodebalancer" "${APP_NODES_DIR}/node_balancer/cmd/nodebalancer/*.go" +HOME=/home/ubuntu /usr/local/go/bin/go build -o "${APP_NODES_DIR}/node_balancer/nodebalancer" "${APP_NODES_DIR}/node_balancer/cmd/nodebalancer/*.go" cd "${EXEC_DIR}" echo echo echo -e "${PREFIX_INFO} Update nodebalancer configuration file" -if [ ! -d "$NODE_BALANCER_CONFIG_PATH" ]; then - mkdir "$NODE_BALANCER_CONFIG_PATH" - echo -e "${PREFIX_WARN} Created new node balancer config directory" +if [ ! -z "$NODE_BALANCER_CONFIG_PATH" ]; then + echo -e "${PREFIX_CRIT} Node balancer configuration not found" + exit 1 fi -cp "${SCRIPT_DIR}/${NODE_BALANCER_CONFIG_SOURCE_FILE}" "${NODE_BALANCER_CONFIG_PATH}/config.txt" echo echo echo -e "${PREFIX_INFO} Replacing existing load balancer for nodes service definition with ${NODE_BALANCER_SERVICE_FILE}" chmod 644 "${SCRIPT_DIR}/${NODE_BALANCER_SERVICE_FILE}" -cp "${SCRIPT_DIR}/${NODE_BALANCER_SERVICE_FILE}" "/etc/systemd/system/${NODE_BALANCER_SERVICE_FILE}" -systemctl daemon-reload -systemctl restart "${NODE_BALANCER_SERVICE_FILE}" +cp "${SCRIPT_DIR}/${NODE_BALANCER_SERVICE_FILE}" "/home/ubuntu/.config/systemd/user/${NODE_BALANCER_SERVICE_FILE}" +systemctl --user daemon-reload +systemctl --user restart "${NODE_BALANCER_SERVICE_FILE}" diff --git a/nodes/deploy/node-balancer-config.txt b/nodes/deploy/node-balancer-config.txt deleted file mode 100644 index cc203e88..00000000 --- a/nodes/deploy/node-balancer-config.txt +++ /dev/null @@ -1,5 +0,0 @@ -ethereum,a.ethereum.moonstream.internal,8545 -ethereum,b.ethereum.moonstream.internal,8545 -polygon,a.polygon.moonstream.internal,8545 -polygon,b.polygon.moonstream.internal,8545 -xdai,a.xdai.moonstream.internal,8545 \ No newline at end of file diff --git a/nodes/deploy/node-balancer.service b/nodes/deploy/nodebalancer.service similarity index 87% rename from nodes/deploy/node-balancer.service rename to nodes/deploy/nodebalancer.service index f9ecdc9a..cd82f0a2 100644 --- a/nodes/deploy/node-balancer.service +++ b/nodes/deploy/nodebalancer.service @@ -6,7 +6,7 @@ StartLimitBurst=3 [Service] User=ubuntu -Group=www-data +Group=ubuntu WorkingDirectory=/home/ubuntu/moonstream/nodes/node_balancer EnvironmentFile=/home/ubuntu/moonstream-secrets/app.env Restart=on-failure @@ -16,7 +16,7 @@ ExecStart=/home/ubuntu/moonstream/nodes/node_balancer/nodebalancer server \ -port 8544 \ -healthcheck \ -config /home/ubuntu/.nodebalancer/config.json -SyslogIdentifier=node-balancer +SyslogIdentifier=nodebalancer [Install] -WantedBy=multi-user.target +WantedBy=multi-user.target \ No newline at end of file From f522a03a15fb9731adf6c6180d1b687a358deb0e Mon Sep 17 00:00:00 2001 From: kompotkot Date: Fri, 15 Jul 2022 15:37:34 +0000 Subject: [PATCH 9/9] Tested new deployment as user --- nodes/deploy/deploy.bash | 14 +++----------- nodes/deploy/nodebalancer.service | 2 -- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/nodes/deploy/deploy.bash b/nodes/deploy/deploy.bash index 6d065d74..6d34d1ae 100755 --- a/nodes/deploy/deploy.bash +++ b/nodes/deploy/deploy.bash @@ -57,21 +57,13 @@ echo echo -e "${PREFIX_INFO} Building executable load balancer for nodes script with Go" EXEC_DIR=$(pwd) cd "${APP_NODES_DIR}/node_balancer" -HOME=/home/ubuntu /usr/local/go/bin/go build -o "${APP_NODES_DIR}/node_balancer/nodebalancer" "${APP_NODES_DIR}/node_balancer/cmd/nodebalancer/*.go" +HOME=/home/ubuntu /usr/local/go/bin/go build -o "${APP_NODES_DIR}/node_balancer/nodebalancer" "${APP_NODES_DIR}/node_balancer/cmd/nodebalancer/" cd "${EXEC_DIR}" -echo -echo -echo -e "${PREFIX_INFO} Update nodebalancer configuration file" -if [ ! -z "$NODE_BALANCER_CONFIG_PATH" ]; then - echo -e "${PREFIX_CRIT} Node balancer configuration not found" - exit 1 -fi - echo echo echo -e "${PREFIX_INFO} Replacing existing load balancer for nodes service definition with ${NODE_BALANCER_SERVICE_FILE}" chmod 644 "${SCRIPT_DIR}/${NODE_BALANCER_SERVICE_FILE}" cp "${SCRIPT_DIR}/${NODE_BALANCER_SERVICE_FILE}" "/home/ubuntu/.config/systemd/user/${NODE_BALANCER_SERVICE_FILE}" -systemctl --user daemon-reload -systemctl --user restart "${NODE_BALANCER_SERVICE_FILE}" +XDG_RUNTIME_DIR="/run/user/$UID" systemctl --user daemon-reload +XDG_RUNTIME_DIR="/run/user/$UID" systemctl --user restart "${NODE_BALANCER_SERVICE_FILE}" diff --git a/nodes/deploy/nodebalancer.service b/nodes/deploy/nodebalancer.service index cd82f0a2..1bd9faab 100644 --- a/nodes/deploy/nodebalancer.service +++ b/nodes/deploy/nodebalancer.service @@ -5,8 +5,6 @@ StartLimitIntervalSec=300 StartLimitBurst=3 [Service] -User=ubuntu -Group=ubuntu WorkingDirectory=/home/ubuntu/moonstream/nodes/node_balancer EnvironmentFile=/home/ubuntu/moonstream-secrets/app.env Restart=on-failure