diff --git a/auto-fix-qortal.sh b/auto-fix-qortal.sh index a1a2a18..400c002 100644 --- a/auto-fix-qortal.sh +++ b/auto-fix-qortal.sh @@ -1,21 +1,21 @@ #!/bin/sh # Regular Colors -BLACK='\033[0;30m' # Black -RED='\033[0;31m' # Red -GREEN='\033[0;32m' # Green -YELLOW='\033[0;33m' # Yellow -BLUE='\033[0;34m' # Blue -PURPLE='\033[0;35m' # Purple -CYAN='\033[0;36m' # Cyan -WHITE='\033[0;37m' # White -NC='\033[0m' # No Color +BLACK='\033[0;30m' +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[0;33m' +BLUE='\033[0;34m' +PURPLE='\033[0;35m' +CYAN='\033[0;36m' +WHITE='\033[0;37m' +NC='\033[0m' # No Color PI_32_DETECTED=false PI_64_DETECTED=false UPDATED_SETTINGS=false -# Function to update the script +# Function to update the script initially if needed initial_update() { if [ ! -f "${HOME}/auto_fix_updated" ]; then echo "${YELLOW}Checking for the latest version of the script...${NC}\n" @@ -30,26 +30,27 @@ initial_update() { } check_internet() { - echo "${YELLOW} Checking internet connection ${NC}\n" - + echo "${YELLOW}Checking internet connection${NC}\n" INTERNET_STATUS="UNKNOWN" TIMESTAMP=$(date +%s) ping -c 1 -W 0.7 8.8.4.4 > /dev/null 2>&1 if [ $? -eq 0 ]; then + # Internet is UP if [ "$INTERNET_STATUS" != "UP" ]; then echo "${BLUE}Internet connection is UP, continuing${NC}\n $(date +%Y-%m-%dT%H:%M:%S%Z) $(( $(date +%s) - $TIMESTAMP ))" INTERNET_STATUS="UP" rm -rf "${HOME}/Desktop/check-qortal-status.sh" - cd + cd || exit 1 curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/check-qortal-status.sh && mv check-qortal-status.sh "${HOME}/qortal" && chmod +x "${HOME}/qortal/check-qortal-status.sh" curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/start-qortal.sh && chmod +x start-qortal.sh curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/refresh-qortal.sh && chmod +x refresh-qortal.sh check_for_pi fi else + # Internet is DOWN if [ "$INTERNET_STATUS" = "UP" ]; then - echo "${RED}Internet Connection is DOWN, please fix connection and restart device, script will re-run automatically after 7 min.${NC}\n $(date +%Y-%m-%dT%H:%M:%S%Z) $(( $(date +%s) - $TIMESTAMP ))" + echo "${RED}Internet Connection is DOWN, please fix connection and restart device.${NC}\n$(date +%Y-%m-%dT%H:%M:%S%Z) $(( $(date +%s) - $TIMESTAMP ))" INTERNET_STATUS="DOWN" sleep 30 exit 1 @@ -59,10 +60,10 @@ check_internet() { check_for_pi() { if command -v raspi-config >/dev/null 2>&1; then - echo "${YELLOW} Raspberry Pi machine detected, checking for 32bit or 64bit pi...${NC}\n" + echo "${YELLOW}Raspberry Pi machine detected, checking for 32bit or 64bit...${NC}\n" if [ "$(uname -m | grep 'armv7l')" != "" ]; then - echo "${WHITE} 32bit ARM detected, using ARM 32bit compatible modified start script${NC}\n" + echo "${WHITE}32bit ARM detected, using ARM 32bit compatible modified start script${NC}\n" PI_32_DETECTED=true 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 @@ -71,30 +72,28 @@ check_for_pi() { mv start-modified-memory-args.sh "${HOME}/qortal/start.sh" check_qortal else - echo "${WHITE} 64bit ARM detected, proceeding accordingly...${NC}\n" + echo "${WHITE}64bit ARM detected, proceeding accordingly...${NC}\n" PI_64_DETECTED=true check_memory - 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" + echo "${YELLOW}Checking system RAM ... $totalm MB 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" + echo "${WHITE}Machine has less than 6GB of RAM, downloading correct start script...${NC}\n" curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/4GB-start.sh && mv 4GB-start.sh "${HOME}/qortal/start.sh" && chmod +x "${HOME}/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" + echo "${WHITE}Machine has between 6GB and 16GB of RAM, downloading correct start script...${NC}\n" curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/start-6001-to-16000m.sh && mv start-6001-to-16000m.sh "${HOME}/qortal/start.sh" && chmod +x "${HOME}/qortal/start.sh" else - echo "${WHITE} Machine has more than 16GB of RAM, using high-RAM start script and continuing...${NC}\n" + echo "${WHITE}Machine has more than 16GB of RAM, using high-RAM start script...${NC}\n" curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/start-high-RAM.sh && mv start-high-RAM.sh "${HOME}/qortal/start.sh" && chmod +x "${HOME}/qortal/start.sh" fi @@ -102,178 +101,36 @@ check_memory() { } check_qortal() { - echo "${YELLOW} Checking the version of qortal on local machine VS the version on github... ${NC}\n" + echo "${YELLOW}Checking qortal version (local vs remote)...${NC}\n" core_running=$(curl -s localhost:12391/admin/status) - if [ -z ${core_running} ]; then - echo "${RED} CORE DOES NOT SEEM TO BE RUNNING, WAITING 1 MINUTE IN CASE IT IS STILL STARTING UP... ${NC}\n" + if [ -z "$core_running" ]; then + echo "${RED}CORE DOES NOT SEEM TO BE RUNNING, WAITING 1 MINUTE...${NC}\n" sleep 60 fi LOCAL_VERSION=$(curl -s localhost:12391/admin/info | grep -oP '"buildVersion":"qortal-\K[^-]*' | sed 's/-.*//' | tr -d '.') REMOTE_VERSION=$(curl -s "https://api.github.com/repos/qortal/qortal/releases/latest" | grep -oP '"tag_name": "v\K[^"]*' | tr -d '.') - 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 + if [ -n "$LOCAL_VERSION" ] && [ -n "$REMOTE_VERSION" ]; then + if [ "$LOCAL_VERSION" -ge "$REMOTE_VERSION" ]; then + echo "${GREEN}Local version is >= remote version, no qortal updates needed... continuing...${NC}\n" + check_for_GUI + else + check_hash_update_qortal + fi else + # If version checks fail, fallback to hash checking 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=0 - 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=0 - 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=0 - 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 - fi - - check_for_GUI -} - - -zero_peer_settings_mod() { - echo "${YELLOW} this should not be seen... skipping${NC}\n" - check_for_GUI - # Define backup file name - BACKUP_FILE="${HOME}/backups/qortal-settings/settings-$(date +%Y%m%d%H%M%S).json" - - # Create backup folder if not exists and backup settings.json - mkdir -p "${HOME}/backups/qortal-settings" - cp "${HOME}/qortal/settings.json" "$BACKUP_FILE" - - # If jq is installed, use jq to modify settings.json - if command -v jq >/dev/null 2>&1; then - echo "${YELLOW} Using jq to modify settings.json...${NC}\n" - - # Modify or add necessary settings - jq '.allowConnectionsWithOlderPeerVersions = false | .minPeerVersion = "4.6.0"' "${HOME}/qortal/settings.json" > tmp.$$.json && mv tmp.$$.json "${HOME}/qortal/settings.json" - - # Validate the modified JSON - if ! jq empty "${HOME}/qortal/settings.json" >/dev/null 2>&1; then - echo "${RED} Error: settings.json is invalid after modifications. Restoring backup... ${NC}\n" - cp "$BACKUP_FILE" "${HOME}/qortal/settings.json" - return 1 - fi - else - # If jq is not available, fallback to using sed and other text processing - echo "${YELLOW} jq is not installed, using sed for settings modifications...${NC}\n" - - # Ensure settings.json modifications with sed - if ! grep -q '"allowConnectionsWithOlderPeerVersions"' "${HOME}/qortal/settings.json"; then - sed -i '/^{/a \ "allowConnectionsWithOlderPeerVersions": false,' "${HOME}/qortal/settings.json" - else - sed -i 's/"allowConnectionsWithOlderPeerVersions":.*/"allowConnectionsWithOlderPeerVersions": false,/' "${HOME}/qortal/settings.json" - fi - - if ! grep -q '"minPeerVersion"' "${HOME}/qortal/settings.json"; then - sed -i '/^{/a \ "minPeerVersion": "4.6.0",' "${HOME}/qortal/settings.json" - else - sed -i 's/"minPeerVersion":.*/"minPeerVersion": "4.6.0",/' "${HOME}/qortal/settings.json" - fi - - # Validate JSON structure - if ! grep -q '}' "${HOME}/qortal/settings.json"; then - echo "}" >> "${HOME}/qortal/settings.json" - fi - - # Ensure the last line does not end with a comma - sed -i ':a;N;$!ba;s/,\n}/\n}/' "${HOME}/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" "${HOME}/qortal/settings.json" - bash "${HOME}/qortal/stop.sh" - sleep 30 - killall -9 java - bash "${HOME}/qortal/start.sh" - fi - fi - check_for_GUI -} - - -block_china() { - echo "${YELLOW} no longer doing this... shouldn't be seeing this...${NC}\n" -} - 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 "${HOME}/qortal" + echo "${RED}API-based version check failed or outdated. Proceeding to HASH CHECK...${NC}\n" + cd "${HOME}/qortal" || exit 1 md5sum qortal.jar > "local.md5" - cd - echo "${CYAN} Grabbing newest released jar to check hash... ${NC}\n" + cd || exit 1 + echo "${CYAN}Grabbing newest released jar to check hash...${NC}\n" curl -L -O https://github.com/qortal/qortal/releases/latest/download/qortal.jar md5sum qortal.jar > "remote.md5" @@ -281,12 +138,12 @@ check_hash_update_qortal() { REMOTE=$(cat "${HOME}/remote.md5") if [ "$LOCAL" = "$REMOTE" ]; then - echo "${CYAN} Hash check says your Qortal core is UP-TO-DATE, checking environment... ${NC}\n" + echo "${CYAN}Hash check: Qortal core is up-to-date, checking environment...${NC}\n" check_for_GUI 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 "${HOME}/qortal" + echo "${RED}Hash check confirmed outdated qortal core.${NC}${YELLOW} Updating and bootstrapping...${NC}\n" + cd "${HOME}/qortal" || exit 1 killall -9 java sleep 3 rm -rf db log.t* qortal.log run.log run.pid qortal.jar @@ -295,52 +152,44 @@ check_hash_update_qortal() { rm "${HOME}/remote.md5" local.md5 potentially_update_settings ./start.sh - cd - + cd || exit 1 check_for_GUI 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" + echo "${CYAN}Machine has GUI, setting up auto-fix-visible for GUI-based machines...${NC}\n" if [ "${PI_32_DETECTED}" = true ] || [ "${PI_64_DETECTED}" = true ]; then - echo "${YELLOW} Pi machine detected with GUI, skipping autostart setup for GUI and setting cron jobs instead...${NC}\n" + echo "${YELLOW}Pi machine with GUI, skipping autostart GUI setup, 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" + echo "${YELLOW}Setting up auto-fix-visible on GUI-based system...${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 "${HOME}/.config/autostart" cp auto-fix-qortal-GUI.desktop "${HOME}/.config/autostart" rm -rf "${HOME}/auto-fix-qortal-GUI.desktop" - echo "${YELLOW} Your machine will now run 'auto-fix-qortal.sh' script in a pop-up terminal, 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" - + echo "${YELLOW}Auto-fix-qortal.sh will run in a pop-up terminal 7 min after startup.${NC}\n" + 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" + echo "${YELLOW}Non-GUI system detected, configuring cron then checking node height...${NC}\n" setup_pi_cron fi } setup_pi_cron() { - echo "${YELLOW} Setting up cron jobs for Raspberry Pi or headless machines... ${NC}\n" + echo "${YELLOW}Setting up cron jobs for Raspberry Pi or headless machines...${NC}\n" mkdir -p "${HOME}/backups/cron-backups" crontab -l > "${HOME}/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 } @@ -349,64 +198,53 @@ check_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" + if [ -n "$previous_local_height" ]; then + if [ "$local_height" = "$previous_local_height" ]; then + echo "${RED}Local height unchanged since last run, waiting 3 minutes to re-check...${NC}\n" sleep 188 - checked_height=$(curl "localhost:12391/blocks/height") + checked_height=$(curl -s "http://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" + if [ "$checked_height" = "$previous_local_height" ]; then + echo "${RED}Block height still unchanged... 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 30 MINUTES... ${NC}\n" + if [ -z "$local_height" ]; then + echo "${RED}Local API call for block height returned empty. Is Qortal running?${NC}\n" no_local_height else - echo ${local_height} > auto_fix_last_height.txt + echo "$local_height" > auto_fix_last_height.txt + remote_height_checks fi - - remote_height_checks } no_local_height() { - # height checks failed, is qortal running? - # make another action here... - echo "${WHITE} Checking if node is bootstrapping or not...${NC}\n" + echo "${WHITE}Checking if node is bootstrapping or not...${NC}\n" - # Check if the main log file exists if [ -f "${HOME}/qortal/qortal.log" ]; then - if tail -n 5 "${HOME}/qortal/qortal.log" | grep -E -i 'bootstrap|bootstrapping' > /dev/null; then - echo "${RED} NODE SEEMS TO BE BOOTSTRAPPING, UPDATING SCRIPT AND EXITING, NEXT RUN WILL FIND/FIX ANY ISSUES ${NC}\n" + if tail -n 5 "${HOME}/qortal/qortal.log" | grep -Ei 'bootstrap|bootstrapping' > /dev/null; then + echo "${RED}Node seems to be bootstrapping, updating script and exiting...${NC}\n" update_script fi else - echo "Checking for old log method..." + # Check for old log files old_log_found=false - - # Check for old log files and process them - for log_file in "${HOME}/qortal/log.t*"; do + for log_file in "${HOME}/qortal/log.t"*; do if [ -f "$log_file" ]; then old_log_found=true echo "${YELLOW}Old log method found, backing up old logs and updating logging method...${NC}\n" mkdir -p "${HOME}/qortal/backup/logs" - # Move old log files to the backup directory - mv "${HOME}/qortal/log.t*" "${HOME}/qortal/backup/logs" + mv "${HOME}/qortal/log.t"* "${HOME}/qortal/backup/logs" mv "${HOME}/qortal/log4j2.properties" "${HOME}/qortal/backup/logs" - # Download the new log4j2.properties file curl -L -O https://raw.githubusercontent.com/Qortal/qortal/master/log4j2.properties - # Move the new log4j2.properties file to the qortal directory mv log4j2.properties "${HOME}/qortal" echo -e "${RED}Stopping Qortal to apply new logging method...${NC}\n" - # Stop Qortal to apply changes - cd "${HOME}/qortal" + cd "${HOME}/qortal" || exit 1 ./stop.sh - cd + cd || exit 1 break fi done @@ -416,22 +254,21 @@ no_local_height() { fi fi - echo "${GREEN} Starting Qortal Core and sleeping for 2.5 min to let it startup fully, PLEASE WAIT... ${NC}\n" + echo "${GREEN}Starting Qortal Core and sleeping 2.5 min to let it start fully, PLEASE WAIT...${NC}\n" potentially_update_settings - cd "${HOME}/qortal" - ./start.sh + cd "${HOME}/qortal" || exit 1 + ./start.sh sleep 166 - cd - echo "${GREEN} Checking if Qortal started correctly... ${NC}\n" + cd || exit 1 + 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 - 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" + echo "${GREEN}Local height is ${CYAN}${local_height_check}${NC}" + echo "${GREEN}Node is good, re-checking height and continuing...${NC}\n" 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" + echo "${RED}Starting Qortal Core FAILED. Please consider restarting the computer and waiting 30 minutes.${NC}\n" update_script fi } @@ -442,107 +279,155 @@ remote_height_checks() { local_height=$(curl -sS --connect-timeout 10 "http://localhost:12391/blocks/height") if [ -z "$height_api_qortal_org" ] || [ -z "$height_qortal_link" ]; then - echo "${RED}Failed to fetch data from one or more remote URLs. Skipping remote node checks and updating script ${NC}\n" + echo "${RED}Failed to fetch data from remote nodes. Skipping remote checks and updating script.${NC}\n" update_script + return fi if [ "$height_api_qortal_org" -ge $((local_height - 1500)) ] && [ "$height_api_qortal_org" -le $((local_height + 1500)) ]; then - echo "${YELLOW}Local height ${NC}(${CYAN}${local_height}${NC})${YELLOW} is within 1500 block range of api.qortal.org node height ${NC}(${GREEN}${height_api_qortal_org}${NC})." - echo "${GREEN}api.qortal.org height checks PASSED updating script...${NC}" + echo "${YELLOW}Local height (${CYAN}${local_height}${YELLOW}) is within 1500 blocks of api.qortal.org (${GREEN}${height_api_qortal_org}${YELLOW}).${NC}" + echo "${GREEN}api.qortal.org height checks PASSED, updating script...${NC}" update_script else - echo "${RED}Node is outside the 1500 block range of api.qortal.org, checking another node to be sure...${NC}" + echo "${RED}Local node is outside 1500 block range of api.qortal.org, checking qortal.link...${NC}" if [ "$height_qortal_link" -ge $((local_height - 1500)) ] && [ "$height_qortal_link" -le $((local_height + 1500)) ]; then - echo "${YELLOW}Local height ${NC}(${CYAN}${local_height}${NC})${YELLOW} is within 1500 block range of qortal.link node height ${NC}(${GREEN}${height_qortal_link}${NC})." - echo "${GREEN}qortal.link height checks PASSED updating script...${NC}" + echo "${YELLOW}Local height (${CYAN}${local_height}${YELLOW}) is within 1500 blocks of qortal.link (${GREEN}${height_qortal_link}${YELLOW}).${NC}" + echo "${GREEN}qortal.link height checks PASSED, updating script...${NC}" update_script else - echo "${RED}SECOND remote node check FAILED... ${NC}${YELLOW}assuming local node needs bootstrapping... bootstrapping in 5 seconds...${NC}\n" + echo "${RED}Second remote check FAILED... assuming need for bootstrap...${NC}\n" force_bootstrap - #update_script fi fi } force_bootstrap() { - echo "${RED} height check found issues, forcing bootstrap... ${NC}\n" - cd "${HOME}/qortal" + echo "${RED}ISSUES DETECTED...Forcing bootstrap...${NC}\n" + cd "${HOME}/qortal" || exit 1 killall -9 java sleep 3 rm -rf db log.t* qortal.log run.log run.pid sleep 5 ./start.sh - cd + cd || exit 1 update_script } + potentially_update_settings() { - echo "${GREEN}Backing up settings to a timestamped backup file...${NC}" - echo "${YELLOW}Changing to qortal directory...${NC}" - cd "${HOME}/qortal" - if [ ${SETTINGS_UPDATED} ]; then - echo "${YELLOW} Settings already updated this run, no need to attempt again...${NC}" - return + echo "${GREEN}Backing up settings and checking for modifications...${NC}" + cd "${HOME}/qortal" || exit 1 + + if [ "$UPDATED_SETTINGS" = true ]; then + echo "${YELLOW}Settings already updated this run, no need to attempt again...${NC}" + cd || exit 1 + return fi TIMESTAMP=$(date +%Y%m%d%H%M%S) + BACKUP_FOLDER="${HOME}/qortal/qortal-backup/auto-fix-settings-backup" + if [ -f backup-settings*.json ]; then + mv backup-settings*.json "${BACKUP_FOLDER}" + fi BACKUP_FILE="backup-settings-${TIMESTAMP}.json" cp settings.json "${BACKUP_FILE}" - SETTINGS_FILE="settings.json" + mkdir -p "$BACKUP_FOLDER" + + - echo "${YELLOW}Checking for${NC} ${GREEN}archivingPause${NC} ${YELLOW}setting...${NC}" + echo "${YELLOW}Checking if 'archivingPause' is present...${NC}" if grep -q '"archivingPause"' "${SETTINGS_FILE}"; then - echo "${BLUE}archivingPause exists...${NC}${GREEN} removing it...${NC}" + echo "${BLUE}'archivingPause' found, removing it...${NC}" if command -v jq &> /dev/null; then - echo "${GREEN}jq exists,${NC}${YELLOW} using jq to modify setting...${NC}" + echo "${GREEN}jq found, using jq to remove setting...${NC}" jq 'del(.archivingPause)' "${SETTINGS_FILE}" > "settings.tmp" if [ $? -eq 0 ]; then mv "settings.tmp" "${SETTINGS_FILE}" - SETTINGS_UPDATED=true + UPDATED_SETTINGS=true + mv "${BACKUP_FILE}" "${BACKUP_FOLDER}" + mv "backup-settings*.json" "${BACKUP_FOLDER}" else echo "${RED}jq edit failed, restoring backup...${NC}" - mv "${BACKUP_FILE}" "${SETTINGS_FILE}" + cp "${BACKUP_FILE}" "${SETTINGS_FILE}" + mv "${BACKUP_FILE}" "${BACKUP_FOLDER}" + cd || exit 1 return 1 fi else - echo "${BLUE}jq doesn't exist, modifying with sed...${NC}" - sed -i '/"archivingPause"[[:space:]]*:/d' "${SETTINGS_FILE}" - if [ $? -ne 0 ]; then + # If jq is not available, use grep and sed: + echo "${YELLOW}jq is not installed, using grep/sed for settings modifications...${NC}\n" + + # Remove the entire line containing "archivingPause" + grep -v '"archivingPause"' "${HOME}/qortal/settings.json" > "${HOME}/qortal/settings.tmp" && mv "${HOME}/qortal/settings.tmp" "${HOME}/qortal/settings.json" + + # After removing that line, there might be a trailing comma in the previous line. + # For example: + # "someSetting": true, + # "anotherSetting": "value", + # "archivingPause": false + # After removing the archivingPause line, we might have: + # "someSetting": true, + # "anotherSetting": "value", + # If the removed line was the last entry before a closing bracket, this may leave a trailing comma. + + # The following sed command removes a trailing comma before a closing brace: + sed -i 's/,\s*}/}/' "${HOME}/qortal/settings.json" + if [ $? -ne 0 ]; then echo "${RED}sed edit failed, restoring backup...${NC}" mv "${BACKUP_FILE}" "${SETTINGS_FILE}" - return 1 + if [ -f backup-settings*.json ]; then + mv backup-settings*.json "${BACKUP_FOLDER}" + cd || exit 1 + return 1 + fi fi - SETTINGS_UPDATED=true + + # If needed, also ensure the file still ends with a proper closing brace: + # Just a sanity check if the JSON was formatted line by line originally. + # The next line only adds a `}` if one is missing at the end of file. + if ! tail -n1 "${HOME}/qortal/settings.json" | grep -q '}'; then + echo "}" >> "${HOME}/qortal/settings.json" + fi + + # At this point, we've removed the archivingPause line and fixed trailing commas. + # The file should remain valid JSON assuming it was valid before. + + UPDATED_SETTINGS=true + mv "${BACKUP_FILE}" "${BACKUP_FOLDER}" + if [ -f backup-settings*.json ]; then + mv backup-settings*.json "${BACKUP_FOLDER}" + cd || exit 1 + return 1 + fi fi else - echo "${BLUE}archivingPause does not exist, no changes needed...${NC}" + echo "${BLUE}'archivingPause' not present, no changes needed...${NC}" + return fi echo "${GREEN}Settings modification complete.${NC}" - cd "${HOME}" + cd || exit 1 return 0 } - - update_script() { echo "${YELLOW}Updating script to newest version and backing up old one...${NC}\n" mkdir -p "${HOME}/qortal/new-scripts/backups" - cp "${HOME}/qortal/new-scripts/auto-fix-qortal.sh" "${HOME}/qortal/new-scripts/backups" + cp "${HOME}/qortal/new-scripts/auto-fix-qortal.sh" "${HOME}/qortal/new-scripts/backups" 2>/dev/null rm -rf "${HOME}/qortal/new-scripts/auto-fix-qortal.sh" cp "${HOME}/auto-fix-qortal.sh" "${HOME}/qortal/new-scripts/backups/original.sh" - cd "${HOME}/qortal/new-scripts" + cd "${HOME}/qortal/new-scripts" || exit 1 curl -L -O https://raw.githubusercontent.com/crowetic/QORTector-scripts/main/auto-fix-qortal.sh chmod +x auto-fix-qortal.sh - cd + cd || exit 1 cp "${HOME}/qortal/new-scripts/auto-fix-qortal.sh" "${HOME}/auto-fix-qortal.sh" chmod +x auto-fix-qortal.sh rm -rf "${HOME}/auto_fix_updated" - echo "${YELLOW} Checking for any settings changes required...${NC}" + echo "${YELLOW}Checking for any settings changes required...${NC}" sleep 2 potentially_update_settings - echo "${YELLOW} Auto-fix script run complete.${NC}\n" + echo "${YELLOW}Auto-fix script run complete.${NC}\n" sleep 5 exit }