Skip to content

Commit

Permalink
Added script to generate config.toml fromstart.sh (#518)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pratikspatil024 committed Sep 27, 2022
1 parent ce69e71 commit 9390e81
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
9 changes: 9 additions & 0 deletions internal/cli/flagset/flagset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/server/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (<number>=<hash>)",
Usage: "Comma separated block number-to-hash mappings to require for peering (<number>=<hash>)",
Value: &c.cliConfig.RequiredBlocks,
})
f.BoolFlag(&flagset.BoolFlag{
Expand Down
27 changes: 27 additions & 0 deletions scripts/getconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ func updateArgsAdd(args []string) []string {
return args
}

<<<<<<< HEAD
func handlePrometheus(args []string, updatedArgs []string) []string {
var newUpdatedArgs []string

Expand All @@ -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]
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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...)
Expand Down
87 changes: 87 additions & 0 deletions scripts/getconfig.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9390e81

Please sign in to comment.