BlueGit

timfa filemanager

Files

filemanager / installsystemdservice.sh

installsystemdservice.sh

dl video fix
timfa Tim Falken · pushed by timfa · 5 months ago
a0162cfc History
installsystemdservice.sh 6.5 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_COMMANDS=(node npm git ffmpeg ffprobe vips)
16KNOWN_PACKAGE_MANAGERS=(apt-get apt dnf yum pacman zypper apk)
17
18# Base packages plus libvips + libheif for Sharp HEIF/HEIC thumbnails and previews.
19# Names differ per distro; see required_packages_for().
20REQUIRED_PACKAGES_HINT="nodejs npm git ffmpeg + libvips + libheif (package names vary by distro)"
21
22# Resolve the user who owns the project directory (i.e. the user who cloned it,
23# not root). Falls back to the SUDO_USER env var, then 'pi'.
24RUN_AS="${SUDO_USER:-pi}"
25SERVICE_RUN_AS_ROOT=""
26
27bool_is_true() {
28 case "${1,,}" in
29 1|true|yes|on)
30 return 0
31 ;;
32 *)
33 return 1
34 ;;
35 esac
36}
37
38confirm_root_service_mode() {
39 if [[ ! -t 0 ]]; then
40 echo "[install] Non-interactive shell detected; defaulting to non-root service mode." >&2
41 return 1
42 fi
43
44 local reply
45 read -r -p "[install] Run the filemanager service as root? [y/N] " reply
46 case "${reply,,}" in
47 y|yes)
48 return 0
49 ;;
50 *)
51 return 1
52 ;;
53 esac
54}
55
56if [[ -n "${FILEMANAGER_SERVICE_RUN_AS_ROOT:-}" ]]; then
57 SERVICE_RUN_AS_ROOT="${FILEMANAGER_SERVICE_RUN_AS_ROOT}"
58elif confirm_root_service_mode; then
59 SERVICE_RUN_AS_ROOT="1"
60else
61 SERVICE_RUN_AS_ROOT="0"
62fi
63
64SERVICE_USER_LINE="User=${RUN_AS}"
65SERVICE_CAPABILITY_LINES=$'AmbientCapabilities=CAP_NET_BIND_SERVICE\nCapabilityBoundingSet=CAP_NET_BIND_SERVICE'
66SERVICE_ENV_LINE=""
67
68if bool_is_true "$SERVICE_RUN_AS_ROOT"; then
69 SERVICE_USER_LINE="# Running as root (User= omitted intentionally)"
70 SERVICE_CAPABILITY_LINES="# Root mode selected: privileged-port capability lines not required"
71 SERVICE_ENV_LINE="Environment=FILEMANAGER_LIST_ACCESS_CHMOD=1"
72fi
73
74echo "[install] Project directory : $SCRIPT_DIR"
75if bool_is_true "$SERVICE_RUN_AS_ROOT"; then
76 echo "[install] Service will run as root (FILEMANAGER_SERVICE_RUN_AS_ROOT=1)."
77 echo "[install] Enabling chmod-on-folder-list via FILEMANAGER_LIST_ACCESS_CHMOD=1"
78else
79 echo "[install] Service will run as user: $RUN_AS"
80fi
81
82log_warn() {
83 echo "[install][warn] $*" >&2
84}
85
86collect_missing_commands() {
87 local missing=()
88 local cmd
89 for cmd in "${REQUIRED_COMMANDS[@]}"; do
90 if ! command -v "$cmd" >/dev/null 2>&1; then
91 missing+=("$cmd")
92 fi
93 done
94 printf '%s\n' "${missing[@]}"
95}
96
97confirm_dependency_install() {
98 if [[ ! -t 0 ]]; then
99 log_warn "Non-interactive shell detected; skipping optional dependency installation prompt."
100 return 1
101 fi
102
103 local reply
104 read -r -p "[install] Install required system dependencies now? [Y/n] " reply
105 case "${reply,,}" in
106 ""|y|yes)
107 return 0
108 ;;
109 *)
110 return 1
111 ;;
112 esac
113}
114
115required_packages_for() {
116 local pm="$1"
117 local packages=(nodejs npm git ffmpeg)
118
119 case "$pm" in
120 apt-get|apt)
121 # libvips-tools provides the `vips` CLI; libheif1 enables HEIF in Sharp on Linux.
122 packages+=(libvips42 libheif1 libvips-tools)
123 ;;
124 dnf|yum)
125 packages+=(vips libheif)
126 ;;
127 pacman)
128 packages+=(vips libheif)
129 ;;
130 zypper)
131 packages+=(vips libheif)
132 ;;
133 apk)
134 packages+=(vips libheif)
135 ;;
136 *)
137 return 1
138 ;;
139 esac
140
141 printf '%s\n' "${packages[@]}"
142}
143
144install_with_package_manager() {
145 local pm="$1"
146 local -a packages=()
147
148 mapfile -t packages < <(required_packages_for "$pm")
149 if [[ "${#packages[@]}" -eq 0 ]]; then
150 log_warn "Unsupported package manager: $pm"
151 return 1
152 fi
153
154 case "$pm" in
155 apt-get)
156 apt-get update && apt-get install -y "${packages[@]}"
157 ;;
158 apt)
159 apt update && apt install -y "${packages[@]}"
160 ;;
161 dnf)
162 dnf install -y "${packages[@]}"
163 ;;
164 yum)
165 yum install -y "${packages[@]}"
166 ;;
167 pacman)
168 pacman -Sy --noconfirm "${packages[@]}"
169 ;;
170 zypper)
171 zypper --non-interactive install --no-confirm "${packages[@]}"
172 ;;
173 apk)
174 apk add --no-cache "${packages[@]}"
175 ;;
176 *)
177 log_warn "Unsupported package manager: $pm"
178 return 1
179 ;;
180 esac
181}
182
183maybe_install_system_dependencies() {
184 mapfile -t missing_commands < <(collect_missing_commands)
185
186 if [[ "${#missing_commands[@]}" -eq 0 ]]; then
187 echo "[install] Required system dependencies already available (${REQUIRED_COMMANDS[*]})."
188 return 0
189 fi
190
191 echo "[install] Missing commands detected: ${missing_commands[*]}"
192 if ! confirm_dependency_install; then
193 echo "[install] Skipping system dependency installation by user choice."
194 return 0
195 fi
196
197 local available_package_managers=()
198 local pm
199 for pm in "${KNOWN_PACKAGE_MANAGERS[@]}"; do
200 if command -v "$pm" >/dev/null 2>&1; then
201 available_package_managers+=("$pm")
202 else
203 log_warn "Known package manager '$pm' not found on this system."
204 fi
205 done
206
207 if [[ "${#available_package_managers[@]}" -eq 0 ]]; then
208 log_warn "Could not find a supported package manager. Install dependencies manually: ${REQUIRED_PACKAGES_HINT} (commands needed: ${REQUIRED_COMMANDS[*]})."
209 return 0
210 fi
211
212 for pm in "${available_package_managers[@]}"; do
213 echo "[install] Trying dependency installation via '$pm'..."
214 if install_with_package_manager "$pm"; then
215 echo "[install] Dependencies installed successfully via '$pm'."
216 return 0
217 fi
218 log_warn "Dependency installation via '$pm' failed."
219 done
220
221 log_warn "All package manager installation attempts failed. Install dependencies manually: ${REQUIRED_PACKAGES_HINT} (commands needed: ${REQUIRED_COMMANDS[*]})."
222}
223
224maybe_install_system_dependencies
225
226# Make sure start.sh is executable
227chmod +x "$SCRIPT_DIR/start.sh"
228
229cat > "$SERVICE_FILE" <<EOF
230[Unit]
231Description=File Manager
232After=network.target
233
234[Service]
235Type=simple
236${SERVICE_USER_LINE}
237WorkingDirectory=${SCRIPT_DIR}
238ExecStart=/bin/sh ${SCRIPT_DIR}/start.sh
239Restart=always
240RestartSec=5
241# Allow binding to privileged ports (< 1024) without running as root.
242${SERVICE_CAPABILITY_LINES}
243${SERVICE_ENV_LINE}
244
245[Install]
246WantedBy=multi-user.target
247EOF
248
249echo "[install] Service file written to $SERVICE_FILE"
250
251systemctl daemon-reload
252systemctl enable "${SERVICE_NAME}"
253systemctl restart "${SERVICE_NAME}"
254
255echo "[install] Done. Service status:"
256systemctl status "${SERVICE_NAME}" --no-pager