install-service.sh.in 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. #!/usr/bin/env sh
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. # Handle installation of the Netdata agent as a system service.
  4. #
  5. # Exit codes:
  6. # 0 - Successfully installed service.
  7. # 1 - Invalid arguments or other internal error.
  8. # 2 - Unable to detect system service type.
  9. # 3 - Detected system service type, but type not supported.
  10. # 4 - Detected system service type, but could not install due to other issues.
  11. # 5 - Platform not supported.
  12. set -e
  13. SCRIPT_SOURCE="$(
  14. self=${0}
  15. while [ -L "${self}" ]
  16. do
  17. cd "${self%/*}" || exit 1
  18. self=$(readlink "${self}")
  19. done
  20. cd "${self%/*}" || exit 1
  21. echo "$(pwd -P)/${self##*/}"
  22. )"
  23. DUMP_CMDS=0
  24. ENABLE="auto"
  25. EXPORT_CMDS=0
  26. INSTALL=1
  27. LINUX_INIT_TYPES="systemd openrc lsb initd runit"
  28. PLATFORM="$(uname -s)"
  29. SHOW_SVC_TYPE=0
  30. SVC_SOURCE="@libsysdir_POST@"
  31. SVC_TYPE="detect"
  32. 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."
  33. # =====================================================================
  34. # Utility functions
  35. cleanup() {
  36. ec="${?}"
  37. if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
  38. if [ -n "${NETDATA_PROPAGATE_WARNINGS}" ]; then
  39. export NETDATA_WARNINGS="${NETDATA_WARNINGS}${SAVED_WARNINGS}"
  40. fi
  41. fi
  42. trap - EXIT
  43. exit "${ec}"
  44. }
  45. info() {
  46. printf >&2 "%s\n" "${*}"
  47. }
  48. warning() {
  49. if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
  50. SAVED_WARNINGS="${SAVED_WARNINGS}\n - ${*}"
  51. fi
  52. printf >&2 "WARNING: %s\n" "${*}"
  53. }
  54. error() {
  55. if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
  56. SAVED_WARNINGS="${SAVED_WARNINGS}\n - ${*}"
  57. fi
  58. printf >&2 "ERROR: %s\n" "${*}"
  59. }
  60. get_os_key() {
  61. if [ -f /etc/os-release ]; then
  62. # shellcheck disable=SC1091
  63. . /etc/os-release || return 1
  64. echo "${ID}-${VERSION_ID}"
  65. elif [ -f /etc/redhat-release ]; then
  66. cat /etc/redhat-release
  67. else
  68. echo "unknown"
  69. fi
  70. }
  71. valid_types() {
  72. case "${PLATFORM}" in
  73. Linux)
  74. echo "detect systemd openrc lsb initd"
  75. ;;
  76. FreeBSD)
  77. echo "detect freebsd"
  78. ;;
  79. Darwin)
  80. echo "detect launchd"
  81. ;;
  82. *)
  83. echo "detect"
  84. ;;
  85. esac
  86. }
  87. install_generic_service() {
  88. svc_type="${1}"
  89. svc_type_name="${2}"
  90. svc_file="${3}"
  91. svc_enable_hook="${4}"
  92. svc_disable_hook="${5}"
  93. info "Installing ${svc_type_name} service file."
  94. if [ ! -f "${svc_file}" ] && [ "${ENABLE}" = "auto" ]; then
  95. ENABLE="enable"
  96. fi
  97. if ! install -p -m 0755 -o 0 -g 0 "${SVC_SOURCE}/netdata-${svc_type}" "${svc_file}"; then
  98. error "Failed to install service file."
  99. exit 4
  100. fi
  101. case "${ENABLE}" in
  102. auto) true ;;
  103. disable)
  104. info "Disabling Netdata service."
  105. ${svc_disable_hook}
  106. ;;
  107. enable)
  108. info "Enabling Netdata service."
  109. ${svc_enable_hook}
  110. ;;
  111. esac
  112. }
  113. dump_cmds() {
  114. [ -n "${NETDATA_START_CMD}" ] && echo "NETDATA_START_CMD='${NETDATA_START_CMD}'"
  115. [ -n "${NETDATA_STOP_CMD}" ] && echo "NETDATA_STOP_CMD='${NETDATA_STOP_CMD}'"
  116. [ -n "${NETDATA_INSTALLER_START_CMD}" ] && echo "NETDATA_INSTALLER_START_CMD='${NETDATA_INSTALLER_START_CMD}'"
  117. return 0
  118. }
  119. export_cmds() {
  120. [ -n "${NETDATA_START_CMD}" ] && export NETDATA_START_CMD="${NETDATA_START_CMD}"
  121. [ -n "${NETDATA_STOP_CMD}" ] && export NETDATA_STOP_CMD="${NETDATA_STOP_CMD}"
  122. [ -n "${NETDATA_INSTALLER_START_CMD}" ] && export NETDATA_INSTALLER_START_CMD="${NETDATA_INSTALLER_START_COMMAND}"
  123. return 0
  124. }
  125. save_cmds() {
  126. dump_cmds > "${SAVE_CMDS_PATH}"
  127. }
  128. # =====================================================================
  129. # Help functions
  130. usage() {
  131. cat << HEREDOC
  132. USAGE: install-service.sh [options]
  133. where options include:
  134. --source Specify where to find the service files to install (default ${SVC_SOURCE}).
  135. --type Specify the type of service file to install. Specify a type of 'help' to get a list of valid types for your platform.
  136. --show-type Display information about what service managers are detected.
  137. --cmds Additionally print a list of commands for starting and stopping the agent with the detected service type.
  138. --export-cmds Export the variables that would be printed by the --cmds option.
  139. --cmds-only Don't install, just handle the --cmds or --export-cmds option.
  140. --enable Explicitly enable the service on install (default is to enable if not already installed).
  141. --disable Explicitly disable the service on install.
  142. --help Print this help information.
  143. HEREDOC
  144. }
  145. help_types() {
  146. cat << HEREDOC
  147. Valid service types for ${PLATFORM} are:
  148. $(valid_types)
  149. HEREDOC
  150. }
  151. # =====================================================================
  152. # systemd support functions
  153. _check_systemd() {
  154. pids=''
  155. p=''
  156. myns=''
  157. ns=''
  158. # if the directory /lib/systemd/system OR /usr/lib/systemd/system (SLES 12.x) does not exit, it is not systemd
  159. if [ ! -d /lib/systemd/system ] && [ ! -d /usr/lib/systemd/system ]; then
  160. echo "NO" && return 0
  161. fi
  162. # if there is no systemctl command, it is not systemd
  163. [ -z "$(command -v systemctl 2>/dev/null || true)" ] && echo "NO" && return 0
  164. # if pid 1 is systemd, it is systemd
  165. [ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "systemd" ] && echo "YES" && return 0
  166. # it ‘is’ systemd at this point, but systemd might not be running
  167. # if not, return 2 to indicate ‘systemd, but not running’
  168. pids=$(safe_pidof systemd 2> /dev/null)
  169. [ -z "${pids}" ] && echo "OFFLINE" && return 0
  170. # check if the running systemd processes are not in our namespace
  171. myns="$(readlink /proc/self/ns/pid 2> /dev/null)"
  172. for p in ${pids}; do
  173. ns="$(readlink "/proc/${p}/ns/pid" 2> /dev/null)"
  174. # if pid of systemd is in our namespace, it is systemd
  175. [ -n "${myns}" ] && [ "${myns}" = "${ns}" ] && echo "YES" && return 0
  176. done
  177. # else, it is not systemd
  178. echo "NO"
  179. }
  180. check_systemd() {
  181. if [ -z "${IS_SYSTEMD}" ]; then
  182. IS_SYSTEMD="$(_check_systemd)"
  183. fi
  184. echo "${IS_SYSTEMD}"
  185. }
  186. get_systemd_service_dir() {
  187. if [ -w "/lib/systemd/system" ]; then
  188. echo "/lib/systemd/system"
  189. elif [ -w "/usr/lib/systemd/system" ]; then
  190. echo "/usr/lib/systemd/system"
  191. elif [ -w "/etc/systemd/system" ]; then
  192. echo "/etc/systemd/system"
  193. else
  194. error "Unable to detect systemd service directory."
  195. exit 4
  196. fi
  197. }
  198. install_systemd_service() {
  199. SRCFILE="${SVC_SOURCE}/netdata.service"
  200. if [ "$(systemctl --version | head -n 1 | cut -f 2 -d ' ')" -le 235 ]; then
  201. SRCFILE="${SVC_SOURCE}/netdata.service.v235"
  202. fi
  203. if [ "${ENABLE}" = "auto" ]; then
  204. if [ "$(check_systemd)" = "YES" ]; then
  205. IS_NETDATA_ENABLED="$(systemctl is-enabled netdata 2> /dev/null || echo "Netdata not there")"
  206. fi
  207. if [ "${IS_NETDATA_ENABLED}" = "disabled" ]; then
  208. ENABLE="disable"
  209. else
  210. ENABLE="enable"
  211. fi
  212. fi
  213. info "Installing systemd service..."
  214. if ! install -p -m 0644 -o 0 -g 0 "${SRCFILE}" "$(get_systemd_service_dir)/netdata.service"; then
  215. error "Failed to install systemd service file."
  216. exit 4
  217. fi
  218. if [ "$(check_systemd)" = "YES" ]; then
  219. if ! systemctl daemon-reload; then
  220. warning "Failed to reload systemd unit files."
  221. fi
  222. if ! systemctl ${ENABLE} netdata; then
  223. warning "Failed to ${ENABLE} Netdata service."
  224. fi
  225. fi
  226. }
  227. systemd_cmds() {
  228. if [ "$(check_systemd)" = "YES" ]; then
  229. NETDATA_START_CMD='systemctl start netdata'
  230. NETDATA_STOP_CMD='systemctl stop netdata'
  231. else # systemd is not running, use external defaults by providing no commands
  232. warning "Detected systemd, but not booted using systemd. Unable to provide commands to start or stop Netdata using the service manager."
  233. fi
  234. }
  235. # =====================================================================
  236. # OpenRC support functions
  237. _check_openrc() {
  238. # if /lib/rc/sh/functions.sh does not exist, it's not OpenRC
  239. [ ! -f /lib/rc/sh/functions.sh ] && echo "NO" && return 0
  240. # if there is no /etc/init.d, it's not OpenRC
  241. [ ! -d /etc/init.d ] && echo "NO" && return 0
  242. # if there is no rc-update command, it's not OpenRC
  243. [ -z "$(command -v rc-update 2>/dev/null || true)" ] && echo "NO" && return 0
  244. # If /run/openrc/softlevel exists, it's OpenRC
  245. [ -f /run/openrc/softlevel ] && echo "YES" && return 0
  246. # if PID 1 is openrc-init, it's OpenRC
  247. [ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "openrc-init" ] && echo "YES" && return 0
  248. # if there is an openrc command, it's OpenRC, but not booted as such
  249. [ -n "$(command -v openrc 2>/dev/null || true)" ] && echo "OFFLINE" && return 0
  250. # if /etc/init.d/local exists and has `openrc-run` in it's shebang line, it’s OpenRC, but not booted as such
  251. [ -r /etc/init.d/local ] && head -n 1 /etc/init.d/local | grep -q openrc-run && echo "OFFLINE" && return 0
  252. # Otherwise, it’s not OpenRC
  253. echo "NO" && return 0
  254. }
  255. check_openrc() {
  256. if [ -z "${IS_OPENRC}" ]; then
  257. IS_OPENRC="$(_check_openrc)"
  258. fi
  259. echo "${IS_OPENRC}"
  260. }
  261. enable_openrc() {
  262. if [ "$(check_openrc)" = "YES" ]; then
  263. runlevel="$(rc-status -r)"
  264. fi
  265. runlevel="${runlevel:-default}"
  266. if ! rc-update add netdata "${runlevel}"; then
  267. warning "Failed to enable Netdata service in runlevel ${runlevel}."
  268. fi
  269. }
  270. disable_openrc() {
  271. for runlevel in /etc/runlevels/*; do
  272. if [ -e "${runlevel}/netdata" ]; then
  273. runlevel="$(basename "${runlevel}")"
  274. if ! rc-update del netdata "${runlevel}"; then
  275. warning "Failed to disable Netdata service in runlevel ${runlevel}."
  276. fi
  277. fi
  278. done
  279. }
  280. install_openrc_service() {
  281. install_generic_service openrc OpenRC /etc/init.d/netdata enable_openrc disable_openrc
  282. }
  283. openrc_cmds() {
  284. if [ "$(check_openrc)" = "YES" ]; then
  285. NETDATA_START_CMD='rc-service netdata start'
  286. NETDATA_STOP_CMD='rc-service netdata stop'
  287. else # Not booted using OpenRC, use external defaults by not providing commands.
  288. warning "Detected OpenRC, but the system is not booted using OpenRC. Unable to provide commands to start or stop Netdata using the service manager."
  289. fi
  290. }
  291. # =====================================================================
  292. # LSB init script support functions
  293. _check_lsb_ignore_systemd() {
  294. # if there is no /etc/init.d directory, it’s not an LSB system
  295. [ ! -d /etc/init.d ] && echo "NO" && return 0
  296. # If it's an OpenRC system, then it's not an LSB system
  297. [ "$(check_openrc)" != "NO" ] && echo "NO" && return 0
  298. # If /lib/lsb/init-functions exists, it’s an LSB system
  299. [ -f /lib/lsb/init-functions ] && echo "YES" && return 0
  300. echo "NO" && return 0
  301. }
  302. _check_lsb() {
  303. # if there is _any_ systemd, it’s not an LSB system
  304. [ "$(check_systemd)" != "NO" ] && echo "NO" && return 0
  305. _check_lsb_ignore_systemd
  306. }
  307. check_lsb() {
  308. if [ -z "${IS_LSB}" ]; then
  309. IS_LSB="$(_check_lsb)"
  310. fi
  311. echo "${IS_LSB}"
  312. }
  313. enable_lsb() {
  314. if ! update-rc.d netdata defaults; then
  315. warning "Failed to enable Netdata service."
  316. elif ! update-rc.d netdata defaults-disabled; then
  317. warning "Failed to fully enable Netdata service."
  318. fi
  319. }
  320. disable_lsb() {
  321. if ! update-rc.d netdata remove; then
  322. warning "Failed to disable Netdata service."
  323. fi
  324. }
  325. install_lsb_service() {
  326. install_generic_service lsb LSB /etc/init.d/netdata enable_lsb disable_lsb
  327. }
  328. lsb_cmds() {
  329. NETDATA_START_CMD='/etc/init.d/netdata start'
  330. NETDATA_STOP_CMD='/etc/init.d/netdata stop'
  331. }
  332. # =====================================================================
  333. # init.d init script support functions
  334. _check_initd_ignore_systemd() {
  335. # if there is no /etc/init.d directory, it’s not an init.d system
  336. [ ! -d /etc/init.d ] && echo "NO" && return 1
  337. # if there is no chkconfig command, it's not a (usable) init.d system
  338. [ -z "$(command -v chkconfig 2>/dev/null || true)" ] && echo "NO" && return 0
  339. # if there is _any_ openrc, it’s not init.d
  340. [ "$(check_openrc)" != "NO" ] && echo "NO" && return 0
  341. # if it's not an LSB setup, it’s init.d
  342. [ "$(check_lsb)" != "NO" ] && echo "NO" && return 0
  343. echo "YES" && return 0
  344. }
  345. _check_initd() {
  346. # if there is _any_ systemd, it’s not init.d
  347. [ "$(check_systemd)" != "NO" ] && echo "NO" && return 0
  348. _check_initd_ignore_systemd
  349. }
  350. check_initd() {
  351. if [ -z "${IS_INITD}" ]; then
  352. IS_INITD="$(_check_initd)"
  353. fi
  354. echo "${IS_INITD}"
  355. }
  356. enable_initd() {
  357. if ! chkconfig netdata on; then
  358. warning "Failed to enable Netdata service."
  359. fi
  360. }
  361. disable_initd() {
  362. if ! chkconfig netdata off; then
  363. warning "Failed to disable Netdata service."
  364. fi
  365. }
  366. install_initd_service() {
  367. install_generic_service init-d init.d /etc/init.d/netdata enable_initd disable_initd
  368. }
  369. initd_cmds() {
  370. NETDATA_START_CMD='/etc/init.d/netdata start'
  371. NETDATA_STOP_CMD='/etc/init.d/netdata stop'
  372. }
  373. # =====================================================================
  374. # runit support functions
  375. #
  376. # Currently not supported, this exists to provide useful error messages.
  377. _check_runit() {
  378. # if there is no runsvdir command, then it's not runit
  379. [ -z "$(command -v runsvdir 2>/dev/null || true)" ] && echo "NO" && return 0
  380. # if there is no runit command, then it's not runit
  381. [ -z "$(command -v runit 2>/dev/null || true)" ] && echo "NO" && return 0
  382. # if /run/runit exists, then it's runit
  383. [ -d /run/runit ] && echo "YES" && return 0
  384. # if /etc/runit/1 exists and is executable, then it's runit
  385. [ -x /etc/runit/1 ] && echo "YES" && return 0
  386. echo "NO" && return 0
  387. }
  388. check_runit() {
  389. if [ -z "${IS_RUNIT}" ]; then
  390. IS_RUNIT="$(_check_runit)"
  391. fi
  392. echo "${IS_RUNIT}"
  393. }
  394. install_runit_service() {
  395. error "Detected runit, which we do not currently support."
  396. exit 3
  397. }
  398. runit_cmds() {
  399. error "Detected runit, which we do not currently support."
  400. exit 3
  401. }
  402. # =====================================================================
  403. # WSL support functions
  404. #
  405. # Cannot be supported, this exists to provide useful error messages.
  406. _check_wsl() {
  407. # If uname -r contains the string WSL, then it's WSL.
  408. uname -r | grep -q 'WSL' && echo "YES" && return 0
  409. # If uname -r contains the string Microsoft, then it's WSL.
  410. # This probably throws a false positive on CBL-Mariner, but it's part of what MS officially recommends for
  411. # detecting if you're running under WSL.
  412. uname -r | grep -q "Microsoft" && echo "YES" && return 0
  413. echo "NO" && return 0
  414. }
  415. check_wsl() {
  416. if [ -z "${IS_WSL}" ]; then
  417. IS_WSL="$(_check_wsl)"
  418. fi
  419. echo "${IS_WSL}"
  420. }
  421. install_wsl_service() {
  422. error "${WSL_ERROR_MSG}"
  423. exit 3
  424. }
  425. wsl_cmds() {
  426. error "${WSL_ERROR_MSG}"
  427. exit 3
  428. }
  429. # =====================================================================
  430. # FreeBSD support functions
  431. enable_freebsd() {
  432. if ! sysrc netdata_enable=YES; then
  433. warning "Failed to enable netdata service."
  434. fi
  435. }
  436. disable_freebsd() {
  437. if ! sysrc netdata_enable=NO; then
  438. warning "Failed to disable netdata service."
  439. fi
  440. }
  441. install_freebsd_service() {
  442. install_generic_service freebsd "FreeBSD rc.d" /usr/local/etc/rc.d/netdata enable_freebsd disable_freebsd
  443. }
  444. freebsd_cmds() {
  445. NETDATA_START_CMD='service netdata start'
  446. NETDATA_STOP_CMD='service netdata stop'
  447. NETDATA_INSTALLER_START_CMD='service netdata onestart'
  448. }
  449. # =====================================================================
  450. # macOS support functions
  451. install_darwin_service() {
  452. info "Installing macOS plist file for launchd."
  453. if ! install -C -S -p -m 0644 -o 0 -g 0 system/netdata.plist /Library/LaunchDaemons/com.github.netdata.plist; then
  454. error "Failed to copy plist file."
  455. exit 4
  456. fi
  457. if ! launchctl load /Library/LaunchDaemons/com.github.netdata.plist; then
  458. error "Failed to load plist file."
  459. exit 4
  460. fi
  461. }
  462. darwin_cmds() {
  463. NETDATA_START_CMD='launchctl start com.github.netdata'
  464. NETDATA_STOP_CMD='launchctl stop com.github.netdata'
  465. }
  466. # =====================================================================
  467. # Linux support functions
  468. detect_linux_svc_type() {
  469. if [ "${SVC_TYPE}" = "detect" ]; then
  470. found_types=''
  471. for t in wsl ${LINUX_INIT_TYPES}; do
  472. case "$("check_${t}")" in
  473. YES)
  474. SVC_TYPE="${t}"
  475. break
  476. ;;
  477. NO) continue ;;
  478. OFFLINE)
  479. if [ -z "${found_types}" ]; then
  480. found_types="${t}"
  481. else
  482. found_types="${found_types} ${t}"
  483. fi
  484. ;;
  485. esac
  486. done
  487. if [ "${SVC_TYPE}" = "detect" ]; then
  488. if [ -z "${found_types}" ]; then
  489. error "Failed to detect what type of service manager is in use."
  490. else
  491. SVC_TYPE="$(echo "${found_types}" | cut -f 1 -d ' ')"
  492. warning "Failed to detect a running service manager, using detected (but not running) ${SVC_TYPE}."
  493. fi
  494. elif [ "${SVC_TYPE}" = "wsl" ]; then
  495. if [ "$(check_systemd)" = "YES" ]; then
  496. # Support for systemd in WSL.
  497. SVC_TYPE="systemd"
  498. elif [ "$(_check_lsb_ignore_systemd)" = "YES" ]; then
  499. # Support for LSB init.d in WSL.
  500. SVC_TYPE="lsb"
  501. elif [ "$(_check_initd_ignore_systemd)" = "YES" ]; then
  502. # Support for ‘generic’ init.d in WSL.
  503. SVC_TYPE="initd"
  504. fi
  505. fi
  506. fi
  507. echo "${SVC_TYPE}"
  508. }
  509. install_linux_service() {
  510. t="$(detect_linux_svc_type)"
  511. if [ -z "${t}" ]; then
  512. exit 2
  513. fi
  514. "install_${t}_service"
  515. }
  516. linux_cmds() {
  517. t="$(detect_linux_svc_type)"
  518. if [ -z "${t}" ]; then
  519. exit 2
  520. fi
  521. "${t}_cmds"
  522. }
  523. # =====================================================================
  524. # Service type display function
  525. show_service_type() {
  526. info "Detected platform: ${PLATFORM}"
  527. case "${PLATFORM}" in
  528. FreeBSD)
  529. info "Detected service managers:"
  530. info " - freebsd: YES"
  531. info "Would use freebsd service management."
  532. ;;
  533. Darwin)
  534. info "Detected service managers:"
  535. info " - launchd: YES"
  536. info "Would use launchd service management."
  537. ;;
  538. Linux)
  539. [ "$(check_wsl)" = "YES" ] && info "Detected WSL environment."
  540. info "Detected service managers:"
  541. for t in ${LINUX_INIT_TYPES}; do
  542. info " - ${t}: $("check_${t}")"
  543. done
  544. info "Would use $(detect_linux_svc_type) service management."
  545. ;;
  546. *)
  547. info "${PLATFORM} is not supported by this script. No service file would be installed."
  548. esac
  549. exit 0
  550. }
  551. # =====================================================================
  552. # Argument handling
  553. parse_args() {
  554. while [ -n "${1}" ]; do
  555. case "${1}" in
  556. "--source" | "-s")
  557. SVC_SOURCE="${2}"
  558. shift 1
  559. ;;
  560. "--type" | "-t")
  561. if [ "${2}" = "help" ]; then
  562. help_types
  563. exit 0
  564. else
  565. SVC_TYPE="${2}"
  566. shift 1
  567. fi
  568. ;;
  569. "--show-type") SHOW_SVC_TYPE=1 ; INSTALL=0 ;;
  570. "--save-cmds")
  571. if [ -z "${2}" ]; then
  572. info "No path specified to save command variables."
  573. exit 1
  574. else
  575. SAVE_CMDS_PATH="${2}"
  576. shift 1
  577. fi
  578. ;;
  579. "--cmds" | "-c") DUMP_CMDS=1 ;;
  580. "--cmds-only") INSTALL=0 ;;
  581. "--export-cmds") EXPORT_CMDS=1 ;;
  582. "--enable" | "-e") ENABLE="enable" ;;
  583. "--disable" | "-d") ENABLE="disable" ;;
  584. "--help" | "-h")
  585. usage
  586. exit 0
  587. ;;
  588. *)
  589. info "Unrecognized option '${1}'."
  590. exit 1
  591. ;;
  592. esac
  593. shift 1
  594. done
  595. if [ "${SVC_SOURCE#@}" = "libsysdir_POST@" ]; then
  596. SVC_SOURCE="$(dirname "${SCRIPT_SOURCE}")/../../lib/netdata/system"
  597. warning "SVC_SOURCE not templated, using ${SVC_SOURCE} as source directory."
  598. fi
  599. if [ ! -d "${SVC_SOURCE}" ] && [ "${INSTALL}" -eq 1 ]; then
  600. error "${SVC_SOURCE} does not appear to be a directory. Please specify a valid source for service files with the --source option."
  601. exit 1
  602. fi
  603. if valid_types | grep -vw "${SVC_TYPE}"; then
  604. error "${SVC_TYPE} is not supported on this platform."
  605. help_types
  606. exit 1
  607. fi
  608. }
  609. # =====================================================================
  610. # Core logic
  611. main() {
  612. trap "cleanup" EXIT
  613. parse_args "${@}"
  614. if [ "${SHOW_SVC_TYPE}" -eq 1 ]; then
  615. show_service_type
  616. else
  617. case "${PLATFORM}" in
  618. FreeBSD)
  619. [ "${INSTALL}" -eq 1 ] && install_freebsd_service
  620. freebsd_cmds
  621. ;;
  622. Darwin)
  623. [ "${INSTALL}" -eq 1 ] && install_darwin_service
  624. darwin_cmds
  625. ;;
  626. Linux)
  627. [ "${INSTALL}" -eq 1 ] && install_linux_service
  628. linux_cmds
  629. ;;
  630. *)
  631. error "${PLATFORM} is not supported by this script."
  632. exit 5
  633. ;;
  634. esac
  635. [ "${DUMP_CMDS}" -eq 1 ] && dump_cmds
  636. [ "${EXPORT_CMDS}" -eq 1 ] && export_cmds
  637. [ -n "${SAVE_CMDS_PATH}" ] && save_cmds
  638. fi
  639. exit 0
  640. }
  641. main "${@}"