BlueGit

timfa filemanager

Files

filemanager / installsystemdservice.sh

installsystemdservice.sh

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