Advertisement
Guest User

Untitled

a guest
Apr 8th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.71 KB | None | 0 0
  1. #!/bin/bash
  2. # Sinusbot Installer by Philipp Eßwein - DAThosting.eu philipp.esswein@dathosting.eu
  3.  
  4. # Functions
  5.  
  6. function test {
  7. "$@"
  8. local status=$?
  9. if [ $status -ne 0 ]; then
  10. redMessage "Password doesn't match. Try again, change user or exit?"
  11. OPTIONS=("Try again" "Change User" "Exit")
  12. select OPTION in "${OPTIONS[@]}"; do
  13. case "$REPLY" in
  14. 1|2 ) break;;
  15. 3 ) errorQuit;;
  16. *) errorContinue;;
  17. esac
  18. done
  19.  
  20. if [ "$OPTION" == "Try again" ]; then
  21. cyanMessage "Type new password."
  22.  
  23. test passwd $SINUSBOTUSER
  24.  
  25. elif [ "$OPTION" == "Change User" ]; then
  26. cyanMessage 'Please enter the name of the sinusbot user. Typically "sinusbot". If it does not exists, the installer will create it.'
  27.  
  28. read SINUSBOTUSER
  29.  
  30. if [ "$SINUSBOTUSER" == "" ]; then
  31. errorExit "Fatal Error: No sinusbot user specified"
  32.  
  33. fi
  34.  
  35. if [ "`id $SINUSBOTUSER 2> /dev/null`" == "" ]; then
  36. if [ -d /home/$SINUSBOTUSER ]; then
  37. $GROUPADD $SINUSBOTUSER
  38. $USERADD -d /home/$SINUSBOTUSER -s /bin/bash -g $SINUSBOTUSER $SINUSBOTUSER
  39. else
  40. $GROUPADD $SINUSBOTUSER
  41. $USERADD -m -b /home -s /bin/bash -g $SINUSBOTUSER $SINUSBOTUSER
  42.  
  43. fi
  44. else
  45. greenMessage "User \"$SINUSBOTUSER\" already exists."
  46.  
  47. fi
  48. fi
  49.  
  50. fi
  51. return $status
  52. }
  53.  
  54. function greenMessage {
  55. echo -e "\\033[32;1m${@}\033[0m"
  56. }
  57.  
  58. function magentaMessage {
  59. echo -e "\\033[35;1m${@}\033[0m"
  60. }
  61.  
  62. function cyanMessage {
  63. echo -e "\\033[36;1m${@}\033[0m"
  64. }
  65.  
  66. function redMessage {
  67. echo -e "\\033[31;1m${@}\033[0m"
  68. }
  69.  
  70. function yellowMessage {
  71. echo -e "\\033[33;1m${@}\033[0m"
  72. }
  73.  
  74. function errorQuit {
  75. errorExit "Exit now!"
  76. }
  77.  
  78. function errorExit {
  79. redMessage ${@}
  80. exit 0
  81. }
  82.  
  83. function errorContinue {
  84. redMessage "Invalid option."
  85. return
  86. }
  87.  
  88. function makeDir {
  89. if [ "$1" != "" -a ! -d $1 ]; then
  90. mkdir -p $1
  91. fi
  92. }
  93.  
  94. function checkInstall {
  95. if [ "`dpkg-query -s $1 2>/dev/null`" == "" ]; then
  96. greenMessage "Installing package $1"
  97. apt-get install -y $1
  98. fi
  99. }
  100.  
  101. function compare()
  102. {
  103. echo "Fetching YT-DL MD5 sums..."
  104. curl -s ${MD5_REMOTE} -O /tmp/yt-dl.md5
  105. sed -i 2D /tmp/yt-dl.md5
  106. sed -i 2D /tmp/yt-dl.md5
  107.  
  108. md5_remote=`cat /tmp/yt-dl.md5 | awk '{ print $1 }'`
  109.  
  110. md5_local=$(md5sum ${YT_DL_PATH} | awk '{ print $1 }')
  111.  
  112.  
  113. echo ${md5_remote}
  114. echo ${md5_local}
  115.  
  116. if [ "$md5_local" != "$md5_remote" ]; then
  117. redMessage "MD5 check failed..."
  118. compare_state=1
  119. else
  120. greenMessage "MD5 check OK..."
  121. compare_state=0
  122. fi
  123.  
  124. if [ "$compare_state" = 1 ]; then
  125. get_yt_dl
  126. fi
  127.  
  128. }
  129.  
  130. function get_yt_dl()
  131. {
  132. echo "Downloading youtube-dl..."
  133. curl ${YT_DL_REMOTE} -O ${YT_DL_PATH}
  134. echo "Setting permissions..."
  135. chmod a+rx ${YT_DL_PATH}
  136. }
  137.  
  138. # Vars
  139.  
  140. USERADD=`which useradd`
  141. USERMOD=`which usermod`
  142. USERDEL=`which userdel`
  143. GROUPADD=`which groupadd`
  144. MACHINE=`uname -m`
  145. OS=`lsb_release -i 2> /dev/null | grep 'Distributor' | awk '{print tolower($3)}'`
  146. OSVERSION=`lsb_release -r 2> /dev/null | grep 'Release' | awk '{print $2}'`
  147. OSBRANCH=`lsb_release -c 2> /dev/null | grep 'Codename' | awk '{print $2}'`
  148. YT_DL_PATH="/usr/local/bin/youtube-dl"
  149. MD5_REMOTE="https://yt-dl.org/downloads/latest/MD5SUMS"
  150. YT_DL_REMOTE="https://yt-dl.org/downloads/latest/youtube-dl"
  151. ipaddress=`ip route get 8.8.8.8 | awk '{print $NF; exit}'`
  152. chengedir=`echo $PWD`
  153.  
  154. # Must be root. Checking...
  155.  
  156. if [ "`id -u`" != "0" ]; then
  157. cyanMessage "Change to root account required"
  158. su root
  159.  
  160. fi
  161.  
  162. if [ "`id -u`" != "0" ]; then
  163. errorExit "Still not root, aborting"
  164.  
  165. fi
  166.  
  167. # Start installer
  168.  
  169. if [ -f /etc/debian_version ]; then
  170. greenMessage "This is the automatic installer for latest Sinusbot."
  171. sleep 1
  172. cyanMessage "Installing following: Sinusbot, TeamSpeak3-Client and the YT-DL (includes Cronjob to be every time up to date)."
  173. sleep 1
  174. redMessage "Installer by Philipp Esswein | DAThosting.eu - Your game-/voiceserver hoster (only german)."
  175. sleep 1
  176. yellowMessage "Use YT-DL and start/stop script by Xuxe."
  177. sleep 1
  178. magentaMessage "Please rate this script on: https://forum.sinusbot.com/resources/sinusbot-installer-script.58/"
  179. sleep 1
  180.  
  181. # Private usage only!
  182.  
  183. redMessage "This Sinusbot version is only for private usage! Accept?"
  184.  
  185. OPTIONS=("Yes" "No")
  186. select OPTION in "${OPTIONS[@]}"; do
  187. case "$REPLY" in
  188. 1 ) break;;
  189. 2 ) errorQuit;;
  190. *) errorContinue;;
  191. esac
  192. done
  193.  
  194. # Update packages or not
  195.  
  196. redMessage 'Update the system packages to the latest version? Recommended, as otherwise dependencies might brake! Option "No" = Exit'
  197.  
  198. OPTIONS=("Yes" "Try without" "No")
  199. select OPTION in "${OPTIONS[@]}"; do
  200. case "$REPLY" in
  201. 1|2 ) break;;
  202. 3 ) errorQuit;;
  203. *) errorContinue;;
  204. esac
  205. done
  206.  
  207. greenMessage "Start installer now!"
  208. sleep 2
  209.  
  210. if [ "$OPTION" == "Yes" ]; then
  211. greenMessage "Updating the system in a few seconds silently (no optical output)!"
  212. sleep 1
  213. redMessage "This could take a while. Please give it up to 10 minutes!"
  214. sleep 3
  215. apt-get -qq update && apt-get -qq upgrade -y && apt-get -qq dist-upgrade -y && apt-get -qq install curl -y
  216.  
  217. elif [ "$OPTION" == "No" ]; then
  218. apt-get -qq install curl -y
  219.  
  220. fi
  221. fi
  222.  
  223. checkInstall debconf-utils
  224. checkInstall lsb-release
  225.  
  226.  
  227. # Check which OS
  228.  
  229. if [ "$OS" == "" ]; then
  230. errorExit "Error: Could not detect OS. Currently only Debian and Ubuntu are supported. Aborting!"
  231. else
  232. greenMessage "Detected OS $OS"
  233.  
  234. fi
  235.  
  236. if [ "$OSBRANCH" == "" ]; then
  237. errorExit "Error: Could not detect branch of OS. Aborting"
  238. else
  239. greenMessage "Detected branch $OSBRANCH"
  240.  
  241. fi
  242.  
  243. if [ "$MACHINE" == "x86_64" ]; then
  244. ARCH="amd64"
  245. else
  246. errorExit "$MACHINE is not supported!"
  247.  
  248. fi
  249.  
  250. # TeamSpeak3-Client latest check
  251.  
  252. greenMessage "Searching latest TS3-Client build for hardware type $MACHINE with arch $ARCH."
  253.  
  254. for VERSION in ` curl -s http://dl.4players.de/ts/releases/ | grep -Po '(?<=href=")[0-9]+(\.[0-9]+){2,3}(?=/")' | sort -Vr | head -1`; do
  255. DOWNLOAD_URL_VERSION="http://dl.4players.de/ts/releases/$VERSION/TeamSpeak3-Client-linux_$ARCH-$VERSION.run"
  256. STATUS=`curl -I $DOWNLOAD_URL_VERSION 2>&1 | grep "HTTP/" | awk '{print $2}'`
  257. if [ "$STATUS" == "200" ]; then
  258. DOWNLOAD_URL=$DOWNLOAD_URL_VERSION
  259. break
  260.  
  261. fi
  262. done
  263.  
  264. if [ "$STATUS" == "200" -a "$DOWNLOAD_URL" != "" ]; then
  265. greenMessage "Detected latest TS3-Client version as $VERSION with download URL $DOWNLOAD_URL"
  266. else
  267. errorExit "Could not detect latest TS3-Client version"
  268.  
  269. fi
  270.  
  271. # Install necessary aptitudes for sinusbot.
  272.  
  273. magentaMessage "Installing necessary packages! Please wait..."
  274.  
  275. apt-get -qq install screen nano x11vnc xvfb libxcursor1 ca-certificates bzip2 psmisc libglib2.0-0 -y
  276. update-ca-certificates
  277.  
  278. greenMessage "Packages installed!"
  279.  
  280. # Create/check user for sinusbot.
  281.  
  282. cyanMessage 'Please enter the name of the sinusbot user. Typically "sinusbot". If it does not exists, the installer will create it.'
  283.  
  284. read SINUSBOTUSER
  285. if [ "$SINUSBOTUSER" == "" ]; then
  286. errorExit "Fatal Error: No sinusbot user specified"
  287.  
  288. fi
  289.  
  290. if [ "`id $SINUSBOTUSER 2> /dev/null`" == "" ]; then
  291. if [ -d /home/$SINUSBOTUSER ]; then
  292. $GROUPADD $SINUSBOTUSER
  293. $USERADD -d /home/$SINUSBOTUSER -s /bin/bash -g $SINUSBOTUSER $SINUSBOTUSER
  294. else
  295. $GROUPADD $SINUSBOTUSER
  296. $USERADD -m -b /home -s /bin/bash -g $SINUSBOTUSER $SINUSBOTUSER
  297.  
  298. fi
  299. else
  300. greenMessage "User \"$SINUSBOTUSER\" already exists."
  301.  
  302. fi
  303.  
  304. # Setting password. Recheck if success or not.
  305.  
  306. redMessage "Setting $SINUSBOTUSER password:"
  307. test passwd $SINUSBOTUSER
  308.  
  309. # Create dirs or remove them.
  310.  
  311. ps -u $SINUSBOTUSER | grep ts3client | awk '{print $1}' | while read PID; do
  312. kill $PID
  313. done
  314. if [ -f /opt/sinusbot/ts3client_startscript.run ]; then
  315. rm -rf /opt/sinusbot/*
  316.  
  317. fi
  318.  
  319. makeDir /opt/sinusbot/teamspeak3-client
  320.  
  321. chmod 750 -R /opt/sinusbot
  322. chown -R $SINUSBOTUSER:$SINUSBOTUSER /opt/sinusbot
  323. cd /opt/sinusbot/teamspeak3-client
  324.  
  325. # Downloading TS3-Client files.
  326.  
  327. if [ ! -f ts3client_linux_amd64 ]; then
  328. greenMessage "Downloading TS3 client files."
  329. su -c "curl -O -s $DOWNLOAD_URL" $SINUSBOTUSER
  330.  
  331. fi
  332.  
  333. if [ ! -f TeamSpeak3-Client-linux_$ARCH-$VERSION.run -a ! -f ts3client_linux_$ARCH ]; then
  334. errorExit "Download failed! Exiting now!"
  335.  
  336. fi
  337.  
  338. # Installing TS3-Client.
  339.  
  340. if [ -f TeamSpeak3-Client-linux_$ARCH-$VERSION.run ]; then
  341. greenMessage "Installing the TS3 client."
  342. redMessage "Read the eula!"
  343. sleep 1
  344. yellowMessage 'Do following: Press "ENTER" then press "q" after that press "y" and accept it with another "ENTER".'
  345. sleep 2
  346.  
  347. chmod 777 ./TeamSpeak3-Client-linux_$ARCH-$VERSION.run
  348.  
  349. su -c "./TeamSpeak3-Client-linux_$ARCH-$VERSION.run" $SINUSBOTUSER
  350.  
  351. cp -R ./TeamSpeak3-Client-linux_$ARCH/* ./
  352. sleep 2
  353. rm ./ts3client_runscript.sh
  354. rm ./TeamSpeak3-Client-linux_$ARCH-$VERSION.run
  355. rm -R ./TeamSpeak3-Client-linux_$ARCH
  356.  
  357. greenMessage "TS3 client install done."
  358. else
  359. redMessage "TS3 already installed!"
  360.  
  361. fi
  362.  
  363. cd /opt/sinusbot
  364.  
  365. # Downloading latest Sinusbot.
  366.  
  367. if [ ! -f sinusbot ]; then
  368. greenMessage "Downloading latest Sinusbot."
  369. su -c "curl -O -s https://www.sinusbot.com/dl/sinusbot-beta.tar.bz2" $SINUSBOTUSER
  370.  
  371. fi
  372.  
  373. if [ ! -f sinusbot-beta.tar.bz2 -a ! -f sinusbot ]; then
  374. errorExit "Download failed! Exiting now!"
  375.  
  376. fi
  377.  
  378. # Installing latest Sinusbot.
  379.  
  380. if [ ! -f sinusbot ]; then
  381. greenMessage "Extracting Sinusbot files."
  382. su -c "tar -xjf sinusbot-beta.tar.bz2" $SINUSBOTUSER
  383. rm -f sinusbot-beta.tar.bz2
  384.  
  385. chown -R $SINUSBOTUSER:$SINUSBOTUSER /opt/sinusbot
  386.  
  387. su -c "cp plugin/libsoundbot_plugin.so /opt/sinusbot/teamspeak3-client/plugins" $SINUSBOTUSER
  388.  
  389. chmod 755 sinusbot
  390.  
  391. greenMessage "Sinusbot installation done."
  392.  
  393. else
  394. redMessage "Sinusbot already installed!"
  395.  
  396. fi
  397.  
  398. if [ $(/etc/init.d/sinusbot status | awk '{print $NF; exit}') == "UP" ]; then
  399. su -c "/etc/init.d/sinusbot stop" $SINUSBOTUSER
  400. su -c "screen -wipe" $SINUSBOTUSER
  401. update-rc.d -f sinusbot remove
  402.  
  403. fi
  404.  
  405. if [ ! -f /etc/init.d/sinusbot ]; then
  406. cd /etc/init.d
  407. curl -O -s https://raw.githubusercontent.com/Xuxe/Sinusbot-Startscript/master/sinusbot
  408. sed -i 's/USER="mybotuser"/USER="'$SINUSBOTUSER'"/g' /etc/init.d/sinusbot
  409. sed -i 's/DIR_ROOT="\/opt\/ts3soundboard\/"/DIR_ROOT="\/opt\/sinusbot\/"/g' /etc/init.d/sinusbot
  410.  
  411. chmod +x /etc/init.d/sinusbot
  412. update-rc.d sinusbot defaults
  413.  
  414. greenMessage 'Installed init.d file to start the Sinusbot with "/etc/init.d/sinusbot {start|stop|status|restart|console|update|backup}"'
  415.  
  416. else
  417. redMessage "/etc/init.d/sinusbot already exists!"
  418.  
  419. fi
  420.  
  421. if [ ! -f /opt/sinusbot/config.ini ]; then
  422. echo 'ListenPort = 8087
  423. ListenHost = "0.0.0.0"
  424. TS3Path = "/opt/sinusbot/teamspeak3-client/ts3client_linux_amd64"
  425. YoutubeDLPath = "/usr/local/bin/youtube-dl"'>>/opt/sinusbot/config.ini
  426. greenMessage "Config.ini created successfully."
  427. else
  428. redMessage "Config.ini already exists or creation error!"
  429.  
  430. fi
  431.  
  432. if [ "$(grep -c 'youtube' /etc/crontab)" -ge 1 ]; then
  433. sed -i '/\0 \0 \* \* \* youtube-dl -U >\/dev\/null 2>&1/d' /etc/crontab
  434. redMessage "Removed old YT-DL cronjob."
  435.  
  436. fi
  437.  
  438. if [ "$(grep -c 'sinusbot' /etc/crontab)" -ge 1 ]; then
  439. sed -i '/\0 \0 \* \* \* su "$SINUSBOTUSER" sinusbot -update >\/dev\/null 2>&1/d' /etc/crontab
  440. redMessage "Removed old Sinusbot cronjob."
  441.  
  442. fi
  443.  
  444. if [ "$(grep -c 'youtube' /etc/cron.d/sinusbot)" -ge 1 ]; then
  445. redMessage "Cronjob already set for YT-DL updater!"
  446. else
  447. greenMessage "Inject Cronjob for automatic YT-DL update..."
  448. echo "0 0 * * * youtube-dl -U >/dev/null 2>&1">>/etc/cron.d/sinusbot
  449. greenMessage "Inject successful."
  450.  
  451. fi
  452.  
  453. if [ "$(grep -c 'sinusbot' /etc/cron.d/sinusbot)" -ge 1 ]; then
  454. redMessage "Cronjob already set for Sinusbot updater!"
  455. else
  456. greenMessage "Inject Cronjob for automatic Sinusbot update..."
  457. echo "0 0 * * * su $SINUSBOTUSER sinusbot -update >/dev/null 2>&1">>/etc/cron.d/sinusbot
  458. greenMessage "Inject successful."
  459.  
  460. fi
  461. # Installing YT-DL.
  462.  
  463. greenMessage "Installing YT-Downloader now!"
  464. yellowMessage "Script by Xuxe."
  465.  
  466. if [ -a "$YT_DL_PATH" ]; then
  467. compare
  468. else
  469. get_yt_dl
  470.  
  471. fi
  472.  
  473. # Creating Readme
  474.  
  475. if [ -a "/opt/sinusbot/README.txt" ]; then
  476. rm /opt/sinusbot/README.txt
  477.  
  478. fi
  479.  
  480. if [ ! -a "/opt/sinusbot/README_installer.txt" ]; then
  481. echo '##################################################################################
  482. # #
  483. # Usage: /etc/init.d/sinusbot {start|stop|status|restart|console|update|backup} #
  484. # - start: start the bot #
  485. # - stop: stop the bot #
  486. # - status: display the status of the bot (down or up) #
  487. # - restart: restart the bot #
  488. # - console: display the bot console #
  489. # - update: runs the bot updater (with start & stop)
  490. # - backup: archives your bot root directory
  491. # To exit the console without stopping the server, press CTRL + A then D. #
  492. # #
  493. ##################################################################################'>>/opt/sinusbot/README_installer.txt
  494. fi
  495.  
  496. chown -R $SINUSBOTUSER:$SINUSBOTUSER /opt/sinusbot
  497.  
  498. # Starting Sinusbot
  499.  
  500. greenMessage 'Starting the Sinusbot.'
  501. /etc/init.d/sinusbot start
  502. yellowMessage "Please wait..."
  503. sleep 15
  504.  
  505. # If startup failed, the script will start normal sinusbot without screen for looking about errors. If startup successed => installation done.
  506.  
  507. if [ $(/etc/init.d/sinusbot status | awk '{print $NF; exit}') == "DOWN" ]; then
  508. redMessage "Sinusbot could not start! Starting it without screen. Look for errors! Stop it again in 10 seconds."
  509. su -c "/opt/sinusbot/sinusbot" $SINUSBOTUSER
  510. sleep 10
  511. killall sinusbot
  512.  
  513. else
  514. greenMessage "Install done!"
  515. yellowMessage 'Generated a README_installer.txt in /opt/sinusbot with all commands for the sinusbot...'
  516. greenMessage 'All right. Everything is installed successfully. Sinusbot is UP on "'$ipaddress':8087" | Username: "admin" | Password: "foobar" :)'
  517. redMessage 'Stop it with "/etc/init.d/sinusbot stop".'
  518. cyanMessage 'Also you can enter "/etc/init.d/sinusbot help" for help ;)'
  519. magentaMessage "Don't forget to rate this script on: https://forum.sinusbot.com/resources/sinusbot-installer-script.58/"
  520. greenMessage "Thank you for using this script! :)"
  521.  
  522. fi
  523.  
  524. # remove installer
  525.  
  526. cd $changedir
  527. rm $0
  528.  
  529. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement