big update to auto-fix script, and saved backup of old version in auto-fix-qortal-oldVersion.sh

This commit is contained in:
crowetic 2024-10-19 17:03:58 -07:00
parent de8f9aaeb6
commit 866f597e40

View File

@ -11,6 +11,20 @@ CYAN='\033[0;36m' # Cyan
WHITE='\033[0;37m' # White
NC='\033[0m' # No Color
# Function to update the script
initial_update() {
if [ ! -f ~/auto_fix_updated ]; then
echo "${YELLOW}Checking for the latest version of the script...${NC}\n"
curl -L -o ~/auto-fix-qortal.sh https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/auto-fix-qortal.sh
chmod +x ~/auto-fix-qortal.sh
echo "${GREEN}Script updated. Restarting...${NC}\n"
touch ~/auto_fix_updated
exec ~/auto-fix-qortal.sh
else
check_internet
fi
}
check_internet() {
echo "${YELLOW} Checking internet connection ${NC}\n"
@ -38,14 +52,17 @@ else
fi
}
check_for_pi() {
if command -v raspi-config >/dev/null 2>&1; then
echo "${YELLOW} Raspberry Pi machine detected, checking for 32bit pi...${NC}\n"
echo "${YELLOW} Raspberry Pi machine detected, checking for 32bit or 64bit pi...${NC}\n"
if [ "$(uname -m | grep 'armv7l')" != "" ]; then
echo "${WHITE} 32bit ARM detected, using ARM 32bit compatible modified start script${NC}\n"
32_BIT_PI_DETECTED=true
else
echo "${WHITE} 64bit ARM detected, proceeding accordingly...${NC}\n"
64_BIT_PI_DETECTED=true
fi
curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/start-modified-memory-args.sh
curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/auto-fix-cron
crontab auto-fix-cron
@ -53,36 +70,35 @@ if command -v raspi-config >/dev/null 2>&1 ; then
mv start-modified-memory-args.sh ~/qortal/start.sh
check_qortal
else
echo "${WHITE} Machine is not ARM 32bit, checking RAM amount and adding correct start script...${NC}\n"
totalm=$(free -m | awk '/^Mem:/{print $2}')
echo "${YELLOW} configuring auto-fix cron...${NC}\n"
curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/auto-fix-cron
crontab auto-fix-cron
rm -rf auto-fix-cron
if [ "$totalm" -le 6000 ]; then
echo "${WHITE} 4GB 64bit pi detected, grabbing correct start script and continuing...${NC}\n"
curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/4GB-start.sh && mv 4GB-start.sh ~/qortal/start.sh && chmod +x ~/qortal/start.sh
check_qortal
else
echo "${WHITE} 8GB 64bit pi detected, grabbing correct start script and continuing...${NC}\n"
curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/start-6001-to-16000m.sh && mv start-6001-to-16000m.sh ~/qortal/start.sh && chmod +x ~/qortal/start.sh
check_qortal
fi
fi
else echo "${YELLOW} Not a Raspberry pi machine, continuing...${NC}\n"
echo "${YELLOW} Not a Raspberry pi machine, continuing...${NC}\n"
check_memory
fi
}
check_memory() {
totalm=$(free -m | awk '/^Mem:/{print $2}')
echo "${YELLOW} Checking system RAM ... $totalm System RAM ... Configuring system for optimal RAM settings...${NC}\n"
if [ "$totalm" -le 6000 ]; then
echo "${WHITE} Machine has less than 6GB of RAM, Downloading correct start script for your configuration...${NC}\n"
curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/4GB-start.sh && mv 4GB-start.sh ~/qortal/start.sh && chmod +x ~/qortal/start.sh
elif [ "$totalm" -ge 6001 ] && [ "$totalm" -le 16000 ]; then
echo "${WHITE} Machine has between 6GB and 16GB of RAM, Downloading correct start script for your configuration...${NC}\n"
curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/start-6001-to-16000m.sh && mv start-6001-to-16000m.sh ~/qortal/start.sh && chmod +x ~/qortal/start.sh
else
echo "${WHITE} Machine has more than 16GB of RAM, using high-RAM start script and continuing...${NC}\n"
curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/start-high-RAM.sh && mv start-high-RAM.sh ~/qortal/start.sh && chmod +x ~/qortal/start.sh
fi
check_qortal
}
check_qortal() {
echo "${YELLOW} Checking the version of qortal on local machine VS the version on github... ${NC}\n"
core_running=$(curl -s localhost:12391/admin/status)
if [ -z ${core_running} ]; then
echo "${RED} CORE DOES NOT SEEM TO BE RUNING, WAITING 1 MINUTE IN CASE IT IS STILL STARTING UP... ${NC}\n"
echo "${RED} CORE DOES NOT SEEM TO BE RUNNING, WAITING 1 MINUTE IN CASE IT IS STILL STARTING UP... ${NC}\n"
sleep 60
fi
@ -91,16 +107,165 @@ REMOTE_VERSION=$(curl -s "https://api.github.com/repos/qortal/qortal/releases/la
if [ "$LOCAL_VERSION" -ge "$REMOTE_VERSION" ]; then
echo "${GREEN} Local version is higher than or equal to the remote version, no qortal updates needed... continuing...${NC}\n"
check_for_GUI
check_peer_count
else
check_hash_update_qortal
fi
}
check_peer_count() {
echo "${YELLOW} Checking peer count... ${NC}\n"
# Check if jq is installed
if command -v jq >/dev/null 2>&1; then
# Use jq to parse the number of connections from admin/status call
peer_count=$(curl -s localhost:12391/admin/status | jq '.numberOfConnections')
else
# Use curl and line count if jq is not installed
peer_data=$(curl -s localhost:12391/peers)
line_count=$(echo "$peer_data" | wc -l)
if [ "$line_count" -gt 20 ]; then
peer_count=20 # Set to a reasonable value indicating peers are present
else
peer_count=$line_count
fi
fi
if [ "$peer_count" -lt 3 ]; then
echo "${YELLOW} Peer count is low, waiting 10 seconds and trying again...${NC}\n"
sleep 10
# Repeat the check after waiting
if command -v jq >/dev/null 2>&1; then
peer_count=$(curl -s localhost:12391/admin/status | jq '.numberOfConnections')
else
peer_data=$(curl -s localhost:12391/peers)
line_count=$(echo "$peer_data" | wc -l)
if [ "$line_count" -gt 20 ]; then
peer_count=20
else
peer_count=$line_count
fi
fi
if [ "$peer_count" -lt 3 ]; then
echo "${RED} Peer count continues to be low (${peer_count}), checking for 0 peers...${NC}${YELLOW}\n"
sleep 5
# Final check
if command -v jq >/dev/null 2>&1; then
peer_count=$(curl -s localhost:12391/admin/status | jq '.numberOfConnections')
else
peer_data=$(curl -s localhost:12391/peers)
line_count=$(echo "$peer_data" | wc -l)
if [ "$line_count" -gt 20 ]; then
peer_count=20
else
peer_count=$line_count
fi
fi
if [ "$peer_count" -eq 0 ]; then
echo "${RED} Peer count is 0, executing settings modifications, blocking Chinese peers, and applying iptables-based rate limits...${NC}\n"
zero_peer_settings_mod
fi
fi
else
check_for_GUI
fi
}
zero_peer_settings_mod() {
echo "${YELLOW} Modifying settings for zero peers detected...${NC}\n"
mkdir -p ~/backups/qortal-settings
BACKUP_FILE=~/backups/qortal-settings/settings-$(date +%Y%m%d%H%M%S).json
cp ~/qortal/settings.json "$BACKUP_FILE"
if command -v jq >/dev/null 2>&1; then
# Using jq to modify JSON if available
if jq -e '.allowConnectionsWithOlderPeerVersions' ~/qortal/settings.json >/dev/null; then
jq '.allowConnectionsWithOlderPeerVersions = false' ~/qortal/settings.json > tmp.$$.json && mv tmp.$$.json ~/qortal/settings.json
else
jq '. + {"allowConnectionsWithOlderPeerVersions": false}' ~/qortal/settings.json > tmp.$$.json && mv tmp.$$.json ~/qortal/settings.json
fi
if jq -e '.minPeerVersion' ~/qortal/settings.json >/dev/null; then
jq '.minPeerVersion = "4.6.0"' ~/qortal/settings.json > tmp.$$.json && mv tmp.$$.json ~/qortal/settings.json
else
jq '. + {"minPeerVersion": "4.6.0"}' ~/qortal/settings.json > tmp.$$.json && mv tmp.$$.json ~/qortal/settings.json
fi
# Verify the JSON structure is valid
if ! jq empty ~/qortal/settings.json >/dev/null 2>&1; then
echo "${RED} Error: settings.json is invalid after modifications. Restoring backup... ${NC}\n"
cp "$BACKUP_FILE" ~/qortal/settings.json
return 1
fi
else
# Fallback: using sed if jq is not available
echo "${YELLOW} jq is not installed, using sed for settings modifications...${NC}\n"
# Ensure settings.json modifications
if ! grep -q '"allowConnectionsWithOlderPeerVersions"' ~/qortal/settings.json; then
sed -i '/^{/a \ "allowConnectionsWithOlderPeerVersions": false,' ~/qortal/settings.json
else
sed -i 's/"allowConnectionsWithOlderPeerVersions":.*/"allowConnectionsWithOlderPeerVersions": false,/' ~/qortal/settings.json
fi
if ! grep -q '"minPeerVersion"' ~/qortal/settings.json; then
sed -i '/^{/a \ "minPeerVersion": "4.6.0",' ~/qortal/settings.json
else
sed -i 's/"minPeerVersion":.*/"minPeerVersion": "4.6.0",/' ~/qortal/settings.json
fi
# Validate JSON format
if ! grep -q '}' ~/qortal/settings.json; then
echo "}" >> ~/qortal/settings.json
fi
# Ensure the last line does not end with a comma
sed -i ':a;N;$!ba;s/,\n}/\n}/' ~/qortal/settings.json
fi
# Restart Qortal and verify
block_china
cd qortal
./stop.sh
sleep 45
./start.sh
cd
# Verify if Qortal started correctly
sleep 240
core_status=$(curl -s localhost:12391/admin/status)
if [ -z "$core_status" ]; then
echo "${RED} Qortal did not start correctly, retrying...${NC}\n"
sleep 120
core_status=$(curl -s localhost:12391/admin/status)
if [ -z "$core_status" ]; then
echo "${RED} Qortal still did not start correctly, restoring previous settings...${NC}\n"
cp "$BACKUP_FILE" ~/qortal/settings.json
bash ~/qortal/stop.sh
sleep 30
killall -9 java
bash ~/qortal/start.sh
fi
fi
check_for_GUI
}
block_china() {
echo "${YELLOW} Blocking Chinese peers and applying rate-limiting to Qortal P2P port...${NC}\n"
curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/refs/heads/main/block-china.sh && chmod +x block-china.sh && ./block-china.sh
}
check_hash_update_qortal() {
echo "${RED}API-call-based version checking FAILED${NC}${YELLOW}. ${NC}${CYAN}Proceeding to HASH CHECK${NC}${YELLOW}, checking hash of qortal.jar on local machine VS newest released qortal.jar on github and updating your qortal.jar if needed... ${NC}\n"
cd ~/qortal || exit
cd ~/qortal
md5sum qortal.jar > "local.md5"
cd
echo "${CYAN} Grabbing newest released jar to check hash... ${NC}\n"
@ -112,11 +277,11 @@ REMOTE=$(cat ~/remote.md5)
if [ "$LOCAL" = "$REMOTE" ]; then
echo "${CYAN} Hash check says your Qortal core is UP-TO-DATE, checking environment... ${NC}\n"
check_for_GUI
check_peer_count
exit 1
else
echo "${RED} Hash check confirmed your qortal core is OUTDATED, ${NC}${YELLOW}updating, bootstrapping, and starting qortal...then checking environment and updating scripts... ${NC}\n"
cd qortal
cd ~/qortal
killall -9 java
sleep 3
rm -rf db log.t* qortal.log run.log run.pid qortal.jar
@ -126,14 +291,17 @@ else
./start.sh
cd
check_for_GUI_already_bootstrapped
check_peer_count
fi
}
check_for_GUI() {
if [ -n "$DISPLAY" ]; then
echo "${CYAN} Machine is logged in via GUI, setting up auto-fix-visible for GUI-based machines... ${NC}\n"
if [ "$32_BIT_PI_DETECTED" = true ] || [ "$64_BIT_PI_DETECTED" = true ]; then
echo "${YELLOW} Pi machine detected with GUI, skipping autostart setup for GUI and setting cron jobs instead...${NC}\n"
setup_pi_cron
else
echo "${YELLOW} Setting up auto-fix-visible on GUI-based system... first, creating new crontab entry without auto-fix-startup... ${NC}\n"
sleep 2
curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/auto-fix-GUI-cron
@ -148,43 +316,28 @@ if [ -n "$DISPLAY" ]; then
echo "${CYAN} continuing to verify node height...${NC}\n"
check_height
fi
else
echo "${YELLOW} Non-GUI system detected, skipping 'auto-fix-visible' setup... ${NC}${CYAN}configuring cron then checking node height... ${NC}\n"
setup_pi_cron
fi
}
else echo "${YELLOW} Non-GUI system detected, skipping 'auto-fix-visible' setup... ${NC}${CYAN}configuring cron then checking node height... ${NC}\n"
curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/auto-fix-cron-new
crontab auto-fix-cron-new
rm -rf auto-fix-cron-new
setup_pi_cron() {
echo "${YELLOW} Setting up cron jobs for Raspberry Pi or headless machines... ${NC}\n"
mkdir -p ~/backups/cron-backups
crontab -l > ~/backups/cron-backups/crontab-backup-$(date +%Y%m%d%H%M%S)
curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/refs/heads/main/auto-fix-cron
crontab auto-fix-cron
rm -rf auto-fix-cron
# Check if the cron entries already exist, if not add them
#crontab -l | grep -q "@reboot sleep 6 && ~/start-qortal.sh" || (crontab -l ; echo "@reboot sleep 6 && ~/start-qortal.sh") | crontab -
#crontab -l | grep -q "@reboot sleep 420 && ~/auto-fix-qortal.sh" || (crontab -l ; echo "@reboot sleep 420 && ~/auto-fix-qortal.sh") | crontab -
#crontab -l | grep -q "1 1 */3 * * ~/auto-fix-qortal.sh" || (crontab -l ; echo "1 1 */3 * * ~/auto-fix-qortal.sh") | crontab -
check_height
fi
}
check_for_GUI_already_bootstrapped() {
if [ -n "$DISPLAY" ]; then
echo "${CYAN} Machine is logged in via GUI, setting up auto-fix-visible for GUI-based machines... ${NC}\n"
echo "${YELLOW} Setting up auto-fix-visible on GUI-based system... first, creating new crontab entry without auto-fix-startup... ${NC}\n"
sleep 2
curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/auto-fix-GUI-cron
crontab auto-fix-GUI-cron
rm -rf auto-fix-GUI-cron
echo "${YELLOW} Setting up new ${NC}\n ${WHITE} 'auto-fix-qortal-GUI.desktop' ${NC}\n ${YELLOW} file for GUI-based machines to run 7 min after startup in a visual fashion. Entry in 'startup' will be called ${NC}\n ${WHITE} 'auto-fix-visible' ${NC}\n"
curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/auto-fix-qortal-GUI.desktop
mkdir -p ~/.config/autostart
cp auto-fix-qortal-GUI.desktop ~/.config/autostart
rm -rf ~/auto-fix-qortal-GUI.desktop
echo "${YELLOW} Your machine will now run 'auto-fix-qortal.sh' script in a fashion you can SEE, 7 MIN AFTER YOU REBOOT your machine. The normal 'background' process for auto-fix-qortal will continue as normal.${NC}\n"
echo "${CYAN} continuing to verify node height...${NC}\n"
update_script
else echo "${YELLOW} Non-GUI system detected, skipping 'auto-fix-visible' setup ${NC}\n"
curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/auto-fix-cron-new
crontab auto-fix-cron-new
rm -rf auto-fix-cron-new
update_script
fi
}
check_height() {
local_height=$(curl -sS "http://localhost:12391/blocks/height")
@ -200,14 +353,13 @@ if [ -f auto_fix_last_height.txt ]; then
echo "${RED} block height still has not changed... forcing bootstrap... ${NC}\n"
force_bootstrap
fi
fi
fi
fi
if [ -z ${local_height} ]; then
echo "${RED} local API call for block height returned empty, IS YOUR QORTAL CORE RUNNING? ${NC}\n"
echo "${RED} if this doesn't work, then the script encountered an issue that it isn't fully equipped to handle, it may fix it upon a restart, TRY RESTARTING THE COMPUTER and WAITING 15 MINUTES... ${NC}\n"
echo "${RED} if this doesn't work, then the script encountered an issue that it isn't fully equipped to handle, it may fix it upon a restart, TRY RESTARTING THE COMPUTER and WAITING 30 MINUTES... ${NC}\n"
no_local_height
else
echo ${local_height} > auto_fix_last_height.txt
@ -216,7 +368,6 @@ fi
remote_height_checks
}
no_local_height() {
# height checks failed, is qortal running?
# make another action here...
@ -268,17 +419,16 @@ echo "${GREEN} Checking if Qortal started correctly... ${NC}\n"
local_height_check=$(curl -sS "http://localhost:12391/blocks/height")
node_works=$(curl -sS "http://localhost:12391/admin/status")
if [ -n ${local_height_check} ]; then
if [ -n "$local_height_check" ]; then
echo "${GREEN} local height is ${NC}${CYAN} ${local_height_check}${NC}\n"
echo "${GREEN} node is GOOD, re-trying height check and continuing...${NC}\n"
check_height_2
check_height
else
echo "${RED} starting Qortal Core FAILED... script will exit now until future updates add additional features...sorry the script couldn't resolve your issues! It will update automatically if you have it configured to run automatically! ${NC}${CYAN} It is possible that the script will fix the issue IF YOU RESTART YOUR COMPUTER AND WAIT 15 MINUTES...${NC}\n"
update_script
fi
}
remote_height_checks() {
height_api_qortal_org=$(curl -sS --connect-timeout 10 "https://api.qortal.org/blocks/height")
height_qortal_link=$(curl -sS --connect-timeout 10 "https://qortal.link/blocks/height")
@ -306,43 +456,9 @@ remote_height_checks() {
fi
}
check_height_2() {
local_height=$(curl -sS "http://localhost:12391/blocks/height")
if [ -f auto_fix_last_height.txt ]; then
previous_local_height=$(cat auto_fix_last_height.txt)
if [ -n ${previous_local_height} ]; then
if [ "${local_height}" = "${previous_local_height}" ]; then
echo "${RED} local height has not changed since previous script run... waiting 3 minutes and checking height again, if height still hasn't changed, forcing bootstrap... ${NC}\n"
sleep 188
checked_height=$(curl "localhost:12391/blocks/height")
sleep 2
if [ "${checked_height}" = "${previous_local_height}" ]; then
echo "${RED} block height still has not changed... forcing bootstrap... ${NC}\n"
force_bootstrap
fi
fi
fi
fi
if [ -z ${local_height} ]; then
echo "${RED} SECOND height check failed, unsure what is going on, but a restart of the node and waiting may fix it... ${NC}\n"
echo "${CYAN} TRY RESTARTING THE COMPUTER and WAITING 15 MINUTES... ${NC}\n"
update_script
else
echo ${local_height} > auto_fix_last_height.txt
fi
remote_height_checks
}
force_bootstrap() {
echo "${RED} height check found issues, forcing bootstrap... ${NC}\n"
cd qortal
cd ~/qortal
killall -9 java
sleep 3
rm -rf db log.t* qortal.log run.log run.pid
@ -352,7 +468,6 @@ cd
update_script
}
update_script() {
echo "${YELLOW}Updating script to newest version and backing up old one...${NC}\n"
mkdir -p ~/qortal/new-scripts
@ -365,29 +480,10 @@ curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/aut
chmod +x auto-fix-qortal.sh
cd
cp ~/qortal/new-scripts/auto-fix-qortal.sh .
rm ~/auto_fix_updated
echo "${YELLOW} Auto-fix script run complete.${NC}\n"
sleep 5
exit
}
initial_update
check_memory(){
totalm=$(free -m | awk '/^Mem:/{print $2}')
echo "${YELLOW} Checking system RAM ... $totalm System RAM ... Configuring system for optimal RAM settings...${NC}\n"
if [ "$totalm" -le 6000 ]; then
echo "${WHITE} Machine has less than 6GB of RAM, Downloading correct start script for your configuration...${NC}\n"
curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/4GB-start.sh && mv 4GB-start.sh ~/qortal/start.sh && chmod +x ~/qortal/start.sh
elif [ "$totalm" -ge 6001 ] && [ "$totalm" -le 16000 ]; then
echo "${WHITE} Machine has between 6GB and 16GB of RAM, Downloading correct start script for your configuration...${NC}\n"
curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/start-6001-to-16000m.sh && mv start-6001-to-16000m.sh ~/qortal/start.sh && chmod +x ~/qortal/start.sh
else echo "${WHITE} Machine has more than 16GB of RAM, using high-RAM start script and continuing...${NC}\n"
curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/start-high-RAM.sh && mv start-high-RAM.sh ~/qortal/start.sh && chmod +x ~/qortal/start.sh
fi
check_qortal
}
check_internet