BlueGit

timfa filemanager

Files

filemanager / installsystemdservice.sh

installsystemdservice.sh

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