Improve comments and cleanup in stage_itscreen installer

This commit is contained in:
Andrei Smaliak 2026-03-14 08:57:41 +04:00
parent 90ca1255f9
commit 9742c3ed3f
1 changed files with 366 additions and 1 deletions

367
stage_itscreen/stage_itscreen_install.sh Executable file → Normal file
View File

@ -1 +1,366 @@
Wif-c@th-un-ceep22a #!/usr/bin/env bash
set -euo pipefail
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
export DEBIAN_FRONTEND=noninteractive
LOG="/var/log/iomatex-stage1-itscreen.log"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ITS_USER="itscreen"
ITS_GROUP="itscreen"
ITS_BINARY="/opt/itscreen/bin/itscreen"
ITS_WRAPPER="/usr/local/bin/itscreen-run"
ITS_SERVICE="itscreen-x11.service"
LIGHTDM_SERVICE="lightdm.service"
NM_WAIT_SERVICE="NetworkManager-wait-online.service"
SUDOERS_FILE="/etc/sudoers.d/90-itscreen-power"
XSESSION_HOOK="/etc/X11/Xsession.d/99-iomatex-allow-itscreen"
XWRAPPER_CONFIG="/etc/X11/Xwrapper.config"
ITS_UNIT_FILE="/etc/systemd/system/${ITS_SERVICE}"
LOCK_DIR="/run/itscreen"
LOCK_FILE="${LOCK_DIR}/itscreen.lock"
step_n=0
log() {
local line="[$(date -u +'%F %T UTC')] $*"
echo "$line"
echo "$line" >>"$LOG" 2>/dev/null || true
}
warn() {
log "WARN: $*"
}
die() {
log "ERROR: $*"
exit 1
}
step() {
step_n=$((step_n + 1))
echo
echo "=== Step ${step_n}: $* ==="
log "STEP ${step_n}: $*"
}
require_root() {
if [[ "${EUID:-$(id -u)}" -ne 0 ]]; then
exec sudo -E /bin/bash "$0" "$@"
fi
}
require_cmd() {
local cmd="$1"
command -v "$cmd" >/dev/null 2>&1 || die "Required command not found: $cmd"
}
ensure_dir() {
local path="$1"
local mode="$2"
local owner="$3"
local group="$4"
install -d -m "$mode" -o "$owner" -g "$group" "$path"
}
write_file() {
local path="$1"
local mode="$2"
local owner="$3"
local group="$4"
local parent tmp
parent="$(dirname "$path")"
tmp="$(mktemp "${parent}/.tmp.$(basename "$path").XXXXXX")"
cat >"$tmp"
if ! install -D -m "$mode" -o "$owner" -g "$group" "$tmp" "$path"; then
rm -f "$tmp"
return 1
fi
rm -f "$tmp"
}
enable_service_safe() {
local unit="$1"
systemctl enable "$unit" >/dev/null 2>&1 || true
}
service_is_masked() {
local unit="$1"
local state
state="$(systemctl is-enabled "$unit" 2>/dev/null || true)"
[[ "$state" == "masked" ]]
}
restart_service_safe() {
local unit="$1"
if systemctl is-active --quiet "$unit"; then
systemctl restart "$unit"
else
systemctl start "$unit"
fi
}
spinner() {
local pid="$1"
local msg="${2:-Working}"
local spin='|/-\'
local i=0
if command -v tput >/dev/null 2>&1 && [[ -t 1 ]]; then
tput civis || true
fi
while kill -0 "$pid" 2>/dev/null; do
i=$(((i + 1) % 4))
if [[ -t 1 ]]; then
printf "\r[%c] %s..." "${spin:$i:1}" "$msg"
fi
sleep 0.2
done
if [[ -t 1 ]]; then
printf "\r[OK] %s. \n" "$msg"
else
echo "[OK] $msg"
fi
if command -v tput >/dev/null 2>&1 && [[ -t 1 ]]; then
tput cnorm || true
fi
}
run_long() {
local msg="$1"
local timeout_s="$2"
shift 2
log "RUN: $msg :: $*"
( timeout "$timeout_s" "$@" >>"$LOG" 2>&1 ) &
local pid=$!
spinner "$pid" "$msg"
wait "$pid"
}
soft_run() {
local msg="$1"
local timeout_s="$2"
shift 2
run_long "$msg" "$timeout_s" "$@" || true
}
find_itscreen_binary() {
if [[ -f "${SCRIPT_DIR}/itscreen" ]]; then
echo "${SCRIPT_DIR}/itscreen"
return 0
fi
if [[ -f /home/iomatex/Downloads/itscreen ]]; then
echo "/home/iomatex/Downloads/itscreen"
return 0
fi
return 1
}
install_base_packages() {
step "Preflight"
require_cmd apt-get
require_cmd install
require_cmd timeout
require_cmd flock
require_cmd systemctl
require_cmd visudo
require_cmd mktemp
require_cmd ldconfig
require_cmd id
run_long "APT update" 900 apt-get update
soft_run "Ensure base packages" 600 apt-get install -y procps util-linux x11-xserver-utils sudo
id "$ITS_USER" >/dev/null 2>&1 || { echo "Run Stage_0 first"; exit 2; }
}
ensure_runtime_directories() {
step "Ensure runtime directories"
ensure_dir /opt/itscreen 0755 "$ITS_USER" "$ITS_GROUP"
ensure_dir /opt/itscreen/bin 0755 "$ITS_USER" "$ITS_GROUP"
ensure_dir /var/lib/itscreen 0755 "$ITS_USER" "$ITS_GROUP"
ensure_dir /var/lib/itscreen/config 0770 "$ITS_USER" "$ITS_GROUP"
ensure_dir /var/lib/itscreen/data 0770 "$ITS_USER" "$ITS_GROUP"
ensure_dir /var/lib/itscreen/logs 0770 "$ITS_USER" "$ITS_GROUP"
ensure_dir /var/log/itscreen 0755 "$ITS_USER" "$ITS_GROUP"
ensure_dir /var/log 0755 root root
ensure_dir /etc/sudoers.d 0755 root root
ensure_dir /etc/X11/Xsession.d 0755 root root
ensure_dir /etc/systemd/system 0755 root root
ensure_dir /usr/local/bin 0755 root root
ensure_dir "$LOCK_DIR" 0755 root root
}
install_qt_packages() {
step "Install Qt runtime (exact list)"
run_long "Install Qt runtime" 1800 apt-get install -y \
libqt5webview5 \
libqt5websockets5 \
libqt5texttospeech5 \
libqt5serialbus5
soft_run "Install Qt extras" 1800 apt-get install -y \
gedit lxterminal clang qtcreator \
libqt5serialbus5-bin \
qtpositioning5-dev qml-module-qtpositioning \
libqt5texttospeech5-dev \
qml-module-qtquick-virtualkeyboard qtvirtualkeyboard-plugin \
libqt5webview5-dev qml-module-qtwebview \
qml-module-qt-labs-settings qml-module-qtquick-xmllistmodel \
libqt5serialbus5-dev \
qml-module-qtquick-controls2 \
qml-module-qt-websockets libqt5websockets5-dev \
qml-module-qt-labs-platform
ldconfig
}
install_itscreen_binary() {
local src
step "Install itscreen binary"
src="$(find_itscreen_binary)" || { echo "Binary not found"; exit 2; }
install -m 0755 -o "$ITS_USER" -g "$ITS_GROUP" "$src" "$ITS_BINARY"
}
install_sudoers() {
step "Grant power actions (reboot/shutdown) to itscreen (sudoers)"
write_file "$SUDOERS_FILE" 0440 root root <<'EOF'
Defaults:itscreen !requiretty
itscreen ALL=(root) NOPASSWD: /sbin/reboot, /usr/sbin/reboot, /bin/systemctl reboot, /usr/bin/systemctl reboot
itscreen ALL=(root) NOPASSWD: /sbin/poweroff, /usr/sbin/poweroff, /bin/systemctl poweroff, /usr/bin/systemctl poweroff
itscreen ALL=(root) NOPASSWD: /sbin/shutdown, /usr/sbin/shutdown, /bin/systemctl halt, /usr/bin/systemctl halt
EOF
visudo -cf "$SUDOERS_FILE" >/dev/null
}
install_x11_access_files() {
step "X11 access for itscreen (xhost)"
write_file "$XSESSION_HOOK" 0755 root root <<'EOF'
#!/bin/sh
DISPLAY=${DISPLAY:-:0} xhost +SI:localuser:itscreen >/dev/null 2>&1 || true
EOF
step "FIX A: X11 permission for system services (Xwrapper)"
write_file "$XWRAPPER_CONFIG" 0644 root root <<'EOF'
allowed_users=anybody
needs_root_rights=yes
EOF
}
install_wrapper() {
step "Wrapper with flock"
write_file "$ITS_WRAPPER" 0755 root root <<EOF
#!/usr/bin/env bash
set -euo pipefail
LOCK="${LOCK_FILE}"
export XDG_CONFIG_HOME=/var/lib/itscreen/config
export XDG_DATA_HOME=/var/lib/itscreen/data
export ITSCREEN_LOG_DIR=/var/lib/itscreen/logs
exec flock -n "\$LOCK" /opt/itscreen/bin/itscreen "\$@"
EOF
}
install_systemd_unit() {
step "Systemd services (X11 ONLY)"
write_file "$ITS_UNIT_FILE" 0644 root root <<'EOF'
[Unit]
Description=ITSCREEN X11 (requires LightDM)
After=lightdm.service
Wants=network-online.target
After=NetworkManager.service network-online.target
[Service]
User=itscreen
WorkingDirectory=/opt/itscreen
RuntimeDirectory=itscreen
ExecStartPre=-/usr/bin/pkill -u itscreen -x itscreen
ExecStartPre=/usr/bin/nm-online -s -q --timeout=45
Environment=DISPLAY=:0
Environment=QT_QPA_PLATFORM=xcb
ExecStart=/usr/local/bin/itscreen-run
Restart=always
RestartSec=2
StartLimitIntervalSec=300
StartLimitBurst=3
[Install]
WantedBy=multi-user.target
EOF
}
enable_managed_services() {
step "Reload and enable services"
systemctl daemon-reload
if systemctl is-enabled "$NM_WAIT_SERVICE" >/dev/null 2>&1; then
:
elif service_is_masked "$NM_WAIT_SERVICE"; then
warn "${NM_WAIT_SERVICE} is masked; continuing without enabling it"
else
enable_service_safe "$NM_WAIT_SERVICE"
fi
enable_service_safe "$LIGHTDM_SERVICE"
if ! systemctl is-active --quiet "$LIGHTDM_SERVICE"; then
systemctl start "$LIGHTDM_SERVICE"
fi
enable_service_safe "$ITS_SERVICE"
restart_service_safe "$ITS_SERVICE"
}
print_summary() {
echo
echo "Stage_3 ready (X11 ONLY)."
echo "Binary path:"
echo " ${ITS_BINARY}"
echo "Runtime/config/data/log directories:"
echo " /opt/itscreen/bin"
echo " /var/lib/itscreen/config"
echo " /var/lib/itscreen/data"
echo " /var/lib/itscreen/logs"
echo " /var/log/itscreen"
echo " ${LOCK_DIR}"
echo "Managed files written by installer:"
echo " ${ITS_BINARY}"
echo " ${SUDOERS_FILE}"
echo " ${XSESSION_HOOK}"
echo " ${XWRAPPER_CONFIG}"
echo " ${ITS_WRAPPER}"
echo " ${ITS_UNIT_FILE}"
echo "Enabled services:"
echo " ${LIGHTDM_SERVICE}"
echo " ${ITS_SERVICE}"
echo "Main log file:"
echo " ${LOG}"
echo "Quick verification commands:"
echo " systemctl status ${ITS_SERVICE} --no-pager"
echo " systemctl status ${LIGHTDM_SERVICE} --no-pager"
echo " journalctl -u ${ITS_SERVICE} -n 80 --no-pager"
echo " ls -l ${ITS_BINARY} ${ITS_WRAPPER} ${ITS_UNIT_FILE}"
}
main() {
require_root "$@"
ensure_dir /var/log 0755 root root
touch "$LOG" >/dev/null 2>&1 || true
log "=== IOMATEX STAGE_3 ITSCREEN (X11 ONLY) START ==="
install_base_packages
ensure_runtime_directories
install_qt_packages
install_itscreen_binary
install_sudoers
install_x11_access_files
install_wrapper
install_systemd_unit
enable_managed_services
print_summary
}
main "$@"