Make installer idempotent with fast update path
This commit is contained in:
95
install.sh
95
install.sh
@@ -9,19 +9,74 @@ fi
|
||||
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
APP_ROOT="/opt/serial-bridge"
|
||||
CONFIG_ROOT="/etc/serial-bridge"
|
||||
VENV_PY="${APP_ROOT}/.venv/bin/python"
|
||||
REQ_HASH_FILE="${APP_ROOT}/.requirements.sha256"
|
||||
NEEDS_APT=0
|
||||
NEEDS_VENV=0
|
||||
NEEDS_PIP=0
|
||||
|
||||
apt-get update
|
||||
apt-get install -y \
|
||||
python3 \
|
||||
python3-venv \
|
||||
python3-pip \
|
||||
hostapd \
|
||||
dnsmasq \
|
||||
iw \
|
||||
wpasupplicant \
|
||||
iproute2 \
|
||||
rfkill \
|
||||
REQUIRED_PACKAGES=(
|
||||
python3
|
||||
python3-venv
|
||||
python3-pip
|
||||
hostapd
|
||||
dnsmasq
|
||||
iw
|
||||
wpasupplicant
|
||||
iproute2
|
||||
rfkill
|
||||
i2c-tools
|
||||
)
|
||||
|
||||
is_installed() {
|
||||
[[ -d "${APP_ROOT}/src" ]] && [[ -f "/etc/systemd/system/serial-bridge.service" ]]
|
||||
}
|
||||
|
||||
all_packages_installed() {
|
||||
local pkg
|
||||
for pkg in "${REQUIRED_PACKAGES[@]}"; do
|
||||
if ! dpkg -s "${pkg}" >/dev/null 2>&1; then
|
||||
return 1
|
||||
fi
|
||||
done
|
||||
return 0
|
||||
}
|
||||
|
||||
compute_req_hash() {
|
||||
sha256sum "${PROJECT_ROOT}/requirements.txt" | awk '{print $1}'
|
||||
}
|
||||
|
||||
mode="install"
|
||||
if is_installed; then
|
||||
mode="update"
|
||||
fi
|
||||
|
||||
echo "Modus: ${mode}"
|
||||
|
||||
if ! all_packages_installed; then
|
||||
NEEDS_APT=1
|
||||
fi
|
||||
|
||||
if [[ ! -x "${VENV_PY}" ]]; then
|
||||
NEEDS_VENV=1
|
||||
fi
|
||||
|
||||
current_req_hash="$(compute_req_hash)"
|
||||
stored_req_hash=""
|
||||
if [[ -f "${REQ_HASH_FILE}" ]]; then
|
||||
stored_req_hash="$(cat "${REQ_HASH_FILE}" 2>/dev/null || true)"
|
||||
fi
|
||||
if [[ "${NEEDS_VENV}" -eq 1 ]] || [[ "${current_req_hash}" != "${stored_req_hash}" ]]; then
|
||||
NEEDS_PIP=1
|
||||
fi
|
||||
|
||||
if [[ "${NEEDS_APT}" -eq 1 ]]; then
|
||||
echo "Installiere fehlende Systempakete..."
|
||||
apt-get update
|
||||
apt-get install -y "${REQUIRED_PACKAGES[@]}"
|
||||
else
|
||||
echo "Systempakete bereits vorhanden, ueberspringe apt."
|
||||
fi
|
||||
|
||||
install -d -m 755 "${APP_ROOT}" "${APP_ROOT}/src" "${APP_ROOT}/templates" "${APP_ROOT}/static" "${CONFIG_ROOT}"
|
||||
|
||||
@@ -30,9 +85,19 @@ cp -a "${PROJECT_ROOT}/templates/." "${APP_ROOT}/templates/"
|
||||
cp -a "${PROJECT_ROOT}/static/." "${APP_ROOT}/static/"
|
||||
cp "${PROJECT_ROOT}/requirements.txt" "${APP_ROOT}/requirements.txt"
|
||||
|
||||
python3 -m venv "${APP_ROOT}/.venv"
|
||||
"${APP_ROOT}/.venv/bin/pip" install --upgrade pip
|
||||
"${APP_ROOT}/.venv/bin/pip" install -r "${APP_ROOT}/requirements.txt"
|
||||
if [[ "${NEEDS_VENV}" -eq 1 ]]; then
|
||||
echo "Erstelle Python venv..."
|
||||
python3 -m venv "${APP_ROOT}/.venv"
|
||||
fi
|
||||
|
||||
if [[ "${NEEDS_PIP}" -eq 1 ]]; then
|
||||
echo "Aktualisiere Python-Abhaengigkeiten..."
|
||||
"${APP_ROOT}/.venv/bin/pip" install --upgrade pip
|
||||
"${APP_ROOT}/.venv/bin/pip" install -r "${APP_ROOT}/requirements.txt"
|
||||
echo "${current_req_hash}" > "${REQ_HASH_FILE}"
|
||||
else
|
||||
echo "requirements.txt unveraendert, ueberspringe pip install."
|
||||
fi
|
||||
|
||||
install -m 644 "${PROJECT_ROOT}/config/hostapd.conf" "${CONFIG_ROOT}/hostapd.conf"
|
||||
install -m 644 "${PROJECT_ROOT}/config/dnsmasq.conf" "${CONFIG_ROOT}/dnsmasq.conf"
|
||||
@@ -56,4 +121,4 @@ systemctl daemon-reload
|
||||
systemctl enable serial-bridge.service
|
||||
systemctl restart serial-bridge.service
|
||||
|
||||
echo "Installation abgeschlossen. Logs: journalctl -u serial-bridge -f"
|
||||
echo "Fertig (${mode}). Logs: journalctl -u serial-bridge -f"
|
||||
|
||||
Reference in New Issue
Block a user