freedombone-app-peertube 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # PeerTube server
  12. #
  13. # There's a problem with installing this onto mesh images, which is
  14. # that qemu appears to run out of RAM when using yarn to add webpack.
  15. #
  16. # License
  17. # =======
  18. #
  19. # Copyright (C) 2017 Bob Mottram <bob@freedombone.net>
  20. #
  21. # This program is free software: you can redistribute it and/or modify
  22. # it under the terms of the GNU Affero General Public License as published by
  23. # the Free Software Foundation, either version 3 of the License, or
  24. # (at your option) any later version.
  25. #
  26. # This program is distributed in the hope that it will be useful,
  27. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. # GNU Affero General Public License for more details.
  30. #
  31. # You should have received a copy of the GNU Affero General Public License
  32. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  33. VARIANTS="full full-vim media"
  34. IN_DEFAULT_INSTALL=0
  35. SHOW_ON_ABOUT=1
  36. PEERTUBE_DOMAIN_NAME=
  37. PEERTUBE_CODE=
  38. PEERTUBE_REPO="https://github.com/Chocobozzz/PeerTube"
  39. PEERTUBE_COMMIT='fef2c7164e025b12a64185dbab058ef4129733c6'
  40. PEERTUBE_ONION_PORT=8136
  41. PEERTUBE_PORT=9004
  42. MESH_PEERTUBE_PORT=8500
  43. PEERTUBE_DIR=/etc/peertube
  44. peertube_variables=(PEERTUBE_DOMAIN_NAME
  45. PEERTUBE_CODE
  46. PEERTUBE_ADMIN_PASSWORD
  47. ONION_ONLY
  48. DDNS_PROVIDER
  49. MY_USERNAME
  50. MY_EMAIL_ADDRESS)
  51. function peertube_create_database {
  52. if [ -f $IMAGE_PASSWORD_FILE ]; then
  53. PEERTUBE_ADMIN_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  54. else
  55. if [ ! $PEERTUBE_ADMIN_PASSWORD ]; then
  56. PEERTUBE_ADMIN_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  57. fi
  58. fi
  59. if [ ! $PEERTUBE_ADMIN_PASSWORD ]; then
  60. return
  61. fi
  62. systemctl restart postgresql
  63. run_system_query_postgresql "CREATE USER peertube WITH PASSWORD '$PEERTUBE_ADMIN_PASSWORD';"
  64. run_system_query_postgresql "CREATE DATABASE peertube OWNER peertube;"
  65. run_system_query_postgresql "GRANT ALL PRIVILEGES ON DATABASE peertube to peertube;"
  66. run_system_query_postgresql "set statement_timeout to 40000;"
  67. }
  68. function logging_on_peertube {
  69. echo -n ''
  70. }
  71. function logging_off_peertube {
  72. echo -n ''
  73. }
  74. function remove_user_peertube {
  75. remove_username="$1"
  76. }
  77. function add_user_peertube {
  78. if [[ $(app_is_installed peertube) == "0" ]]; then
  79. echo '0'
  80. return
  81. fi
  82. new_username="$1"
  83. new_user_password="$2"
  84. echo '0'
  85. }
  86. function install_interactive_peertube {
  87. if [ ! $ONION_ONLY ]; then
  88. ONION_ONLY='no'
  89. fi
  90. if [[ $ONION_ONLY != "no" ]]; then
  91. PEERTUBE_DOMAIN_NAME='peertube.local'
  92. write_config_param "PEERTUBE_DOMAIN_NAME" "$PEERTUBE_DOMAIN_NAME"
  93. else
  94. function_check interactive_site_details
  95. interactive_site_details "peertube" "PEERTUBE_DOMAIN_NAME" "PEERTUBE_CODE"
  96. fi
  97. APP_INSTALLED=1
  98. }
  99. function peertube_set_admin_email {
  100. read_config_param $MY_EMAIL_ADDRESS
  101. data=$(tempfile 2>/dev/null)
  102. trap "rm -f $data" 0 1 2 5 15
  103. dialog --title $"Set PeerTube administrator email address" \
  104. --backtitle $"Freedombone Control Panel" \
  105. --inputbox $"Admin email address" 8 75 "$MY_EMAIL_ADDRESS" 2>$data
  106. sel=$?
  107. case $sel in
  108. 0) peertube_email=$(<$data)
  109. if [[ "$peertube_email" != *' '* && "$peertube_email" != *','* && "$peertube_email" != *';'* && "$peertube_email" == *'@'* && "$peertube_email" == *'.'* ]]; then
  110. if [ ${#peertube_email} -gt 8 ]; then
  111. sed -i "s|email:.*|email: '${peertube_email}'|g" $PEERTUBE_DIR/config/production.yaml
  112. systemctl restart peertube
  113. dialog --title $"Set PeerTube administrator email address" \
  114. --msgbox $"Set to $peertube_email" 6 75
  115. fi
  116. fi
  117. ;;
  118. esac
  119. rm $data
  120. }
  121. function peertube_disable_signups {
  122. dialog --title $"Disable PeerTube signups" \
  123. --backtitle $"Freedombone Control Panel" \
  124. --yesno $"\nDo you wish to disable further PeerTube signups?" 8 75
  125. sel=$?
  126. case $sel in
  127. 0) sed "0,/RE/s/enabled:.*/enabled: false/" $PEERTUBE_DIR/config/production.yaml;;
  128. 1) sed "0,/RE/s/enabled:.*/enabled: true/" $PEERTUBE_DIR/config/production.yaml;;
  129. 255) return;;
  130. esac
  131. systemctl restart peertube
  132. }
  133. function configure_interactive_peertube {
  134. while true
  135. do
  136. data=$(tempfile 2>/dev/null)
  137. trap "rm -f $data" 0 1 2 5 15
  138. dialog --backtitle $"Freedombone Control Panel" \
  139. --title $"PeerTube" \
  140. --radiolist $"Choose an operation:" 10 70 4 \
  141. 1 $"Set administrator email address" off \
  142. 2 $"Disable or enable signups" off \
  143. 3 $"Exit" on 2> $data
  144. sel=$?
  145. case $sel in
  146. 1) break;;
  147. 255) break;;
  148. esac
  149. case $(cat $data) in
  150. 1) peertube_set_admin_email;;
  151. 2) peertube_disable_signups;;
  152. 3) break;;
  153. esac
  154. done
  155. }
  156. function change_password_peertube {
  157. PEERTUBE_USERNAME="$1"
  158. PEERTUBE_PASSWORD="$2"
  159. if [ ${#PEERTUBE_PASSWORD} -lt 8 ]; then
  160. echo $'Peertube password is too short'
  161. return
  162. fi
  163. #${PROJECT_NAME}-pass -u $PEERTUBE_USERNAME -a peertube -p "$PEERTUBE_PASSWORD"
  164. }
  165. function reconfigure_peertube {
  166. echo -n ''
  167. }
  168. function upgrade_peertube {
  169. CURR_PEERTUBE_COMMIT=$(get_completion_param "peertube commit")
  170. if [[ "$CURR_PEERTUBE_COMMIT" == "$PEERTUBE_COMMIT" ]]; then
  171. return
  172. fi
  173. read_config_param PEERTUBE_DOMAIN_NAME
  174. systemctl stop peertube
  175. cd $PEERTUBE_DIR
  176. function_check set_repo_commit
  177. set_repo_commit $PEERTUBE_DIR "peertube commit" "$PEERTUBE_COMMIT" $PEERTUBE_REPO
  178. npm run upgrade-peertube
  179. chown -R peertube:peertube $PEERTUBE_DIR
  180. systemctl start peertube
  181. }
  182. function backup_local_peertube {
  183. PEERTUBE_DOMAIN_NAME='peertube.local'
  184. if grep -q "peertube domain" $COMPLETION_FILE; then
  185. PEERTUBE_DOMAIN_NAME=$(get_completion_param "peertube domain")
  186. fi
  187. systemctl stop peertube
  188. USE_POSTGRESQL=1
  189. function_check backup_database_to_usb
  190. backup_database_to_usb peertube
  191. systemctl start peertube
  192. peertube_path=$PEERTUBE_DIR/videos
  193. if [ -d $peertube_path ]; then
  194. suspend_site ${PEERTUBE_DOMAIN_NAME}
  195. systemctl stop peertube
  196. backup_directory_to_usb $peertube_path peertubevideos
  197. systemctl start peertube
  198. restart_site
  199. fi
  200. }
  201. function restore_local_peertube {
  202. PEERTUBE_DOMAIN_NAME='peertube.local'
  203. if grep -q "peertube domain" $COMPLETION_FILE; then
  204. PEERTUBE_DOMAIN_NAME=$(get_completion_param "peertube domain")
  205. fi
  206. if [ $PEERTUBE_DOMAIN_NAME ]; then
  207. suspend_site ${PEERTUBE_DOMAIN_NAME}
  208. systemctl stop peertube
  209. USE_POSTGRESQL=1
  210. restore_database peertube
  211. temp_restore_dir=/root/temppeertubevideos
  212. function_check restore_directory_from_usb
  213. restore_directory_from_usb $temp_restore_dir peertubevideos
  214. if [ -d $temp_restore_dir ]; then
  215. if [ -d $temp_restore_dir$PEERTUBE_DIR/videos ]; then
  216. cp -r $temp_restore_dir$PEERTUBE_DIR/videos/* $PEERTUBE_DIR/videos/
  217. else
  218. cp -r $temp_restore_dir/* $PEERTUBE_DIR/videos/
  219. fi
  220. chown -R peertube:peertube $PEERTUBE_DIR
  221. rm -rf $temp_restore_dir
  222. fi
  223. systemctl start peertube
  224. restart_site
  225. fi
  226. }
  227. function backup_remote_peertube {
  228. PEERTUBE_DOMAIN_NAME='peertube.local'
  229. if grep -q "peertube domain" $COMPLETION_FILE; then
  230. PEERTUBE_DOMAIN_NAME=$(get_completion_param "peertube domain")
  231. fi
  232. systemctl stop peertube
  233. USE_POSTGRESQL=1
  234. function_check backup_database_to_friend
  235. backup_database_to_friend peertube
  236. systemctl start peertube
  237. temp_backup_dir=$PEERTUBE_DIR/videos
  238. if [ -d $temp_backup_dir ]; then
  239. systemctl stop peertube
  240. suspend_site ${PEERTUBE_DOMAIN_NAME}
  241. backup_directory_to_friend $temp_backup_dir peertubevideos
  242. restart_site
  243. systemctl start peertube
  244. else
  245. echo $"Peertube domain specified but not found in $temp_backup_dir"
  246. exit 6383523
  247. fi
  248. }
  249. function restore_remote_peertube {
  250. PEERTUBE_DOMAIN_NAME='peertube.local'
  251. if grep -q "peertube domain" $COMPLETION_FILE; then
  252. PEERTUBE_DOMAIN_NAME=$(get_completion_param "peertube domain")
  253. fi
  254. suspend_site ${PEERTUBE_DOMAIN_NAME}
  255. systemctl stop peertube
  256. USE_POSTGRESQL=1
  257. function_check restore_database_from_friend
  258. restore_database_from_friend peertube
  259. temp_restore_dir=/root/temppeertubevideos
  260. function_check restore_directory_from_friend
  261. restore_directory_from_friend $temp_restore_dir peertubevideos
  262. if [ -d $temp_restore_dir ]; then
  263. if [ -d $temp_restore_dir$PEERTUBE_DIR/videos ]; then
  264. cp -r $temp_restore_dir$PEERTUBE_DIR/videos/* $PEERTUBE_DIR/videos/
  265. else
  266. cp -r $temp_restore_dir/* $PEERTUBE_DIR/videos/
  267. fi
  268. chown -R peertube: $PEERTUBE_DIR
  269. rm -rf $temp_restore_dir
  270. fi
  271. systemctl start peertube
  272. restart_site
  273. }
  274. function remove_peertube {
  275. if [ ${#PEERTUBE_DOMAIN_NAME} -eq 0 ]; then
  276. return
  277. fi
  278. systemctl stop peertube
  279. systemctl disable peertube
  280. rm /etc/systemd/system/peertube.service
  281. systemctl daemon-reload
  282. function_check remove_nodejs
  283. remove_nodejs peertube
  284. read_config_param "PEERTUBE_DOMAIN_NAME"
  285. nginx_dissite $PEERTUBE_DOMAIN_NAME
  286. remove_certs ${PEERTUBE_DOMAIN_NAME}
  287. if [ -f /etc/nginx/sites-available/$PEERTUBE_DOMAIN_NAME ]; then
  288. rm -f /etc/nginx/sites-available/$PEERTUBE_DOMAIN_NAME
  289. fi
  290. if [ -d /var/www/$PEERTUBE_DOMAIN_NAME ]; then
  291. rm -rf /var/www/$PEERTUBE_DOMAIN_NAME
  292. fi
  293. remove_config_param PEERTUBE_DOMAIN_NAME
  294. remove_config_param PEERTUBE_CODE
  295. function_check remove_onion_service
  296. remove_onion_service peertube ${PEERTUBE_ONION_PORT}
  297. remove_completion_param "install_peertube"
  298. sed -i '/peertube/d' $COMPLETION_FILE
  299. function_check drop_database_postgresql
  300. drop_database_postgresql peertube
  301. remove_postgresql_user peertube
  302. groupdel -f peertube
  303. userdel -r peertube
  304. if [ -d $PEERTUBE_DIR ]; then
  305. rm -rf $PEERTUBE_DIR
  306. fi
  307. function_check remove_ddns_domain
  308. remove_ddns_domain $PEERTUBE_DOMAIN_NAME
  309. }
  310. function peertube_setup_web {
  311. peertube_nginx_file=/etc/nginx/sites-available/$PEERTUBE_DOMAIN_NAME
  312. if [[ $ONION_ONLY == "no" ]]; then
  313. echo 'server {' > $peertube_nginx_file
  314. echo ' listen 80;' >> $peertube_nginx_file
  315. echo ' listen [::]:80;' >> $peertube_nginx_file
  316. echo " server_name $PEERTUBE_DOMAIN_NAME;" >> $peertube_nginx_file
  317. echo ' rewrite ^ https://$server_name$request_uri? permanent;' >> $peertube_nginx_file
  318. echo '}' >> $peertube_nginx_file
  319. echo '' >> $peertube_nginx_file
  320. echo 'server {' >> $peertube_nginx_file
  321. echo ' listen 443 ssl http2;' >> $peertube_nginx_file
  322. echo ' listen [::]:443 ssl http2;' >> $peertube_nginx_file
  323. echo " server_name $PEERTUBE_DOMAIN_NAME;" >> $peertube_nginx_file
  324. echo '' >> $peertube_nginx_file
  325. function_check nginx_ssl
  326. nginx_ssl $PEERTUBE_DOMAIN_NAME mobile
  327. function_check nginx_disable_sniffing
  328. nginx_disable_sniffing $PEERTUBE_DOMAIN_NAME
  329. echo ' add_header Strict-Transport-Security max-age=15768000;' >> $peertube_nginx_file
  330. echo '' >> $peertube_nginx_file
  331. echo ' location / {' >> $peertube_nginx_file
  332. echo " proxy_pass http://localhost:${PEERTUBE_PORT};" >> $peertube_nginx_file
  333. echo ' proxy_set_header X-Real-IP $remote_addr;' >> $peertube_nginx_file
  334. echo ' proxy_set_header Host $host;' >> $peertube_nginx_file
  335. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $peertube_nginx_file
  336. echo '' >> $peertube_nginx_file
  337. echo ' # For the video upload' >> $peertube_nginx_file
  338. echo ' client_max_body_size 2G;' >> $peertube_nginx_file
  339. echo ' }' >> $peertube_nginx_file
  340. echo '' >> $peertube_nginx_file
  341. echo ' location /static/webseed {' >> $peertube_nginx_file
  342. echo " if (\$request_method = 'OPTIONS') {" >> $peertube_nginx_file
  343. echo " add_header 'Access-Control-Allow-Origin' '*';" >> $peertube_nginx_file
  344. echo " add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';" >> $peertube_nginx_file
  345. echo " add_header 'Access-Control-Allow-Headers' 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';" >> $peertube_nginx_file
  346. echo " add_header 'Access-Control-Max-Age' 1728000;" >> $peertube_nginx_file
  347. echo " add_header 'Content-Type' 'text/plain charset=UTF-8';" >> $peertube_nginx_file
  348. echo " add_header 'Content-Length' 0;" >> $peertube_nginx_file
  349. echo ' return 204;' >> $peertube_nginx_file
  350. echo ' }' >> $peertube_nginx_file
  351. echo '' >> $peertube_nginx_file
  352. echo " if (\$request_method = 'GET') {" >> $peertube_nginx_file
  353. echo " add_header 'Access-Control-Allow-Origin' '*';" >> $peertube_nginx_file
  354. echo " add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';" >> $peertube_nginx_file
  355. echo " add_header 'Access-Control-Allow-Headers' 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';" >> $peertube_nginx_file
  356. echo ' }' >> $peertube_nginx_file
  357. echo '' >> $peertube_nginx_file
  358. echo " alias $PEERTUBE_DIR/videos;" >> $peertube_nginx_file
  359. echo ' }' >> $peertube_nginx_file
  360. echo '' >> $peertube_nginx_file
  361. echo ' # Websocket tracker' >> $peertube_nginx_file
  362. echo ' location /tracker/socket {' >> $peertube_nginx_file
  363. echo ' # Peers send a message to the tracker every 15 minutes' >> $peertube_nginx_file
  364. echo ' # Dont close the websocket before this time' >> $peertube_nginx_file
  365. echo ' proxy_read_timeout 1200s;' >> $peertube_nginx_file
  366. echo ' proxy_set_header Upgrade $http_upgrade;' >> $peertube_nginx_file
  367. echo ' proxy_set_header Connection "upgrade";' >> $peertube_nginx_file
  368. echo ' proxy_http_version 1.1;' >> $peertube_nginx_file
  369. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $peertube_nginx_file
  370. echo ' proxy_set_header Host $host;' >> $peertube_nginx_file
  371. echo " proxy_pass http://localhost:${PEERTUBE_PORT};" >> $peertube_nginx_file
  372. echo ' }' >> $peertube_nginx_file
  373. echo '}' >> $peertube_nginx_file
  374. else
  375. echo -n '' > $peertube_nginx_file
  376. fi
  377. echo 'server {' >> $peertube_nginx_file
  378. echo " listen 127.0.0.1:$PEERTUBE_ONION_PORT default_server;" >> $peertube_nginx_file
  379. echo " server_name $PEERTUBE_ONION_HOSTNAME;" >> $peertube_nginx_file
  380. echo '' >> $peertube_nginx_file
  381. echo ' location / {' >> $peertube_nginx_file
  382. echo " proxy_pass http://localhost:${PEERTUBE_PORT};" >> $peertube_nginx_file
  383. echo ' proxy_set_header X-Real-IP $remote_addr;' >> $peertube_nginx_file
  384. echo ' proxy_set_header Host $host;' >> $peertube_nginx_file
  385. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $peertube_nginx_file
  386. echo '' >> $peertube_nginx_file
  387. echo ' # For the video upload' >> $peertube_nginx_file
  388. echo ' client_max_body_size 2G;' >> $peertube_nginx_file
  389. echo ' }' >> $peertube_nginx_file
  390. echo '' >> $peertube_nginx_file
  391. echo ' location /static/webseed {' >> $peertube_nginx_file
  392. echo " if (\$request_method = 'OPTIONS') {" >> $peertube_nginx_file
  393. echo " add_header 'Access-Control-Allow-Origin' '*';" >> $peertube_nginx_file
  394. echo " add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';" >> $peertube_nginx_file
  395. echo " add_header 'Access-Control-Allow-Headers' 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';" >> $peertube_nginx_file
  396. echo " add_header 'Access-Control-Max-Age' 1728000;" >> $peertube_nginx_file
  397. echo " add_header 'Content-Type' 'text/plain charset=UTF-8';" >> $peertube_nginx_file
  398. echo " add_header 'Content-Length' 0;" >> $peertube_nginx_file
  399. echo ' return 204;' >> $peertube_nginx_file
  400. echo ' }' >> $peertube_nginx_file
  401. echo '' >> $peertube_nginx_file
  402. echo " if (\$request_method = 'GET') {" >> $peertube_nginx_file
  403. echo " add_header 'Access-Control-Allow-Origin' '*';" >> $peertube_nginx_file
  404. echo " add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';" >> $peertube_nginx_file
  405. echo " add_header 'Access-Control-Allow-Headers' 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';" >> $peertube_nginx_file
  406. echo ' }' >> $peertube_nginx_file
  407. echo '' >> $peertube_nginx_file
  408. echo " alias $PEERTUBE_DIR/videos;" >> $peertube_nginx_file
  409. echo ' }' >> $peertube_nginx_file
  410. echo '' >> $peertube_nginx_file
  411. echo ' # Websocket tracker' >> $peertube_nginx_file
  412. echo ' location /tracker/socket {' >> $peertube_nginx_file
  413. echo ' # Peers send a message to the tracker every 15 minutes' >> $peertube_nginx_file
  414. echo ' # Dont close the websocket before this time' >> $peertube_nginx_file
  415. echo ' proxy_read_timeout 1200s;' >> $peertube_nginx_file
  416. echo ' proxy_set_header Upgrade $http_upgrade;' >> $peertube_nginx_file
  417. echo ' proxy_set_header Connection "upgrade";' >> $peertube_nginx_file
  418. echo ' proxy_http_version 1.1;' >> $peertube_nginx_file
  419. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $peertube_nginx_file
  420. echo ' proxy_set_header Host $host;' >> $peertube_nginx_file
  421. echo " proxy_pass http://localhost:${PEERTUBE_PORT};" >> $peertube_nginx_file
  422. echo ' }' >> $peertube_nginx_file
  423. echo '}' >> $peertube_nginx_file
  424. # CSP currently causes an error
  425. sed -i '/Content-Security-Policy/d' $peertube_nginx_file
  426. function_check create_site_certificate
  427. create_site_certificate $PEERTUBE_DOMAIN_NAME 'yes'
  428. function_check nginx_ensite
  429. nginx_ensite $PEERTUBE_DOMAIN_NAME
  430. }
  431. function mesh_peertube_setup_web {
  432. peertube_nginx_file=$rootdir/etc/nginx/sites-available/peertube
  433. echo 'server {' >> $peertube_nginx_file
  434. echo " listen $MESH_PEERTUBE_PORT http2;" >> $peertube_nginx_file
  435. echo ' listen [::]:$MESH_PEERTUBE_PORT http2;' >> $peertube_nginx_file
  436. echo ' server_name $HOSTNAME;' >> $peertube_nginx_file
  437. echo '' >> $peertube_nginx_file
  438. echo ' location / {' >> $peertube_nginx_file
  439. echo " proxy_pass http://localhost:${PEERTUBE_PORT};" >> $peertube_nginx_file
  440. echo ' proxy_set_header X-Real-IP $remote_addr;' >> $peertube_nginx_file
  441. echo ' proxy_set_header Host $host;' >> $peertube_nginx_file
  442. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $peertube_nginx_file
  443. echo '' >> $peertube_nginx_file
  444. echo ' # For the video upload' >> $peertube_nginx_file
  445. echo ' client_max_body_size 2G;' >> $peertube_nginx_file
  446. echo ' }' >> $peertube_nginx_file
  447. echo '' >> $peertube_nginx_file
  448. echo ' location /static/webseed {' >> $peertube_nginx_file
  449. echo " if (\$request_method = 'OPTIONS') {" >> $peertube_nginx_file
  450. echo " add_header 'Access-Control-Allow-Origin' '*';" >> $peertube_nginx_file
  451. echo " add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';" >> $peertube_nginx_file
  452. echo " add_header 'Access-Control-Allow-Headers' 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';" >> $peertube_nginx_file
  453. echo " add_header 'Access-Control-Max-Age' 1728000;" >> $peertube_nginx_file
  454. echo " add_header 'Content-Type' 'text/plain charset=UTF-8';" >> $peertube_nginx_file
  455. echo " add_header 'Content-Length' 0;" >> $peertube_nginx_file
  456. echo ' return 204;' >> $peertube_nginx_file
  457. echo ' }' >> $peertube_nginx_file
  458. echo '' >> $peertube_nginx_file
  459. echo " if (\$request_method = 'GET') {" >> $peertube_nginx_file
  460. echo " add_header 'Access-Control-Allow-Origin' '*';" >> $peertube_nginx_file
  461. echo " add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';" >> $peertube_nginx_file
  462. echo " add_header 'Access-Control-Allow-Headers' 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';" >> $peertube_nginx_file
  463. echo ' }' >> $peertube_nginx_file
  464. echo '' >> $peertube_nginx_file
  465. echo " alias $PEERTUBE_DIR/videos;" >> $peertube_nginx_file
  466. echo ' }' >> $peertube_nginx_file
  467. echo '' >> $peertube_nginx_file
  468. echo ' # Websocket tracker' >> $peertube_nginx_file
  469. echo ' location /tracker/socket {' >> $peertube_nginx_file
  470. echo ' # Peers send a message to the tracker every 15 minutes' >> $peertube_nginx_file
  471. echo ' # Dont close the websocket before this time' >> $peertube_nginx_file
  472. echo ' proxy_read_timeout 1200s;' >> $peertube_nginx_file
  473. echo ' proxy_set_header Upgrade $http_upgrade;' >> $peertube_nginx_file
  474. echo ' proxy_set_header Connection "upgrade";' >> $peertube_nginx_file
  475. echo ' proxy_http_version 1.1;' >> $peertube_nginx_file
  476. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $peertube_nginx_file
  477. echo ' proxy_set_header Host $host;' >> $peertube_nginx_file
  478. echo " proxy_pass http://localhost:${PEERTUBE_PORT};" >> $peertube_nginx_file
  479. echo ' }' >> $peertube_nginx_file
  480. echo '}' >> $peertube_nginx_file
  481. }
  482. function peertube_create_config {
  483. peertube_prefix=$1
  484. peertube_config_file=$peertube_prefix$PEERTUBE_DIR/config/production.yaml
  485. echo 'listen:' > $peertube_config_file
  486. echo " port: $PEERTUBE_PORT" >> $peertube_config_file
  487. echo '' >> $peertube_config_file
  488. echo '# Correspond to your reverse proxy "listen" configuration' >> $peertube_config_file
  489. echo 'webserver:' >> $peertube_config_file
  490. if [ ! $peertube_prefix ]; then
  491. if [[ $ONION_ONLY == 'no' ]]; then
  492. echo ' https: true' >> $peertube_config_file
  493. echo " hostname: '$PEERTUBE_DOMAIN_NAME'" >> $peertube_config_file
  494. echo ' port: 443' >> $peertube_config_file
  495. else
  496. echo ' https: false' >> $peertube_config_file
  497. echo " hostname: '$PEERTUBE_ONION_HOSTNAME'" >> $peertube_config_file
  498. echo ' port: 80' >> $peertube_config_file
  499. fi
  500. else
  501. echo ' https: false' >> $peertube_config_file
  502. echo " hostname: ''" >> $peertube_config_file
  503. echo " port: $MESH_PEERTUBE_PORT" >> $peertube_config_file
  504. fi
  505. echo '' >> $peertube_config_file
  506. echo '# Your database name will be "peertube"+database.suffix' >> $peertube_config_file
  507. echo 'database:' >> $peertube_config_file
  508. echo " hostname: 'localhost'" >> $peertube_config_file
  509. echo ' port: 5432' >> $peertube_config_file
  510. echo " suffix: ''" >> $peertube_config_file
  511. echo " username: 'peertube'" >> $peertube_config_file
  512. if [ ! $peertube_prefix ]; then
  513. echo " password: '$PEERTUBE_ADMIN_PASSWORD'" >> $peertube_config_file
  514. else
  515. echo " password: ''" >> $peertube_config_file
  516. fi
  517. echo '' >> $peertube_config_file
  518. echo '# From the project root directory' >> $peertube_config_file
  519. echo 'storage:' >> $peertube_config_file
  520. echo " certs: 'certs/'" >> $peertube_config_file
  521. echo " videos: 'videos/'" >> $peertube_config_file
  522. echo " logs: 'logs/'" >> $peertube_config_file
  523. echo " previews: 'previews/'" >> $peertube_config_file
  524. echo " thumbnails: 'thumbnails/'" >> $peertube_config_file
  525. echo " torrents: 'torrents/'" >> $peertube_config_file
  526. echo " cache: 'cache/'" >> $peertube_config_file
  527. echo '' >> $peertube_config_file
  528. echo 'cache:' >> $peertube_config_file
  529. echo ' previews:' >> $peertube_config_file
  530. echo ' size: 10 # Max number of previews you want to cache' >> $peertube_config_file
  531. echo '' >> $peertube_config_file
  532. echo 'admin:' >> $peertube_config_file
  533. echo " email: 'root@local'" >> $peertube_config_file
  534. echo '' >> $peertube_config_file
  535. echo 'signup:' >> $peertube_config_file
  536. echo ' enabled: true' >> $peertube_config_file
  537. echo ' limit: 2 # When the limit is reached, registrations are disabled. -1 == unlimited' >> $peertube_config_file
  538. echo '' >> $peertube_config_file
  539. echo 'user:' >> $peertube_config_file
  540. echo ' # Default value of maximum video BYTES the user can upload (does not take into account transcoded files).' >> $peertube_config_file
  541. echo ' # -1 == unlimited' >> $peertube_config_file
  542. echo ' video_quota: -1' >> $peertube_config_file
  543. echo '' >> $peertube_config_file
  544. echo '# If enabled, the video will be transcoded to mp4 (x264) with "faststart" flag' >> $peertube_config_file
  545. echo '# Uses a lot of CPU!' >> $peertube_config_file
  546. echo 'transcoding:' >> $peertube_config_file
  547. echo ' enabled: true' >> $peertube_config_file
  548. echo ' threads: 2' >> $peertube_config_file
  549. echo ' resolutions: # Only created if the original video has a higher resolution' >> $peertube_config_file
  550. echo ' 240p: true' >> $peertube_config_file
  551. echo ' 360p: false' >> $peertube_config_file
  552. echo ' 480p: false' >> $peertube_config_file
  553. echo ' 720p: false' >> $peertube_config_file
  554. echo ' 1080p: false' >> $peertube_config_file
  555. }
  556. function mesh_install_peertube {
  557. if [[ $VARIANT != "meshclient" && $VARIANT != "meshusb" ]]; then
  558. return
  559. fi
  560. if [[ $ARCHITECTURE != 'x86_64' && $ARCHITECTURE != 'amd64' ]]; then
  561. return
  562. fi
  563. chroot "$rootdir" apt-get -yq install ffmpeg curl
  564. function_check install_postgresql
  565. install_postgresql
  566. if [ -d $rootdir$PEERTUBE_DIR ]; then
  567. rm -rf $rootdir$PEERTUBE_DIR
  568. fi
  569. git clone $PEERTUBE_REPO $rootdir$PEERTUBE_DIR
  570. chroot "$rootdir" groupadd peertube
  571. chroot "$rootdir" useradd -c "PeerTube system account" -d $PEERTUBE_DIR -m -r -g peertube peertube
  572. cd $rootdir$PEERTUBE_DIR
  573. git checkout $PEERTUBE_COMMIT -b $PEERTUBE_COMMIT
  574. get_npm_arch
  575. cat <<EOF > $rootdir/usr/bin/install_peertube
  576. #!/bin/bash
  577. cd $PEERTUBE_DIR
  578. curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
  579. echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
  580. apt-get -y update
  581. apt-get -yq install yarn
  582. #npm install --arch=$NPM_ARCH -g yarn
  583. #if [ ! "\$?" = "0" ]; then
  584. # echo $'PeerTube Failed to install yarn'
  585. # exit 79353234
  586. #fi
  587. #npm install --arch=$NPM_ARCH webpack@3.10.0 --no-optional
  588. #if [ ! "\$?" = "0" ]; then
  589. # echo $'PeerTube failed to install webpack'
  590. # exit 68386353
  591. #fi
  592. yarn install
  593. yarn run build:prod
  594. yarn add -D webpack
  595. if [ ! "\$?" = "0" ]; then
  596. echo $'PeerTube failed to add webpack'
  597. exit 67342823
  598. fi
  599. yarn install --ignore-optional
  600. if [ ! "\$?" = "0" ]; then
  601. echo $'PeerTube failed to run yarn install'
  602. exit 63754235
  603. fi
  604. npm install --arch=$NPM_ARCH
  605. if [ ! "\$?" = "0" ]; then
  606. echo $'PeerTube failed to install peertube'
  607. exit 7835243
  608. fi
  609. npm run build --arch=$NPM_ARCH
  610. if [ ! "\$?" = "0" ]; then
  611. echo $'PeerTube failed to build peertube'
  612. exit 5293593
  613. fi
  614. EOF
  615. chmod +x $rootdir/usr/bin/install_peertube
  616. chroot "$rootdir" /usr/bin/install_peertube
  617. if [ ! "$?" = "0" ]; then
  618. echo $'PeerTube install failed'
  619. exit 735638
  620. fi
  621. echo '[Unit]' > $rootdir/etc/systemd/system/peertube.service
  622. echo 'Description=PeerTube Decentralized video streaming platform' >> $rootdir/etc/systemd/system/peertube.service
  623. echo 'After=syslog.target' >> $rootdir/etc/systemd/system/peertube.service
  624. echo 'After=network.target' >> $rootdir/etc/systemd/system/peertube.service
  625. echo '' >> $rootdir/etc/systemd/system/peertube.service
  626. echo '[Service]' >> $rootdir/etc/systemd/system/peertube.service
  627. echo 'User=peertube' >> $rootdir/etc/systemd/system/peertube.service
  628. echo 'Group=peertube' >> $rootdir/etc/systemd/system/peertube.service
  629. echo "WorkingDirectory=$PEERTUBE_DIR" >> $rootdir/etc/systemd/system/peertube.service
  630. echo "ExecStart=/usr/local/bin/npm start" >> $rootdir/etc/systemd/system/peertube.service
  631. echo "ExecStop=/usr/local/bin/npm stop" >> $rootdir/etc/systemd/system/peertube.service
  632. echo 'StandardOutput=syslog' >> $rootdir/etc/systemd/system/peertube.service
  633. echo 'StandardError=syslog' >> $rootdir/etc/systemd/system/peertube.service
  634. echo 'SyslogIdentifier=peertube' >> $rootdir/etc/systemd/system/peertube.service
  635. echo 'Restart=always' >> $rootdir/etc/systemd/system/peertube.service
  636. echo "Environment=NODE_ENV=production" >> $rootdir/etc/systemd/system/peertube.service
  637. echo '' >> $rootdir/etc/systemd/system/peertube.service
  638. echo '[Install]' >> $rootdir/etc/systemd/system/peertube.service
  639. echo 'WantedBy=multi-user.target' >> $rootdir/etc/systemd/system/peertube.service
  640. peertube_create_config $rootdir
  641. chroot "$rootdir" chown -R peertube:peertube $PEERTUBE_DIR
  642. mesh_peertube_setup_web
  643. }
  644. function install_peertube {
  645. if [[ $VARIANT == "mesh"* ]]; then
  646. mesh_install_peertube
  647. return
  648. fi
  649. if [[ $ARCHITECTURE != 'x86_64' && $ARCHITECTURE != 'amd64' ]]; then
  650. return
  651. fi
  652. if [ ! $ONION_ONLY ]; then
  653. ONION_ONLY='no'
  654. fi
  655. if [ ! $PEERTUBE_DOMAIN_NAME ]; then
  656. echo $'The peertube domain name was not specified'
  657. exit 783523
  658. fi
  659. apt-get -yq install ffmpeg
  660. function_check install_postgresql
  661. install_postgresql
  662. if [ ! -d /var/www/$PEERTUBE_DOMAIN_NAME/htdocs ]; then
  663. mkdir -p /var/www/$PEERTUBE_DOMAIN_NAME/htdocs
  664. fi
  665. if [ -d $PEERTUBE_DIR ]; then
  666. rm -rf $PEERTUBE_DIR
  667. fi
  668. groupadd peertube
  669. useradd -c "PeerTube system account" -d $PEERTUBE_DIR -m -r -g peertube peertube
  670. peertube_create_database
  671. function_check install_nodejs
  672. install_nodejs peertube
  673. if [ -d /repos/peertube ]; then
  674. mkdir -p $PEERTUBE_DIR
  675. cp -r -p /repos/peertube/. $PEERTUBE_DIR
  676. cd $PEERTUBE_DIR
  677. git pull
  678. else
  679. function_check git_clone
  680. git_clone $PEERTUBE_REPO $PEERTUBE_DIR
  681. fi
  682. cd $PEERTUBE_DIR
  683. git checkout $PEERTUBE_COMMIT -b $PEERTUBE_COMMIT
  684. set_completion_param "peertube commit" "$PEERTUBE_COMMIT"
  685. npm install -g yarn
  686. if [ ! "$?" = "0" ]; then
  687. echo $'Failed to install yarn'
  688. exit 79353234
  689. fi
  690. yarn add -D webpack --network-concurrency 1
  691. yarn install
  692. if [ ! "$?" = "0" ]; then
  693. echo $'Failed to run yarn install'
  694. exit 63754235
  695. fi
  696. npm install
  697. if [ ! "$?" = "0" ]; then
  698. echo $'Failed to install peertube'
  699. exit 7835243
  700. fi
  701. npm run build
  702. if [ ! "$?" = "0" ]; then
  703. echo $'Failed to build peertube'
  704. exit 5293593
  705. fi
  706. PEERTUBE_ONION_HOSTNAME=$(add_onion_service peertube 80 ${PEERTUBE_ONION_PORT})
  707. echo '[Unit]' > /etc/systemd/system/peertube.service
  708. echo 'Description=PeerTube Decentralized video streaming platform' >> /etc/systemd/system/peertube.service
  709. echo 'After=syslog.target' >> /etc/systemd/system/peertube.service
  710. echo 'After=network.target' >> /etc/systemd/system/peertube.service
  711. echo '' >> /etc/systemd/system/peertube.service
  712. echo '[Service]' >> /etc/systemd/system/peertube.service
  713. echo 'User=peertube' >> /etc/systemd/system/peertube.service
  714. echo 'Group=peertube' >> /etc/systemd/system/peertube.service
  715. echo "WorkingDirectory=$PEERTUBE_DIR" >> /etc/systemd/system/peertube.service
  716. echo "ExecStart=/usr/local/bin/npm start" >> /etc/systemd/system/peertube.service
  717. echo "ExecStop=/usr/local/bin/npm stop" >> /etc/systemd/system/peertube.service
  718. echo 'StandardOutput=syslog' >> /etc/systemd/system/peertube.service
  719. echo 'StandardError=syslog' >> /etc/systemd/system/peertube.service
  720. echo 'SyslogIdentifier=peertube' >> /etc/systemd/system/peertube.service
  721. echo 'Restart=always' >> /etc/systemd/system/peertube.service
  722. echo "Environment=NODE_ENV=production" >> /etc/systemd/system/peertube.service
  723. echo '' >> /etc/systemd/system/peertube.service
  724. echo '[Install]' >> /etc/systemd/system/peertube.service
  725. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/peertube.service
  726. peertube_create_config
  727. chown -R peertube:peertube $PEERTUBE_DIR
  728. peertube_setup_web
  729. ${PROJECT_NAME}-pass -u $MY_USERNAME -a peertube -p "$PEERTUBE_ADMIN_PASSWORD"
  730. function_check add_ddns_domain
  731. add_ddns_domain $PEERTUBE_DOMAIN_NAME
  732. systemctl enable peertube
  733. systemctl daemon-reload
  734. systemctl start peertube
  735. systemctl restart nginx
  736. set_completion_param "peertube domain" "$PEERTUBE_DOMAIN_NAME"
  737. APP_INSTALLED=1
  738. }
  739. # NOTE: deliberately no exit 0