BlueGit

timfa filemanager

Files

filemanager / installsystemdservice.sh

installsystemdservice.sh

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