raspiblitz/home.admin/config.scripts/bonus.lnbits.sh

230 lines
7.5 KiB
Bash
Raw Normal View History

2020-02-10 12:33:03 +01:00
#!/bin/bash
# https://github.com/arcbtc/lnbits
# command info
if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
echo "small config script to switch LNbits on or off"
echo "bonus.lnbits.sh [on|off|status|menu|write-macaroons]"
exit 1
2020-02-10 12:33:03 +01:00
fi
2020-04-28 18:01:50 +02:00
source /mnt/hdd/raspiblitz.conf
2020-02-10 12:33:03 +01:00
# show info menu
if [ "$1" = "menu" ]; then
# get status
echo "# collecting status info ... (please wait)"
source <(sudo /home/admin/config.scripts/bonus.lnbits.sh status)
# get network info
localip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/')
toraddress=$(sudo cat /mnt/hdd/tor/lnbits/hostname 2>/dev/null)
if [ "${runBehindTor}" = "on" ] && [ ${#toraddress} -gt 0 ]; then
# TOR
/home/admin/config.scripts/blitz.lcd.sh qr "${toraddress}"
whiptail --title " LNbits " --msgbox "Open the following URL in your local web browser:
2020-02-10 12:33:03 +01:00
http://${localip}:5000\n
Hidden Service address for TOR Browser (QR see LCD):
${toraddress}
" 11 67
/home/admin/config.scripts/blitz.lcd.sh hide
else
# IP + Domain
whiptail --title " LNbits " --msgbox "Open the following URL in your local web browser:
2020-02-10 12:33:03 +01:00
http://${localip}:5000\n
Activate TOR to access from outside your local network.
" 12 54
fi
echo "please wait ..."
exit 0
fi
# add default value to raspi config if needed
2020-04-23 23:21:32 +02:00
if ! grep -Eq "^LNBits=" /mnt/hdd/raspiblitz.conf; then
echo "LNBits=off" >> /mnt/hdd/raspiblitz.conf
2020-02-10 12:33:03 +01:00
fi
# status
if [ "$1" = "status" ]; then
2020-04-23 23:08:14 +02:00
if [ "${LNBits}" = "on" ]; then
2020-02-10 12:33:03 +01:00
echo "installed=1"
# check for error
isDead=$(sudo systemctl status lnbits | grep -c 'inactive (dead)')
if [ ${isDead} -eq 1 ]; then
echo "error='Service Failed'"
exit 1
fi
else
echo "installed=0"
fi
exit 0
fi
# status
if [ "$1" = "write-macaroons" ]; then
2020-02-10 12:33:03 +01:00
# make sure its run as user admin
adminUserId=$(id -u admin)
2020-02-10 12:47:33 +01:00
if [ "${EUID}" != "${adminUserId}" ]; then
2020-02-10 12:33:03 +01:00
echo "error='please run as admin user'"
exit 1
fi
2020-04-28 13:38:18 +01:00
# copy macaroons and for lnbits environment
# set tls.cert path
sudo -u lnbits sed -i "s/^LND_CERT=.*/LND_CERT=\/home\/admin\/.lnd\/tls.cert/g" /home/lnbits/lnbits/.env
# copy macaroons
echo "copy macaroons to lnbits user"
sudo -u lnbits mkdir -p /home/lnbits/.lnd/data/chain/${network}/${chain}net/
sudo cp /home/bitcoin/.lnd/data/chain/${network}/${chain}net/admin.macaroon /home/lnbits/.lnd/data/chain/${network}/${chain}net/
sudo cp /home/bitcoin/.lnd/data/chain/${network}/${chain}net/invoice.macaroon /home/lnbits/.lnd/data/chain/${network}/${chain}net/
sudo cp /home/bitcoin/.lnd/data/chain/${network}/${chain}net/readonly.macaroon /home/lnbits/.lnd/data/chain/${network}/${chain}net/
sudo chown lnbits:lnbits -R /home/lnbits/.lnd/data/chain/${network}/${chain}net/*.macaroon
sudo chmod 600 /home/lnbits/.lnd/data/chain/${network}/${chain}net/*.macaroon
2020-04-28 13:38:18 +01:00
echo "OK DONE"
#set macaroons paths in .env
sudo -u lnbits sed -i "s/^LND_ADMIN_MACAROON=.*/LND_ADMIN_MACAROON=\/home\/lnbits\/.lnd\/data\/chain\/${network}\/${chain}net\/admin.macaroon/g" /home/lnbits/lnbits/.env
sudo -u lnbits sed -i "s/^LND_INVOICE_MACAROON=.*/LND_INVOICE_MACAROON=\/home\/lnbits\/.lnd\/data\/chain\/${network}\/${chain}net\/invoice.macaroon/g" /home/lnbits/lnbits/.env
sudo -u lnbits sed -i "s/^LND_READ_MACAROON=.*/LND_READ_MACAROON=\/home\/lnbits\/.lnd\/data\/chain\/${network}\/${chain}net\/read.macaroon/g" /home/lnbits/lnbits/.env
2020-04-28 09:27:03 +01:00
echo "# OK - macaroons written to /home/lnbits/lnbits/.env"
2020-02-10 12:33:03 +01:00
exit 0
fi
# stop service
echo "making sure services are not running"
sudo systemctl stop lnbits 2>/dev/null
# switch on
if [ "$1" = "1" ] || [ "$1" = "on" ]; then
echo "*** INSTALL LNbits ***"
2020-02-10 12:33:03 +01:00
isInstalled=$(sudo ls /etc/systemd/system/lnbits.service 2>/dev/null | grep -c 'lnbits.service')
if [ ${isInstalled} -eq 0 ]; then
2020-04-28 09:27:03 +01:00
echo "*** Add the 'lnbits' user ***"
sudo adduser --disabled-password --gecos "" lnbits
2020-02-10 12:33:03 +01:00
# make sure needed debian packages are installed
echo "# installing needed packages"
sudo apt-get install -y pipenv 2>/dev/null
# install from GitHub
2020-02-10 12:33:03 +01:00
echo "# get the github code"
2020-04-28 09:27:03 +01:00
sudo rm -r /home/lnbits/lnbits 2>/dev/null
cd /home/lnbits
sudo -u lnbits git clone https://github.com/arcbtc/lnbits.git
cd /home/lnbits/lnbits
sudo -u lnbits git checkout tags/0.1.0
# prepare .env file
2020-02-10 12:33:03 +01:00
echo "# preparing env file"
2020-04-28 09:27:03 +01:00
sudo rm /home/lnbits/lnbits/.env 2>/dev/null
sudo -u lnbits touch /home/lnbits/lnbits/.env
sudo bash -c "echo 'FLASK_APP=lnbits' >> /home/lnbits/lnbits/.env"
sudo bash -c "echo 'FLASK_ENV=production' >> /home/lnbits/lnbits/.env"
sudo bash -c "echo 'LNBITS_BACKEND_WALLET_CLASS=LndWallet' >> /home/lnbits/lnbits/.env"
sudo bash -c "echo 'LND_GRPC_ENDPOINT=127.0.0.1' >> /home/lnbits/lnbits/.env"
sudo bash -c "echo 'LND_GRPC_PORT=10009' >> /home/lnbits/lnbits/.env"
sudo bash -c "echo 'LNBITS_FORCE_HTTPS=0' >> /home/lnbits/lnbits/.env"
2020-04-28 13:38:18 +01:00
sudo bash -c "echo 'LND_CERT=' >> /home/lnbits/lnbits/.env"
sudo bash -c "echo 'LND_ADMIN_MACAROON=' >> /home/lnbits/lnbits/.env"
sudo bash -c "echo 'LND_INVOICE_MACAROON=' >> /home/lnbits/lnbits/.env"
sudo bash -c "echo 'LND_READ_MACAROON=' >> /home/lnbits/lnbits/.env"
2020-04-28 09:27:03 +01:00
/home/admin/config.scripts/bonus.lnbits.sh write-macaroons
2020-02-10 21:07:24 +01:00
# set database path to HDD data so that its survives updates and migrations
2020-04-23 08:07:49 +02:00
sudo mkdir /mnt/hdd/app-data/LNBits 2>/dev/null
2020-04-28 09:27:03 +01:00
sudo chown lnbits:lnbits -R /mnt/hdd/app-data/LNBits
sudo bash -c "echo 'LNBITS_DATA_FOLDER=/mnt/hdd/app-data/LNBits' >> sudo /home/lnbits/lnbits/.env"
2020-02-10 12:33:03 +01:00
2020-02-10 14:29:28 +01:00
# to the install
2020-04-24 06:52:33 +02:00
echo "# installing application dependencies"
2020-04-28 09:27:03 +01:00
cd /home/lnbits/lnbits
sudo -u lnbits pipenv install
sudo -u lnbits /usr/bin/pipenv run pip install python-dotenv
2020-04-24 06:52:33 +02:00
# to the install
echo "# updating databases"
2020-04-28 09:27:03 +01:00
sudo -u lnbits /usr/bin/pipenv run flask migrate
2020-04-24 06:52:33 +02:00
2020-02-10 12:33:03 +01:00
# open firewall
2020-02-10 13:15:18 +01:00
echo
2020-02-10 12:33:03 +01:00
echo "*** Updating Firewall ***"
sudo ufw allow 5000 comment 'lnbits'
sudo ufw --force enable
echo ""
# install service
2020-02-10 14:29:28 +01:00
echo "*** Install systemd ***"
2020-02-10 18:27:26 +01:00
cat > /home/admin/lnbits.service <<EOF
2020-02-10 18:16:55 +01:00
# systemd unit for lnbits
2020-02-10 12:33:03 +01:00
[Unit]
Description=lnbits
Wants=lnd.service
After=lnd.service
[Service]
2020-04-28 09:27:03 +01:00
WorkingDirectory=/home/lnbits/lnbits
ExecStart=/bin/sh -c 'cd /home/lnbits/lnbits && pipenv run gunicorn -b :5000 lnbits:app -k gevent'
User=lnbits
2020-02-10 12:33:03 +01:00
Restart=always
TimeoutSec=120
RestartSec=30
StandardOutput=null
StandardError=journal
2020-02-10 18:40:53 +01:00
[Install]
WantedBy=multi-user.target
2020-02-10 12:33:03 +01:00
EOF
sudo mv /home/admin/lnbits.service /etc/systemd/system/lnbits.service
2020-02-10 12:33:03 +01:00
sudo systemctl enable lnbits
2020-02-10 21:43:35 +01:00
echo "# OK - service needs starting: sudo systemctl start lnbits"
2020-02-10 12:33:03 +01:00
else
echo "LNbits already installed."
2020-02-10 12:33:03 +01:00
fi
# setting value in raspi blitz config
2020-04-23 23:08:14 +02:00
sudo sed -i "s/^LNBits=.*/LNBits=on/g" /mnt/hdd/raspiblitz.conf
2020-02-10 14:29:28 +01:00
# Hidden Service if Tor is active
2020-02-10 12:33:03 +01:00
source /mnt/hdd/raspiblitz.conf
if [ "${runBehindTor}" = "on" ]; then
/home/admin/config.scripts/internet.hiddenservice.sh lnbits 80 5000
fi
exit 0
fi
# switch off
if [ "$1" = "0" ] || [ "$1" = "off" ]; then
# setting value in raspi blitz config
2020-04-23 23:08:14 +02:00
sudo sed -i "s/^LNBits=.*/LNBits=off/g" /mnt/hdd/raspiblitz.conf
2020-02-10 12:33:03 +01:00
isInstalled=$(sudo ls /etc/systemd/system/lnbits.service 2>/dev/null | grep -c 'lnbits.service')
2020-04-23 23:08:14 +02:00
if [ ${isInstalled} -eq 1 ] || [ "${LNBits}" == "on" ]; then
echo "*** REMOVING LNbits ***"
2020-02-10 12:33:03 +01:00
sudo systemctl stop lnbits
sudo systemctl disable lnbits
sudo rm /etc/systemd/system/lnbits.service
2020-04-28 09:27:03 +01:00
sudo userdel -rf lnbits
echo "OK LNbits removed."
else
echo "LNbits is not installed."
2020-02-10 12:33:03 +01:00
fi
exit 0
fi
echo "FAIL - Unknown Parameter $1"
exit 1