Skip to content

Commit

Permalink
Update to 0.9.3
Browse files Browse the repository at this point in the history
Update to 0.9.3
  • Loading branch information
mrv777 authored Dec 24, 2016
2 parents 67f8964 + b21d00c commit 1115e1c
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 31 deletions.
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Lisk Delegate Scripts (BETA v0.9.2)
# Lisk Delegate Scripts (BETA v0.9.3)

## Thank you
Thank you to cc001, corsaro, liberspirita, wannabe_RoteBaron, hagie, isabella, Nerigal, doweig, and anyone I might have missed for their help and/or contributions.
Thank you to cc001, corsaro, liberspirita, wannabe_RoteBaron, hagie, isabella, stoner19, punkrock, Nerigal, doweig, and anyone I might have missed for their help and/or contributions.

## Control Script
This is the wrapper script for check_height_and_rebuild.sh and check_consensus.sh. You can run this on all forging servers. You only need to use this script directly and not check_height_and_rebuild.sh or check_consensus.sh. Commands are:
* start -- starts both scripts
* startc -- starts consensus script
* starth -- starts height_rebuild script
* startm -- starts management script
* stop -- stops both scripts
* stopc -- stops consensus script
* stoph -- stops height_rebuild script
Expand Down Expand Up @@ -46,18 +47,18 @@ Compares the height of your 100 connected peers and gets the highest height. Th

## My Management script

### manage3.sh
This script will check the block heights of 3 servers and make sure the one forging is near the top height of the 3, if not it switches to the next server (1->2,2->3,3->1). It also makes sure only one server is forging.

Currently designed to run in a screen on it's own monitoring server.
### manage.sh
**User does not need to directly do anything with this. control.sh interfaces with it automatically**
This script will check the block heights of all listed servers. It will attempt to have one and only one server forging and prefer to have server 1 do the forging. If the forging server swtiches by something other then this script, it will pause for 30 seconds and then check the status of the servers after that.

#### How to run:

1. `sudo apt-get install jq screen`
2. `wget https://raw.githubusercontent.com/mrv777/LiskScripts/master/manage3.sh`
3. `nano manage3.sh` and edit the top variables
4. `screen`
5. `bash manage3.sh`
6. CTRL+A, D to detach the screen
1. `sudo apt-get install jq`
2. `wget https://raw.githubusercontent.com/mrv777/LiskScripts/master/control.sh`
3. `bash control.sh startm` - Start management script

To check the logs and what the script is going:

* `tail -f manage.log`

*Donation Address: 3532362465127676136L*
2 changes: 1 addition & 1 deletion check_height_and_rebuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ local_height() {
if [ "$diff" -gt "4" ]
then
## Thank you doweig for better output formating
date +"%Y-%m-%d %H:%M:%S || ${yellow}Reloading! Local: $CHECKSRV, Highest: $HEIGHT, Diff: $diff{resetColor}"
date +"%Y-%m-%d %H:%M:%S || ${yellow}Reloading! Local: $CHECKSRV, Highest: $HEIGHT, Diff: $diff${resetColor}"
ChangeDirectory ## Make sure we are in the correct directory
bash lisk.sh reload # 0.5.1 often solves short stucks by itself, but reload anyways :)
sleep 20
Expand Down
36 changes: 18 additions & 18 deletions control.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
## control_mrvscripts.sh
## Version 0.9.2
## control.sh
## Version 0.9.3
## Tested with jq 1.5.1 on Ubuntu 16.04.1
#!/bin/bash

SH_FILE="check_height_and_rebuild.sh"
LOG_FILE="heightRebuild.log"
CONSENSUS_SH_FILE="check_consensus.sh"
CONSENSUS_LOG_FILE="consensus.log"
MANAGE_SH_FILE="manage3.sh"
MANAGE_SH_FILE="manage.sh"
MANAGE_LOG_FILE="manage.log"

start_height() {
Expand Down Expand Up @@ -121,26 +121,26 @@ upgrade_consensus() {
nohup bash $CONSENSUS_SH_FILE -S $SRV > $CONSENSUS_SH_FILE 2>&1&
}

start_manage3() {
start_manage() {
if [[ ! -e "$MANAGE_LOG_FILE" ]] ; then
touch "$MANAGE_LOG_FILE"
fi
if [[ ! -e "$MANAGE_SH_FILE" ]] ; then
wget "https://raw.githubusercontent.com/mrv777/LiskScripts/master/manage3.sh"
wget "https://raw.githubusercontent.com/mrv777/LiskScripts/master/manage.sh"
fi

echo "Starting manage3 Script"
echo "Starting Management Script"
nohup bash $MANAGE_SH_FILE -S $SRV > $MANAGE_LOG_FILE 2>&1&
}

check_manage3_running() {
check_manage_running() {
# Check if it is running
if pgrep -fl $MANAGE_SH_FILE > /dev/null
then
echo "Killing manage3 process"
echo "Killing management script process"
pkill -f $MANAGE_SH_FILE -9
else
echo "Manage3 is not currently running"
echo "Management script is not currently running"
fi
}

Expand All @@ -161,12 +161,12 @@ status() {
echo "HeightRebuild is not currently running"
fi

# Check if manage3 is running
# Check if manage.sh is running
if pgrep -fl $MANAGE_SH_FILE > /dev/null
then
echo "Manage3 is running"
echo "Management script is running"
else
echo "Manage3 is not currently running"
echo "Management script is not currently running"
fi
}

Expand All @@ -175,11 +175,11 @@ usage() {
echo "start -- starts consensus & height_rebuild scripts"
echo "startc -- starts consensus script"
echo "starth -- starts height_rebuild script"
echo "startm -- starts manage3 script"
echo "startm -- starts manage script"
echo "stop -- stops all scripts"
echo "stopc -- stops consensus script"
echo "stoph -- stops height_rebuild script"
echo "stopm -- stops manage3 script"
echo "stopm -- stops manage script"
echo "status -- check if scripts are running"
echo "upgrade -- upgrades and runs both scripts"
}
Expand All @@ -200,13 +200,13 @@ case $1 in
start_height
;;
"startm" )
check_manage3_running
start_manage3
check_manage_running
start_manage
;;
"stop" )
check_height_running
check_consensus_running
check_manage3_running
check_manage_running
;;
"stopc" )
check_consensus_running
Expand All @@ -215,7 +215,7 @@ case $1 in
check_height_running
;;
"stopm" )
check_manage3_running
check_manage_running
;;
"status" )
status
Expand Down
189 changes: 189 additions & 0 deletions manage.sh
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
5 changes: 5 additions & 0 deletions mrv_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"lisk_directory":"~/lisk-main",
"pbk":"YOUR PUBLIC KEY",
"secret":"YOUR SECRET",
"manage_servers": [
"xxx.xxx.xxx.xxx",
"xxx.xxx.xxx.xxx",
"xxx.xxx.xxx.xxx"
],
"srv1":"localhost",
"servers": [
"xxx.xxx.xxx.xxx",
Expand Down

0 comments on commit 1115e1c

Please sign in to comment.