-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update to 0.9.3
- Loading branch information
Showing
5 changed files
with
226 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
## Version 0.9.3 | ||
#!/bin/bash | ||
|
||
## Check for config file | ||
CONFIG_FILE="mrv_config.json" | ||
|
||
## Read config file | ||
CONFIGFILE=$(cat "$CONFIG_FILE") | ||
SECRET=$( echo "$CONFIGFILE" | jq -r '.secret') | ||
PRT=$( echo "$CONFIGFILE" | jq -r '.port') | ||
PRTS=$( echo "$CONFIGFILE" | jq -r '.https_port') | ||
PBK=$( echo "$CONFIGFILE" | jq -r '.pbk') | ||
SERVERS=() | ||
### Get servers array | ||
SIZE=$( echo "$CONFIGFILE" | jq '.manage_servers | length') | ||
i=0 | ||
|
||
while [ $i -le $((SIZE-1)) ] | ||
do | ||
SERVERS[$i]=$(echo "$CONFIGFILE" | jq -r --argjson i $i '.manage_servers[$i]') | ||
i=$(( i + 1 )) | ||
done | ||
### | ||
######################### | ||
|
||
## Set colors | ||
RED=$(tput setaf 1) | ||
GREEN=$(tput setaf 2) | ||
YELLOW=$(tput setaf 3) | ||
CYAN=$(tput setaf 6) | ||
RESETCOLOR=$(tput sgr0) | ||
|
||
FORGING=0 | ||
PREVIOUSFORGING=0 | ||
|
||
## Log start of script | ||
date +"%Y-%m-%d %H:%M:%S || ${GREEN}Starting MrV's management script${RESETCOLOR}" | ||
|
||
while true; do | ||
SERVERSINFO=() | ||
SERVERSFORGING=() | ||
SERVERSCONSENSUS=() | ||
NUM=0 | ||
HIGHHEIGHT=0 | ||
|
||
## Get info on all servers | ||
for SERVER in "${SERVERS[@]}" | ||
do | ||
## Get next server's height and consensus | ||
SERVERINFO=$(curl --connect-timeout 2 -s -S "http://"$SERVER""$PRT"/api/loader/status/sync") | ||
if [[ -z "$SERVERINFO" ]]; ## If null, try one more time to get server status | ||
then | ||
SERVERINFO=$(curl --connect-timeout 2 -s -S "http://"$SERVER""$PRT"/api/loader/status/sync") | ||
fi | ||
HEIGHT=$( echo "$SERVERINFO" | jq '.height') | ||
CONSENSUS=$( echo "$SERVERINFO" | jq '.consensus') | ||
|
||
## Check if server is off | ||
if ! [[ "$HEIGHT" =~ ^[0-9]+$ ]]; | ||
then | ||
date +"%Y-%m-%d %H:%M:%S || ${RED}$SERVER is off?${RESETCOLOR}" | ||
HEIGHT="0" | ||
FORGE="false" | ||
CONSENSUS="0" | ||
else | ||
## Get forging status of server | ||
FORGE=$(curl --connect-timeout 2 -s "http://"$SERVER""$PRT"/api/delegates/forging/status?publicKey="$PBK| jq '.enabled') | ||
if [[ -z "$FORGE" ]]; ## If null, try one more time to get forging status | ||
then | ||
FORGE=$(curl --connect-timeout 2 -s "http://"$SERVER""$PRT"/api/delegates/forging/status?publicKey="$PBK| jq '.enabled') | ||
fi | ||
if [[ "$FORGE" == "true" ]]; ## Current server forging | ||
then | ||
FORGING=$NUM | ||
fi | ||
fi | ||
|
||
## Find highest height | ||
if [ "$HEIGHT" -gt "$HIGHHEIGHT" ]; | ||
then | ||
HIGHHEIGHT=$HEIGHT | ||
fi | ||
|
||
SERVERSINFO[$NUM]=$HEIGHT | ||
SERVERSFORGING[$NUM]=$FORGE | ||
SERVERSCONSENSUS[$NUM]=$CONSENSUS | ||
date +"%Y-%m-%d %H:%M:%S || $SERVER - Height:$HEIGHT - Consensus:$CONSENSUS - Forging:$FORGE" | ||
|
||
((NUM++)) | ||
done | ||
|
||
NUM=0 | ||
## Check if any servers are forging | ||
if ! [[ ${SERVERSFORGING[*]} =~ "true" ]]; | ||
then | ||
for SERVER in "${SERVERS[@]}" | ||
do | ||
DIFF=$(( HIGHHEIGHT - ${SERVERSINFO[$NUM]} )) | ||
if [ "$DIFF" -lt "4" ] && [ "${SERVERSCONSENSUS[$NUM]}" -gt "50" ]; | ||
then | ||
date +"%Y-%m-%d %H:%M:%S || ${YELLOW}No node forging. Starting on $SERVER${RESETCOLOR}" | ||
curl -s -S --connect-timeout 3 -k -H "Content-Type: application/json" -X POST -d '{"secret":"'"$SECRET"'"}' https://"$SERVER""$PRTS"/api/delegates/forging/enable | ||
PREVIOUSFORGING=$NUM | ||
break ## Exit loop once we find the first server at an acceptable height and consensus | ||
fi | ||
((NUM++)) | ||
done | ||
continue ## Start back at top of loop, now that one server is forging | ||
fi | ||
|
||
## Check that only one server is forging | ||
FORGINGCOUNT=0 | ||
for FSTATUS in ${SERVERSFORGING[*]}; do | ||
if [[ $FSTATUS =~ true ]]; then | ||
(( FORGINGCOUNT++ )) | ||
fi | ||
done | ||
if [ "$FORGINGCOUNT" -gt "1" ] | ||
then | ||
date +"%Y-%m-%d %H:%M:%S || ${RED}Multiple servers forging!${RESETCOLOR}" | ||
for SERVER in "${SERVERS[@]}" | ||
do | ||
## Disable forging on all servers first | ||
curl -s -S --connect-timeout 3 -k -H "Content-Type: application/json" -X POST -d '{"secret":"'"$SECRET"'"}' https://"$SERVER""$PRTS"/api/delegates/forging/disable | ||
done | ||
for index in "${!SERVERS[@]}" | ||
do | ||
DIFF=$(( HIGHHEIGHT - ${SERVERSINFO[$index]} )) | ||
if [ "$DIFF" -lt "4" ] && [ "${SERVERSCONSENSUS[$NUM]}" -gt "50" ]; | ||
then | ||
curl -s -S --connect-timeout 3 -k -H "Content-Type: application/json" -X POST -d '{"secret":"'"$SECRET"'"}' https://"${SERVERS[$index]}""$PRTS"/api/delegates/forging/enable | ||
PREVIOUSFORGING=$index | ||
date +"%Y-%m-%d %H:%M:%S || ${CYAN}Setting forging to ${SERVERS[$index]}${RESETCOLOR}" | ||
break ## Exit loop once we find the first server at an acceptable height and consensus | ||
fi | ||
done | ||
fi | ||
|
||
if [[ $PREVIOUSFORGING != "$FORGING" ]]; | ||
then | ||
date +"%Y-%m-%d %H:%M:%S || ${YELLOW}Different server forging! Previous=${SERVERS[$PREVIOUSFORGING]},Current=${SERVERS[$FORGING]}. Waiting 30 seconds${RESETCOLOR}" | ||
sleep 24 | ||
else ## Same server still forging, check that everything still looks good on it | ||
date +"%Y-%m-%d %H:%M:%S || Highest Height: $HEIGHT" | ||
|
||
##Check that it is the main server forging | ||
if [ "$FORGING" != "0" ]; | ||
then | ||
date +"%Y-%m-%d %H:%M:%S || ${YELLOW}Main server not forging${RESETCOLOR}" | ||
DIFF=$(( HIGHHEIGHT - ${SERVERSINFO[0]} )) | ||
if [ "$DIFF" -lt "4" ] && [ "${SERVERSCONSENSUS[0]}" -gt "50" ]; | ||
then | ||
curl -s -S --connect-timeout 3 -k -H "Content-Type: application/json" -X POST -d '{"secret":"'"$SECRET"'"}' https://"${SERVERS[$FORGING]}""$PRTS"/api/delegates/forging/disable | ||
curl -s -S --connect-timeout 3 -k -H "Content-Type: application/json" -X POST -d '{"secret":"'"$SECRET"'"}' https://"${SERVERS[0]}""$PRTS"/api/delegates/forging/enable | ||
PREVIOUSFORGING=0 | ||
date +"%Y-%m-%d %H:%M:%S || ${CYAN}Setting forging to back to main server: ${SERVERS[0]}${RESETCOLOR}" | ||
continue ## Exit loop once if we can set forging back to main server | ||
fi | ||
fi | ||
|
||
DIFF=$(( HIGHHEIGHT - ${SERVERSINFO[$FORGING]} )) | ||
if [ "$DIFF" -gt "3" ] | ||
then | ||
date +"%Y-%m-%d %H:%M:%S || ${RED}${SERVERS[$FORGING]} too low of height.${RESETCOLOR}" | ||
for SERVER in "${SERVERS[@]}" | ||
do | ||
## Disable forging on all servers first | ||
curl -s -S --connect-timeout 3 -k -H "Content-Type: application/json" -X POST -d '{"secret":"'"$SECRET"'"}' https://"$SERVER""$PRTS"/api/delegates/forging/disable | ||
done | ||
for index in "${!SERVERS[@]}" | ||
do | ||
DIFF=$(( HIGHHEIGHT - ${SERVERSINFO[$index]} )) | ||
if [ "$DIFF" -lt "4" ] && [ "${SERVERSCONSENSUS[$NUM]}" -gt "50" ]; | ||
then | ||
curl -s -S --connect-timeout 3 -k -H "Content-Type: application/json" -X POST -d '{"secret":"'"$SECRET"'"}' https://"${SERVERS[$index]}""$PRTS"/api/delegates/forging/enable | ||
FORGING=$index | ||
date +"%Y-%m-%d %H:%M:%S || ${CYAN}Switching to ${SERVERS[$index]}${RESETCOLOR}" | ||
break ## Exit loop once we find the first server at an acceptable height and consensus | ||
fi | ||
done | ||
fi | ||
fi | ||
|
||
|
||
## Record which server was forging before sleep | ||
PREVIOUSFORGING=$FORGING | ||
## Sleep for 6 seconds | ||
sleep 6 | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters