From 83ebf619b831f660a4c4e0e32c1409797027ee62 Mon Sep 17 00:00:00 2001 From: rootzoll Date: Thu, 16 Sep 2021 17:20:16 +0200 Subject: [PATCH] connect electrs & btc-rpc-explorer by prestart --- home.admin/_background.sh | 12 -- .../config.scripts/bonus.btc-rpc-explorer.sh | 17 +- home.admin/config.scripts/bonus.electrs.sh | 6 - .../config.scripts/bonus.electrsexplorer.sh | 148 ------------------ 4 files changed, 9 insertions(+), 174 deletions(-) delete mode 100755 home.admin/config.scripts/bonus.electrsexplorer.sh diff --git a/home.admin/_background.sh b/home.admin/_background.sh index 469f08e0c..fc75419bb 100755 --- a/home.admin/_background.sh +++ b/home.admin/_background.sh @@ -510,18 +510,6 @@ do fi fi - ############################### - # Set the address API use for BTC-RPC-Explorer depending on Electrs status - ############################### - - # check every 10 minutes - electrsExplorer=$((($counter % 600)+1)) - if [ ${electrsExplorer} -eq 1 ]; then - if [ "${BTCRPCexplorer}" = "on" ]; then - /home/admin/config.scripts/bonus.electrsexplorer.sh - fi - fi - ############################### # Prepare next loop ############################### diff --git a/home.admin/config.scripts/bonus.btc-rpc-explorer.sh b/home.admin/config.scripts/bonus.btc-rpc-explorer.sh index b7547466b..aeb2717ea 100755 --- a/home.admin/config.scripts/bonus.btc-rpc-explorer.sh +++ b/home.admin/config.scripts/bonus.btc-rpc-explorer.sh @@ -123,10 +123,7 @@ if [ "$1" = "prestart" ]; then # check if electrs is installed & running if [ "${ElectRS}" == "on" ]; then - # CHECK THAT ELECTRS IS PART OF CONFIG - echo "# TODO: check electrs config ..." - - # CHECK THAT ELECTRS INDEX IS BUILD + # CHECK THAT ELECTRS INDEX IS BUILD (WAITLOOP) # electrs listening in port 50001 means index is build echo "# electrs is on .. checking if index is build" echo "# waiting electrs on port 50001 (= index is ready)" @@ -134,7 +131,15 @@ if [ "$1" = "prestart" ]; then sleep 1 done + # CHECK THAT ELECTRS IS PART OF CONFIG + sed -i 's/^BTCEXP_ADDRESS_API=*/BTCEXP_ADDRESS_API=electrumx/g' /home/btcrpcexplorer/.config/btc-rpc-explorer.env + echo "# electrs started, launching BTC-RPC-Explorer ..." + else + + # ELECTRS=OFF --> MAKE SURE IT IS NOT CONNECTED + sed -i 's/^BTCEXP_ADDRESS_API=*/BTCEXP_ADDRESS_API=none/g' /home/btcrpcexplorer/.config/btc-rpc-explorer.env + fi exit 0 @@ -291,10 +296,6 @@ EOF cd /home/btcrpcexplorer/btc-rpc-explorer/ sudo npm audit fix - ## Enable BTCEXP_ADDRESS_API if BTC-RPC-Explorer is active - # see /home/admin/config.scripts/bonus.electrsexplorer.sh - # run every 10 min by _background.sh - # Hidden Service for BTC-RPC-explorer if Tor is active source /mnt/hdd/raspiblitz.conf if [ "${runBehindTor}" = "on" ]; then diff --git a/home.admin/config.scripts/bonus.electrs.sh b/home.admin/config.scripts/bonus.electrs.sh index d1ac9d2ad..f24d5cbc1 100755 --- a/home.admin/config.scripts/bonus.electrs.sh +++ b/home.admin/config.scripts/bonus.electrs.sh @@ -438,10 +438,6 @@ WantedBy=multi-user.target /home/admin/config.scripts/internet.hiddenservice.sh electrs 50002 50002 50001 50001 fi - ## Enable BTCEXP_ADDRESS_API if BTC-RPC-Explorer is active - # see /home/admin/config.scripts/bonus.electrsexplorer.sh - # run every 10 min by _background.sh - echo echo "# To connect through SSL from outside of the local network make sure the port 50002 is forwarded on the router" echo @@ -479,8 +475,6 @@ if [ "$1" = "0" ] || [ "$1" = "off" ]; then sudo ufw deny 50002 echo "# OK ElectRS removed." - ## Disable BTCEXP_ADDRESS_API if BTC-RPC-Explorer is active - /home/admin/config.scripts/bonus.electrsexplorer.sh else echo "# ElectRS is not installed." fi diff --git a/home.admin/config.scripts/bonus.electrsexplorer.sh b/home.admin/config.scripts/bonus.electrsexplorer.sh deleted file mode 100755 index d337c65b3..000000000 --- a/home.admin/config.scripts/bonus.electrsexplorer.sh +++ /dev/null @@ -1,148 +0,0 @@ -# config script to make BTC-RPC-Explorer use Electrs if both active -# thx to PatrickScheich for improving the script - -source /mnt/hdd/raspiblitz.conf - -# explorer start script (waits to start btc-rpc-explorer until eletrs is responsive) -explorerStartDir="/home/admin/system" -explorerStartScript="${explorerStartDir}/btc-rpc-explorer.run.sh" -explorerStartScriptEscaped=$(echo "${explorerStartScript}" | sed 's/\//\\\//g') - -# check if "^BTCEXP_ADDRESS_API=electrumx" -btcaddrapiEnabled=$(sudo cat /home/btcrpcexplorer/.config/btc-rpc-explorer.env 2>/dev/null | grep -c "^BTCEXP_ADDRESS_API=electrumx") - -# check if service starts the shell script "btc-rpc-explorer.run.sh" -serviceStartsScript=$(sudo cat /etc/systemd/system/btc-rpc-explorer.service 2>/dev/null | grep -c "^ExecStart=${explorerStartScript}") - -# optional return status -if [ "$1" = "status" ]; then - if [ "${BTCRPCexplorer}" = "" ]; then - BTCRPCexplorer="off" - fi - if [ "${ElectRS}" = "" ]; then - ElectRS="off" - fi - echo "BTCRPCexplorer=${BTCRPCexplorer}" - echo "ElectRS=${ElectRS}" - echo "explorerStartScript='${explorerStartScript}'" - echo "explorerStartScriptEscaped='${explorerStartScriptEscaped}'" - echo "# if electrum is set as address api in btc-prc-explorer" - echo "btcaddrapiEnabled=${btcaddrapiEnabled}" - echo "# if btc-prc-explorer is started by systemd with btc-rpc-explorer.run.sh" - echo "# that waits for electrum to become responsive" - echo "serviceStartsScript=${serviceStartsScript}" - exit 0 -fi - -# variable to track if service restart is needed -serviceNeedsRestart=0 - -# both services are "switched on" in raspiblitz.conf -if [ "${BTCRPCexplorer}" = "on" ] & [ "${ElectRS}" = "on" ]; then - - # make sure that "btc-rpc-explorer.run.sh" exists... - # if it does not exist, create it and make it executable - # it is fine to create the script, even the BTC-RPC-Explorer might be started directly - if [ ! -f ${explorerStartScript} ]; then - echo "script \"${explorerStartScript}\" does not exist, create it and make it executable" - sudo -u admin mkdir -p ${explorerStartDir} - cat > ${explorerStartScript} < change it in "/home/btcrpcexplorer/.config/btc-rpc-explorer.env" - if [ ${btcaddrapiEnabled} -ne 1 ]; then - echo "electrs is active - switching address API support on in BTC-RPC-Explorer" - sudo -u btcrpcexplorer sed -i 's/^BTCEXP_ADDRESS_API=none/BTCEXP_ADDRESS_API=electrumx/g' /home/btcrpcexplorer/.config/btc-rpc-explorer.env - # make sure to restart the service - serviceNeedsRestart=1 - else - echo "electrs is active - address API support in BTC-RPC-Explorer is already enabled, nothing to do here" - fi - - # make sure that explorer is started thru script - if [ ${serviceStartsScript} -ne 1 ]; then - echo "btc-rpc-explorer.service change to start via script: ${explorerStartScript}" - sudo sed -i "s/^ExecStart=\/usr\/bin\/npm start/ExecStart=${explorerStartScriptEscaped}/g" /etc/systemd/system/btc-rpc-explorer.service - sudo sed -i "s/^User=.*/User=admin/g" /etc/systemd/system/btc-rpc-explorer.service - # make sure to restart the service - serviceNeedsRestart=1 - else - echo "electrs is active - service start via script is already enabled, nothing to do here" - fi - - # electrs service is offline - else - echo "electrs is offline" - - # make sure to switch address API support off - if [ ${btcaddrapiEnabled} -ne 1 ]; then - echo "electrs is not active - address API support in BTC-RPC-Explorer is already disabled, nothing to do here" - else - echo "electrs is not active - switching address API support off in BTC-RPC-Explorer" - sudo -u btcrpcexplorer sed -i 's/^BTCEXP_ADDRESS_API=electrumx/BTCEXP_ADDRESS_API=none/g' /home/btcrpcexplorer/.config/btc-rpc-explorer.env - # make sure to restart the service - serviceNeedsRestart=1 - fi - - # make sure to start explorer directly - if [ ${serviceStartsScript} -ne 1 ]; then - echo "electrs is not active - service direct start is already enabled, nothing to do here" - else - echo "btc-rpc-explorer.service change to start directly" - sudo sed -i "s/^ExecStart=${explorerStartScriptEscaped}/ExecStart=\/usr\/bin\/npm start/g" /etc/systemd/system/btc-rpc-explorer.service - sudo sed -i "s/^User=.*/User=btcrpcexplorer/g" /etc/systemd/system/btc-rpc-explorer.service - # make sure to restart the service - serviceNeedsRestart=1 - fi - fi - -# both services are NOT "switched on" in raspiblitz.conf -else - - # electrs if OFF and explorer ON - if [ "${BTCRPCexplorer}" = "on" ]; then - - # Disable BTCEXP_ADDRESS_API if BTC-RPC-Explorer is active - echo "electrs is off in raspiblitz.conf" - if [ ${btcaddrapiEnabled} -ne 1 ]; then - echo "electrs is not active - address API support in BTC-RPC-Explorer is already disabled, nothing to do here" - else - echo "electrs is not active - switching address API support off in BTC-RPC-Explorer" - sudo -u btcrpcexplorer sed -i 's/^BTCEXP_ADDRESS_API=electrumx/BTCEXP_ADDRESS_API=none/g' /home/btcrpcexplorer/.config/btc-rpc-explorer.env - # make sure to restart the service - serviceNeedsRestart=1 - fi - - # make sure that service is started directly - if [ ${serviceStartsScript} -ne 1 ]; then - echo "electrs is not active - service direct start is already enabled, nothing to do here" - else - echo "btc-rpc-explorer.service change to start directly" - sudo sed -i "s/^ExecStart=${explorerStartScriptEscaped}/ExecStart=\/usr\/bin\/npm start/g" /etc/systemd/system/btc-rpc-explorer.service - sudo sed -i "s/^User=.*/User=btcrpcexplorer/g" /etc/systemd/system/btc-rpc-explorer.service - # make sure to restart the service - serviceNeedsRestart=1 - fi - - fi -fi - -if [ ${serviceNeedsRestart} -eq 1 ]; then - echo "BTC-RPC-Explorer service needs restart" - sudo systemctl daemon-reload - sudo systemctl restart btc-rpc-explorer -fi \ No newline at end of file