Update 91-configure.sh

This commit is contained in:
Alexandre
2026-01-10 10:29:52 +01:00
committed by GitHub
parent 8a5e058f1b
commit 0f781792f6

View File

@@ -8,48 +8,28 @@ set -e
APP_UID=20211 APP_UID=20211
# CRITICAL: ensure newly-created files (like /config/config/app.conf on first run) are not root-only # 1. Fix the directories
umask 022 for folder in /tmp/run/tmp /tmp/api /tmp/log /tmp/run /tmp/nginx/active-config "$TMP_DIR" "$NETALERTX_DATA" "$NETALERTX_DB" "$NETALERTX_CONFIG"; do
export UMASK=022 mkdir -p "$folder"
chown -R $APP_UID:$APP_UID "$folder"
# Ensure base dirs exist and have sane perms for the app user chmod -R 755 "$folder"
mkdir -p /config/config /config/db
chown -R "${APP_UID}:${APP_UID}" /config/config /config/db
chmod 775 /config/config /config/db
# 1. Fix the directories (skip empty vars)
for folder in \
/tmp/run/tmp \
/tmp/api \
/tmp/log \
/tmp/run \
/tmp/nginx/active-config \
"${TMP_DIR:-}" \
"${NETALERTX_DATA:-}" \
"${NETALERTX_DB:-}" \
"${NETALERTX_CONFIG:-}"
do
[[ -z "${folder}" ]] && continue
mkdir -p "${folder}"
chown -R "${APP_UID}:${APP_UID}" "${folder}"
chmod -R 755 "${folder}"
done done
# 2. Fix /tmp and Standard Streams (CRITICAL) # 2. Fix /tmp and Standard Streams (CRITICAL)
chmod -R 1777 /tmp chmod -R 1777 /tmp
# Allow non-root user to write to container logs # This allows the non-root user to write to the container logs
chmod 666 /dev/stdout /dev/stderr chmod 666 /dev/stdout /dev/stderr
# 3. Pre-create and chown log files # 3. Pre-create and chown log files
touch /tmp/log/app.php_errors.log /tmp/log/cron.log /tmp/log/stdout.log /tmp/log/stderr.log touch /tmp/log/app.php_errors.log /tmp/log/cron.log /tmp/log/stdout.log /tmp/log/stderr.log
chown "${APP_UID}:${APP_UID}" /tmp/log/*.log chown $APP_UID:$APP_UID /tmp/log/*.log
# 4. Create Symlinks # 4. Create Symlinks
for item in db config; do for item in db config; do
rm -rf "/data/${item}" rm -rf "/data/$item"
ln -sf "/config/${item}" "/data/${item}" ln -sf "/config/$item" "/data/$item"
chown -R "${APP_UID}:${APP_UID}" "/data/${item}" chown -R $APP_UID:$APP_UID "/data/$item"
chmod -R 755 "/data/${item}" chmod -R 755 "/data/$item"
done done
# Fix php # Fix php
@@ -61,64 +41,79 @@ sed -i "/default_type/a include /etc/nginx/http.d/ingress.conf;" "${SYSTEM_NGINX
# Configure network # # Configure network #
##################### #####################
# Configuration file path
config_file="/config/config/app.conf" config_file="/config/config/app.conf"
# If DB already exists, ensure its readable/writable by the app user if [ -f /config/db/app.db ]; then
if [[ -f /config/db/app.db ]]; then chmod a+rwx /config/db/app.db
chown "${APP_UID}:${APP_UID}" /config/db/app.db || true
chmod 664 /config/db/app.db || true
fi fi
# Function to execute the main logic
execute_main_logic() { execute_main_logic() {
bashio::log.info "Initiating scan of Home Assistant network configuration..." bashio::log.info "Initiating scan of Home Assistant network configuration..."
# Get the local IPv4 address
local_ip="$(bashio::network.ipv4_address)" local_ip="$(bashio::network.ipv4_address)"
local_ip="${local_ip%/*}" local_ip="${local_ip%/*}" # Remove CIDR notation
echo "... Detected local IP: ${local_ip}" echo "... Detected local IP: $local_ip"
echo "... Scanning network for changes" echo "... Scanning network for changes"
if ! command -v arp-scan &>/dev/null; then # Ensure arp-scan is installed
if ! command -v arp-scan &> /dev/null; then
bashio::log.error "arp-scan command not found. Please install arp-scan to proceed." bashio::log.error "arp-scan command not found. Please install arp-scan to proceed."
exit 1 exit 1
fi fi
if [[ ! -f "${config_file}" ]]; then # Get current settings
bashio::log.warning "Config file not present yet (${config_file}); skipping network scan update." if ! grep -q "^SCAN_SUBNETS" "$config_file"; then
return 0 bashio::log.fatal "SCAN_SUBNETS is not found in your $config_file, please correct your file first"
fi
# Make sure the app user can read it (covers upgrades / odd umask cases)
chown "${APP_UID}:${APP_UID}" "${config_file}" 2>/dev/null || true
chmod 664 "${config_file}" 2>/dev/null || true
if ! grep -q "^SCAN_SUBNETS" "${config_file}"; then
bashio::log.fatal "SCAN_SUBNETS is not found in ${config_file}, please correct your file first"
return 1
fi fi
# Iterate over network interfaces
for interface in $(bashio::network.interfaces); do for interface in $(bashio::network.interfaces); do
echo "Scanning interface: ${interface}" echo "Scanning interface: $interface"
if grep -q "${interface}" "${config_file}"; then # Check if the interface is already configured
echo "... ${interface} is already configured in app.conf" if grep -q "$interface" "$config_file"; then
continue echo "... $interface is already configured in app.conf"
fi else
# Update SCAN_SUBNETS in app.conf
SCAN_SUBNETS="$(grep "^SCAN_SUBNETS" "${config_file}" | head -1)" SCAN_SUBNETS="$(grep "^SCAN_SUBNETS" "$config_file" | head -1)"
if [[ "${SCAN_SUBNETS}" != *"${local_ip}"*"${interface}"* ]]; then if [[ "$SCAN_SUBNETS" != *"$local_ip"*"$interface"* ]]; then
NEW_SCAN_SUBNETS="${SCAN_SUBNETS%]}, '${local_ip}/24 --interface=${interface}']" # Add to the app.conf
sed -i "/^SCAN_SUBNETS/c\\${NEW_SCAN_SUBNETS}" "${config_file}" NEW_SCAN_SUBNETS="${SCAN_SUBNETS%]}, '${local_ip}/24 --interface=${interface}']"
sed -i "/^SCAN_SUBNETS/c\\$NEW_SCAN_SUBNETS" "$config_file"
VALUE="$(arp-scan --interface="${interface}" "${local_ip}/24" 2>/dev/null \ # Check availability of hosts
| grep "responded" \ VALUE="$(arp-scan --interface="$interface" "${local_ip}/24" 2> /dev/null \
| awk -F'.' '{print $NF}' \ | grep "responded" \
| awk '{print $1}' || true)" | awk -F'.' '{print $NF}' \
| awk '{print $1}' || true)"
echo "... ${interface} is available in Home Assistant (with ${VALUE} devices), added to app.conf" echo "... $interface is available in Home Assistant (with $VALUE devices), added to app.conf"
fi
fi fi
done done
bashio::log.info "Network scan completed." bashio::log.info "Network scan completed."
} }
execute_main_logic || true # Function to wait for the config file
wait_for_config_file() {
if [ ! -f "$config_file" ]; then
echo "Waiting for $config_file to become available..."
while [ ! -f "$config_file" ]; do
sleep 5 # Wait for 5 seconds before checking again
done
echo "$config_file is now available. Restarting the addon."
bashio::addon.restart
fi
execute_main_logic
}
# Main script logic
if [ -f "$config_file" ]; then
execute_main_logic
else
wait_for_config_file &
true
fi