Advertisement
Guest User

Untitled

a guest
May 11th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.11 KB | None | 0 0
  1. #!/bin/bash -e
  2. #-e Causes bash script to exit if any of the installers
  3. #return with a non-zero return value.
  4.  
  5. if [[ $EUID -ne 0 ]]; then
  6. echo "Please run as root user."
  7. exit 1
  8. fi
  9.  
  10. SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )"
  11. AIRTIMEROOT=${SCRIPT_DIR}
  12.  
  13. showhelp () {
  14. echo "Usage: sudo bash install [options]
  15. -h, --help, -?
  16. Display usage information
  17. -V, --version
  18. Display version information
  19. -v, --verbose
  20. More output
  21. -q, --quiet, --silent
  22. No output except errors
  23. -f, --force
  24. Turn off interactive prompts
  25. --distribution=DISTRIBUTION
  26. Linux distribution the installation is being run on
  27. --release=RELEASE
  28. Distribution release
  29. -d, --ignore-dependencies
  30. Don't install binary dependencies
  31. -w, --web-user=WEB_USER
  32. Set the apache web user. Defaults to www-data. Only change
  33. this setting if you've changed the default apache web user
  34. -r, --web-root=WEB_ROOT
  35. Set the web root for Airtime files
  36. This will copy the Airtime application files, but you will need
  37. to give your web user access to the given directory if it is
  38. not accessible
  39. -I, --in-place
  40. Set the current Airtime directory as the web root
  41. Note that you will need to give your web user permission to
  42. access this directory if it is not accessible
  43. -p, --postgres
  44. Create a default postgres user named 'airtime' with password
  45. 'airtime'
  46. -a, --apache
  47. Install apache and deploy a basic configuration for Airtime
  48. -i, --icecast
  49. Install Icecast 2 and deploy a basic configuration for Airtime"
  50. exit 0
  51. }
  52.  
  53. showversion () {
  54. version=$(php -r 'require_once(__DIR__ . "/airtime_mvc/application/configs/constants.php"); echo AIRTIME_CODE_VERSION;')
  55. echo "Airtime Version ${version}"
  56. exit 0
  57. }
  58.  
  59. web_user="www-data"
  60. web_root=""
  61. in_place="f"
  62. postgres="f"
  63. apache="f"
  64. icecast="f"
  65. ignore_dependencies="f"
  66. # Interactive
  67. _i=1
  68. # Verbose
  69. _v=0
  70. # Quiet
  71. _q=0
  72. upgrade="f"
  73. dist=""
  74. code=""
  75.  
  76. function verbose() {
  77. if [[ ${_v} -eq 1 ]]; then
  78. echo -e "$@"
  79. fi
  80. }
  81.  
  82. function loud() {
  83. if [[ ${_q} -eq 0 ]]; then
  84. echo -e "$@"
  85. fi
  86. }
  87.  
  88. # Evaluate commands silently if quiet
  89. function loudCmd() {
  90. if [[ ${_q} -eq 0 ]]; then
  91. eval $@
  92. else
  93. eval $@ > /dev/null
  94. fi
  95. }
  96.  
  97. function checkCommandExists() {
  98. set +e
  99. command=$@
  100. eval hash ${command} 2>/dev/null
  101. commandFound=$?
  102. if [[ ! ${commandFound} -eq 0 ]]; then
  103. echo -e "Error: ${command} not found. Please ensure you have the corresponding dependency installed."
  104. exit
  105. fi
  106. set -e
  107. }
  108.  
  109. while :; do
  110. case "$1" in
  111. --help)
  112. showhelp
  113. ;;
  114. --version)
  115. showversion
  116. ;;
  117. --verbose)
  118. _v=1
  119. ;;
  120. --quiet|--silent)
  121. _q=1
  122. ;;
  123. --force)
  124. _i=0
  125. ;;
  126. --distribution)
  127. if [ "$2" ]; then
  128. dist=$2
  129. shift 2
  130. continue
  131. else
  132. echo 'ERROR: Must specify a non-empty "--distribution DISTRIBUTION" argument.' >&2
  133. exit 1
  134. fi
  135. ;;
  136. --distribution=?*)
  137. dist=${1#*=} # Delete everything up to "=" and assign the remainder.
  138. ;;
  139. --distribution=)
  140. echo 'ERROR: Must specify a non-empty "--distribution DISTRIBUTION" argument.' >&2
  141. exit 1
  142. ;;
  143. --release)
  144. if [ "$2" ]; then
  145. code=$2
  146. shift 2
  147. continue
  148. else
  149. echo 'ERROR: Must specify a non-empty "--release RELEASE" argument.' >&2
  150. exit 1
  151. fi
  152. ;;
  153. --release=?*)
  154. code=${1#*=} # Delete everything up to "=" and assign the remainder.
  155. ;;
  156. --release=)
  157. echo 'ERROR: Must specify a non-empty "--release RELEASE" argument.' >&2
  158. exit 1
  159. ;;
  160. --ignore-dependencies)
  161. ignore_dependencies="t"
  162. ;;
  163. --apache)
  164. apache="t"
  165. ;;
  166. --icecast)
  167. icecast="t"
  168. ;;
  169. --postgres)
  170. postgres="t"
  171. ;;
  172. --in-place)
  173. in_place="t"
  174. ;;
  175. --web-user)
  176. if [ "$2" ]; then
  177. web_user=$2
  178. shift 2
  179. continue
  180. else
  181. echo 'ERROR: Must specify a non-empty "--web-user WEB_USER" argument.' >&2
  182. exit 1
  183. fi
  184. ;;
  185. --web-user=?*)
  186. web_user=${1#*=} # Delete everything up to "=" and assign the remainder.
  187. ;;
  188. --web-user=)
  189. echo 'ERROR: Must specify a non-empty "--web-user=WEB_USER" argument.' >&2
  190. exit 1
  191. ;;
  192. --web-root)
  193. if [ "$2" ]; then
  194. web_root=$(readlink -f $2)
  195. shift 2
  196. continue
  197. else
  198. echo 'ERROR: Must specify a non-empty "--web-root WEB_ROOT" argument.' >&2
  199. exit 1
  200. fi
  201. ;;
  202. --web-root=?*)
  203. web_root=${1#*=} # Delete everything up to "=" and assign the remainder.
  204. ;;
  205. --web-root=)
  206. echo 'ERROR: Must specify a non-empty "--web-root=WEB_ROOT" argument.' >&2
  207. exit 1
  208. ;;
  209. --)
  210. shift
  211. break
  212. ;;
  213. -?*)
  214. for ((i = 1; i < ${#1}; i++)); do
  215. case "${1:$i:1}" in
  216. h|\?)
  217. showhelp
  218. ;;
  219. V)
  220. showversion
  221. ;;
  222. v)
  223. _v=1
  224. ;;
  225. q)
  226. _q=1
  227. ;;
  228. f)
  229. _i=0
  230. ;;
  231. d)
  232. ignore_dependencies="t"
  233. ;;
  234. a)
  235. apache="t"
  236. ;;
  237. i)
  238. icecast="t"
  239. ;;
  240. p)
  241. postgres="t"
  242. ;;
  243. I)
  244. in_place="t"
  245. ;;
  246. w)
  247. if [ "$2" ]; then
  248. web_user=$2
  249. continue
  250. else
  251. echo 'ERROR: Must specify a non-empty "-w WEB_USER" argument.' >&2
  252. exit 1
  253. fi
  254. ;;
  255. r)
  256. if [ "$2" ]; then
  257. web_root=$(readlink -f $2)
  258. continue
  259. else
  260. echo 'ERROR: Must specify a non-empty "-d WEB_ROOT" argument.' >&2
  261. exit 1
  262. fi
  263. ;;
  264. *)
  265. echo "$0: error - unrecognized option '${1:$i:1}'" >&2;
  266. echo "Try 'install --help' for more information."
  267. exit 1
  268. esac
  269. done
  270. ;;
  271. *)
  272. break
  273. esac
  274. shift
  275. done
  276.  
  277. if [ -z web_root -a ! -d web_root ]; then
  278. echo "$web_root doesn't exist!"
  279. exit 1
  280. fi
  281.  
  282. echo -e "\n _____ .________________________.___ _____ ___________ "
  283. echo " / _ \ | \______ \__ ___/| | / \ \_ _____/ "
  284. echo " / /_\ \| || _/ | | | |/ \ / \ | __)_ "
  285. echo "/ | \ || | \ | | | / Y \| \ "
  286. echo "\____|__ /___||____|_ / |____| |___\____|__ /_______ / "
  287. echo -e " \/ \/ \/ \/ \n"
  288.  
  289. if [ "$ignore_dependencies" = "f" ]; then
  290. set +e
  291. loudCmd "apt-get update"
  292. if [ -z "${dist}" ]; then
  293. loudCmd "apt-get -y --force-yes install lsb-release"
  294. dist=`lsb_release -ds | awk '{print tolower($1);}'`
  295. code=`lsb_release -cs`
  296. fi
  297.  
  298. loud "\n-----------------------------------------------------"
  299. loud " * Installing External Dependencies * "
  300. loud "-----------------------------------------------------"
  301.  
  302. verbose "\n * Reading requirements-${dist,,}-${code,,}.apt..."
  303. if [ -f ${SCRIPT_DIR}/installer/lib/requirements-${dist,,}-${code,,}.apt ]; then
  304. loudCmd "DEBIAN_FRONTEND=noninteractive apt-get -y -m --force-yes install $(grep -vE '^\s*#' ${SCRIPT_DIR}/installer/lib/requirements-${dist,,}-${code,,}.apt | tr '\n' ' ')"
  305. else
  306. loudCmd "DEBIAN_FRONTEND=noninteractive apt-get -y -m --force-yes install $(grep -vE '^\s*#' ${SCRIPT_DIR}/installer/lib/requirements-ubuntu-trusty.apt | tr '\n' ' ')"
  307. fi
  308. set -e
  309. else
  310. checkCommandExists "apache2"
  311. checkCommandExists "rabbitmqctl"
  312. checkCommandExists "psql"
  313. fi
  314.  
  315. if [ -f /etc/airtime/airtime.conf ]; then
  316. OLD_CONF=$(grep "[media-monitor]" /etc/airtime/airtime.conf)
  317.  
  318. if [ -n "${OLD_CONF}" ]; then
  319. upgrade="t"
  320.  
  321. set +e
  322. verbose "Stopping airtime services..."
  323. loudCmd "service airtime-playout stop-with-monit"
  324. loudCmd "service airtime-media-monitor stop-with-monit"
  325. loudCmd "service airtime-liquidsoap stop-with-monit"
  326. verbose "...Done"
  327.  
  328. echo "Looks like you have an old version of Airtime. Your current /etc/airtime/airtime.conf \
  329. will be moved to /etc/airtime/airtime.conf.tmp"
  330. # If we don't remove the existing python files in /usr/lib and the
  331. # /etc/init.d startup scripts, services won't work properly
  332. if [ -d /usr/lib/airtime/ ]; then
  333. rm -rf /usr/lib/airtime/
  334. fi
  335.  
  336. rm /etc/init.d/airtime-*
  337.  
  338. if [ "$apache" = "t" ]; then
  339. # If the user selects an "in-place" install or passes in a web root,
  340. # we need to replace the old apache airtime.conf
  341. rm /etc/apache2/sites-available/airtime.conf
  342. fi
  343.  
  344. if [ -d /usr/share/airtime -a web_root = /usr/share/airtime ]; then
  345. rm -rf /usr/share/airtime
  346. fi
  347.  
  348. mv /etc/airtime/airtime.conf /etc/airtime/airtime.conf.tmp
  349. set -e
  350. fi
  351. fi
  352.  
  353. if [ "$apache" = "f" -a ${_i} -eq 1 ]; then
  354. echo -e "Install default Airtime apache configuration? (Y/n): \c"
  355. read IN
  356. if [ "$IN" = "y" -o "$IN" = "Y" ]; then
  357. apache="t"
  358. fi
  359. fi
  360.  
  361. if [ "$in_place" = "t" ]; then
  362. verbose "\n * Setting current Airtime directory as web root..."
  363. web_root=${AIRTIMEROOT}/airtime_mvc/public
  364. elif [ -n "$web_root" ]; then
  365. verbose "\n * Creating Apache web root directory..."
  366. cp -R ${AIRTIMEROOT}/airtime_mvc/* ${web_root}
  367. web_root=${web_root}/public/
  368. else
  369. verbose "\n * Creating default Apache web root directory /usr/share/airtime/..."
  370. web_root="/usr/share/airtime"
  371. mkdir -p ${web_root}
  372. cp -R ${AIRTIMEROOT}/airtime_mvc/* ${web_root}
  373. web_root=${web_root}/public/
  374. fi
  375. verbose "...Done"
  376.  
  377. if [ "$apache" = "t" ]; then
  378. loud "\n-----------------------------------------------------"
  379. loud " * Configuring Apache * "
  380. loud "-----------------------------------------------------"
  381.  
  382. set +e
  383. apache2 -v | grep "2\.4" > /dev/null
  384. apacheversion=$?
  385. set -e
  386.  
  387. if [ "$apacheversion" != "1" ]; then
  388. airtimeconfigfile="airtime.conf"
  389. oldconfigfile="airtime-vhost.conf"
  390. else
  391. airtimeconfigfile="airtime"
  392. oldconfigfile="airtime-vhost"
  393. fi
  394.  
  395. # If we're upgrading (installing over an existing Airtime install) and we've been told to
  396. # install apache, we should overwrite any existing configuration. If we don't do this, doing
  397. # an in-place installation over an old Airtime install (which installs to /usr/share by default)
  398. # will fail
  399. if [ "$upgrade" = "t" -o ! -f /etc/apache2/sites-available/${airtimeconfigfile} ]; then
  400. verbose "\n * Creating Apache config for Airtime..."
  401.  
  402. if [ "$apacheversion" != "1" ]; then
  403. sed -e "s@WEB_ROOT@${web_root}@g" ${SCRIPT_DIR}/installer/apache/airtime-vhost-2.4 > /etc/apache2/sites-available/${airtimeconfigfile}
  404. else
  405. sed -e "s@WEB_ROOT@${web_root}@g" ${SCRIPT_DIR}/installer/apache/airtime-vhost > /etc/apache2/sites-available/${airtimeconfigfile}
  406. fi
  407. loudCmd "a2dissite 000-default"
  408. # If Airtime was previously installed with apt, the vhost file name is different,
  409. # so we need to specifically disable it.
  410. if [ -f "/etc/apache2/sites-available/${oldconfigfile}" ]; then
  411. loudCmd "a2dissite airtime-vhost"
  412. fi
  413. loudCmd "a2ensite airtime"
  414. else
  415. verbose "\nApache config for Airtime already exists, skipping"
  416. fi
  417. fi
  418.  
  419. if [ "$icecast" = "f" -a ${_i} -eq 1 ]; then
  420. echo -e "Install default Airtime Icecast configuration? (Y/n): \c"
  421. read IN
  422. if [ "$IN" = "y" -o "$IN" = "Y" ]; then
  423. icecast="t"
  424. fi
  425. fi
  426.  
  427. if [ "$icecast" = "t" ]; then
  428. loud "\n-----------------------------------------------------"
  429. loud " * Configuring Icecast * "
  430. loud "-----------------------------------------------------"
  431.  
  432. verbose "\n * Enabling Icecast 2..."
  433. sed -i 's/ENABLE=false/ENABLE=true/g' /etc/default/icecast2
  434. set +e
  435. loudCmd "service icecast2 start"
  436. set -e
  437. verbose "...Done"
  438. fi
  439.  
  440. loud "\n-----------------------------------------------------"
  441. loud " * Installing Airtime Services * "
  442. loud "-----------------------------------------------------"
  443.  
  444. verbose "\n * Installing necessary python services..."
  445. loudCmd "pip install setuptools"
  446. verbose "...Done"
  447.  
  448. verbose "\n * Creating /run/airtime..."
  449. mkdir -p /run/airtime
  450. chmod 755 /run/airtime
  451. chown -R ${web_user}:${web_user} /run/airtime
  452. verbose "...Done"
  453.  
  454. verbose "\n * Installing log writer..."
  455. loudCmd "python ${AIRTIMEROOT}/python_apps/std_err_override/setup.py install --install-scripts=/usr/bin"
  456. verbose "...Done"
  457.  
  458. verbose "\n * Installing API client..."
  459. loudCmd "python ${AIRTIMEROOT}/python_apps/api_clients/setup.py install --install-scripts=/usr/bin"
  460. verbose "...Done"
  461.  
  462. verbose "\n * Installing media-monitor..."
  463. loudCmd "python ${AIRTIMEROOT}/python_apps/media-monitor/setup.py install --install-scripts=/usr/bin"
  464. verbose "...Done"
  465.  
  466. verbose "\n * Installing pypo..."
  467. loudCmd "python ${AIRTIMEROOT}/python_apps/pypo/setup.py install --install-scripts=/usr/bin"
  468. verbose "...Done"
  469.  
  470. for i in /etc/init/airtime*.template; do
  471. chmod 644 $i
  472. sed -i "s/WEB_USER/${web_user}/g" $i
  473. mv $i ${i%.template}
  474. done
  475.  
  476. set +e
  477. loudCmd "initctl reload-configuration"
  478. loudCmd "systemctl daemon-reload" #systemd hipsters
  479. loudCmd "update-rc.d airtime-playout defaults" # Start at bootup, on Debian
  480. loudCmd "update-rc.d airtime-media-monitor defaults" # Start at bootup, on Debian
  481. loudCmd "update-rc.d airtime-liquidsoap defaults" # Start at bootup, on Debian
  482. set -e
  483.  
  484. if [ ! -d /var/log/airtime ]; then
  485. loud "\n-----------------------------------------------------"
  486. loud " * Installing Log Files * "
  487. loud "-----------------------------------------------------"
  488.  
  489. verbose "\n * Creating /var/tmp/airtime..."
  490. mkdir -p /var/tmp/airtime/show-recorder/
  491.  
  492. verbose "\n * Copying logrotate files..."
  493. cp ${AIRTIMEROOT}/airtime_mvc/build/airtime-php.logrotate /etc/logrotate.d/airtime-php
  494. cp ${AIRTIMEROOT}/python_apps/pypo/pypo/liquidsoap_scripts/airtime-liquidsoap.logrotate /etc/logrotate.d/airtime-liquidsoap
  495. fi
  496.  
  497. verbose "\n * Setting permissions on /var/log/airtime..."
  498. chmod -R a+x /var/log/airtime
  499. chown -R ${web_user}:${web_user} /var/log/airtime/
  500.  
  501. verbose "\n * Setting permissions on /var/tmp/airtime..."
  502. chmod -R a+x /var/tmp/airtime
  503. chown -R ${web_user}:${web_user} /var/tmp/airtime/
  504.  
  505. # PHP Config File for Apache
  506. if [ ! -f "/etc/php5/apache2/conf.d/airtime.ini" ]; then
  507. verbose "\n * Creating Airtime PHP config for Apache..."
  508. cp ${SCRIPT_DIR}/installer/php/airtime.ini /etc/php5/apache2/conf.d/airtime.ini
  509. else
  510. verbose "\nAirtime PHP config for Apache already exists, skipping"
  511. fi
  512.  
  513. # Enable Apache modules
  514. loudCmd "a2enmod rewrite php5"
  515.  
  516. loud "\n-----------------------------------------------------"
  517. loud " * Configuring PostgreSQL * "
  518. loud "-----------------------------------------------------"
  519.  
  520. # Ensure postgres is running - It isn't after you install the postgres package on Ubuntu 15.04
  521. loudCmd "service postgresql start"
  522.  
  523. setupAirtimePostgresUser() {
  524. # here-doc to execute this block as postgres user
  525. su postgres <<'EOF'
  526. set +e
  527. psql -d postgres -tAc "CREATE USER airtime WITH ENCRYPTED PASSWORD 'airtime'; ALTER USER airtime CREATEDB;"
  528. set -e
  529. # don't indent this!
  530. EOF
  531. }
  532.  
  533. if [ "$postgres" = "t" ]; then
  534. setupAirtimePostgresUser
  535. elif [ ${_i} -eq 1 ]; then
  536. echo -e "Create default airtime postgres user? (Y/n): \c"
  537. read IN
  538. if [ "$IN" = "y" -o "$IN" = "Y" ]; then
  539. setupAirtimePostgresUser
  540. fi
  541. fi
  542.  
  543. loud "\n-----------------------------------------------------"
  544. loud " * Configuring RabbitMQ * "
  545. loud "-----------------------------------------------------"
  546.  
  547. RABBITMQ_VHOST=/airtime
  548. RABBITMQ_USER=airtime
  549. RABBITMQ_PASSWORD=airtime
  550. EXCHANGES="airtime-pypo|pypo-fetch|airtime-media-monitor|media-monitor"
  551.  
  552. # Ignore errors in this check to avoid dying when vhost isn't found
  553. set +e
  554. rabbitmq-server& sleep 1 && rabbitmqctl list_vhosts | grep -w ${RABBITMQ_VHOST} > /dev/null
  555. RESULT="$?"
  556. set -e
  557.  
  558. # Only run these if the vhost doesn't exist
  559. if [ "$RESULT" != "0" ]; then
  560. verbose "\n * Creating RabbitMQ user ${RABBITMQ_USER}..."
  561.  
  562. rabbitmqctl add_vhost ${RABBITMQ_VHOST}
  563. rabbitmqctl add_user ${RABBITMQ_USER} ${RABBITMQ_PASSWORD}
  564. else
  565. verbose "\nRabbitMQ user already exists, skipping creation"
  566. fi
  567.  
  568. verbose "\n * Setting RabbitMQ user permissions..."
  569. loudCmd "rabbitmqctl set_permissions -p ${RABBITMQ_VHOST} ${RABBITMQ_USER} \"$EXCHANGES\" \"$EXCHANGES\" \"$EXCHANGES\""
  570.  
  571. if [ ! -d "/etc/airtime" ]; then
  572. loud "\n-----------------------------------------------------"
  573. loud " * Installing Airtime * "
  574. loud "-----------------------------------------------------"
  575.  
  576. verbose "\n * Creating /etc/airtime/ directory..."
  577. mkdir /etc/airtime
  578. fi
  579. chown -R ${web_user}:${web_user} /etc/airtime
  580.  
  581. if [ ! -d "/srv/airtime" ]; then
  582. mkdir -p /srv/airtime
  583. fi
  584. chown -R ${web_user}:${web_user} /srv/airtime
  585.  
  586.  
  587. # We only generate the locales for Airtime if you're allowing us
  588. # to install our dependencies, so that we won't automatically do this
  589. # when this install script runs from our DEB package.
  590. if [ "$ignore_dependencies" = "f" ]; then
  591. loud "\n-----------------------------------------------------"
  592. loud " * Installing Locales * "
  593. loud "-----------------------------------------------------"
  594.  
  595. set +e
  596. verbose "\n * Generating locales"
  597. for i in `ls ${web_root}/../locale | grep ".._.."`; do
  598. if [ "$dist" = "debian" ]; then
  599. grep -qi "^$i" /etc/locale.gen
  600. if [ $? -ne 0 ]; then
  601. verbose "$i.UTF-8 UTF-8" >> /etc/locale.gen
  602. fi
  603. else
  604. loudCmd "locale-gen \"$i.utf8\""
  605. fi
  606. done
  607. set -e
  608.  
  609. if [ "$dist" = "debian" ]; then
  610. loudCmd "/usr/sbin/locale-gen"
  611. fi
  612. fi
  613.  
  614. verbose "\n * Reloading apache..."
  615. loudCmd "service apache2 reload 2>/dev/null"
  616.  
  617. IP=$(ifconfig eth0 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://')
  618.  
  619. echo -e "\n-----------------------------------------------------"
  620. echo " * Basic Setup DONE! * "
  621. echo " "
  622. echo " To get started with Airtime, visit ${IP} "
  623. echo " or, if you've set up your own web configuration, "
  624. echo " the Airtime webroot on your webserver "
  625. echo "-----------------------------------------------------"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement