Advertisement
Guest User

Untitled

a guest
Dec 14th, 2016
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.72 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. ## ------------------------------------------------------------------------------------------
  4. ## Emby Server : "Take Your Home Media Anywhere With Emby, Your personal media on any device"
  5. ## Website : https://emby.media/
  6. ## Source : https://github.com/MediaBrowser/Emby
  7. ## License : https://github.com/MediaBrowser/Emby/blob/master/LICENSE.md
  8. ## Copyright (C) 2016 | solabc16
  9. ##
  10. ## This program is free software; you can redistribute it and/or
  11. ## modify it under the terms of the GNU General Public License
  12. ## as published by the Free Software Foundation; either version 2
  13. ## of the License, or (at your option) any later version.
  14. ##
  15. ## This program is distributed in the hope that it will be useful,
  16. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ## GNU General Public License for more details.
  19. ##
  20. ## You should have received a copy of the GNU General Public License
  21. ## along with this program; if not, write to the Free Software
  22. ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  23. ## ------------------------------------------------------------------------------------------
  24.  
  25. ##
  26. ## VARIABLES: Core, must be declared at head of file.
  27. ##
  28.  
  29. script_version="3.0.8500.0-1"
  30. pkg_name="EmbyServer"
  31. script_directory=$(dirname $0)
  32. script_name=$(basename $0)
  33.  
  34. ##
  35. ## Workaround to enable support for DSM 5.x users.
  36. ##
  37.  
  38. if [ "$(grep "major" "/etc.defaults/VERSION" | cut -d"\"" -f2)" == "5" ]; then
  39. if [ -z "${BASH}" ]; then
  40. target_shell="/var/packages/${pkg_name}/scripts/utilities/sbin/bash"
  41. if [ -x "${target_shell}" ]; then
  42. "${target_shell}" "$0" "$@"
  43. exit
  44. fi
  45. fi
  46. fi
  47.  
  48. ##
  49. ## VARIABLES: DEPLOYMENT SPECIFIC
  50. ##
  51.  
  52. PKG_DISPNAME="Emby Server"
  53. EMBY_USER_LOGIN="embysvr"
  54. PKG_SERVICING_LOG="/var/log/synopkg_embyserver_servicing.log"
  55.  
  56. ##
  57. ## VARIABLES: GENERAL
  58. ##
  59.  
  60. ES=-1
  61. INSTALL_DIR="/var/packages/${SYNOPKG_PKGNAME}/target"
  62. LOG_ROOT="${INSTALL_DIR}/var/logs"
  63. SSS_LOG="/dev/null"
  64. TMP_LOG=""
  65. LOGAVAIL="false"
  66. EMBY_PID=${INSTALL_DIR}/var/embysvr.pid
  67. sss_mode=""
  68.  
  69. ##
  70. ## VARIABLES: EMBY
  71. ##
  72.  
  73. EMBY_EXEC="${INSTALL_DIR}/server/MediaBrowser.Server.Mono.exe"
  74.  
  75. ##
  76. ## VARIABLES: MONO
  77. ##
  78.  
  79. MONO_HOME="${INSTALL_DIR}/mono"
  80. MONO_BINS="${MONO_HOME}/bin"
  81. MONO_LIBS="${MONO_HOME}/lib"
  82. MONO_EXEC="${MONO_BINS}/mono"
  83.  
  84. ##
  85. ## VARIABLES: FFMPEG
  86. ##
  87.  
  88. FFMPEG_HOME="${INSTALL_DIR}/ffmpeg"
  89. FFMPEG_BINS="${FFMPEG_HOME}/bin"
  90. FFMPEG_LIBS="${FFMPEG_HOME}/lib"
  91.  
  92. ##
  93. ## VARIABLES: IMAGEMAGICK
  94. ##
  95.  
  96. MAGICK_HOME="${INSTALL_DIR}/imagemagick"
  97. MAGICK_BINS="${MAGICK_HOME}/bin"
  98. MAGICK_LIBS="${MAGICK_HOME}/lib"
  99.  
  100. ##
  101. ## VARIABLES: SQLITE
  102. ##
  103.  
  104. SQLITE_HOME="${INSTALL_DIR}/sqlite"
  105. SQLITE_BINS="${SQLITE_HOME}/bin"
  106. SQLITE_LIBS="${SQLITE_HOME}/lib"
  107.  
  108. ##
  109. ## Functions : Logging functions, helper functions not intended to be called directly.
  110. ##
  111.  
  112. create_timestamp(){
  113.  
  114. echo "$(date -u +'%Y-%m-%d %H:%M:%S %Z')"
  115.  
  116. }
  117.  
  118. log_entry(){
  119.  
  120. local ENTRY="[$(create_timestamp)]"
  121. ENTRY+=" $@"
  122.  
  123. if [ "${LOGAVAIL}" == "true" ]; then
  124.  
  125. echo "$ENTRY" >> ${TMP_LOG}
  126.  
  127. fi
  128.  
  129. }
  130.  
  131. ##
  132. ## Functions : Logging functions, log file helper functions
  133. ##
  134.  
  135. log_init(){
  136.  
  137. local RC=-1
  138.  
  139. if [ -d "${LOG_ROOT}" ]; then
  140.  
  141. SSS_LOG="${LOG_ROOT}/start-stop-status_$(date -u +'%Y%m%d').log"
  142.  
  143. if [ ! -f ${SSS_LOG} ]; then
  144.  
  145. touch ${SSS_LOG}
  146.  
  147. echo "* This log is rotated daily at 00:00 UTC, files are located at [${LOG_ROOT}]." >> ${SSS_LOG}
  148. echo "* The package servicing log can be found at [${PKG_SERVICING_LOG}]." >> ${SSS_LOG}
  149. echo "* Launch the package URL and navigate to Help -> Logs for ${PKG_DISPNAME} logs." >> ${SSS_LOG}
  150. echo "" >> ${SSS_LOG}
  151. echo "File Version : ${script_version}" >> ${SSS_LOG}
  152. echo "" >> ${SSS_LOG}
  153. echo "******************** LOG BEGINS ********************" >> ${SSS_LOG}
  154. echo "" >> ${SSS_LOG}
  155.  
  156. fi
  157.  
  158. TMP_LOG="/tmp/tmp.$(< /dev/urandom tr -dc A-Za-z0-9 | head -c10)"
  159.  
  160. LOGAVAIL="true"
  161.  
  162. RC=0
  163.  
  164. else
  165.  
  166. RC=1
  167.  
  168. fi
  169.  
  170. return ${RC}
  171.  
  172. }
  173.  
  174. log_commit(){
  175.  
  176. if [ "${LOGAVAIL}" == "true" ]; then
  177.  
  178. cat ${TMP_LOG} >> ${SSS_LOG}
  179. rm ${TMP_LOG}
  180.  
  181. fi
  182.  
  183. }
  184.  
  185. ##
  186. ## Functions : Logging functions, use these to write entries to the log.
  187. ##
  188.  
  189. log_message(){
  190.  
  191. log_entry "$1"
  192.  
  193. }
  194.  
  195. ##
  196. ## Functions: Synology system helper functions.
  197. ##
  198.  
  199. syno_ensure_pkgtl_status_is_stopped(){
  200.  
  201. local ES=-1
  202.  
  203. ##
  204.  
  205. if [ "${SYNOPKG_DSM_VERSION_MAJOR}" == "6" ]; then
  206.  
  207. if [ "${sss_mode}" == "status" ]; then
  208.  
  209. status pkgctl-${SYNOPKG_PKGNAME} | grep -q "stop/waiting" &> /dev/null
  210. ES=$?
  211.  
  212. if [ "${ES}" -ne 0 ]; then
  213.  
  214. stop pkgctl-${SYNOPKG_PKGNAME}
  215.  
  216. log_message ">> Ensuring package status is correct in DSM."
  217.  
  218. fi
  219.  
  220. fi
  221.  
  222. fi
  223.  
  224. }
  225.  
  226. ##
  227. ## FUNCTIONS: start/stop/status/log method implementations.
  228. ##
  229.  
  230. start_daemon(){
  231.  
  232. local RC=-1
  233. local ES=-1
  234. local COMMAND=""
  235. local STDOUT="${LOG_ROOT}/embysvr.stdout"
  236. local STDERR="${LOG_ROOT}/embysvr.stderr"
  237.  
  238. ##
  239.  
  240. daemon_status
  241. ES=$?
  242.  
  243. if [ "${ES}" -ne 0 ]; then
  244.  
  245. log_message ">> Starting ${PKG_DISPNAME}."
  246.  
  247. echo "" | tee -a ${STDOUT} ${STDERR} > /dev/null
  248. echo "------------------------------ $(create_timestamp) ------------------------------" | tee -a ${STDOUT} ${STDERR} > /dev/null
  249. echo "" | tee -a ${STDOUT} ${STDERR} > /dev/null
  250.  
  251. chown ${EMBY_USER_LOGIN}:users ${STDOUT} ${STDERR}
  252.  
  253. if [[ -d "/dev/dri" ]]; then
  254.  
  255. log_message ">> Looking for DRM devices: FOUND"
  256.  
  257. chmod -R go+rw /dev/dri/*
  258.  
  259. else
  260.  
  261. log_message ">> Looking for DRM devices: NONE"
  262.  
  263. fi
  264.  
  265. COMMAND+="env "
  266. COMMAND+="PATH=${MONO_BINS}:${FFMPEG_BINS}:${MAGICK_BINS}:${SQLITE_BINS}:${PATH} "
  267. COMMAND+="LD_LIBRARY_PATH=${FFMPEG_LIBS} "
  268. COMMAND+="${MONO_EXEC} "
  269. COMMAND+="${EMBY_EXEC} "
  270. COMMAND+="-package synology "
  271. COMMAND+="-programdata ${INSTALL_DIR}/var "
  272. COMMAND+="-ffmpeg ${FFMPEG_BINS}/ffmpeg "
  273. COMMAND+="-ffprobe ${FFMPEG_BINS}/ffprobe "
  274. COMMAND+="1>> ${STDOUT} "
  275. COMMAND+="2>> ${STDERR} & "
  276. COMMAND+="echo \$! > ${EMBY_PID};"
  277.  
  278. log_message ">> Launch command [${COMMAND}]."
  279.  
  280. su - ${EMBY_USER_LOGIN} -s /bin/sh -c "${COMMAND}"
  281. ES=$?
  282.  
  283. if [ "${ES}" -eq 0 ]; then
  284.  
  285. wait_for_status 0 20
  286. ES=$?
  287.  
  288. if [ "${ES}" -eq 0 ]; then
  289.  
  290. sleep 8
  291.  
  292. log_message ">> ${PKG_DISPNAME} started successfully."
  293.  
  294. RC=0
  295.  
  296. else
  297.  
  298. log_message ">> ${PKG_DISPNAME} failed to start."
  299. log_message ">> CHECK: [${STDOUT}]."
  300. log_message ">> CHECK: [${STDERR}]."
  301.  
  302. RC=1
  303.  
  304. fi
  305.  
  306. else
  307.  
  308. log_message ">> Launch invocation failed with status [${ES}]."
  309.  
  310. RC=1
  311.  
  312. fi
  313.  
  314. else
  315.  
  316. if [ "${ES}" -eq 0 ]; then
  317.  
  318. log_message ">> Start request ignored, package is running!"
  319.  
  320. RC=0
  321.  
  322. else
  323.  
  324. log_message ">> Package is not startable, status is [${ES}]."
  325.  
  326. RC=1
  327.  
  328. fi
  329.  
  330. fi
  331.  
  332. ##
  333.  
  334. return ${RC}
  335.  
  336. }
  337.  
  338. stop_daemon(){
  339.  
  340. local RC=-1
  341. local ES=-1
  342.  
  343. ##
  344.  
  345. daemon_status
  346. ES=$?
  347.  
  348. if [ "${ES}" -eq 0 ]; then
  349.  
  350. log_message ">> Stopping ${PKG_DISPNAME}."
  351.  
  352. if [ -f "${EMBY_PID}" ]; then
  353.  
  354. kill $(cat "${EMBY_PID}")
  355.  
  356. wait_for_status 3 20
  357. ES=$?
  358.  
  359. if [ "${ES}" -ne 0 ]; then
  360.  
  361. log_message ">> ${PKG_DISPNAME} is still running, forcing shutdown."
  362.  
  363. kill -9 $(cat "${EMBY_PID}")
  364.  
  365. RC=1
  366.  
  367. else
  368.  
  369. log_message ">> ${PKG_DISPNAME} shutdown successfully."
  370.  
  371. RC=0
  372.  
  373. fi
  374.  
  375. else
  376.  
  377. log_message ">> PID file [${EMBY_PID}] does not exist."
  378.  
  379. RC=1
  380.  
  381. fi
  382.  
  383. else
  384.  
  385. log_message ">> Stop request ignored, package is stopped!"
  386.  
  387. RC=0
  388.  
  389. fi
  390.  
  391. return ${RC}
  392.  
  393. }
  394.  
  395. daemon_status(){
  396.  
  397. ##
  398. ## 3rd-Party Package Developer Guide : Status Codes
  399. ##
  400. ## 0 : package is running.
  401. ## 1 : program of package is dead and /var/run pid file exists.
  402. ## 2 : program of package is dead and /var/lock lock file exists
  403. ## 3 : package is not running
  404. ## 4 : package status is unknown
  405. ## 150 : package is broken and should be reinstalled. Please note, broken status (150) is only supported by DSM 4.2 and later.
  406. ##
  407.  
  408. local RC=-1
  409.  
  410. if [ ! -f "${EMBY_PID}" ]; then
  411.  
  412. log_message ">> PID file [${EMBY_PID}] does not exist."
  413.  
  414. syno_ensure_pkgtl_status_is_stopped
  415.  
  416. RC=3
  417.  
  418. else
  419.  
  420. if [ -s "${EMBY_PID}" ]; then
  421.  
  422. if [ -d /proc/$(cat "${EMBY_PID}") ]; then
  423.  
  424. log_message ">> Process [$(cat ${EMBY_PID})] exists."
  425.  
  426. RC=0
  427.  
  428. else
  429.  
  430. log_message ">> Process [$(cat ${EMBY_PID})] does not exist."
  431.  
  432. rm ${EMBY_PID}
  433.  
  434. log_message ">> Removed PID file [${EMBY_PID}]."
  435.  
  436. syno_ensure_pkgtl_status_is_stopped
  437.  
  438. RC=1
  439.  
  440. fi
  441.  
  442. else
  443.  
  444. log_message ">> Removing PID file [${EMBY_PID}] as it is zero bytes."
  445.  
  446. rm ${EMBY_PID}
  447.  
  448. syno_ensure_pkgtl_status_is_stopped
  449.  
  450. RC=1
  451.  
  452. fi
  453.  
  454. fi
  455.  
  456. if [ "${RC}" -eq 0 ]; then
  457.  
  458. log_message ">> ${PKG_DISPNAME} is running."
  459.  
  460. else
  461.  
  462. log_message ">> ${PKG_DISPNAME} is not running."
  463.  
  464. fi
  465.  
  466. return ${RC}
  467.  
  468. }
  469.  
  470. wait_for_status(){
  471.  
  472. local RC=-1
  473. local ES=-1
  474. local COUNTER=$2
  475.  
  476. ##
  477.  
  478. log_message ">> Waiting for daemon status to become [$1]."
  479.  
  480. while [ ${COUNTER} -gt 0 ]; do
  481.  
  482. log_message ">> Checking status [${COUNTER}/$2]."
  483. daemon_status
  484. ES=$?
  485.  
  486. if [ ${ES} == $1 ]; then
  487.  
  488. COUNTER=-1
  489. RC=0
  490.  
  491. else
  492.  
  493. COUNTER=$(($COUNTER-1))
  494. sleep 1
  495.  
  496. fi
  497.  
  498. done
  499.  
  500. return ${RC}
  501.  
  502. }
  503.  
  504. ##
  505. ## FUNCTIONS: Script Entry Point Implementation
  506. ##
  507.  
  508. main(){
  509.  
  510. local RC=-1
  511. local ES=-1
  512.  
  513. ##
  514.  
  515. log_init
  516. ES=$?
  517.  
  518. if [ "${ES}" -eq 0 ]; then
  519.  
  520. log_message "Script called with action [$1]..."
  521.  
  522. case $1 in
  523.  
  524. start)
  525.  
  526. sss_mode="start"
  527.  
  528. start_daemon
  529. ES=$?
  530.  
  531. RC=${ES}
  532.  
  533. ;;
  534.  
  535. stop)
  536.  
  537. sss_mode="stop"
  538.  
  539. stop_daemon
  540. ES=$?
  541.  
  542. RC=${ES}
  543.  
  544. ;;
  545.  
  546. status)
  547.  
  548. sss_mode="status"
  549.  
  550. daemon_status
  551. ES=$?
  552.  
  553. RC=${ES}
  554.  
  555. ;;
  556.  
  557. log)
  558.  
  559. log_message ">> Returning log file location [${SSS_LOG}]."
  560.  
  561. echo "${SSS_LOG}"
  562.  
  563. RC=0
  564.  
  565. ;;
  566.  
  567. *)
  568.  
  569. log_message ">> Requested function is not supported."
  570.  
  571. RC=1
  572.  
  573. ;;
  574.  
  575. esac
  576.  
  577. fi
  578.  
  579. return ${RC}
  580.  
  581. }
  582.  
  583. ##
  584. ## SCRIPT: Entry Point Begin
  585. ##
  586.  
  587. main "$@"
  588. ES=$?
  589.  
  590. log_message ">> Exit status is [${ES}]."
  591. log_commit
  592.  
  593. exit ${ES}
  594.  
  595. ##
  596. ## SCRIPT: Entry Point End
  597. ##
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement