From 9742c3ed3ff452a87f5f1a1343fd163cc6d76bb7 Mon Sep 17 00:00:00 2001 From: Andrei Smaliak Date: Sat, 14 Mar 2026 08:57:41 +0400 Subject: [PATCH] Improve comments and cleanup in stage_itscreen installer --- stage_itscreen/stage_itscreen_install.sh | 367 ++++++++++++++++++++++- 1 file changed, 366 insertions(+), 1 deletion(-) mode change 100755 => 100644 stage_itscreen/stage_itscreen_install.sh diff --git a/stage_itscreen/stage_itscreen_install.sh b/stage_itscreen/stage_itscreen_install.sh old mode 100755 new mode 100644 index cfa701b..97b52c0 --- a/stage_itscreen/stage_itscreen_install.sh +++ b/stage_itscreen/stage_itscreen_install.sh @@ -1 +1,366 @@ -Wif-c@th-un-ceep22a \ No newline at end of file +#!/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 </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 "$@"