freedombone-app-peertube 28KB

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