mktemp
This commit is contained in:
parent
e8d14cd081
commit
9dad0b81cd
2 changed files with 45 additions and 52 deletions
|
|
@ -1,17 +1,14 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
# Use user-space log file
|
# --- Use a secure temporary log file during installation ---
|
||||||
LOG_FILE="$HOME/lightning_pub/install.log"
|
TMP_LOG_FILE=$(mktemp)
|
||||||
|
|
||||||
mkdir -p "$(dirname "$LOG_FILE")"
|
|
||||||
touch $LOG_FILE
|
|
||||||
chmod 644 $LOG_FILE
|
|
||||||
|
|
||||||
log() {
|
log() {
|
||||||
local message="$(date '+%Y-%m-%d %H:%M:%S') $1"
|
local message="$(date '+%Y-%m-%d %H:%M:%S') $1"
|
||||||
echo -e "$message"
|
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"
|
SCRIPT_VERSION="0.2.0"
|
||||||
|
|
@ -107,4 +104,13 @@ else
|
||||||
get_log_info || log_error "Failed to get log info" 1
|
get_log_info || log_error "Failed to get log info" 1
|
||||||
|
|
||||||
log "Installation process completed successfully"
|
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
|
fi
|
||||||
|
|
@ -31,57 +31,36 @@ install_lightning_pub() {
|
||||||
}
|
}
|
||||||
rm $USER_HOME/lightning_pub.tar.gz
|
rm $USER_HOME/lightning_pub.tar.gz
|
||||||
|
|
||||||
# Check if directory exists and is not empty to determine if it's an upgrade
|
# Decide flow based on whether a valid previous installation exists.
|
||||||
if [ -d "$USER_HOME/lightning_pub" ] && [ "$(ls -A $USER_HOME/lightning_pub)" ]; then
|
if [ -f "$USER_HOME/lightning_pub/.installed_commit" ] || [ -f "$USER_HOME/lightning_pub/db.sqlite" ]; then
|
||||||
log "Checking if Lightning.Pub update is needed..."
|
# --- UPGRADE PATH ---
|
||||||
|
log "Existing installation found. Checking for updates..."
|
||||||
|
|
||||||
# Check if update is needed by comparing commit hashes
|
# 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)
|
API_RESPONSE=$(wget -qO- "https://api.github.com/repos/${REPO}/commits/${BRANCH}" 2>&1 | tee /tmp/api_response.log)
|
||||||
|
if grep -q '"message"[[:space:]]*:[[:space:]]*"API rate limit exceeded"' <<< "$API_RESPONSE"; then
|
||||||
# Parse commit hash from the commit html_url first (most robust, dependency-free)
|
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}')
|
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 [ -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
|
|
||||||
log "GitHub API response was not as expected. Full response for debugging:"
|
log "GitHub API response was not as expected. Full response for debugging:"
|
||||||
log "$API_RESPONSE"
|
log "$API_RESPONSE"
|
||||||
log_error "Could not retrieve latest version from GitHub. Upgrade check failed. Aborting." 1
|
log_error "Could not retrieve latest version from GitHub. Upgrade check failed. Aborting." 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check if we have a stored commit hash and compare
|
CURRENT_COMMIT=$(cat "$USER_HOME/lightning_pub/.installed_commit" 2>/dev/null | head -c 40)
|
||||||
if [ -f "$USER_HOME/lightning_pub/.installed_commit" ]; then
|
if [ "$CURRENT_COMMIT" = "$LATEST_COMMIT" ]; then
|
||||||
CURRENT_COMMIT=$(cat "$USER_HOME/lightning_pub/.installed_commit" 2>/dev/null | head -c 40)
|
log "${SECONDARY_COLOR}Lightning.Pub${RESET_COLOR} is already at the latest commit. No update needed."
|
||||||
|
rm -rf $USER_HOME/lightning_pub_temp
|
||||||
if [ "$CURRENT_COMMIT" = "$LATEST_COMMIT" ]; then
|
return 2
|
||||||
log "${SECONDARY_COLOR}Lightning.Pub${RESET_COLOR} is already at the latest commit. No update needed."
|
fi
|
||||||
rm -rf $USER_HOME/lightning_pub_temp
|
|
||||||
return 2 # Special exit code to indicate no changes
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
log "Upgrading existing Lightning.Pub installation..."
|
log "Upgrading existing Lightning.Pub installation..."
|
||||||
upgrade_status=100 # Use 100 to indicate an upgrade
|
upgrade_status=100
|
||||||
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
|
|
||||||
|
|
||||||
# Merge if upgrade
|
|
||||||
if [ $upgrade_status -eq 100 ]; then
|
|
||||||
log "Backing up user data before upgrade..."
|
log "Backing up user data before upgrade..."
|
||||||
BACKUP_DIR="$USER_HOME/lightning_pub_backup_$(date +%s)"
|
BACKUP_DIR="$USER_HOME/lightning_pub_backup_$(date +%s)"
|
||||||
mkdir -p "$BACKUP_DIR"
|
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"/*.sqlite "$BACKUP_DIR/" 2>/dev/null || true
|
||||||
mv "$USER_HOME/lightning_pub"/.env "$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
|
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
|
mv "$USER_HOME/lightning_pub"/admin.enroll "$BACKUP_DIR/" 2>/dev/null || true
|
||||||
|
|
||||||
log "Replacing application files..."
|
log "Replacing application files..."
|
||||||
# Remove the old application directory (user data is now backed up)
|
|
||||||
rm -rf "$USER_HOME/lightning_pub"
|
rm -rf "$USER_HOME/lightning_pub"
|
||||||
# Move the new version into place
|
|
||||||
mv "$USER_HOME/lightning_pub_temp" "$USER_HOME/lightning_pub"
|
mv "$USER_HOME/lightning_pub_temp" "$USER_HOME/lightning_pub"
|
||||||
|
|
||||||
log "Restoring user data..."
|
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/"
|
cp -r "$BACKUP_DIR"/* "$USER_HOME/lightning_pub/"
|
||||||
|
fi
|
||||||
# Clean up the backup directory
|
|
||||||
rm -rf "$BACKUP_DIR"
|
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
|
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"
|
mv "$USER_HOME/lightning_pub_temp" "$USER_HOME/lightning_pub"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
rm -rf $USER_HOME/lightning_pub_temp
|
rm -rf $USER_HOME/lightning_pub_temp
|
||||||
|
|
||||||
# Load nvm and npm
|
# Load nvm and npm
|
||||||
|
|
@ -131,9 +118,9 @@ install_lightning_pub() {
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Store the commit hash for future update checks
|
# 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.
|
||||||
echo "$LATEST_COMMIT" > "$USER_HOME/lightning_pub/.installed_commit"
|
# The file will be created, and the next run will be an upgrade.
|
||||||
fi
|
echo "$LATEST_COMMIT" > "$USER_HOME/lightning_pub/.installed_commit"
|
||||||
|
|
||||||
return 0
|
return $upgrade_status
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue