install.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "abort, please install lsb_release"; 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}/objs $install_root
  64. ) >>$log 2>&1
  65. ret=$?; if [[ 0 -ne ${ret} ]]; then failed_msg "copy core components failed"; exit $ret; fi
  66. ok_msg "copy core components success"
  67. # install init.d scripts
  68. ok_msg "install init.d scripts"
  69. (
  70. rm -rf /etc/init.d/srs &&
  71. ln -sf $install_root/etc/init.d/srs /etc/init.d/srs
  72. ) >>$log 2>&1
  73. ret=$?; if [[ 0 -ne ${ret} ]]; then failed_msg "install init.d scripts failed"; exit $ret; fi
  74. ok_msg "install init.d scripts success"
  75. # install system service
  76. lsb_release --id|grep "CentOS" >/dev/null 2>&1; os_id_centos=$?
  77. lsb_release --id|grep "Ubuntu" >/dev/null 2>&1; os_id_ubuntu=$?
  78. lsb_release --id|grep "Debian" >/dev/null 2>&1; os_id_debian=$?
  79. lsb_release --id|grep "Raspbian" >/dev/null 2>&1; os_id_rasabian=$?
  80. if [[ 0 -eq $os_id_centos ]]; then
  81. ok_msg "install system service for CentOS"
  82. /sbin/chkconfig --add srs && /sbin/chkconfig srs on
  83. ret=$?; if [[ 0 -ne ${ret} ]]; then failed_msg "install system service failed"; exit $ret; fi
  84. ok_msg "install system service success"
  85. elif [[ 0 -eq $os_id_ubuntu ]]; then
  86. ok_msg "install system service for Ubuntu"
  87. update-rc.d srs defaults
  88. ret=$?; if [[ 0 -ne ${ret} ]]; then failed_msg "install system service failed"; exit $ret; fi
  89. ok_msg "install system service success"
  90. elif [[ 0 -eq $os_id_debian ]]; then
  91. ok_msg "install system service for Debian"
  92. update-rc.d srs defaults
  93. ret=$?; if [[ 0 -ne ${ret} ]]; then failed_msg "install system service failed"; exit $ret; fi
  94. ok_msg "install system service success"
  95. elif [[ 0 -eq $os_id_rasabian ]]; then
  96. ok_msg "install system service for RaspberryPi"
  97. update-rc.d srs defaults
  98. ret=$?; if [[ 0 -ne ${ret} ]]; then failed_msg "install system service failed"; exit $ret; fi
  99. ok_msg "install system service success"
  100. else
  101. warn_msg "ignore and donot install system service for `lsb_release --id|awk '{print $3}'`."
  102. fi
  103. echo ""
  104. echo "see: https://github.com/ossrs/srs/wiki/v1_CN_LinuxService"
  105. echo "install success, you can:"
  106. echo -e "${GREEN} sudo /etc/init.d/srs start${BLACK}"
  107. echo "srs root is ${INSTALL}"
  108. exit 0