From 9390e81dd9a83f8235e84a63eae9fbbf8c665478 Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Fri, 23 Sep 2022 13:37:04 +0530 Subject: [PATCH] Added script to generate config.toml fromstart.sh (#518) * added go and bash script to get config out of start.sh and updated flagset.go * changed 'requiredblocks' flag back to 'eth.requiredblocks' * updated script * changed 'requiredblocks' flag back to 'eth.requiredblocks' * updated tests, and removed requiredblocks from json and hcl * addressed comments --- go.mod | 1 + go.sum | 2 + internal/cli/flagset/flagset.go | 9 ++++ internal/cli/server/flags.go | 2 +- scripts/getconfig.go | 27 ++++++++++ scripts/getconfig.sh | 87 +++++++++++++++++++++++++++++++++ 6 files changed, 127 insertions(+), 1 deletion(-) create mode 100755 scripts/getconfig.sh diff --git a/go.mod b/go.mod index 39bbeb240d..e31612cfe3 100644 --- a/go.mod +++ b/go.mod @@ -119,6 +119,7 @@ require ( github.com/mitchellh/pointerstructure v1.2.0 // indirect github.com/mitchellh/reflectwalk v1.0.0 // indirect github.com/opentracing/opentracing-go v1.1.0 // indirect + github.com/pelletier/go-toml v1.9.5 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/posener/complete v1.1.1 // indirect diff --git a/go.sum b/go.sum index 88c9152eef..593c8e11d0 100644 --- a/go.sum +++ b/go.sum @@ -391,6 +391,8 @@ github.com/opentracing/opentracing-go v1.0.3-0.20180606204148-bd9c31933947/go.mo github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/paulbellamy/ratecounter v0.2.0/go.mod h1:Hfx1hDpSGoqxkVVpBi/IlYD7kChlfo5C6hzIHwPqfFE= +github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= +github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/peterh/liner v1.0.1-0.20180619022028-8c1271fcf47f/go.mod h1:xIteQHvHuaLYG9IFj6mSxM0fCKrs34IrEQUhOYuGPHc= github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7 h1:oYW+YCJ1pachXTQmzR3rNLYGGz4g/UgFcjb28p/viDM= github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7/go.mod h1:CRroGNssyjTd/qIG2FyxByd2S8JEAZXBl4qUrZf8GS0= diff --git a/internal/cli/flagset/flagset.go b/internal/cli/flagset/flagset.go index d9e204f0e4..933fe59060 100644 --- a/internal/cli/flagset/flagset.go +++ b/internal/cli/flagset/flagset.go @@ -44,6 +44,15 @@ func (f *Flagset) Help() string { return str + strings.Join(items, "\n\n") } +func (f *Flagset) GetAllFlags() []string { + flags := []string{} + for _, flag := range f.flags { + flags = append(flags, flag.Name) + } + + return flags +} + // MarkDown implements cli.MarkDown interface func (f *Flagset) MarkDown() string { if len(f.flags) == 0 { diff --git a/internal/cli/server/flags.go b/internal/cli/server/flags.go index e19e578c57..835ff0ab95 100644 --- a/internal/cli/server/flags.go +++ b/internal/cli/server/flags.go @@ -57,7 +57,7 @@ func (c *Command) Flags() *flagset.Flagset { }) f.MapStringFlag(&flagset.MapStringFlag{ Name: "eth.requiredblocks", - Usage: "Comma separated block number-to-hash mappings to enforce (=)", + Usage: "Comma separated block number-to-hash mappings to require for peering (=)", Value: &c.cliConfig.RequiredBlocks, }) f.BoolFlag(&flagset.BoolFlag{ diff --git a/scripts/getconfig.go b/scripts/getconfig.go index 689ed68fbf..b400d6c48d 100644 --- a/scripts/getconfig.go +++ b/scripts/getconfig.go @@ -445,6 +445,7 @@ func updateArgsAdd(args []string) []string { return args } +<<<<<<< HEAD func handlePrometheus(args []string, updatedArgs []string) []string { var newUpdatedArgs []string @@ -453,10 +454,19 @@ func handlePrometheus(args []string, updatedArgs []string) []string { pAddr := "" pPort := "" +======= +func handleGRPC(args []string, updatedArgs []string) []string { + var newUpdatedArgs []string + + var addr string + + var port string +>>>>>>> 77db80cc9 (Added script to generate config.toml fromstart.sh (#518)) newUpdatedArgs = append(newUpdatedArgs, updatedArgs...) for i, val := range args { +<<<<<<< HEAD if strings.Contains(val, "metrics.addr") && strings.HasPrefix(val, "-") { mAddr = args[i+1] } @@ -481,6 +491,19 @@ func handlePrometheus(args []string, updatedArgs []string) []string { newUpdatedArgs = append(newUpdatedArgs, "--metrics.prometheus-addr") newUpdatedArgs = append(newUpdatedArgs, pAddr+":"+pPort) } +======= + if strings.Contains(val, "pprof.addr") && strings.Contains(val, "-") { + addr = args[i+1] + } + + if strings.Contains(val, "pprof.port") && strings.Contains(val, "-") { + port = args[i+1] + } + } + + newUpdatedArgs = append(newUpdatedArgs, "--grpc.addr") + newUpdatedArgs = append(newUpdatedArgs, addr+":"+port) +>>>>>>> 77db80cc9 (Added script to generate config.toml fromstart.sh (#518)) return newUpdatedArgs } @@ -673,7 +696,11 @@ func main() { outOfDateFlags := checkFlag(allFlags, flagsToCheck) updatedArgs := updateArgsClean(args, outOfDateFlags) updatedArgs = updateArgsAdd(updatedArgs) +<<<<<<< HEAD updatedArgs = handlePrometheus(args, updatedArgs) +======= + updatedArgs = handleGRPC(args, updatedArgs) +>>>>>>> 77db80cc9 (Added script to generate config.toml fromstart.sh (#518)) if temp == notYet { updatedArgs = append(updatedArgs, ignoreForNow...) diff --git a/scripts/getconfig.sh b/scripts/getconfig.sh new file mode 100755 index 0000000000..26d5d0138c --- /dev/null +++ b/scripts/getconfig.sh @@ -0,0 +1,87 @@ +#!/usr/bin/env sh + +# Instructions: +# Execute `./getconfig.sh`, and follow the instructions displayed on the terminal +# The `*-config.toml` file will be created in the same directory as start.sh +# It is recommended to check the flags generated in config.toml + + +read -p "* Path to start.sh: " startPath +# check if start.sh is present +if [[ ! -f $startPath ]] +then + echo "Error: start.sh do not exist." + exit 1 +fi +read -p "* Your validator address (e.g. 0xca67a8D767e45056DC92384b488E9Af654d78DE2), or press Enter to skip if running a sentry node: " ADD + +echo "\nThank you, your inputs are:" +echo "Path to start.sh: "$startPath +echo "Address: "$ADD + +confPath=${startPath%.sh}"-config.toml" +echo "Path to the config file: "$confPath +# check if config.toml is present +if [[ -f $confPath ]] +then + echo "WARN: config.toml exists, data will be overwritten." +fi + +tmpDir="$(mktemp -d -t ./temp-dir-XXXXXXXXXXX || oops "Can't create temporary directory")" +cleanup() { + rm -rf "$tmpDir" +} +trap cleanup EXIT INT QUIT TERM + +# SHA1 hash of `tempStart` -> `3305fe263dd4a999d58f96deb064e21bb70123d9` +sed 's/bor --/go run getconfig.go notYet --/g' $startPath > $tmpDir/3305fe263dd4a999d58f96deb064e21bb70123d9.sh +chmod +x $tmpDir/3305fe263dd4a999d58f96deb064e21bb70123d9.sh +$tmpDir/3305fe263dd4a999d58f96deb064e21bb70123d9.sh $ADD +rm $tmpDir/3305fe263dd4a999d58f96deb064e21bb70123d9.sh + + +sed -i '' "s%*%'*'%g" ./temp + +# read the flags from `./temp` +dumpconfigflags=$(head -1 ./temp) + +# run the dumpconfig command with the flags from `./temp` +command="bor dumpconfig "$dumpconfigflags" > "$confPath +bash -c "$command" + +rm ./temp + +if [[ -f ./tempStaticNodes.json ]] +then + echo "JSON StaticNodes found" + staticnodesjson=$(head -1 ./tempStaticNodes.json) + sed -i '' "s%static-nodes = \[\]%static-nodes = \[\"${staticnodesjson}\"\]%" $confPath + rm ./tempStaticNodes.json +elif [[ -f ./tempStaticNodes.toml ]] +then + echo "TOML StaticNodes found" + staticnodestoml=$(head -1 ./tempStaticNodes.toml) + sed -i '' "s%static-nodes = \[\]%static-nodes = \[\"${staticnodestoml}\"\]%" $confPath + rm ./tempStaticNodes.toml +else + echo "neither JSON nor TOML StaticNodes found" +fi + +if [[ -f ./tempTrustedNodes.toml ]] +then + echo "TOML TrustedNodes found" + trustednodestoml=$(head -1 ./tempTrustedNodes.toml) + sed -i '' "s%trusted-nodes = \[\]%trusted-nodes = \[\"${trustednodestoml}\"\]%" $confPath + rm ./tempTrustedNodes.toml +else + echo "neither JSON nor TOML TrustedNodes found" +fi + +# comment flags in $configPath that were not passed through $startPath +# SHA1 hash of `tempStart` -> `3305fe263dd4a999d58f96deb064e21bb70123d9` +sed "s%bor --%go run getconfig.go ${confPath} --%" $startPath > $tmpDir/3305fe263dd4a999d58f96deb064e21bb70123d9.sh +chmod +x $tmpDir/3305fe263dd4a999d58f96deb064e21bb70123d9.sh +$tmpDir/3305fe263dd4a999d58f96deb064e21bb70123d9.sh $ADD +rm $tmpDir/3305fe263dd4a999d58f96deb064e21bb70123d9.sh + +exit 0