Merge pull request #839 from shocknet/script

Script
This commit is contained in:
Justin (shocknet) 2025-09-25 14:49:54 -04:00 committed by GitHub
commit bb31f64d5e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 56 additions and 18 deletions

View file

@ -11,7 +11,7 @@ log() {
echo -e "$(echo "$message" | sed 's/\\e\[[0-9;]*m//g')" >> "$TMP_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.1"
REPO="shocknet/Lightning.Pub" REPO="shocknet/Lightning.Pub"
BRANCH="master" BRANCH="master"
@ -31,10 +31,7 @@ log_error() {
modules=( modules=(
"utils" "utils"
"check_homebrew" # NOTE: Used for macOS, which is untested/unsupported "ports"
"install_rsync_mac" # NOTE: Used for macOS, which is untested/unsupported
"create_launchd_plist" # NOTE: Used for macOS, which is untested/unsupported
"start_services_mac" # NOTE: Used for macOS, which is untested/unsupported
"install_lnd" "install_lnd"
"install_nodejs" "install_nodejs"
"install_lightning_pub" "install_lightning_pub"
@ -94,8 +91,7 @@ log "Detected OS: $OS"
log "Detected ARCH: $ARCH" log "Detected ARCH: $ARCH"
if [ "$OS" = "Mac" ]; then if [ "$OS" = "Mac" ]; then
log "Handling macOS specific setup" log_error "macOS is not currently supported by this install script. Please use a Linux-based system." 1
handle_macos || log_error "macOS setup failed" 1
else else
# Explicit kickoff log for LND so the flow is clear in the install log # Explicit kickoff log for LND so the flow is clear in the install log
log "${PRIMARY_COLOR}Installing${RESET_COLOR} ${SECONDARY_COLOR}LND${RESET_COLOR}..." log "${PRIMARY_COLOR}Installing${RESET_COLOR} ${SECONDARY_COLOR}LND${RESET_COLOR}..."

View file

@ -89,17 +89,33 @@ install_lnd() {
# Create .lnd directory if it doesn't exist # Create .lnd directory if it doesn't exist
mkdir -p $USER_HOME/.lnd mkdir -p $USER_HOME/.lnd
# Check if lnd.conf already exists and avoid overwriting it # Ensure lnd.conf exists.
if [ -f $USER_HOME/.lnd/lnd.conf ]; then touch $USER_HOME/.lnd/lnd.conf
log "${PRIMARY_COLOR}lnd.conf already exists. Skipping creation of new lnd.conf file.${RESET_COLOR}"
else # Check for and add default settings only if the keys are missing.
cat <<EOF > $USER_HOME/.lnd/lnd.conf grep -q "^bitcoin.mainnet=" $USER_HOME/.lnd/lnd.conf || echo "bitcoin.mainnet=true" >> $USER_HOME/.lnd/lnd.conf
bitcoin.mainnet=true grep -q "^bitcoin.node=" $USER_HOME/.lnd/lnd.conf || echo "bitcoin.node=neutrino" >> $USER_HOME/.lnd/lnd.conf
bitcoin.node=neutrino grep -q "^neutrino.addpeer=" $USER_HOME/.lnd/lnd.conf || echo "neutrino.addpeer=neutrino.shock.network" >> $USER_HOME/.lnd/lnd.conf
neutrino.addpeer=neutrino.shock.network grep -q "^fee.url=" $USER_HOME/.lnd/lnd.conf || echo "fee.url=https://nodes.lightning.computer/fees/v1/btc-fee-estimates.json" >> $USER_HOME/.lnd/lnd.conf
fee.url=https://nodes.lightning.computer/fees/v1/btc-fee-estimates.json
EOF chmod 600 $USER_HOME/.lnd/lnd.conf
chmod 600 $USER_HOME/.lnd/lnd.conf
# Port conflict resolution.
local lnd_port=9735
if ! is_port_available $lnd_port; then
# The port is occupied. We should intervene if our service is either in a failed state
# or not active at all (which covers fresh installs and failure loops).
if systemctl --user -q is-failed lnd.service 2>/dev/null || ! systemctl --user -q is-active lnd.service 2>/dev/null; then
log "Port $lnd_port is occupied and LND service is not healthy. Attempting to resolve by finding a new port."
lnd_port_new=$(find_available_port $lnd_port)
log "Configuring LND to use new port $lnd_port_new."
sed -i '/^listen=/d' $USER_HOME/.lnd/lnd.conf
echo "listen=0.0.0.0:$lnd_port_new" >> $USER_HOME/.lnd/lnd.conf
log "LND configuration updated. The service will be restarted by the installer."
else
log "Port $lnd_port is in use by a healthy LND service (assumed to be our own). No changes will be made."
fi
fi fi
log "${SECONDARY_COLOR}LND${RESET_COLOR} installation and configuration completed." log "${SECONDARY_COLOR}LND${RESET_COLOR} installation and configuration completed."

26
scripts/ports.sh Normal file
View file

@ -0,0 +1,26 @@
#!/bin/bash
# --- Port Management ---
# Finds an available TCP port, starting from a given base port.
# If the base port is in use, it increments until a free port is found.
#
# @param base_port The starting port number.
# @return The first available port number.
find_available_port() {
local port=$1
while ! is_port_available "$port"; do
log "Port $port is in use. Checking next port..." >&2
port=$((port + 1))
done
echo "$port"
}
# Checks if a given TCP port is available.
#
# @param port The port number to check.
# @return 0 if the port is available, 1 otherwise.
is_port_available() {
local port=$1
! lsof -iTCP:"$port" -sTCP:LISTEN -P -t >/dev/null 2>&1
}