From 20fed1d4574ed225ed1490c4e886a0bfd23a4e4c Mon Sep 17 00:00:00 2001 From: "Justin (shocknet)" Date: Tue, 23 Jul 2024 14:39:56 -0400 Subject: [PATCH] deploy --- scripts/extract_nprofile.sh | 14 +++++- scripts/install.sh | 22 +++++++-- scripts/install_lnd.sh | 90 +++++++++++++++++++++---------------- scripts/start_services.sh | 64 +++++++++++--------------- 4 files changed, 108 insertions(+), 82 deletions(-) diff --git a/scripts/extract_nprofile.sh b/scripts/extract_nprofile.sh index 44d718b2..7cb20a18 100644 --- a/scripts/extract_nprofile.sh +++ b/scripts/extract_nprofile.sh @@ -36,7 +36,7 @@ get_log_info() { while [ $(($(date +%s) - START_TIME)) -lt $MAX_WAIT_TIME ]; do current_lines=$(wc -l < "$latest_unlocker_log") if [ $current_lines -gt $initial_lines ]; then - latest_entry=$(tail -n $((current_lines - initial_lines)) "$latest_unlocker_log" | grep "unlocker >>" | tail -n 1) + latest_entry=$(tail -n $((current_lines - initial_lines)) "$latest_unlocker_log" | grep -E "unlocker >> (the wallet is already unlocked|created wallet with pub|unlocked wallet with pub)" | tail -n 1) if [ -n "$latest_entry" ]; then break fi @@ -49,7 +49,17 @@ get_log_info() { exit 1 fi - log "Wallet status: $(echo "$latest_entry" | cut -d' ' -f4-)" + if [[ "$latest_entry" == *"the wallet is already unlocked"* ]]; then + log "Wallet status: The wallet is already unlocked" + elif [[ "$latest_entry" == *"created wallet with pub"* ]]; then + log "Wallet status: A new wallet has been created" + elif [[ "$latest_entry" == *"unlocking"* ]]; then + log "Wallet status: The wallet is in the process of unlocking" + elif [[ "$latest_entry" == *"unlocked wallet with pub"* ]]; then + log "Wallet status: The wallet has been successfully unlocked" + else + log "Wallet status: Unknown (unexpected status message)" + fi log "Retrieving connection information..." diff --git a/scripts/install.sh b/scripts/install.sh index d7cd4e25..6d838dc9 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -1,7 +1,6 @@ #!/bin/bash set -e -# Function to log errors log_error() { log "ERROR: $1" log "Exiting with status $2" @@ -33,8 +32,22 @@ detect_os_arch if [ "$OS" = "Mac" ]; then handle_macos else - install_lnd || log_error "LND installation failed" $? - lnd_upgrade_status=$? + lnd_output=$(install_lnd) + install_result=$? + + if [ $install_result -ne 0 ]; then + log_error "LND installation failed" $install_result + fi + + # Extract the LND status from the output + lnd_status=$(echo "$lnd_output" | grep "LND_STATUS:" | cut -d':' -f2) + + case $lnd_status in + 0) log "LND fresh installation completed successfully." ;; + 1) log "LND upgrade completed successfully." ;; + 2) log "LND is already up-to-date. No action needed." ;; + *) log "WARNING: Unexpected status from install_lnd: $lnd_status" ;; + esac install_nodejs || log_error "NodeJS installation failed" $? @@ -57,7 +70,8 @@ else log "Full output: $pub_output" fi - start_services $lnd_upgrade_status $pub_upgrade_status + log "Starting services..." + start_services $lnd_status $pub_upgrade_status get_log_info log "Installation process completed successfully" diff --git a/scripts/install_lnd.sh b/scripts/install_lnd.sh index 5e56b21c..a7a3baaf 100755 --- a/scripts/install_lnd.sh +++ b/scripts/install_lnd.sh @@ -1,6 +1,10 @@ #!/bin/bash install_lnd() { + local lnd_status=0 + + log "Starting LND installation/check process..." + if [ "$EUID" -eq 0 ]; then USER_HOME=$(getent passwd ${SUDO_USER} | cut -d: -f6) USER_NAME=$SUDO_USER @@ -9,81 +13,89 @@ install_lnd() { USER_NAME=$(whoami) fi + log "Checking latest LND version..." LND_VERSION=$(wget -qO- https://api.github.com/repos/lightningnetwork/lnd/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")') + log "Latest LND version: $LND_VERSION" + LND_URL="https://github.com/lightningnetwork/lnd/releases/download/${LND_VERSION}/lnd-${OS}-${ARCH}-${LND_VERSION}.tar.gz" # Check if LND is already installed if [ -d "$USER_HOME/lnd" ]; then + log "LND directory found. Checking current version..." CURRENT_VERSION=$("$USER_HOME/lnd/lnd" --version | grep -oP 'version \K[^\s]+') + log "Current LND version: $CURRENT_VERSION" + if [ "$CURRENT_VERSION" == "${LND_VERSION#v}" ]; then log "${SECONDARY_COLOR}LND${RESET_COLOR} is already up-to-date (version $CURRENT_VERSION)." - return 0 + lnd_status=2 # Set status to 2 to indicate no action needed else if [ "$SKIP_PROMPT" != true ]; then read -p "LND version $CURRENT_VERSION is installed. Do you want to upgrade to version $LND_VERSION? (y/N): " response case "$response" in [yY][eE][sS]|[yY]) log "${PRIMARY_COLOR}Upgrading${RESET_COLOR} ${SECONDARY_COLOR}LND${RESET_COLOR} from version $CURRENT_VERSION to $LND_VERSION..." - PERFORM_UPGRADE=true + lnd_status=1 # Set status to 1 to indicate upgrade ;; *) log "$(date '+%Y-%m-%d %H:%M:%S') Upgrade cancelled." - return 0 + lnd_status=2 # Set status to 2 to indicate no action needed ;; esac else log "${PRIMARY_COLOR}Upgrading${RESET_COLOR} ${SECONDARY_COLOR}LND${RESET_COLOR} from version $CURRENT_VERSION to $LND_VERSION..." - PERFORM_UPGRADE=true + lnd_status=1 # Set status to 1 to indicate upgrade fi fi + else + log "LND not found. Proceeding with fresh installation..." fi - log "${PRIMARY_COLOR}Downloading${RESET_COLOR} ${SECONDARY_COLOR}LND${RESET_COLOR}..." + if [ $lnd_status -eq 0 ] || [ $lnd_status -eq 1 ]; then + log "${PRIMARY_COLOR}Downloading${RESET_COLOR} ${SECONDARY_COLOR}LND${RESET_COLOR}..." - # Start the download - sudo -u $USER_NAME wget -q $LND_URL -O $USER_HOME/lnd.tar.gz || { - log "${PRIMARY_COLOR}Failed to download LND.${RESET_COLOR}" - exit 1 - } + # Start the download + sudo -u $USER_NAME wget -q $LND_URL -O $USER_HOME/lnd.tar.gz || { + log "${PRIMARY_COLOR}Failed to download LND.${RESET_COLOR}" + exit 1 + } - # Check if LND is already running and stop it if necessary (Linux) - if [ "$OS" = "Linux" ] && [ "$SYSTEMCTL_AVAILABLE" = true ]; then - if systemctl is-active --quiet lnd; then - log "${PRIMARY_COLOR}Stopping${RESET_COLOR} ${SECONDARY_COLOR}LND${RESET_COLOR} service..." - sudo systemctl stop lnd + # Check if LND is already running and stop it if necessary (Linux) + if [ "$OS" = "Linux" ] && [ "$SYSTEMCTL_AVAILABLE" = true ]; then + if systemctl is-active --quiet lnd; then + log "${PRIMARY_COLOR}Stopping${RESET_COLOR} ${SECONDARY_COLOR}LND${RESET_COLOR} service..." + sudo systemctl stop lnd + fi + else + log "${PRIMARY_COLOR}systemctl not found. Please stop ${SECONDARY_COLOR}LND${RESET_COLOR} manually if it is running.${RESET_COLOR}" fi - else - log "${PRIMARY_COLOR}systemctl not found. Please stop ${SECONDARY_COLOR}LND${RESET_COLOR} manually if it is running.${RESET_COLOR}" - fi - sudo -u $USER_NAME tar -xzf $USER_HOME/lnd.tar.gz -C $USER_HOME > /dev/null || { - log "${PRIMARY_COLOR}Failed to extract LND.${RESET_COLOR}" - exit 1 - } - rm $USER_HOME/lnd.tar.gz - sudo -u $USER_NAME mv $USER_HOME/lnd-* $USER_HOME/lnd + sudo -u $USER_NAME tar -xzf $USER_HOME/lnd.tar.gz -C $USER_HOME > /dev/null || { + log "${PRIMARY_COLOR}Failed to extract LND.${RESET_COLOR}" + exit 1 + } + rm $USER_HOME/lnd.tar.gz + sudo -u $USER_NAME mv $USER_HOME/lnd-* $USER_HOME/lnd - # Create .lnd directory if it doesn't exist - sudo -u $USER_NAME mkdir -p $USER_HOME/.lnd + # Create .lnd directory if it doesn't exist + sudo -u $USER_NAME mkdir -p $USER_HOME/.lnd - # Check if lnd.conf already exists and avoid overwriting it - if [ -f $USER_HOME/.lnd/lnd.conf ]; then - log "${PRIMARY_COLOR}lnd.conf already exists. Skipping creation of new lnd.conf file.${RESET_COLOR}" - else - sudo -u $USER_NAME bash -c "cat < $USER_HOME/.lnd/lnd.conf + # Check if lnd.conf already exists and avoid overwriting it + if [ -f $USER_HOME/.lnd/lnd.conf ]; then + log "${PRIMARY_COLOR}lnd.conf already exists. Skipping creation of new lnd.conf file.${RESET_COLOR}" + else + sudo -u $USER_NAME bash -c "cat < $USER_HOME/.lnd/lnd.conf bitcoin.mainnet=true bitcoin.node=neutrino neutrino.addpeer=neutrino.shock.network feeurl=https://nodes.lightning.computer/fees/v1/btc-fee-estimates.json EOF" + fi + + log "${SECONDARY_COLOR}LND${RESET_COLOR} installation and configuration completed." fi - log "${SECONDARY_COLOR}LND${RESET_COLOR} installation and configuration completed." - - # Return 1 if it was an upgrade, 0 otherwise - if [ "$PERFORM_UPGRADE" = true ]; then - return 1 - else - return 0 - fi + log "LND installation/check process complete. Status: $lnd_status" + # Echo the LND status + echo "LND_STATUS:$lnd_status" + return 0 # Always return 0 to indicate success } \ No newline at end of file diff --git a/scripts/start_services.sh b/scripts/start_services.sh index 4011ca72..cc1bdd46 100755 --- a/scripts/start_services.sh +++ b/scripts/start_services.sh @@ -1,7 +1,7 @@ #!/bin/bash start_services() { - LND_UPGRADE=$1 + LND_STATUS=$1 PUB_UPGRADE=$2 if [ "$EUID" -eq 0 ]; then @@ -47,55 +47,45 @@ EOF" sudo systemctl enable lnd >/dev/null 2>&1 sudo systemctl enable lightning_pub >/dev/null 2>&1 - if [ "$LND_UPGRADE" = "1" ]; then - log "${PRIMARY_COLOR}Restarting${RESET_COLOR} ${SECONDARY_COLOR}LND${RESET_COLOR} service..." - sudo systemctl restart lnd & - lnd_pid=$! - wait $lnd_pid - if systemctl is-active --quiet lnd; then - log "LND restarted successfully using systemd." + # Always attempt to start or restart LND + if systemctl is-active --quiet lnd; then + if [ "$LND_STATUS" = "1" ]; then + log "${PRIMARY_COLOR}Restarting${RESET_COLOR} ${SECONDARY_COLOR}LND${RESET_COLOR} service..." + sudo systemctl restart lnd else - log "Failed to restart ${SECONDARY_COLOR}LND${RESET_COLOR} using systemd." - exit 1 + log "${SECONDARY_COLOR}LND${RESET_COLOR} service is already running." fi else log "${PRIMARY_COLOR}Starting${RESET_COLOR} ${SECONDARY_COLOR}LND${RESET_COLOR} service..." - sudo systemctl start lnd & - lnd_pid=$! - wait $lnd_pid - if systemctl is-active --quiet lnd; then - log "LND started successfully using systemd." - else - log "Failed to start ${SECONDARY_COLOR}LND${RESET_COLOR} using systemd." - exit 1 - fi + sudo systemctl start lnd + fi + + # Check LND status after attempting to start/restart + if ! systemctl is-active --quiet lnd; then + log "Failed to start or restart ${SECONDARY_COLOR}LND${RESET_COLOR}. Please check the logs." + exit 1 fi log "Giving ${SECONDARY_COLOR}LND${RESET_COLOR} a few seconds to start before starting ${SECONDARY_COLOR}Lightning.Pub${RESET_COLOR}..." sleep 10 - if [ "$PUB_UPGRADE" = "100" ]; then - log "${PRIMARY_COLOR}Restarting${RESET_COLOR} ${SECONDARY_COLOR}Lightning.Pub${RESET_COLOR} service..." - sudo systemctl restart lightning_pub & - lightning_pub_pid=$! - wait $lightning_pub_pid - if systemctl is-active --quiet lightning_pub; then - log "Lightning.Pub restarted successfully using systemd." + # Always attempt to start or restart Lightning.Pub + if systemctl is-active --quiet lightning_pub; then + if [ "$PUB_UPGRADE" = "100" ]; then + log "${PRIMARY_COLOR}Restarting${RESET_COLOR} ${SECONDARY_COLOR}Lightning.Pub${RESET_COLOR} service..." + sudo systemctl restart lightning_pub else - log "Failed to restart ${SECONDARY_COLOR}Lightning.Pub${RESET_COLOR} using systemd." - exit 1 + log "${SECONDARY_COLOR}Lightning.Pub${RESET_COLOR} service is already running." fi else log "${PRIMARY_COLOR}Starting${RESET_COLOR} ${SECONDARY_COLOR}Lightning.Pub${RESET_COLOR} service..." - sudo systemctl start lightning_pub & - lightning_pub_pid=$! - wait $lightning_pub_pid - if systemctl is-active --quiet lightning_pub; then - log "Lightning.Pub started successfully using systemd." - else - log "Failed to start ${SECONDARY_COLOR}Lightning.Pub${RESET_COLOR} using systemd." - exit 1 - fi + sudo systemctl start lightning_pub + fi + + # Check Lightning.Pub status after attempting to start/restart + if ! systemctl is-active --quiet lightning_pub; then + log "Failed to start or restart ${SECONDARY_COLOR}Lightning.Pub${RESET_COLOR}. Please check the logs." + exit 1 fi else