master
in 923 lines 24 KB
Raw
1 #!/usr/bin/env sh
2
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 # Handle installation of the Netdata agent as a system service.
6 #
7 # Exit codes:
8 # 0 - Successfully installed service.
9 # 1 - Invalid arguments or other internal error.
10 # 2 - Unable to detect system service type.
11 # 3 - Detected system service type, but type not supported.
12 # 4 - Detected system service type, but could not install due to other issues.
13 # 5 - Platform not supported.
14
15 set -e
16
17 SCRIPT_SOURCE="$(
18 self=${0}
19 while [ -L "${self}" ]
20 do
21 cd "${self%/*}" || exit 1
22 self=$(readlink "${self}")
23 done
24 cd "${self%/*}" || exit 1
25 echo "$(pwd -P)/${self##*/}"
26 )"
27
28 DUMP_CMDS=0
29 ENABLE="auto"
30 EXPORT_CMDS=0
31 INSTALL=1
32 LINUX_INIT_TYPES="systemd openrc lsb initd runit dinit"
33 PLATFORM="$(uname -s)"
34 SHOW_SVC_TYPE=0
35 SVC_SOURCE="@libsysdir_POST@"
36 SVC_TYPE="detect"
37 WSL_ERROR_MSG="We appear to be running in WSL and were unable to find a usable service manager. We currently support systemd, LSB init scripts, and traditional init.d style init scripts when running under WSL."
38
39 # =====================================================================
40 # Utility functions
41
42 cleanup() {
43 ec="${?}"
44
45 if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
46 if [ -n "${NETDATA_PROPAGATE_WARNINGS}" ]; then
47 export NETDATA_WARNINGS="${NETDATA_WARNINGS}${SAVED_WARNINGS}"
48 fi
49 fi
50
51 trap - EXIT
52
53 exit "${ec}"
54 }
55
56 info() {
57 printf >&2 "%s\n" "${*}"
58 }
59
60 warning() {
61 if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
62 SAVED_WARNINGS="${SAVED_WARNINGS}\n - ${*}"
63 fi
64 printf >&2 "WARNING: %s\n" "${*}"
65 }
66
67 error() {
68 if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
69 SAVED_WARNINGS="${SAVED_WARNINGS}\n - ${*}"
70 fi
71 printf >&2 "ERROR: %s\n" "${*}"
72 }
73
74 get_os_key() {
75 if [ -f /etc/os-release ]; then
76 # shellcheck disable=SC1091
77 . /etc/os-release || return 1
78 echo "${ID}-${VERSION_ID}"
79
80 elif [ -f /etc/redhat-release ]; then
81 cat /etc/redhat-release
82 else
83 echo "unknown"
84 fi
85 }
86
87 valid_types() {
88 case "${PLATFORM}" in
89 Linux)
90 echo "detect ${LINUX_INIT_TYPES}"
91 ;;
92 FreeBSD)
93 echo "detect freebsd"
94 ;;
95 Darwin)
96 echo "detect launchd"
97 ;;
98 *)
99 echo "detect"
100 ;;
101 esac
102 }
103
104 install_generic_service() {
105 svc_path="${1}"
106 svc_type_name="${2}"
107 svc_file="${3}"
108 svc_enable_hook="${4}"
109 svc_disable_hook="${5}"
110
111 info "Installing ${svc_type_name} service file."
112 if [ ! -f "${svc_file}" ] && [ "${ENABLE}" = "auto" ]; then
113 ENABLE="enable"
114 fi
115
116 if ! install -p -m 0755 -o 0 -g 0 "${SVC_SOURCE}/${svc_path}/netdata" "${svc_file}"; then
117 error "Failed to install service file."
118 exit 4
119 fi
120
121 case "${ENABLE}" in
122 auto) true ;;
123 disable)
124 info "Disabling Netdata service."
125 ${svc_disable_hook}
126 ;;
127 enable)
128 info "Enabling Netdata service."
129 ${svc_enable_hook}
130 ;;
131 esac
132 }
133
134 dump_cmds() {
135 [ -n "${NETDATA_START_CMD}" ] && echo "NETDATA_START_CMD='${NETDATA_START_CMD}'"
136 [ -n "${NETDATA_STOP_CMD}" ] && echo "NETDATA_STOP_CMD='${NETDATA_STOP_CMD}'"
137 [ -n "${NETDATA_INSTALLER_START_CMD}" ] && echo "NETDATA_INSTALLER_START_CMD='${NETDATA_INSTALLER_START_CMD}'"
138 return 0
139 }
140
141 export_cmds() {
142 [ -n "${NETDATA_START_CMD}" ] && export NETDATA_START_CMD="${NETDATA_START_CMD}"
143 [ -n "${NETDATA_STOP_CMD}" ] && export NETDATA_STOP_CMD="${NETDATA_STOP_CMD}"
144 [ -n "${NETDATA_INSTALLER_START_CMD}" ] && export NETDATA_INSTALLER_START_CMD="${NETDATA_INSTALLER_START_COMMAND}"
145 return 0
146 }
147
148 save_cmds() {
149 dump_cmds > "${SAVE_CMDS_PATH}"
150 }
151
152 # =====================================================================
153 # Help functions
154
155 usage() {
156 cat << HEREDOC
157 USAGE: install-service.sh [options]
158 where options include:
159
160 --source Specify where to find the service files to install (default ${SVC_SOURCE}).
161 --type Specify the type of service file to install. Specify a type of 'help' to get a list of valid types for your platform.
162 --show-type Display information about what service managers are detected.
163 --cmds Additionally print a list of commands for starting and stopping the agent with the detected service type.
164 --export-cmds Export the variables that would be printed by the --cmds option.
165 --cmds-only Don't install, just handle the --cmds or --export-cmds option.
166 --enable Explicitly enable the service on install (default is to enable if not already installed).
167 --disable Explicitly disable the service on install.
168 --help Print this help information.
169 HEREDOC
170 }
171
172 help_types() {
173 cat << HEREDOC
174 Valid service types for ${PLATFORM} are:
175 $(valid_types)
176 HEREDOC
177 }
178
179 # =====================================================================
180 # systemd support functions
181
182 _check_systemd() {
183 pids=''
184 p=''
185 myns=''
186 ns=''
187
188 # if there is no systemctl command, it is not systemd
189 [ -z "$(command -v systemctl 2>/dev/null || true)" ] && echo "NO" && return 0
190
191 # Check the output of systemctl is-system-running.
192 #
193 # This may return a non-zero exit status in cases when it actually
194 # succeeded for our purposes, so we need to toggle set -e off here.
195 set +e
196 systemd_state="$(systemctl is-system-running)"
197 set -e
198
199 case "${systemd_state}" in
200 offline) echo "OFFLINE" && return 0 ;;
201 unknown) : ;;
202 "") : ;;
203 *) echo "YES" && return 0 ;;
204 esac
205
206 # if pid 1 is systemd, it is systemd
207 [ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "systemd" ] && echo "YES" && return 0
208
209 # check if the running systemd processes are not in our namespace
210 myns="$(readlink /proc/self/ns/pid 2> /dev/null)"
211 for p in ${pids}; do
212 ns="$(readlink "/proc/${p}/ns/pid" 2> /dev/null)"
213
214 # if pid of systemd is in our namespace, it is systemd
215 [ -n "${myns}" ] && [ "${myns}" = "${ns}" ] && echo "YES" && return 0
216 done
217
218 # At this point, we know it’s a systemd system because systemctl
219 # exists, but systemd does not appear to be running, so indicate as such
220 echo "OFFLINE"
221 }
222
223 check_systemd() {
224 if [ -z "${IS_SYSTEMD}" ]; then
225 IS_SYSTEMD="$(_check_systemd)"
226 fi
227
228 echo "${IS_SYSTEMD}"
229 }
230
231 get_systemd_service_dir() {
232 set +e
233 unit_paths="$(systemctl show -p UnitPath | cut -f 2- -d '=' | tr ' ' '\n')"
234 set -e
235
236 if [ -n "${unit_paths}" ]; then
237 lib_paths="$(echo "${unit_paths}" | grep -vE '^/(run|etc)' | awk '{line[NR] = $0} END {for (i = NR; i > 0; i--) print line[i]}')"
238 etc_paths="$(echo "${unit_paths}" | grep -E '^/etc' | grep -vE '(attached|control)$')"
239 else
240 lib_paths="/usr/lib/systemd/system /lib/systemd/system /usr/local/lib/systemd/system"
241 etc_paths="/etc/systemd/system"
242 fi
243
244 for path in ${lib_paths} ${etc_paths}; do
245 if [ -d "${path}" ] && [ -w "${path}" ]; then
246 echo "${path}"
247 return 0
248 fi
249 done
250
251 error "Unable to detect usable systemd service directory."
252 exit 4
253 }
254
255 install_systemd_service() {
256 SRCFILE="${SVC_SOURCE}/systemd/netdata.service"
257 PRESET_FILE="${SVC_SOURCE}/systemd/50-netdata.preset"
258 SVCDIR="$(get_systemd_service_dir)"
259
260 if [ "$(systemctl --version | head -n 1 | cut -f 2 -d ' ')" -le 235 ]; then
261 SRCFILE="${SVC_SOURCE}/systemd/netdata.service.v235"
262 fi
263
264 if [ "${ENABLE}" = "auto" ]; then
265 if [ "$(check_systemd)" = "YES" ]; then
266 IS_NETDATA_ENABLED="$(systemctl is-enabled netdata 2> /dev/null || echo "Netdata not there")"
267 fi
268
269 if [ "${IS_NETDATA_ENABLED}" = "disabled" ]; then
270 ENABLE="disable"
271 else
272 ENABLE="enable"
273 fi
274 fi
275
276 info "Installing systemd service..."
277 if ! install -p -m 0644 -o 0 -g 0 "${SRCFILE}" "${SVCDIR}/netdata.service"; then
278 error "Failed to install systemd service file."
279 exit 4
280 fi
281
282 if [ -f "${PRESET_FILE}" ]; then
283 if ! install -p -m 0644 -o 0 -g 0 "${PRESET_FILE}" "${SVCDIR}-preset/50-netdata.preset"; then
284 warning "Failed to install netdata preset file."
285 fi
286 fi
287
288 if [ "$(check_systemd)" = "YES" ]; then
289 if ! systemctl daemon-reload; then
290 warning "Failed to reload systemd unit files."
291 fi
292
293 if ! systemctl "${ENABLE}" netdata; then
294 warning "Failed to ${ENABLE} Netdata service."
295 fi
296 fi
297 }
298
299 systemd_cmds() {
300 if [ "$(check_systemd)" = "YES" ]; then
301 NETDATA_START_CMD='systemctl start netdata'
302 NETDATA_STOP_CMD='systemctl stop netdata'
303 else # systemd is not running, use external defaults by providing no commands
304 warning "Detected systemd, but not booted using systemd. Unable to provide commands to start or stop Netdata using the service manager."
305 fi
306 }
307
308 # =====================================================================
309 # OpenRC support functions
310
311 _check_openrc() {
312 # if /lib/rc/sh/functions.sh does not exist, it's not OpenRC
313 [ ! -f /lib/rc/sh/functions.sh ] && echo "NO" && return 0
314
315 # if there is no /etc/init.d, it's not OpenRC
316 [ ! -d /etc/init.d ] && echo "NO" && return 0
317
318 # if there is no /etc/conf.d, it's not OpenRC
319 [ ! -d /etc/conf.d ] && echo "NO" && return 0
320
321 # if there is no rc-update command, it's not OpenRC
322 [ -z "$(command -v rc-update 2>/dev/null || true)" ] && echo "NO" && return 0
323
324 # If /run/openrc/softlevel exists, it's OpenRC
325 [ -f /run/openrc/softlevel ] && echo "YES" && return 0
326
327 # if PID 1 is openrc-init, it's OpenRC
328 [ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "openrc-init" ] && echo "YES" && return 0
329
330 # if there is an openrc command, it's OpenRC, but not booted as such
331 [ -n "$(command -v openrc 2>/dev/null || true)" ] && echo "OFFLINE" && return 0
332
333 # if /etc/init.d/local exists and has `openrc-run` in it's shebang line, it’s OpenRC, but not booted as such
334 [ -r /etc/init.d/local ] && head -n 1 /etc/init.d/local | grep -q openrc-run && echo "OFFLINE" && return 0
335
336 # Otherwise, it’s not OpenRC
337 echo "NO" && return 0
338 }
339
340 check_openrc() {
341 if [ -z "${IS_OPENRC}" ]; then
342 IS_OPENRC="$(_check_openrc)"
343 fi
344
345 echo "${IS_OPENRC}"
346 }
347
348 enable_openrc() {
349 if [ "$(check_openrc)" = "YES" ]; then
350 runlevel="$(rc-status -r)"
351 fi
352
353 runlevel="${runlevel:-default}"
354
355 if ! rc-update add netdata "${runlevel}"; then
356 warning "Failed to enable Netdata service in runlevel ${runlevel}."
357 fi
358 }
359
360 disable_openrc() {
361 for runlevel in /etc/runlevels/*; do
362 if [ -e "${runlevel}/netdata" ]; then
363 runlevel="$(basename "${runlevel}")"
364 if ! rc-update del netdata "${runlevel}"; then
365 warning "Failed to disable Netdata service in runlevel ${runlevel}."
366 fi
367 fi
368 done
369 }
370
371 install_openrc_service() {
372 install_generic_service openrc/init.d OpenRC /etc/init.d/netdata enable_openrc disable_openrc
373
374 if [ ! -f /etc/conf.d/netdata ]; then
375 info "Installing OpenRC configuration file."
376
377 if ! install -p -m 0755 -o 0 -g 0 "${SVC_SOURCE}/openrc/conf.d/netdata" "/etc/conf.d/netdata"; then
378 warning "Failed to install configuration file, however the service will still work."
379 fi
380 fi
381 }
382
383 openrc_cmds() {
384 if [ "$(check_openrc)" = "YES" ]; then
385 NETDATA_START_CMD='rc-service netdata start'
386 NETDATA_STOP_CMD='rc-service netdata stop'
387 else # Not booted using OpenRC, use external defaults by not providing commands.
388 warning "Detected OpenRC, but the system is not booted using OpenRC. Unable to provide commands to start or stop Netdata using the service manager."
389 fi
390 }
391
392 # =====================================================================
393 # LSB init script support functions
394
395 _check_lsb_ignore_systemd() {
396 # if there is no /etc/init.d directory, it’s not an LSB system
397 [ ! -d /etc/init.d ] && echo "NO" && return 0
398
399 # If it's an OpenRC system, then it's not an LSB system
400 [ "$(check_openrc)" != "NO" ] && echo "NO" && return 0
401
402 # If /lib/lsb/init-functions exists, it’s an LSB system
403 [ -f /lib/lsb/init-functions ] && echo "YES" && return 0
404
405 echo "NO" && return 0
406 }
407
408 _check_lsb() {
409 # if there is _any_ systemd, it’s not an LSB system
410 [ "$(check_systemd)" != "NO" ] && echo "NO" && return 0
411
412 _check_lsb_ignore_systemd
413 }
414
415 check_lsb() {
416 if [ -z "${IS_LSB}" ]; then
417 IS_LSB="$(_check_lsb)"
418 fi
419
420 echo "${IS_LSB}"
421 }
422
423 enable_lsb() {
424 if ! update-rc.d netdata defaults; then
425 warning "Failed to enable Netdata service."
426 elif ! update-rc.d netdata defaults-disabled; then
427 warning "Failed to fully enable Netdata service."
428 fi
429 }
430
431 disable_lsb() {
432 if ! update-rc.d netdata remove; then
433 warning "Failed to disable Netdata service."
434 fi
435 }
436
437 install_lsb_service() {
438 install_generic_service lsb/init.d LSB /etc/init.d/netdata enable_lsb disable_lsb
439 }
440
441 lsb_cmds() {
442 NETDATA_START_CMD='/etc/init.d/netdata start'
443 NETDATA_STOP_CMD='/etc/init.d/netdata stop'
444 }
445
446 # =====================================================================
447 # init.d init script support functions
448
449 _check_initd_ignore_systemd() {
450 # if there is no /etc/init.d directory, it’s not an init.d system
451 [ ! -d /etc/init.d ] && echo "NO" && return 1
452
453 # if there is no chkconfig command, it's not a (usable) init.d system
454 [ -z "$(command -v chkconfig 2>/dev/null || true)" ] && echo "NO" && return 0
455
456 # if there is _any_ openrc, it’s not init.d
457 [ "$(check_openrc)" != "NO" ] && echo "NO" && return 0
458
459 # if it's not an LSB setup, it’s init.d
460 [ "$(check_lsb)" != "NO" ] && echo "NO" && return 0
461
462 echo "YES" && return 0
463 }
464
465 _check_initd() {
466 # if there is _any_ systemd, it’s not init.d
467 [ "$(check_systemd)" != "NO" ] && echo "NO" && return 0
468
469 _check_initd_ignore_systemd
470 }
471
472 check_initd() {
473 if [ -z "${IS_INITD}" ]; then
474 IS_INITD="$(_check_initd)"
475 fi
476
477 echo "${IS_INITD}"
478 }
479
480 enable_initd() {
481 if ! chkconfig netdata on; then
482 warning "Failed to enable Netdata service."
483 fi
484 }
485
486 disable_initd() {
487 if ! chkconfig netdata off; then
488 warning "Failed to disable Netdata service."
489 fi
490 }
491
492 install_initd_service() {
493 install_generic_service initd/init.d init.d /etc/init.d/netdata enable_initd disable_initd
494 }
495
496 initd_cmds() {
497 NETDATA_START_CMD='/etc/init.d/netdata start'
498 NETDATA_STOP_CMD='/etc/init.d/netdata stop'
499 }
500
501 # =====================================================================
502 # runit support functions
503
504 _check_runit() {
505 # if there is no runsvdir command, then it's not runit
506 [ -z "$(command -v runsvdir 2>/dev/null || true)" ] && echo "NO" && return 0
507
508 # if there is no runit command, then it's not runit
509 [ -z "$(command -v runit 2>/dev/null || true)" ] && echo "NO" && return 0
510
511 # if /run/runit exists, then it's runit
512 [ -d /run/runit ] && echo "YES" && return 0
513
514 # if /etc/runit/1 exists and is executable, then it's runit
515 [ -x /etc/runit/1 ] && echo "YES" && return 0
516
517 echo "NO" && return 0
518 }
519
520 check_runit() {
521 if [ -z "${IS_RUNIT}" ]; then
522 IS_RUNIT="$(_check_runit)"
523 fi
524
525 echo "${IS_RUNIT}"
526 }
527
528 install_runit_service() {
529 if [ -d /etc/sv ]; then
530 svc_dir="/etc/sv/netdata"
531 elif [ -d /etc/runit/sv ]; then
532 svc_dir="/etc/runit/sv/netdata"
533 else
534 error "Failed to locate service directory"
535 exit 4
536 fi
537
538 if [ -d /service ]; then
539 live_svc_dir="/service"
540 elif [ -d /var/service ]; then
541 live_svc_dir="/var/service"
542 elif [ -d /run/runit/service ]; then
543 live_svc_dir="/run/runit/service"
544 elif [ -d /etc/runit/runsvdir/default ]; then
545 live_svc_dir="/etc/runit/runsvdir/default"
546 else
547 error "Failed to locate live service directory"
548 exit 4
549 fi
550
551 svc_file="${svc_dir}/run"
552
553 info "Installing runit service file."
554 if [ ! -f "${svc_file}" ] && [ "${ENABLE}" = "auto" ]; then
555 ENABLE="enable"
556 fi
557
558 if ! install -D -p -m 0755 -o 0 -g 0 "${SVC_SOURCE}/runit/run" "${svc_file}"; then
559 error "Failed to install service file."
560 exit 4
561 fi
562
563 case ${ENABLE} in
564 enable)
565 if ! ln -s "${svc_dir}" "${live_svc_dir}"; then
566 warning "Failed to enable the Netdata service."
567 fi
568 ;;
569 disable)
570 if ! rm "${live_svc_dir}/netdata"; then
571 warning "Failed to disable the Netdata service."
572 fi
573 ;;
574 esac
575 }
576
577 runit_cmds() {
578 NETDATA_START_CMD="sv start netdata"
579 NETDATA_STOP_CMD="sv stop netdata"
580 }
581
582 # =====================================================================
583 # dinit support functions
584
585 _check_dinit() {
586 # if /etc/dinit.d does not exist, it’s not dinit
587 [ ! -d /etc/dinit.d ] && echo "NO" && return 0
588
589 # if PID 1 is dinit, it’s dinit
590 [ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "dinit" ] && echo "YES" && return 0
591
592 # if /run/dinitctl exists and is a socket, it’s dinit
593 [ -S /run/dinitctl ] && echo "YES" && return 0
594
595 # if the dinitctl command exists despite getting to this point, it’s dinit, but not booted as such
596 [ -n "$(command -v dinitctl 2>/dev/null || true)" ] && echo "OFFLINE" && return 0
597
598 echo "NO" && return 0
599 }
600
601 check_dinit() {
602 if [ -z "${IS_DINIT}" ]; then
603 IS_DINIT="$(_check_dinit)"
604 fi
605
606 echo "${IS_DINIT}"
607 }
608
609 _run_dinitctl() {
610 opts=''
611
612 if [ "$(check_dinit)" = "OFFLINE" ]; then
613 opts="-o"
614 fi
615
616 # shellcheck disable=SC2086
617 dinitctl ${opts} "${@}"
618 }
619
620 enable_dinit() {
621 _run_dinitctl enable netdata
622 }
623
624 enable_dinit() {
625 _run_dinitctl disable netdata
626 }
627
628 install_dinit_service() {
629 install_generic_service dinit/netdata "dinit" /etc/dinit.d enable_dinit disable_dinit
630 }
631
632 dinit_cmds() {
633 if [ "$(check_dinit)" = "YES" ]; then
634 NETDATA_START_CMD='dinitctl start netdata'
635 NETDATA_STOP_CMD='dinitct stop netdata'
636 else # Not booted using dinit, use external defaults by not providing commands.
637 warning "Detected dinit, but the system is not booted using dinit. Unable to provide commands to start or stop Netdata using the service manager."
638 fi
639 }
640
641 # =====================================================================
642 # WSL support functions
643
644 _check_wsl() {
645 # If uname -r contains the string WSL, then it's WSL.
646 uname -r | grep -q 'WSL' && echo "YES" && return 0
647
648 # If uname -r contains the string Microsoft, then it's WSL.
649 # This probably throws a false positive on CBL-Mariner, but it's part of what MS officially recommends for
650 # detecting if you're running under WSL.
651 uname -r | grep -q "Microsoft" && echo "YES" && return 0
652
653 echo "NO" && return 0
654 }
655
656 check_wsl() {
657 if [ -z "${IS_WSL}" ]; then
658 IS_WSL="$(_check_wsl)"
659 fi
660
661 echo "${IS_WSL}"
662 }
663
664 install_wsl_service() {
665 error "${WSL_ERROR_MSG}"
666 exit 3
667 }
668
669 wsl_cmds() {
670 error "${WSL_ERROR_MSG}"
671 exit 3
672 }
673
674 # =====================================================================
675 # FreeBSD support functions
676
677 enable_freebsd() {
678 if ! sysrc netdata_enable=YES; then
679 warning "Failed to enable netdata service."
680 fi
681 }
682
683 disable_freebsd() {
684 if ! sysrc netdata_enable=NO; then
685 warning "Failed to disable netdata service."
686 fi
687 }
688
689 install_freebsd_service() {
690 install_generic_service freebsd/rc.d "FreeBSD rc.d" /usr/local/etc/rc.d/netdata enable_freebsd disable_freebsd
691 }
692
693 freebsd_cmds() {
694 NETDATA_START_CMD='service netdata start'
695 NETDATA_STOP_CMD='service netdata stop'
696 NETDATA_INSTALLER_START_CMD='service netdata onestart'
697 }
698
699 # =====================================================================
700 # macOS support functions
701
702 install_darwin_service() {
703 info "Installing macOS plist file for launchd."
704 if ! install -C -S -p -m 0644 -o 0 -g 0 "${SVC_SOURCE}/launchd/netdata.plist" /Library/LaunchDaemons/com.github.netdata.plist; then
705 error "Failed to copy plist file."
706 exit 4
707 fi
708
709 launchctl unload /Library/LaunchDaemons/com.github.netdata.plist 2>/dev/null
710
711 if ! launchctl load /Library/LaunchDaemons/com.github.netdata.plist; then
712 error "Failed to load plist file."
713 exit 4
714 fi
715 }
716
717 darwin_cmds() {
718 NETDATA_START_CMD='launchctl start com.github.netdata'
719 NETDATA_STOP_CMD='launchctl stop com.github.netdata'
720 }
721
722 # =====================================================================
723 # Linux support functions
724
725 detect_linux_svc_type() {
726 if [ "${SVC_TYPE}" = "detect" ]; then
727 found_types=''
728
729 for t in wsl ${LINUX_INIT_TYPES}; do
730 case "$("check_${t}")" in
731 YES)
732 SVC_TYPE="${t}"
733 break
734 ;;
735 NO) continue ;;
736 OFFLINE)
737 if [ -z "${found_types}" ]; then
738 found_types="${t}"
739 else
740 found_types="${found_types} ${t}"
741 fi
742 ;;
743 esac
744 done
745
746 if [ "${SVC_TYPE}" = "detect" ]; then
747 if [ -z "${found_types}" ]; then
748 error "Failed to detect what type of service manager is in use."
749 else
750 SVC_TYPE="$(echo "${found_types}" | cut -f 1 -d ' ')"
751 warning "Failed to detect a running service manager, using detected (but not running) ${SVC_TYPE}."
752 fi
753 elif [ "${SVC_TYPE}" = "wsl" ]; then
754 if [ "$(check_systemd)" = "YES" ]; then
755 # Support for systemd in WSL.
756 SVC_TYPE="systemd"
757 elif [ "$(_check_lsb_ignore_systemd)" = "YES" ]; then
758 # Support for LSB init.d in WSL.
759 SVC_TYPE="lsb"
760 elif [ "$(_check_initd_ignore_systemd)" = "YES" ]; then
761 # Support for ‘generic’ init.d in WSL.
762 SVC_TYPE="initd"
763 fi
764 fi
765 fi
766
767 echo "${SVC_TYPE}"
768 }
769
770 install_linux_service() {
771 t="$(detect_linux_svc_type)"
772
773 if [ -z "${t}" ] || [ "${t}" = 'detect' ]; then
774 exit 2
775 fi
776
777 "install_${t}_service"
778 }
779
780 linux_cmds() {
781 t="$(detect_linux_svc_type)"
782
783 if [ -z "${t}" ] || [ "${t}" = 'detect' ]; then
784 exit 2
785 fi
786
787 "${t}_cmds"
788 }
789
790 # =====================================================================
791 # Service type display function
792
793 show_service_type() {
794 info "Detected platform: ${PLATFORM}"
795
796 case "${PLATFORM}" in
797 FreeBSD)
798 info "Detected service managers:"
799 info " - freebsd: YES"
800 info "Would use freebsd service management."
801 ;;
802 Darwin)
803 info "Detected service managers:"
804 info " - launchd: YES"
805 info "Would use launchd service management."
806 ;;
807 Linux)
808 [ "$(check_wsl)" = "YES" ] && info "Detected WSL environment."
809 info "Detected service managers:"
810 for t in ${LINUX_INIT_TYPES}; do
811 info " - ${t}: $("check_${t}")"
812 done
813 info "Would use $(detect_linux_svc_type) service management."
814 ;;
815 *)
816 info "${PLATFORM} is not supported by this script. No service file would be installed."
817 esac
818
819 exit 0
820 }
821
822 # =====================================================================
823 # Argument handling
824
825 parse_args() {
826 while [ -n "${1}" ]; do
827 case "${1}" in
828 "--source" | "-s")
829 SVC_SOURCE="${2}"
830 shift 1
831 ;;
832 "--type" | "-t")
833 if [ "${2}" = "help" ]; then
834 help_types
835 exit 0
836 else
837 SVC_TYPE="${2}"
838 shift 1
839 fi
840 ;;
841 "--show-type") SHOW_SVC_TYPE=1 ; INSTALL=0 ;;
842 "--save-cmds")
843 if [ -z "${2}" ]; then
844 info "No path specified to save command variables."
845 exit 1
846 else
847 SAVE_CMDS_PATH="${2}"
848 shift 1
849 fi
850 ;;
851 "--cmds" | "-c") DUMP_CMDS=1 ;;
852 "--cmds-only") INSTALL=0 ;;
853 "--export-cmds") EXPORT_CMDS=1 ;;
854 "--enable" | "-e") ENABLE="enable" ;;
855 "--disable" | "-d") ENABLE="disable" ;;
856 "--help" | "-h")
857 usage
858 exit 0
859 ;;
860 *)
861 info "Unrecognized option '${1}'."
862 exit 1
863 ;;
864 esac
865 shift 1
866 done
867
868 if [ "${SVC_SOURCE#@}" = "libsysdir_POST@" ]; then
869 SVC_SOURCE="$(dirname "${SCRIPT_SOURCE}")/../../lib/netdata/system"
870 warning "SVC_SOURCE not templated, using ${SVC_SOURCE} as source directory."
871 fi
872
873 if [ ! -d "${SVC_SOURCE}" ] && [ "${INSTALL}" -eq 1 ]; then
874 error "${SVC_SOURCE} does not appear to be a directory. Please specify a valid source for service files with the --source option."
875 exit 1
876 fi
877
878 if valid_types | grep -vw "${SVC_TYPE}"; then
879 error "${SVC_TYPE} is not supported on this platform."
880 help_types
881 exit 1
882 fi
883 }
884
885 # =====================================================================
886 # Core logic
887
888 main() {
889 trap "cleanup" EXIT
890
891 parse_args "${@}"
892
893 if [ "${SHOW_SVC_TYPE}" -eq 1 ]; then
894 show_service_type
895 else
896 case "${PLATFORM}" in
897 FreeBSD)
898 [ "${INSTALL}" -eq 1 ] && install_freebsd_service
899 freebsd_cmds
900 ;;
901 Darwin)
902 [ "${INSTALL}" -eq 1 ] && install_darwin_service
903 darwin_cmds
904 ;;
905 Linux)
906 [ "${INSTALL}" -eq 1 ] && install_linux_service
907 linux_cmds
908 ;;
909 *)
910 error "${PLATFORM} is not supported by this script."
911 exit 5
912 ;;
913 esac
914
915 [ "${DUMP_CMDS}" -eq 1 ] && dump_cmds
916 [ "${EXPORT_CMDS}" -eq 1 ] && export_cmds
917 [ -n "${SAVE_CMDS_PATH}" ] && save_cmds
918 fi
919
920 exit 0
921 }
922
923 main "${@}"