INSTALL.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/bin/dash
  2. #
  3. # Copyright (c) 2013-2016 Marcus Rohrmoser, http://purl.mro.name/recorder
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  6. # associated documentation files (the "Software"), to deal in the Software without restriction,
  7. # including without limitation the rights to use, copy, modify, merge, publish, distribute,
  8. # sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in all copies or
  12. # substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  15. # NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  17. # OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  18. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. #
  20. # MIT License http://opensource.org/licenses/MIT
  21. #
  22. #
  23. # INSTALLATION
  24. #
  25. # $ curl https://raw.github.com/mro/internet-radio-recorder/master/INSTALL.sh > INSTALL.sh && dash INSTALL.sh
  26. #
  27. git_repo="https://github.com/mro/internet-radio-recorder.git"
  28. www_base="/srv" # has to match simple-vhost.server-root from /etc/lighttpd/conf-available/10-simple-vhost.conf
  29. user="internet-radio-recorder"
  30. group="www-data"
  31. tmp_dir="/tmp/$user"
  32. # check preliminaries
  33. sudo -V > /dev/null || { echo "Sorry, but I need that one." 1>&2 && exit 2; }
  34. apt-get --version > /dev/null || { echo "Sorry, but I need that one." 1>&2 && exit 2; }
  35. apt-mark --version > /dev/null || { echo "Sorry, but I need that one." 1>&2 && exit 2; }
  36. me=$(basename "$0")
  37. git_branch="$1"
  38. if [ "" = "$git_branch" ] ; then
  39. git_branch="master"
  40. fi
  41. RECORDER_DOMAIN="$2"
  42. echo_prefix="RadioPi:"
  43. if [ "" = "$RECORDER_DOMAIN" ] ; then
  44. mkdir "$tmp_dir" || { echo "Sorry, but I insist to create that (clean slate)." 1>&2 && exit 3; }
  45. apt-mark showmanual > "$tmp_dir/apt-mark.showmanual.pre"
  46. apt-mark showauto > "$tmp_dir/apt-mark.showauto.pre"
  47. echo "$echo_prefix installing internet-radio-recorder from $git_repo to /srv/<recorder.example.com>"
  48. read -p "$echo_prefix domain (e.g. recorder.example.com): " RECORDER_DOMAIN
  49. if [ "" = "$RECORDER_DOMAIN" ] ; then
  50. echo "$echo_prefix no domain given, exiting..."
  51. exit 1
  52. fi
  53. recorder_base="$www_base/$RECORDER_DOMAIN"
  54. cd "$www_base"
  55. if [ $? -ne 0 ] ; then exit 2; fi
  56. echo "$echo_prefix creating $recorder_base..."
  57. echo "$echo_prefix grabbing git repo $git_repo ..."
  58. sudo apt-get install git-core || { echo "$echo_prefix couldn't install git-core. Exiting..." 1>&2 && exit 3; }
  59. if [ -d "$recorder_base/.git" ] ; then
  60. echo "$echo_prefix I'm not safe to be re-run for now. Exiting..."
  61. exit 4
  62. else
  63. sudo git clone "$git_repo" "$recorder_base"
  64. cd "$recorder_base"
  65. sudo git checkout "$git_branch"
  66. fi
  67. if [ $? -ne 0 ] ; then
  68. echo "$echo_prefix couldn't fetch git-repo. Exiting..."
  69. exit 5
  70. fi
  71. echo "$echo_prefix handing over to $me from git..."
  72. dash "$me" "$git_branch" "$RECORDER_DOMAIN"
  73. exit 0
  74. else
  75. recorder_base="$www_base/$RECORDER_DOMAIN"
  76. echo "$echo_prefix Continue install for recorder domain '$RECORDER_DOMAIN' ($git_branch)..."
  77. fi
  78. echo "$echo_prefix Prerequisites - apt packages"
  79. pkgs="adduser make"
  80. ### job scheduler cron + at:
  81. pkgs="$pkgs cron at"
  82. ### lighttpd (or just any web server to serve static files + simple CGIs + some convenience redirects):
  83. pkgs="$pkgs lighttpd"
  84. # user/password database:
  85. pkgs="$pkgs apache2-utils"
  86. ### lua, luarocks, lfs:
  87. pkgs="$pkgs luarocks lua-posix lua-filesystem"
  88. ### streamripper:
  89. pkgs="$pkgs streamripper"
  90. echo "$echo_prefix apt-get install $pkgs"
  91. sudo apt-get install $pkgs
  92. if [ $? -ne 0 ] ; then
  93. echo "Couldn't install all preliminaries" 1>&2
  94. exit 6
  95. fi
  96. echo "$echo_prefix Prerequisites - configuration.."
  97. sudo adduser --system --home="$recorder_base" --no-create-home --ingroup "$group" "$user" || { echo "Couldn't create dedicated user" 1>&2 && exit 7; }
  98. sudo cp "$tmp_dir"/apt-mark.show*.pre "$recorder_base/logs/"
  99. rm -rf "$tmp_dir"
  100. apt-mark showmanual | sudo tee "$recorder_base/logs/apt-mark.showmanual.post" > /dev/null
  101. apt-mark showauto | sudo tee "$recorder_base/logs/apt-mark.showauto.post" > /dev/null
  102. sudo chown -R "$user:$group" "$recorder_base" || { echo "Couldn't chown" 1>&2 && exit 8; }
  103. sudo chmod g+w "$recorder_base/logs"
  104. sudo tee /etc/sudoers.d/internet-radio-recorder >/dev/null - <<END_OF_SUDOERS
  105. www-data ALL = ($user:$group) NOPASSWD: $recorder_base/htdocs/enclosures/app/ad_hoc.lua
  106. #
  107. # In case you mess up this file, repair like described here http://ubuntuforums.org/showthread.php?t=2036382&p=12144840#post12144840
  108. # $ pkexec vim /etc/sudoers.d/internet-radio-recorder
  109. #
  110. END_OF_SUDOERS
  111. ### luarocks: lfs, posix
  112. # luarocks show luafilesystem 2>/dev/null || sudo luarocks install luafilesystem
  113. # luarocks show luaposix 2>/dev/null || sudo luarocks install luaposix
  114. ### streamripper:
  115. ### lighttpd (or just any web server to serve static files + simple CGIs + some convenience redirects):
  116. sudo cp "$recorder_base"/etc/lighttpd/conf-available/20-* /etc/lighttpd/conf-available
  117. sudo lighty-enable-mod accesslog auth cgi dir-listing simple-vhost per-vhost-config
  118. read -p "$echo_prefix http user (for authenticated http://$RECORDER_DOMAIN/ mp3 access): " RECORDER_HTTP_USER
  119. if [ "" = "$RECORDER_HTTP_USER" ] ; then
  120. echo "$echo_prefix no username given, exiting..."
  121. echo "$echo_prefix to rerun call"
  122. echo " $ $0 $1"
  123. exit 7
  124. fi
  125. # user/password database:
  126. sudo htdigest -c /etc/lighttpd/internet-radio-recorder.user.htdigest 'Radio Pi' "$RECORDER_HTTP_USER"
  127. echo "http://$RECORDER_DOMAIN" | sudo -u "$user" tee "$recorder_base/htdocs/app/base.url"
  128. sudo /etc/init.d/lighttpd force-reload
  129. ### at & cron jobs
  130. echo "# $git_repo\n$user" | sudo tee --append /etc/at.allow
  131. sudo -u "$user" crontab -ri
  132. sudo -u "$user" crontab - <<END_OF_CRONTAB
  133. # crontab generated by $0 from $git_repo branch $git_branch
  134. RADIO_PI_CRON_DIR=$recorder_base/htdocs/app/cron
  135. 12 05 * * * /bin/dash "\$RADIO_PI_CRON_DIR/daily.sh"
  136. 55 * * * * /bin/dash "\$RADIO_PI_CRON_DIR/hourly.sh"
  137. 10 */3 * * * /bin/dash "\$RADIO_PI_CRON_DIR/cleanup.sh"
  138. END_OF_CRONTAB
  139. sudo -u "$user" crontab -l
  140. echo "######################################################"
  141. echo "Recorder install finished. For initial radio program website scrape, call"
  142. echo " \$ sudo -u $user $recorder_base/htdocs/app/cron/daily.sh"
  143. echo "Follow progress via"
  144. echo " \$ tail -f $recorder_base/htdocs/log/*"