BlueGit

timfa filemanager

Files

filemanager / installsystemdservice.sh

installsystemdservice.sh

installsystemdservice.sh 4.3 KiB Download Raw
1#!/usr/bin/env bash
2# installsystemdservice.sh — Installs filemanager as a systemd service.
3# Must be run as root: sudo ./installsystemdservice.sh
4
5set -euo pipefail
6
7if [[ "$(id -u)" -ne 0 ]]; then
8 echo "Error: this script must be run as root (use sudo)." >&2
9 exit 1
10fi
11
12SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
13SERVICE_NAME="filemanager"
14SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
15REQUIRED_PACKAGES=(nodejs npm git ffmpeg)
16REQUIRED_COMMANDS=(node npm git ffmpeg ffprobe)
17KNOWN_PACKAGE_MANAGERS=(apt-get apt dnf yum pacman zypper apk)
18
19# Resolve the user who owns the project directory (i.e. the user who cloned it,
20# not root). Falls back to the SUDO_USER env var, then 'pi'.
21RUN_AS="${SUDO_USER:-pi}"
22
23echo "[install] Project directory : $SCRIPT_DIR"
24echo "[install] Service will run as user: $RUN_AS"
25
26log_warn() {
27 echo "[install][warn] $*" >&2
28}
29
30collect_missing_commands() {
31 local missing=()
32 local cmd
33 for cmd in "${REQUIRED_COMMANDS[@]}"; do
34 if ! command -v "$cmd" >/dev/null 2>&1; then
35 missing+=("$cmd")
36 fi
37 done
38 printf '%s\n' "${missing[@]}"
39}
40
41confirm_dependency_install() {
42 if [[ ! -t 0 ]]; then
43 log_warn "Non-interactive shell detected; skipping optional dependency installation prompt."
44 return 1
45 fi
46
47 local reply
48 read -r -p "[install] Install required system dependencies now? [Y/n] " reply
49 case "${reply,,}" in
50 ""|y|yes)
51 return 0
52 ;;
53 *)
54 return 1
55 ;;
56 esac
57}
58
59install_with_package_manager() {
60 local pm="$1"
61
62 case "$pm" in
63 apt-get)
64 apt-get update && apt-get install -y "${REQUIRED_PACKAGES[@]}"
65 ;;
66 apt)
67 apt update && apt install -y "${REQUIRED_PACKAGES[@]}"
68 ;;
69 dnf)
70 dnf install -y "${REQUIRED_PACKAGES[@]}"
71 ;;
72 yum)
73 yum install -y "${REQUIRED_PACKAGES[@]}"
74 ;;
75 pacman)
76 pacman -Sy --noconfirm "${REQUIRED_PACKAGES[@]}"
77 ;;
78 zypper)
79 zypper --non-interactive install --no-confirm "${REQUIRED_PACKAGES[@]}"
80 ;;
81 apk)
82 apk add --no-cache "${REQUIRED_PACKAGES[@]}"
83 ;;
84 *)
85 log_warn "Unsupported package manager: $pm"
86 return 1
87 ;;
88 esac
89}
90
91maybe_install_system_dependencies() {
92 mapfile -t missing_commands < <(collect_missing_commands)
93
94 if [[ "${#missing_commands[@]}" -eq 0 ]]; then
95 echo "[install] Required system dependencies already available (${REQUIRED_COMMANDS[*]})."
96 return 0
97 fi
98
99 echo "[install] Missing commands detected: ${missing_commands[*]}"
100 if ! confirm_dependency_install; then
101 echo "[install] Skipping system dependency installation by user choice."
102 return 0
103 fi
104
105 local available_package_managers=()
106 local pm
107 for pm in "${KNOWN_PACKAGE_MANAGERS[@]}"; do
108 if command -v "$pm" >/dev/null 2>&1; then
109 available_package_managers+=("$pm")
110 else
111 log_warn "Known package manager '$pm' not found on this system."
112 fi
113 done
114
115 if [[ "${#available_package_managers[@]}" -eq 0 ]]; then
116 log_warn "Could not find a supported package manager. Install dependencies manually: ${REQUIRED_PACKAGES[*]} (commands needed: ${REQUIRED_COMMANDS[*]})."
117 return 0
118 fi
119
120 for pm in "${available_package_managers[@]}"; do
121 echo "[install] Trying dependency installation via '$pm'..."
122 if install_with_package_manager "$pm"; then
123 echo "[install] Dependencies installed successfully via '$pm'."
124 return 0
125 fi
126 log_warn "Dependency installation via '$pm' failed."
127 done
128
129 log_warn "All package manager installation attempts failed. Install dependencies manually: ${REQUIRED_PACKAGES[*]} (commands needed: ${REQUIRED_COMMANDS[*]})."
130}
131
132maybe_install_system_dependencies
133
134# Make sure start.sh is executable
135chmod +x "$SCRIPT_DIR/start.sh"
136
137cat > "$SERVICE_FILE" <<EOF
138[Unit]
139Description=File Manager
140After=network.target
141
142[Service]
143Type=simple
144User=${RUN_AS}
145WorkingDirectory=${SCRIPT_DIR}
146ExecStart=/bin/sh ${SCRIPT_DIR}/start.sh
147Restart=always
148RestartSec=5
149# Allow binding to privileged ports (< 1024) without running as root.
150AmbientCapabilities=CAP_NET_BIND_SERVICE
151CapabilityBoundingSet=CAP_NET_BIND_SERVICE
152
153[Install]
154WantedBy=multi-user.target
155EOF
156
157echo "[install] Service file written to $SERVICE_FILE"
158
159systemctl daemon-reload
160systemctl enable "${SERVICE_NAME}"
161systemctl restart "${SERVICE_NAME}"
162
163echo "[install] Done. Service status:"
164systemctl status "${SERVICE_NAME}" --no-pager