raspiblitz/home.admin/10setupBlitz.sh
2020-01-16 10:20:04 +01:00

278 lines
8.6 KiB
Bash
Executable File

#!/bin/bash
# CHECK WHAT IS ALREADY WORKING
# check list from top down - so ./10setupBlitz.sh
# and re-enters the setup process at the correct spot
# in case it got interrupted
echo "checking setup script"
# INFOFILE on SD - state data from bootstrap & setup
infoFile="/home/admin/raspiblitz.info"
source ${infoFile}
echo "network(${network})"
echo "chain(${chain})"
echo "setupStep(${setupStep})"
if [ ${#network} -eq 0 ]; then
echo "FAIL: Something is wrong. There is no value for network in ${infoFile}."
echo "Should be at least default value. EXIT"
exit 1
fi
# if no setup step in info file init with 0
if [ ${#setupStep} -eq 0 ];then
echo "Init setupStep=0"
echo "setupStep=0" >> ${infoFile}
setupStep=0
fi
# check if LND needs re-setup
if [ ${setupStep} -gt 79 ];then
source <(sudo /home/admin/config.scripts/lnd.check.sh basic-setup)
if [ ${wallet} -eq 0 ] || [ ${macaroon} -eq 0 ] || [ ${config} -eq 0 ] || [ ${tls} -eq 0 ]; then
echo "WARN: LND needs re-setup"
sudo /home/admin/70initLND.sh
exit 0
fi
fi
# if setup if ready --> REBOOT
if [ ${setupStep} -gt 89 ];then
echo "FINISH by setupstep(${setupStep})"
sleep 3
sudo /home/admin/90finishSetup.sh
sudo /home/admin/95finalSetup.sh
exit 0
fi
# check if lightning is running
lndRunning=$(systemctl status lnd.service 2>/dev/null | grep -c running)
if [ ${lndRunning} -eq 1 ]; then
echo "LND is running ..."
sleep 1
# check if LND wallet exists and if locked
walletExists=$(sudo ls /mnt/hdd/lnd/data/chain/${network}/${chain}net/wallet.db 2>/dev/null | grep wallet.db -c)
locked=0
# only when a wallet exists - it can be locked
if [ ${walletExists} -eq 1 ];then
echo "lnd wallet exists ... checking if locked"
sleep 2
locked=$(sudo tail -n 1 /mnt/hdd/lnd/logs/${network}/${chain}net/lnd.log 2>/dev/null | grep -c unlock)
fi
if [ ${locked} -gt 0 ]; then
# LND wallet is locked
/home/admin/AAunlockLND.sh
/home/admin/10setupBlitz.sh
exit 0
fi
# check if blockchain still syncing (during sync sometimes CLI returns with error at this point)
chainInfo=$(sudo -u bitcoin ${network}-cli getblockchaininfo 2>/dev/null | grep 'initialblockdownload')
chainSyncing=1
if [ ${#chainInfo} -gt 0 ];then
echo "check chaininfo"
chainSyncing=$(echo "${chainInfo}" | grep "true" -c)
else
echo "chaininfo is zero"
fi
if [ ${chainSyncing} -eq 1 ]; then
echo "Sync Chain ..."
sleep 3
/home/admin/70initLND.sh
exit 0
fi
# check if lnd is scanning blockchain
lndInfo=$(sudo -u bitcoin /usr/local/bin/lncli --chain=${network} getinfo 2>/dev/null | grep "synced_to_chain")
lndSyncing=1
if [ ${#lndInfo} -gt 0 ];then
lndSyncing=$(echo "${chainInfo}" | grep "false" -c)
fi
if [ ${lndSyncing} -eq 1 ]; then
echo "Sync LND ..."
sleep 3
/home/admin/70initLND.sh
exit 0
fi
# if unlocked, blockchain synced and LND synced to chain .. finisch Setup
echo "FINSIH ... "
sleep 3
sudo /home/admin/90finishSetup.sh
sudo /home/admin/95finalSetup.sh
exit 0
fi #end - when lighting is running
# check if bitcoin is running
bitcoinRunning=$(systemctl status ${network}d.service 2>/dev/null | grep -c running)
if [ ${bitcoinRunning} -eq 0 ]; then
# double check
seconds=120
if [ ${setupStep} -lt 60 ]; then
seconds=10
fi
dialog --pause " Double checking for ${network}d - please wait .." 8 58 ${seconds}
clear
bitcoinRunning=$(${network}-cli getblockchaininfo 2>/dev/null | grep "initialblockdownload" -c)
else
echo "${network} is running"
fi
if [ ${bitcoinRunning} -eq 1 ]; then
echo "OK - ${network}d is running"
echo "Next step run Lightning"
/home/admin/70initLND.sh
exit 1
else
echo "${network} still not running"
fi #end - when bitcoin is running
# --- so neither bitcoin or lnd or running yet --> find the earlier step in the setup process:
# use blitz.datadrive.sh to analyse HDD situation
source <(sudo /home/admin/config.scripts/blitz.datadrive.sh status ${network})
if [ ${#error} -gt 0 ]; then
echo "# FAIL blitz.datadrive.sh status --> ${error}"
echo "# Please report issue to the raspiblitz github."
exit 1
fi
# check if HDD is auto-mounted
if [ ${isMounted} -eq 1 ]; then
# FAILSAFE: check if raspiblitz.conf is available
configExists=$(ls /mnt/hdd/raspiblitz.conf | grep -c '.conf')
if [ ${configExists} -eq 0 ]; then
echo ""
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "FAIL: /mnt/hdd/raspiblitz.conf should exists at this point, but not found!"
echo "Please report to: https://github.com/rootzoll/raspiblitz/issues/293"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "Press ENTER to EXIT."
read key
exit 1
fi
# are there any signs of blockchain data and activity
# setup running with admin user, but has no permission to read /mnt/hdd/bitcoin/blocks/, sudo needed
blockchainDataExists=$(sudo ls /mnt/hdd/${network}/blocks 2>/dev/null | grep -c '.dat')
configExists=$(sudo ls /mnt/hdd/${network}/${network}.conf | grep -c '.conf')
if [ ${blockchainDataExists} -gt 0 ]; then
if [ ${configExists} -eq 1 ]; then
/home/admin/XXdebugLogs.sh
echo "UNKOWN STATE - there is blockain data config, but blockchain service is not running"
echo "It seems that something went wrong during sync/download/copy of the blockchain."
echo "Or something with the config is not correct."
echo "Sometimes a reboot helps --> sudo shutdown -r now"
exit 1
else
echo "Got mounted blockchain, but no config and running service yet --> finish HDD"
/home/admin/60finishHDD.sh
exit 1
fi
fi
# check if there is torrent data to continue
torrentProgressExists=$(sudo ls /mnt/hdd/ 2>/dev/null | grep "torrent" -c)
if [ ${torrentProgressExists} -eq 1 ]; then
# check if there is a running screen session to return to
noScreenSession=$(screen -ls | grep -c "No Sockets found")
if [ ${noScreenSession} -eq 0 ]; then
echo "found torrent data .. resuming"
/home/admin/50torrentHDD.sh
exit 1
fi
fi
# HDD is empty - get Blockchain
# detect hardware version of RaspberryPi
# https://www.unixtutorial.org/command-to-confirm-raspberry-pi-model
raspberryPi=$(cat /proc/device-tree/model | cut -d " " -f 3 | sed 's/[^0-9]*//g')
if [ ${#raspberryPi} -eq 0 ]; then
raspberryPi=0
fi
syncComment="ULTRA SLOW"
if [ ${raspberryPi} -gt 3 ]; then
syncComment="BEST+SLOW"
fi
#Bitcoin
if [ ${network} = "bitcoin" ]; then
echo "Bitcoin Options"
menuitem=$(dialog --clear --beep --backtitle "RaspiBlitz" --title "Getting the Blockchain" \
--menu "You need a copy of the Bitcoin Blockchain - you have 4 options:" 13 75 5 \
T "TORRENT --> MAINNET + TESTNET thru Torrent (DEFAULT)" \
C "COPY --> BLOCKCHAINDATA from another node with SCP" \
N "CLONE --> BLOCKCHAINDATA from 2nd HDD (extra cable)"\
S "SYNC --> MAINNET thru Bitcoin Network (${syncComment})" 2>&1 >/dev/tty)
# Litecoin
elif [ ${network} = "litecoin" ]; then
echo "Litecoin Options"
menuitem=$(dialog --clear --beep --backtitle "RaspiBlitz" --title "Getting the Blockchain" \
--menu "You need a copy of the Litecoin Blockchain - you have 2 options:" 13 75 4 \
T "TORRENT --> MAINNET thru Torrent (DEFAULT)" \
S "SYNC --> MAINNET thru Litecoin Network (FALLBACK+SLOW)" 2>&1 >/dev/tty)
# error
else
echo "FAIL Unkown network(${network})"
exit 1
fi
# set SetupState
sudo sed -i "s/^setupStep=.*/setupStep=50/g" ${infoFile}
clear
case $menuitem in
T)
/home/admin/50torrentHDD.sh
;;
C)
/home/admin/50copyHDD.sh
;;
N)
/home/admin/50cloneHDD.sh
;;
S)
/home/admin/50syncHDD.sh
/home/admin/10setupBlitz.sh
;;
esac
exit 1
fi # end HDD is already auto-mountes
# --- the HDD is not auto-mounted --> very early stage of setup
# if the script is called for the first time
if [ ${setupStep} -eq 0 ]; then
# run initial user dialog
/home/admin/20setupDialog.sh
fi
# if the script is called for the first time
if [ ${setupStep} -eq 20 ]; then
# run initial user dialog
/home/admin/30initHDD.sh
exit 1
fi
# the HDD is already ext4 formated and called blockchain
if [ "${hddFormat}" = "ext4" ] || [ "${hddFormat}" = "btrfs" ]; then
echo "HDD was already initialized/prepared"
echo "Now needs to be mounted"
/home/admin/40addHDD.sh
exit 1
fi
# the HDD had no init yet
echo "init HDD ..."
/home/admin/30initHDD.sh
exit 1