From 63dc0756b47824e393b3e0412c71501fcf0a7f1a Mon Sep 17 00:00:00 2001 From: Christian Rotzoll Date: Fri, 18 Sep 2020 15:48:12 +0200 Subject: [PATCH] #1428 Fix BTC-RPC-Explorer restart when in use with ElectRS (#1563) --- home.admin/_background.sh | 2 +- .../config.scripts/bonus.electrsexplorer.sh | 160 ++++++++++++++---- 2 files changed, 130 insertions(+), 32 deletions(-) diff --git a/home.admin/_background.sh b/home.admin/_background.sh index 26551da3c..36d44177b 100755 --- a/home.admin/_background.sh +++ b/home.admin/_background.sh @@ -412,7 +412,7 @@ do # check every 10 minutes electrsExplorer=$((($counter % 600)+1)) if [ ${electrsExplorer} -eq 1 ]; then - if [ "${BTCRPCexplorer}" = "on" ] & [ "${ElectRS}" = "on" ]; then + if [ "${BTCRPCexplorer}" = "on" ]; then /home/admin/config.scripts/bonus.electrsexplorer.sh fi fi diff --git a/home.admin/config.scripts/bonus.electrsexplorer.sh b/home.admin/config.scripts/bonus.electrsexplorer.sh index 4c7563548..d337c65b3 100755 --- a/home.admin/config.scripts/bonus.electrsexplorer.sh +++ b/home.admin/config.scripts/bonus.electrsexplorer.sh @@ -1,50 +1,148 @@ # config script to make BTC-RPC-Explorer use Electrs if both active +# thx to PatrickScheich for improving the script source /mnt/hdd/raspiblitz.conf -if [ "${BTCRPCexplorer}" = "on" ] & [ "${ElectRS}" = "on" ]; then - ## Enable BTCEXP_ADDRESS_API if BTC-RPC-Explorer is active - if [ $(sudo -u electrs lsof -i | grep -c 50001) -gt 0 ]; 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 +# 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') - # create ExecStart=/home/admin/btc-rpc-explorer.run.sh - cat > /home/admin/btc-rpc-explorer.run.sh </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 - ## Disable BTCEXP_ADDRESS_API if BTC-RPC-Explorer is active + + # electrs if OFF and explorer ON if [ "${BTCRPCexplorer}" = "on" ]; then - 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 - - sudo sed -i "s/^ExecStart=\/home\/admin\/btc-rpc-explorer.run.sh/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 - sudo systemctl daemon-reload - sudo systemctl restart btc-rpc-explorer + + # 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