This commit is contained in:
shocknet-justin 2025-08-27 14:27:55 -04:00
parent e8d14cd081
commit 9dad0b81cd
2 changed files with 45 additions and 52 deletions

View file

@ -1,17 +1,14 @@
#!/bin/bash
set -e
# Use user-space log file
LOG_FILE="$HOME/lightning_pub/install.log"
mkdir -p "$(dirname "$LOG_FILE")"
touch $LOG_FILE
chmod 644 $LOG_FILE
# --- Use a secure temporary log file during installation ---
TMP_LOG_FILE=$(mktemp)
log() {
local message="$(date '+%Y-%m-%d %H:%M:%S') $1"
echo -e "$message"
echo -e "$(echo $message | sed 's/\\e\[[0-9;]*m//g')" >> $LOG_FILE
# Write to the temporary log file.
echo -e "$(echo "$message" | sed 's/\\e\[[0-9;]*m//g')" >> "$TMP_LOG_FILE"
}
SCRIPT_VERSION="0.2.0"
@ -107,4 +104,13 @@ else
get_log_info || log_error "Failed to get log info" 1
log "Installation process completed successfully"
# --- Move temporary log to permanent location ---
if [ -d "$HOME/lightning_pub" ]; then
mv "$TMP_LOG_FILE" "$HOME/lightning_pub/install.log"
chmod 600 "$HOME/lightning_pub/install.log"
else
# If the installation failed before the dir was created, clean up the temp log.
rm -f "$TMP_LOG_FILE"
fi
fi

View file

@ -31,57 +31,36 @@ install_lightning_pub() {
}
rm $USER_HOME/lightning_pub.tar.gz
# Check if directory exists and is not empty to determine if it's an upgrade
if [ -d "$USER_HOME/lightning_pub" ] && [ "$(ls -A $USER_HOME/lightning_pub)" ]; then
log "Checking if Lightning.Pub update is needed..."
# Decide flow based on whether a valid previous installation exists.
if [ -f "$USER_HOME/lightning_pub/.installed_commit" ] || [ -f "$USER_HOME/lightning_pub/db.sqlite" ]; then
# --- UPGRADE PATH ---
log "Existing installation found. Checking for updates..."
# Check if update is needed by comparing commit hashes
# Get latest commit hash from GitHub API, logging the response for debugging.
API_RESPONSE=$(wget -qO- "https://api.github.com/repos/${REPO}/commits/${BRANCH}" 2>&1 | tee /tmp/api_response.log)
# Parse commit hash from the commit html_url first (most robust, dependency-free)
LATEST_COMMIT=$(echo "$API_RESPONSE" | awk -F'[/"]' '/"html_url": ".*\/commit\// {print $(NF-1); exit}')
# If parsing failed, check explicitly for a rate-limit message; otherwise abort with full response
if [ -z "$LATEST_COMMIT" ]; then
if grep -q '"message"[[:space:]]*:[[:space:]]*"API rate limit exceeded"' <<< "$API_RESPONSE"; then
log_error "GitHub API rate limit exceeded. Please wait a while before trying again." 1
fi
LATEST_COMMIT=$(echo "$API_RESPONSE" | awk -F'[/"]' '/"html_url": ".*\/commit\// {print $(NF-1); exit}')
if [ -z "$LATEST_COMMIT" ]; then
log "GitHub API response was not as expected. Full response for debugging:"
log "$API_RESPONSE"
log_error "Could not retrieve latest version from GitHub. Upgrade check failed. Aborting." 1
fi
# Check if we have a stored commit hash and compare
if [ -f "$USER_HOME/lightning_pub/.installed_commit" ]; then
CURRENT_COMMIT=$(cat "$USER_HOME/lightning_pub/.installed_commit" 2>/dev/null | head -c 40)
if [ "$CURRENT_COMMIT" = "$LATEST_COMMIT" ]; then
log "${SECONDARY_COLOR}Lightning.Pub${RESET_COLOR} is already at the latest commit. No update needed."
rm -rf $USER_HOME/lightning_pub_temp
return 2 # Special exit code to indicate no changes
fi
return 2
fi
log "Upgrading existing Lightning.Pub installation..."
upgrade_status=100 # Use 100 to indicate an upgrade
else
# If directory exists but is empty, remove it to ensure a clean 'mv'
if [ -d "$USER_HOME/lightning_pub" ]; then
rm -d "$USER_HOME/lightning_pub"
fi
log "Performing fresh Lightning.Pub installation..."
upgrade_status=0
fi
upgrade_status=100
# Merge if upgrade
if [ $upgrade_status -eq 100 ]; then
log "Backing up user data before upgrade..."
BACKUP_DIR="$USER_HOME/lightning_pub_backup_$(date +%s)"
mkdir -p "$BACKUP_DIR"
# Move files and folders to preserve to the backup directory
# Use 2>/dev/null to suppress errors if files don't exist
mv "$USER_HOME/lightning_pub"/*.sqlite "$BACKUP_DIR/" 2>/dev/null || true
mv "$USER_HOME/lightning_pub"/.env "$BACKUP_DIR/" 2>/dev/null || true
mv "$USER_HOME/lightning_pub"/logs "$BACKUP_DIR/" 2>/dev/null || true
@ -94,20 +73,28 @@ install_lightning_pub() {
mv "$USER_HOME/lightning_pub"/admin.enroll "$BACKUP_DIR/" 2>/dev/null || true
log "Replacing application files..."
# Remove the old application directory (user data is now backed up)
rm -rf "$USER_HOME/lightning_pub"
# Move the new version into place
mv "$USER_HOME/lightning_pub_temp" "$USER_HOME/lightning_pub"
log "Restoring user data..."
# Move the backed-up data into the new directory
if [ -n "$(ls -A "$BACKUP_DIR" 2>/dev/null)" ]; then
cp -r "$BACKUP_DIR"/* "$USER_HOME/lightning_pub/"
# Clean up the backup directory
fi
rm -rf "$BACKUP_DIR"
elif [ -d "$USER_HOME/lightning_pub" ]; then
# --- CONFLICT/UNSAFE PATH ---
# This handles the case where the directory exists but is not a valid install (e.g., a git clone).
log_error "Directory '~/lightning_pub' already exists but does not appear to be a valid installation. For your safety, please manually back up and remove this directory, then run the installer again." 1
else
# --- FRESH INSTALL PATH ---
# This path is only taken if the ~/lightning_pub directory does not exist.
log "Performing fresh Lightning.Pub installation..."
upgrade_status=0
mv "$USER_HOME/lightning_pub_temp" "$USER_HOME/lightning_pub"
fi
rm -rf $USER_HOME/lightning_pub_temp
# Load nvm and npm
@ -131,9 +118,9 @@ install_lightning_pub() {
fi
# Store the commit hash for future update checks
if [ -n "$LATEST_COMMIT" ]; then
# Note: LATEST_COMMIT will be empty on a fresh install, which is fine.
# The file will be created, and the next run will be an upgrade.
echo "$LATEST_COMMIT" > "$USER_HOME/lightning_pub/.installed_commit"
fi
return 0
return $upgrade_status
}