install.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/bin/bash
  2. # user can config the following configs, then package.
  3. INSTALL=/usr/local/srs
  4. ##################################################################################
  5. ##################################################################################
  6. ##################################################################################
  7. # discover the current work dir, the log and access.
  8. echo "argv[0]=$0"
  9. if [[ ! -f $0 ]]; then
  10. echo "directly execute the scripts on shell.";
  11. work_dir=`pwd`
  12. else
  13. echo "execute scripts in file: $0";
  14. work_dir=`dirname $0`; work_dir=`(cd ${work_dir} && pwd)`
  15. fi
  16. product_dir=$work_dir
  17. log="${work_dir}/logs/package.`date +%s`.log" && . ${product_dir}/scripts/_log.sh && check_log
  18. ret=$?; if [[ $ret -ne 0 ]]; then exit $ret; fi
  19. # check lsb_release
  20. ok_msg "check tools"
  21. lsb_release -v >/dev/null 2>&1; ret=$?
  22. if [[ $ret -ne 0 ]]; then failed_msg "no lsb_release, install: yum install -y redhat-lsb"; exit $ret; fi
  23. # user must stop service first.
  24. ok_msg "check previous install"
  25. if [[ -f /etc/init.d/srs ]]; then
  26. /etc/init.d/srs status >/dev/null 2>&1
  27. ret=$?; if [[ 0 -eq ${ret} ]]; then
  28. failed_msg "you must stop the service first: sudo /etc/init.d/srs stop";
  29. exit 1;
  30. fi
  31. fi
  32. ok_msg "previous install checked"
  33. # backup old srs
  34. ok_msg "backup old srs"
  35. install_root=$INSTALL
  36. install_bin=$install_root/objs/srs
  37. if [[ -d $install_root ]]; then
  38. version="unknown"
  39. if [[ -f $install_bin ]]; then
  40. version=`$install_bin -v 2>/dev/stdout 1>/dev/null`
  41. fi
  42. backup_dir=${install_root}.`date "+%Y-%m-%d_%H-%M-%S"`.v-$version
  43. ok_msg "backup installed dir, version=$version"
  44. ok_msg " to=$backup_dir"
  45. mv $install_root $backup_dir >>$log 2>&1
  46. ret=$?; if [[ 0 -ne ${ret} ]]; then failed_msg "backup installed dir failed"; exit $ret; fi
  47. ok_msg "backup installed dir success"
  48. fi
  49. ok_msg "old srs backuped"
  50. # prepare files.
  51. ok_msg "prepare files"
  52. (
  53. sed -i "s|^ROOT=.*|ROOT=\"${INSTALL}\"|g" $work_dir/${INSTALL}/etc/init.d/srs
  54. ) >> $log 2>&1
  55. ret=$?; if [[ 0 -ne ${ret} ]]; then failed_msg "prepare files failed"; exit $ret; fi
  56. ok_msg "prepare files success"
  57. # copy core files
  58. ok_msg "copy core components"
  59. (
  60. mkdir -p $install_root
  61. cp -r $work_dir/${INSTALL}/conf $install_root &&
  62. cp -r $work_dir/${INSTALL}/etc $install_root &&
  63. cp -r $work_dir/${INSTALL}/usr $install_root &&
  64. cp -r $work_dir/${INSTALL}/objs $install_root
  65. ) >>$log 2>&1
  66. ret=$?; if [[ 0 -ne ${ret} ]]; then failed_msg "copy core components failed"; exit $ret; fi
  67. ok_msg "copy core components success"
  68. # install init.d scripts
  69. ok_msg "install init.d scripts"
  70. (
  71. rm -rf /etc/init.d/srs &&
  72. ln -sf $install_root/etc/init.d/srs /etc/init.d/srs
  73. ) >>$log 2>&1
  74. ret=$?; if [[ 0 -ne ${ret} ]]; then failed_msg "install init.d scripts failed"; exit $ret; fi
  75. ok_msg "install init.d scripts success"
  76. # For systemctl
  77. if [[ -d /usr/lib/systemd/system ]]; then
  78. ok_msg "install srs.service for systemctl"
  79. (
  80. cp -f $install_root/usr/lib/systemd/system/srs.service /usr/lib/systemd/system/srs.service &&
  81. systemctl daemon-reload
  82. ) >>$log 2>&1
  83. ret=$?; if [[ 0 -ne ${ret} ]]; then failed_msg "install srs.service for systemctl failed"; exit $ret; fi
  84. ok_msg "install srs.service for systemctl success"
  85. fi
  86. # install system service
  87. lsb_release --id|grep "CentOS" >/dev/null 2>&1; os_id_centos=$?
  88. lsb_release --id|grep "Ubuntu" >/dev/null 2>&1; os_id_ubuntu=$?
  89. lsb_release --id|grep "Debian" >/dev/null 2>&1; os_id_debian=$?
  90. lsb_release --id|grep "Raspbian" >/dev/null 2>&1; os_id_rasabian=$?
  91. if [[ 0 -eq $os_id_centos ]]; then
  92. ok_msg "install system service for CentOS"
  93. if [[ -d /usr/lib/systemd/system ]]; then
  94. systemctl enable srs
  95. else
  96. /sbin/chkconfig --add srs && /sbin/chkconfig srs on
  97. fi
  98. ret=$?; if [[ 0 -ne ${ret} ]]; then failed_msg "install system service failed"; exit $ret; fi
  99. ok_msg "install system service success"
  100. elif [[ 0 -eq $os_id_ubuntu ]]; then
  101. ok_msg "install system service for Ubuntu"
  102. update-rc.d srs defaults
  103. ret=$?; if [[ 0 -ne ${ret} ]]; then failed_msg "install system service failed"; exit $ret; fi
  104. ok_msg "install system service success"
  105. elif [[ 0 -eq $os_id_debian ]]; then
  106. ok_msg "install system service for Debian"
  107. update-rc.d srs defaults
  108. ret=$?; if [[ 0 -ne ${ret} ]]; then failed_msg "install system service failed"; exit $ret; fi
  109. ok_msg "install system service success"
  110. elif [[ 0 -eq $os_id_rasabian ]]; then
  111. ok_msg "install system service for RaspberryPi"
  112. update-rc.d srs defaults
  113. ret=$?; if [[ 0 -ne ${ret} ]]; then failed_msg "install system service failed"; exit $ret; fi
  114. ok_msg "install system service success"
  115. else
  116. warn_msg "ignore and donot install system service for `lsb_release --id|awk '{print $3}'`."
  117. fi
  118. echo ""
  119. echo "see: https://github.com/ossrs/srs/wiki/v4_CN_LinuxService"
  120. echo "install success, you can start SRS on CentOS6:"
  121. echo -e "${GREEN} sudo /etc/init.d/srs start${BLACK}"
  122. echo "or CentOS7:"
  123. echo -e "${GREEN} sudo systemctl start srs${BLACK}"
  124. echo "srs root is ${INSTALL}"
  125. exit 0