From 351b3e16fa99ad78c0012cb94368a4e50332cceb Mon Sep 17 00:00:00 2001 From: "Justin (shocknet)" Date: Tue, 23 Jul 2024 13:36:14 -0400 Subject: [PATCH] deploy --- scripts/extract_nprofile.sh | 14 +++++-- scripts/install.sh | 50 +++++++++++++++------- scripts/install_lightning_pub.sh | 71 ++++++++++++++------------------ scripts/start_services.sh | 4 +- scripts/utils.sh | 6 +-- 5 files changed, 80 insertions(+), 65 deletions(-) diff --git a/scripts/extract_nprofile.sh b/scripts/extract_nprofile.sh index f908a099..44d718b2 100644 --- a/scripts/extract_nprofile.sh +++ b/scripts/extract_nprofile.sh @@ -29,10 +29,18 @@ get_log_info() { exit 1 fi - # Wait for wallet status in log file + # Get the initial line count + initial_lines=$(wc -l < "$latest_unlocker_log") + + # Wait for new wallet status in log file while [ $(($(date +%s) - START_TIME)) -lt $MAX_WAIT_TIME ]; do - latest_entry=$(grep -E "unlocker >> (macaroon not found, creating wallet|wallet is locked, unlocking|the wallet is already unlocked|created wallet with pub)" "$latest_unlocker_log" | tail -n 1) - [ -n "$latest_entry" ] && break + 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) + if [ -n "$latest_entry" ]; then + break + fi + fi sleep $WAIT_INTERVAL done diff --git a/scripts/install.sh b/scripts/install.sh index 4aa9db8b..d7cd4e25 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -1,6 +1,13 @@ #!/bin/bash set -e +# Function to log errors +log_error() { + log "ERROR: $1" + log "Exiting with status $2" + exit $2 +} + BASE_URL="https://raw.githubusercontent.com/shocknet/Lightning.Pub/master/scripts/" modules=( @@ -21,26 +28,37 @@ for module in "${modules[@]}"; do source "/tmp/${module}.sh" done -# Upgrade flag -SKIP_PROMPT=false -for arg in "$@"; do - case $arg in - --yes) - SKIP_PROMPT=true - shift - ;; - esac -done - detect_os_arch if [ "$OS" = "Mac" ]; then handle_macos else - install_lnd - LND_UPGRADE=$? - install_nodejs - PUB_UPGRADE_STATUS=$(install_lightning_pub) - start_services $LND_UPGRADE $PUB_UPGRADE_STATUS + install_lnd || log_error "LND installation failed" $? + lnd_upgrade_status=$? + + install_nodejs || log_error "NodeJS installation failed" $? + + pub_output=$(install_lightning_pub) + install_result=$? + + if [ $install_result -ne 0 ]; then + log_error "Lightning.Pub installation failed" $install_result + fi + + # Extract the upgrade status from the output + pub_upgrade_status=$(echo "$pub_output" | grep "UPGRADE_STATUS:" | cut -d':' -f2) + + if [ "$pub_upgrade_status" = "100" ]; then + log "Lightning.Pub upgrade completed successfully." + elif [ "$pub_upgrade_status" = "0" ]; then + log "Lightning.Pub fresh installation completed successfully." + else + log "WARNING: Unexpected return status from install_lightning_pub: $pub_upgrade_status" + log "Full output: $pub_output" + fi + + start_services $lnd_upgrade_status $pub_upgrade_status get_log_info + + log "Installation process completed successfully" fi \ No newline at end of file diff --git a/scripts/install_lightning_pub.sh b/scripts/install_lightning_pub.sh index 543572a0..3acb8129 100755 --- a/scripts/install_lightning_pub.sh +++ b/scripts/install_lightning_pub.sh @@ -1,6 +1,8 @@ #!/bin/bash install_lightning_pub() { + local upgrade_status=0 + if [ "$EUID" -eq 0 ]; then USER_HOME=$(getent passwd ${SUDO_USER} | cut -d: -f6) USER_NAME=$SUDO_USER @@ -12,69 +14,58 @@ install_lightning_pub() { log "${PRIMARY_COLOR}Installing${RESET_COLOR} ${SECONDARY_COLOR}Lightning.Pub${RESET_COLOR}..." REPO_URL="https://github.com/shocknet/Lightning.Pub/tarball/master" - sudo -u $USER_NAME wget $REPO_URL -O $USER_HOME/lightning_pub.tar.gz > /dev/null 2>&1 || { + sudo -u $USER_NAME wget -q $REPO_URL -O $USER_HOME/lightning_pub.tar.gz > /dev/null 2>&1 || { log "${PRIMARY_COLOR}Failed to download Lightning.Pub.${RESET_COLOR}" - exit 1 + return 1 } sudo -u $USER_NAME mkdir -p $USER_HOME/lightning_pub_temp - sudo -u $USER_NAME tar -xvzf $USER_HOME/lightning_pub.tar.gz -C $USER_HOME/lightning_pub_temp --strip-components=1 > /dev/null 2>&1 || { + sudo -u $USER_NAME tar -xzf $USER_HOME/lightning_pub.tar.gz -C $USER_HOME/lightning_pub_temp --strip-components=1 > /dev/null 2>&1 || { log "${PRIMARY_COLOR}Failed to extract Lightning.Pub.${RESET_COLOR}" - exit 1 + return 1 } rm $USER_HOME/lightning_pub.tar.gz - if ! command -v rsync &> /dev/null; then - log "${PRIMARY_COLOR}rsync not found, installing...${RESET_COLOR}" - if [ "$OS" = "Mac" ]; then - brew install rsync - elif [ "$OS" = "Linux" ]; then - if [ -x "$(command -v apt-get)" ]; then - sudo apt-get update > /dev/null 2>&1 - sudo apt-get install -y rsync > /dev/null 2>&1 - elif [ -x "$(command -v yum)" ]; then - sudo yum install -y rsync > /dev/null 2>&1 - else - log "${PRIMARY_COLOR}Package manager not found. Please install rsync manually.${RESET_COLOR}" - exit 1 - fi - else - log "${PRIMARY_COLOR}Package manager not found. Please install rsync manually.${RESET_COLOR}" - exit 1 - fi - fi - if [ -d "$USER_HOME/lightning_pub" ]; then - log "${SECONDARY_COLOR}Lightning.Pub${RESET_COLOR} is already installed. Upgrading..." - PUB_UPGRADE=true + log "Upgrading existing Lightning.Pub installation..." + upgrade_status=100 # Use 100 to indicate an upgrade else - PUB_UPGRADE=false + log "Performing fresh Lightning.Pub installation..." + upgrade_status=0 fi # Merge if upgrade - rsync -av --exclude='*.sqlite' --exclude='.env' --exclude='logs' --exclude='node_modules' --exclude='.jwt_secret' --exclude='.wallet_secret' --exclude='admin.npub' --exclude='app.nprofile' --exclude='.admin_connect' --exclude='.admin_enroll' lightning_pub_temp/ lightning_pub/ > /dev/null 2>&1 - rm -rf lightning_pub_temp + if [ $upgrade_status -eq 100 ]; then + rsync -a --quiet --exclude='*.sqlite' --exclude='.env' --exclude='logs' --exclude='node_modules' --exclude='.jwt_secret' --exclude='.wallet_secret' --exclude='admin.npub' --exclude='app.nprofile' --exclude='.admin_connect' --exclude='.admin_enroll' $USER_HOME/lightning_pub_temp/ $USER_HOME/lightning_pub/ + else + mv $USER_HOME/lightning_pub_temp $USER_HOME/lightning_pub + fi + rm -rf $USER_HOME/lightning_pub_temp # Load nvm and npm export NVM_DIR="${NVM_DIR}" [ -s "${NVM_DIR}/nvm.sh" ] && \. "${NVM_DIR}/nvm.sh" - cd lightning_pub + cd $USER_HOME/lightning_pub log "${PRIMARY_COLOR}Installing${RESET_COLOR} npm dependencies..." - npm install > npm_install.log 2>&1 || { - log "${PRIMARY_COLOR}Failed to install npm dependencies.${RESET_COLOR}" - exit 1 - } + npm install > npm_install.log 2>&1 + npm_exit_code=$? - if [ "$PUB_UPGRADE" = true ]; then - PUB_UPGRADE_STATUS=1 - else - PUB_UPGRADE_STATUS=0 + if [ $npm_exit_code -ne 0 ]; then + log "${PRIMARY_COLOR}Failed to install npm dependencies. Check npm_install.log for details.${RESET_COLOR}" + return 1 fi - log "PUB_UPGRADE_STATUS set to $PUB_UPGRADE_STATUS" + if [ ! -d "node_modules" ] || [ -z "$(ls -A node_modules)" ]; then + log "${PRIMARY_COLOR}npm install completed, but node_modules is empty or missing. Installation may have failed.${RESET_COLOR}" + return 1 + fi - echo $PUB_UPGRADE_STATUS + log "npm dependencies installed successfully." + + # Echo a specific string followed by the upgrade status + echo "UPGRADE_STATUS:$upgrade_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 f2abbd11..4011ca72 100755 --- a/scripts/start_services.sh +++ b/scripts/start_services.sh @@ -47,7 +47,7 @@ EOF" sudo systemctl enable lnd >/dev/null 2>&1 sudo systemctl enable lightning_pub >/dev/null 2>&1 - if [ "$LND_UPGRADE" = true ]; then + if [ "$LND_UPGRADE" = "1" ]; then log "${PRIMARY_COLOR}Restarting${RESET_COLOR} ${SECONDARY_COLOR}LND${RESET_COLOR} service..." sudo systemctl restart lnd & lnd_pid=$! @@ -74,7 +74,7 @@ EOF" 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" = "1" ]; then + 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=$! diff --git a/scripts/utils.sh b/scripts/utils.sh index bee666e2..7a8155db 100755 --- a/scripts/utils.sh +++ b/scripts/utils.sh @@ -10,9 +10,7 @@ chmod 644 $LOG_FILE log() { local message="$(date '+%Y-%m-%d %H:%M:%S') $1" - if [ -t 1 ]; then - echo -e "$message" - fi + echo -e "$message" echo -e "$(echo $message | sed 's/\\e\[[0-9;]*m//g')" >> $LOG_FILE } @@ -37,4 +35,4 @@ detect_os_arch() { else SYSTEMCTL_AVAILABLE=false fi -} +} \ No newline at end of file