install-service.sh.in 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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_path="${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}/${svc_path}/netdata" "${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}/systemd/netdata.service"
  200. if [ "$(systemctl --version | head -n 1 | cut -f 2 -d ' ')" -le 235 ]; then
  201. SRCFILE="${SVC_SOURCE}/systemd/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 /etc/conf.d, it's not OpenRC
  243. [ ! -d /etc/conf.d ] && echo "NO" && return 0
  244. # if there is no rc-update command, it's not OpenRC
  245. [ -z "$(command -v rc-update 2>/dev/null || true)" ] && echo "NO" && return 0
  246. # If /run/openrc/softlevel exists, it's OpenRC
  247. [ -f /run/openrc/softlevel ] && echo "YES" && return 0
  248. # if PID 1 is openrc-init, it's OpenRC
  249. [ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "openrc-init" ] && echo "YES" && return 0
  250. # if there is an openrc command, it's OpenRC, but not booted as such
  251. [ -n "$(command -v openrc 2>/dev/null || true)" ] && echo "OFFLINE" && return 0
  252. # if /etc/init.d/local exists and has `openrc-run` in it's shebang line, it’s OpenRC, but not booted as such
  253. [ -r /etc/init.d/local ] && head -n 1 /etc/init.d/local | grep -q openrc-run && echo "OFFLINE" && return 0
  254. # Otherwise, it’s not OpenRC
  255. echo "NO" && return 0
  256. }
  257. check_openrc() {
  258. if [ -z "${IS_OPENRC}" ]; then
  259. IS_OPENRC="$(_check_openrc)"
  260. fi
  261. echo "${IS_OPENRC}"
  262. }
  263. enable_openrc() {
  264. if [ "$(check_openrc)" = "YES" ]; then
  265. runlevel="$(rc-status -r)"
  266. fi
  267. runlevel="${runlevel:-default}"
  268. if ! rc-update add netdata "${runlevel}"; then
  269. warning "Failed to enable Netdata service in runlevel ${runlevel}."
  270. fi
  271. }
  272. disable_openrc() {
  273. for runlevel in /etc/runlevels/*; do
  274. if [ -e "${runlevel}/netdata" ]; then
  275. runlevel="$(basename "${runlevel}")"
  276. if ! rc-update del netdata "${runlevel}"; then
  277. warning "Failed to disable Netdata service in runlevel ${runlevel}."
  278. fi
  279. fi
  280. done
  281. }
  282. install_openrc_service() {
  283. install_generic_service openrc/init.d OpenRC /etc/init.d/netdata enable_openrc disable_openrc
  284. if [ ! -f /etc/conf.d/netdata ]; then
  285. info "Installing OpenRC configuration file."
  286. if ! install -p -m 0755 -o 0 -g 0 "${SVC_SOURCE}/openrc/conf.d/netdata" "/etc/conf.d/netdata"; then
  287. warning "Failed to install configuration file, however the service will still work."
  288. fi
  289. fi
  290. }
  291. openrc_cmds() {
  292. if [ "$(check_openrc)" = "YES" ]; then
  293. NETDATA_START_CMD='rc-service netdata start'
  294. NETDATA_STOP_CMD='rc-service netdata stop'
  295. else # Not booted using OpenRC, use external defaults by not providing commands.
  296. warning "Detected OpenRC, but the system is not booted using OpenRC. Unable to provide commands to start or stop Netdata using the service manager."
  297. fi
  298. }
  299. # =====================================================================
  300. # LSB init script support functions
  301. _check_lsb_ignore_systemd() {
  302. # if there is no /etc/init.d directory, it’s not an LSB system
  303. [ ! -d /etc/init.d ] && echo "NO" && return 0
  304. # If it's an OpenRC system, then it's not an LSB system
  305. [ "$(check_openrc)" != "NO" ] && echo "NO" && return 0
  306. # If /lib/lsb/init-functions exists, it’s an LSB system
  307. [ -f /lib/lsb/init-functions ] && echo "YES" && return 0
  308. echo "NO" && return 0
  309. }
  310. _check_lsb() {
  311. # if there is _any_ systemd, it’s not an LSB system
  312. [ "$(check_systemd)" != "NO" ] && echo "NO" && return 0
  313. _check_lsb_ignore_systemd
  314. }
  315. check_lsb() {
  316. if [ -z "${IS_LSB}" ]; then
  317. IS_LSB="$(_check_lsb)"
  318. fi
  319. echo "${IS_LSB}"
  320. }
  321. enable_lsb() {
  322. if ! update-rc.d netdata defaults; then
  323. warning "Failed to enable Netdata service."
  324. elif ! update-rc.d netdata defaults-disabled; then
  325. warning "Failed to fully enable Netdata service."
  326. fi
  327. }
  328. disable_lsb() {
  329. if ! update-rc.d netdata remove; then
  330. warning "Failed to disable Netdata service."
  331. fi
  332. }
  333. install_lsb_service() {
  334. install_generic_service lsb/init.d LSB /etc/init.d/netdata enable_lsb disable_lsb
  335. }
  336. lsb_cmds() {
  337. NETDATA_START_CMD='/etc/init.d/netdata start'
  338. NETDATA_STOP_CMD='/etc/init.d/netdata stop'
  339. }
  340. # =====================================================================
  341. # init.d init script support functions
  342. _check_initd_ignore_systemd() {
  343. # if there is no /etc/init.d directory, it’s not an init.d system
  344. [ ! -d /etc/init.d ] && echo "NO" && return 1
  345. # if there is no chkconfig command, it's not a (usable) init.d system
  346. [ -z "$(command -v chkconfig 2>/dev/null || true)" ] && echo "NO" && return 0
  347. # if there is _any_ openrc, it’s not init.d
  348. [ "$(check_openrc)" != "NO" ] && echo "NO" && return 0
  349. # if it's not an LSB setup, it’s init.d
  350. [ "$(check_lsb)" != "NO" ] && echo "NO" && return 0
  351. echo "YES" && return 0
  352. }
  353. _check_initd() {
  354. # if there is _any_ systemd, it’s not init.d
  355. [ "$(check_systemd)" != "NO" ] && echo "NO" && return 0
  356. _check_initd_ignore_systemd
  357. }
  358. check_initd() {
  359. if [ -z "${IS_INITD}" ]; then
  360. IS_INITD="$(_check_initd)"
  361. fi
  362. echo "${IS_INITD}"
  363. }
  364. enable_initd() {
  365. if ! chkconfig netdata on; then
  366. warning "Failed to enable Netdata service."
  367. fi
  368. }
  369. disable_initd() {
  370. if ! chkconfig netdata off; then
  371. warning "Failed to disable Netdata service."
  372. fi
  373. }
  374. install_initd_service() {
  375. install_generic_service initd/init.d init.d /etc/init.d/netdata enable_initd disable_initd
  376. }
  377. initd_cmds() {
  378. NETDATA_START_CMD='/etc/init.d/netdata start'
  379. NETDATA_STOP_CMD='/etc/init.d/netdata stop'
  380. }
  381. # =====================================================================
  382. # runit support functions
  383. #
  384. # Currently not supported, this exists to provide useful error messages.
  385. _check_runit() {
  386. # if there is no runsvdir command, then it's not runit
  387. [ -z "$(command -v runsvdir 2>/dev/null || true)" ] && echo "NO" && return 0
  388. # if there is no runit command, then it's not runit
  389. [ -z "$(command -v runit 2>/dev/null || true)" ] && echo "NO" && return 0
  390. # if /run/runit exists, then it's runit
  391. [ -d /run/runit ] && echo "YES" && return 0
  392. # if /etc/runit/1 exists and is executable, then it's runit
  393. [ -x /etc/runit/1 ] && echo "YES" && return 0
  394. echo "NO" && return 0
  395. }
  396. check_runit() {
  397. if [ -z "${IS_RUNIT}" ]; then
  398. IS_RUNIT="$(_check_runit)"
  399. fi
  400. echo "${IS_RUNIT}"
  401. }
  402. install_runit_service() {
  403. error "Detected runit, which we do not currently support."
  404. exit 3
  405. }
  406. runit_cmds() {
  407. error "Detected runit, which we do not currently support."
  408. exit 3
  409. }
  410. # =====================================================================
  411. # WSL support functions
  412. #
  413. # Cannot be supported, this exists to provide useful error messages.
  414. _check_wsl() {
  415. # If uname -r contains the string WSL, then it's WSL.
  416. uname -r | grep -q 'WSL' && echo "YES" && return 0
  417. # If uname -r contains the string Microsoft, then it's WSL.
  418. # This probably throws a false positive on CBL-Mariner, but it's part of what MS officially recommends for
  419. # detecting if you're running under WSL.
  420. uname -r | grep -q "Microsoft" && echo "YES" && return 0
  421. echo "NO" && return 0
  422. }
  423. check_wsl() {
  424. if [ -z "${IS_WSL}" ]; then
  425. IS_WSL="$(_check_wsl)"
  426. fi
  427. echo "${IS_WSL}"
  428. }
  429. install_wsl_service() {
  430. error "${WSL_ERROR_MSG}"
  431. exit 3
  432. }
  433. wsl_cmds() {
  434. error "${WSL_ERROR_MSG}"
  435. exit 3
  436. }
  437. # =====================================================================
  438. # FreeBSD support functions
  439. enable_freebsd() {
  440. if ! sysrc netdata_enable=YES; then
  441. warning "Failed to enable netdata service."
  442. fi
  443. }
  444. disable_freebsd() {
  445. if ! sysrc netdata_enable=NO; then
  446. warning "Failed to disable netdata service."
  447. fi
  448. }
  449. install_freebsd_service() {
  450. install_generic_service freebsd/rc.d "FreeBSD rc.d" /usr/local/etc/rc.d/netdata enable_freebsd disable_freebsd
  451. }
  452. freebsd_cmds() {
  453. NETDATA_START_CMD='service netdata start'
  454. NETDATA_STOP_CMD='service netdata stop'
  455. NETDATA_INSTALLER_START_CMD='service netdata onestart'
  456. }
  457. # =====================================================================
  458. # macOS support functions
  459. install_darwin_service() {
  460. info "Installing macOS plist file for launchd."
  461. if ! install -C -S -p -m 0644 -o 0 -g 0 system/launchd/netdata.plist /Library/LaunchDaemons/com.github.netdata.plist; then
  462. error "Failed to copy plist file."
  463. exit 4
  464. fi
  465. if ! launchctl load /Library/LaunchDaemons/com.github.netdata.plist; then
  466. error "Failed to load plist file."
  467. exit 4
  468. fi
  469. }
  470. darwin_cmds() {
  471. NETDATA_START_CMD='launchctl start com.github.netdata'
  472. NETDATA_STOP_CMD='launchctl stop com.github.netdata'
  473. }
  474. # =====================================================================
  475. # Linux support functions
  476. detect_linux_svc_type() {
  477. if [ "${SVC_TYPE}" = "detect" ]; then
  478. found_types=''
  479. for t in wsl ${LINUX_INIT_TYPES}; do
  480. case "$("check_${t}")" in
  481. YES)
  482. SVC_TYPE="${t}"
  483. break
  484. ;;
  485. NO) continue ;;
  486. OFFLINE)
  487. if [ -z "${found_types}" ]; then
  488. found_types="${t}"
  489. else
  490. found_types="${found_types} ${t}"
  491. fi
  492. ;;
  493. esac
  494. done
  495. if [ "${SVC_TYPE}" = "detect" ]; then
  496. if [ -z "${found_types}" ]; then
  497. error "Failed to detect what type of service manager is in use."
  498. else
  499. SVC_TYPE="$(echo "${found_types}" | cut -f 1 -d ' ')"
  500. warning "Failed to detect a running service manager, using detected (but not running) ${SVC_TYPE}."
  501. fi
  502. elif [ "${SVC_TYPE}" = "wsl" ]; then
  503. if [ "$(check_systemd)" = "YES" ]; then
  504. # Support for systemd in WSL.
  505. SVC_TYPE="systemd"
  506. elif [ "$(_check_lsb_ignore_systemd)" = "YES" ]; then
  507. # Support for LSB init.d in WSL.
  508. SVC_TYPE="lsb"
  509. elif [ "$(_check_initd_ignore_systemd)" = "YES" ]; then
  510. # Support for ‘generic’ init.d in WSL.
  511. SVC_TYPE="initd"
  512. fi
  513. fi
  514. fi
  515. echo "${SVC_TYPE}"
  516. }
  517. install_linux_service() {
  518. t="$(detect_linux_svc_type)"
  519. if [ -z "${t}" ] || [ "${t}" = 'detect' ]; then
  520. exit 2
  521. fi
  522. "install_${t}_service"
  523. }
  524. linux_cmds() {
  525. t="$(detect_linux_svc_type)"
  526. if [ -z "${t}" ] || [ "${t}" = 'detect' ]; then
  527. exit 2
  528. fi
  529. "${t}_cmds"
  530. }
  531. # =====================================================================
  532. # Service type display function
  533. show_service_type() {
  534. info "Detected platform: ${PLATFORM}"
  535. case "${PLATFORM}" in
  536. FreeBSD)
  537. info "Detected service managers:"
  538. info " - freebsd: YES"
  539. info "Would use freebsd service management."
  540. ;;
  541. Darwin)
  542. info "Detected service managers:"
  543. info " - launchd: YES"
  544. info "Would use launchd service management."
  545. ;;
  546. Linux)
  547. [ "$(check_wsl)" = "YES" ] && info "Detected WSL environment."
  548. info "Detected service managers:"
  549. for t in ${LINUX_INIT_TYPES}; do
  550. info " - ${t}: $("check_${t}")"
  551. done
  552. info "Would use $(detect_linux_svc_type) service management."
  553. ;;
  554. *)
  555. info "${PLATFORM} is not supported by this script. No service file would be installed."
  556. esac
  557. exit 0
  558. }
  559. # =====================================================================
  560. # Argument handling
  561. parse_args() {
  562. while [ -n "${1}" ]; do
  563. case "${1}" in
  564. "--source" | "-s")
  565. SVC_SOURCE="${2}"
  566. shift 1
  567. ;;
  568. "--type" | "-t")
  569. if [ "${2}" = "help" ]; then
  570. help_types
  571. exit 0
  572. else
  573. SVC_TYPE="${2}"
  574. shift 1
  575. fi
  576. ;;
  577. "--show-type") SHOW_SVC_TYPE=1 ; INSTALL=0 ;;
  578. "--save-cmds")
  579. if [ -z "${2}" ]; then
  580. info "No path specified to save command variables."
  581. exit 1
  582. else
  583. SAVE_CMDS_PATH="${2}"
  584. shift 1
  585. fi
  586. ;;
  587. "--cmds" | "-c") DUMP_CMDS=1 ;;
  588. "--cmds-only") INSTALL=0 ;;
  589. "--export-cmds") EXPORT_CMDS=1 ;;
  590. "--enable" | "-e") ENABLE="enable" ;;
  591. "--disable" | "-d") ENABLE="disable" ;;
  592. "--help" | "-h")
  593. usage
  594. exit 0
  595. ;;
  596. *)
  597. info "Unrecognized option '${1}'."
  598. exit 1
  599. ;;
  600. esac
  601. shift 1
  602. done
  603. if [ "${SVC_SOURCE#@}" = "libsysdir_POST@" ]; then
  604. SVC_SOURCE="$(dirname "${SCRIPT_SOURCE}")/../../lib/netdata/system"
  605. warning "SVC_SOURCE not templated, using ${SVC_SOURCE} as source directory."
  606. fi
  607. if [ ! -d "${SVC_SOURCE}" ] && [ "${INSTALL}" -eq 1 ]; then
  608. error "${SVC_SOURCE} does not appear to be a directory. Please specify a valid source for service files with the --source option."
  609. exit 1
  610. fi
  611. if valid_types | grep -vw "${SVC_TYPE}"; then
  612. error "${SVC_TYPE} is not supported on this platform."
  613. help_types
  614. exit 1
  615. fi
  616. }
  617. # =====================================================================
  618. # Core logic
  619. main() {
  620. trap "cleanup" EXIT
  621. parse_args "${@}"
  622. if [ "${SHOW_SVC_TYPE}" -eq 1 ]; then
  623. show_service_type
  624. else
  625. case "${PLATFORM}" in
  626. FreeBSD)
  627. [ "${INSTALL}" -eq 1 ] && install_freebsd_service
  628. freebsd_cmds
  629. ;;
  630. Darwin)
  631. [ "${INSTALL}" -eq 1 ] && install_darwin_service
  632. darwin_cmds
  633. ;;
  634. Linux)
  635. [ "${INSTALL}" -eq 1 ] && install_linux_service
  636. linux_cmds
  637. ;;
  638. *)
  639. error "${PLATFORM} is not supported by this script."
  640. exit 5
  641. ;;
  642. esac
  643. [ "${DUMP_CMDS}" -eq 1 ] && dump_cmds
  644. [ "${EXPORT_CMDS}" -eq 1 ] && export_cmds
  645. [ -n "${SAVE_CMDS_PATH}" ] && save_cmds
  646. fi
  647. exit 0
  648. }
  649. main "${@}"