freedombone-app-pleroma 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Pleroma backend application
  12. # https://git.pleroma.social/pleroma/pleroma/wikis/Installing-on-Debian-Based-Distributions
  13. #
  14. # License
  15. # =======
  16. #
  17. # Copyright (C) 2017 Bob Mottram <bob@freedombone.net>
  18. #
  19. # This program is free software: you can redistribute it and/or modify
  20. # it under the terms of the GNU Affero General Public License as published by
  21. # the Free Software Foundation, either version 3 of the License, or
  22. # (at your option) any later version.
  23. #
  24. # This program is distributed in the hope that it will be useful,
  25. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. # GNU Affero General Public License for more details.
  28. #
  29. # You should have received a copy of the GNU Affero General Public License
  30. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  31. VARIANTS='full full-vim social'
  32. IN_DEFAULT_INSTALL=0
  33. SHOW_ON_ABOUT=1
  34. PLEROMA_DOMAIN_NAME=
  35. PLEROMA_CODE=
  36. PLEROMA_PORT=4000
  37. PLEROMA_ONION_PORT=8011
  38. PLEROMA_REPO="https://git.pleroma.social/pleroma/pleroma.git"
  39. PLEROMA_COMMIT='7252f6b054dfdfac1f9bac77c442c5a1ebd898af'
  40. PLEROMA_ADMIN_PASSWORD=
  41. PLEROMA_DIR=/etc/pleroma
  42. PLEROMA_SECRET_KEY=""
  43. PLEROMA_BACKGROUND_IMAGE_URL=
  44. PLEROMA_TITLE='Pleroma Server'
  45. # Number of months after which posts expire
  46. PLEROMA_EXPIRE_MONTHS=3
  47. pleroma_variables=(ONION_ONLY
  48. PLEROMA_DOMAIN_NAME
  49. PLEROMA_CODE
  50. PLEROMA_WELCOME_MESSAGE
  51. PLEROMA_BACKGROUND_IMAGE_URL
  52. DDNS_PROVIDER
  53. PLEROMA_TITLE
  54. PLEROMA_EXPIRE_MONTHS
  55. MY_EMAIL_ADDRESS
  56. MY_USERNAME)
  57. function pleroma_recompile {
  58. # necessary after parameter changes
  59. sudo -u pleroma mix clean
  60. sudo -u pleroma mix deps.compile
  61. sudo -u pleroma mix compile
  62. if [ -f /etc/systemd/system/pleroma.service ]; then
  63. systemctl restart pleroma
  64. fi
  65. }
  66. function logging_on_pleroma {
  67. echo -n ''
  68. }
  69. function logging_off_pleroma {
  70. echo -n ''
  71. }
  72. function remove_user_pleroma {
  73. remove_username="$1"
  74. ${PROJECT_NAME}-pass -u $remove_username --rmapp pleroma
  75. }
  76. function add_user_pleroma {
  77. new_username="$1"
  78. new_user_password="$2"
  79. ${PROJECT_NAME}-pass -u $new_username -a pleroma -p "$new_user_password"
  80. echo '0'
  81. }
  82. function install_interactive_pleroma {
  83. if [ ! $ONION_ONLY ]; then
  84. ONION_ONLY='no'
  85. fi
  86. if [[ $ONION_ONLY != "no" ]]; then
  87. PLEROMA_DOMAIN_NAME='pleroma.local'
  88. else
  89. PLEROMA_DETAILS_COMPLETE=
  90. while [ ! $PLEROMA_DETAILS_COMPLETE ]
  91. do
  92. data=$(tempfile 2>/dev/null)
  93. trap "rm -f $data" 0 1 2 5 15
  94. if [[ $DDNS_PROVIDER == "default@freedns.afraid.org" ]]; then
  95. dialog --backtitle $"Freedombone Configuration" \
  96. --title $"Pleroma Configuration" \
  97. --form $"\nPlease enter your Pleroma details. The background image URL can be left blank.\n\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 16 65 4 \
  98. $"Domain:" 1 1 "$(grep 'PLEROMA_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 25 33 40 \
  99. $"Title:" 2 1 "$(grep '$PLEROMA_TITLE' temp.cfg | awk -F '=' '{print $2}')" 2 25 255 255 \
  100. $"Background image URL:" 3 1 "$(grep '$PLEROMA_BACKGROUND_IMAGE_URL' temp.cfg | awk -F '=' '{print $2}')" 3 25 255 255 \
  101. $"Code:" 4 1 "$(grep 'PLEROMA_CODE' temp.cfg | awk -F '=' '{print $2}')" 4 25 33 255 \
  102. 2> $data
  103. else
  104. dialog --backtitle $"Freedombone Configuration" \
  105. --title $"Pleroma Configuration" \
  106. --form $"\nPlease enter your Pleroma details. The background image URL can be left blank.\n\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 16 65 4 \
  107. $"Domain:" 1 1 "$(grep 'PLEROMA_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 25 33 40 \
  108. $"Title:" 2 1 "$(grep '$PLEROMA_TITLE' temp.cfg | awk -F '=' '{print $2}')" 2 25 255 255 \
  109. $"Background image URL:" 3 1 "$(grep '$PLEROMA_BACKGROUND_IMAGE_URL' temp.cfg | awk -F '=' '{print $2}')" 3 25 255 255 \
  110. 2> $data
  111. fi
  112. sel=$?
  113. case $sel in
  114. 1) exit 1;;
  115. 255) exit 1;;
  116. esac
  117. PLEROMA_DOMAIN_NAME=$(cat $data | sed -n 1p)
  118. title=$(cat $data | sed -n 2p)
  119. if [ ${#title} -gt 1 ]; then
  120. PLEROMA_TITLE=$welcome_msg
  121. fi
  122. img_url=$(cat $data | sed -n 3p)
  123. if [ ${#img_url} -gt 1 ]; then
  124. PLEROMA_BACKGROUND_IMAGE_URL=$img_url
  125. fi
  126. if [ $PLEROMA_DOMAIN_NAME ]; then
  127. if [[ $PLEROMA_DOMAIN_NAME == "$HUBZILLA_DOMAIN_NAME" ]]; then
  128. PLEROMA_DOMAIN_NAME=""
  129. fi
  130. TEST_DOMAIN_NAME=$PLEROMA_DOMAIN_NAME
  131. validate_domain_name
  132. if [[ $TEST_DOMAIN_NAME != $PLEROMA_DOMAIN_NAME ]]; then
  133. PLEROMA_DOMAIN_NAME=
  134. dialog --title $"Domain name validation" --msgbox "$TEST_DOMAIN_NAME" 15 50
  135. else
  136. if [[ $DDNS_PROVIDER == "default@freedns.afraid.org" ]]; then
  137. PLEROMA_CODE=$(cat $data | sed -n 4p)
  138. validate_freedns_code "$PLEROMA_CODE"
  139. if [ ! $VALID_CODE ]; then
  140. PLEROMA_DOMAIN_NAME=
  141. fi
  142. fi
  143. fi
  144. fi
  145. if [ $PLEROMA_DOMAIN_NAME ]; then
  146. PLEROMA_DETAILS_COMPLETE="yes"
  147. fi
  148. done
  149. # remove any invalid characters
  150. if [ ${#PLEROMA_TITLE} -gt 0 ]; then
  151. new_title=$(echo "$PLEROMA_TITLE" | sed "s|'||g")
  152. PLEROMA_TITLE="$new_title"
  153. fi
  154. # save the results in the config file
  155. write_config_param "PLEROMA_CODE" "$PLEROMA_CODE"
  156. write_config_param "PLEROMA_TITLE" "$PLEROMA_TITLE"
  157. write_config_param "PLEROMA_BACKGROUND_IMAGE_URL" "$PLEROMA_BACKGROUND_IMAGE_URL"
  158. fi
  159. write_config_param "PLEROMA_DOMAIN_NAME" "$PLEROMA_DOMAIN_NAME"
  160. APP_INSTALLED=1
  161. }
  162. function change_password_pleroma {
  163. curr_username="$1"
  164. new_user_password="$2"
  165. #${PROJECT_NAME}-pass -u "$curr_username" -a pleroma -p "$new_user_password"
  166. }
  167. function pleroma_create_database {
  168. if [ -f $IMAGE_PASSWORD_FILE ]; then
  169. PLEROMA_ADMIN_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  170. else
  171. if [ ! $PLEROMA_ADMIN_PASSWORD ]; then
  172. PLEROMA_ADMIN_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  173. fi
  174. fi
  175. if [ ! $PLEROMA_ADMIN_PASSWORD ]; then
  176. return
  177. fi
  178. systemctl restart postgresql
  179. add_postgresql_user pleroma "$PLEROMA_ADMIN_PASSWORD" encrypted
  180. run_system_query_postgresql "create database pleroma;"
  181. # temporarily allow the user to create databases
  182. run_system_query_postgresql "ALTER USER pleroma CREATEDB;"
  183. run_system_query_postgresql "ALTER USER pleroma SUPERUSER;"
  184. run_system_query_postgresql "GRANT ALL ON ALL tables IN SCHEMA public TO pleroma;"
  185. run_system_query_postgresql "GRANT ALL ON ALL sequences IN SCHEMA public TO pleroma;"
  186. run_system_query_postgresql "CREATE EXTENSION citext;"
  187. run_system_query_postgresql "set statement_timeout to 20000;"
  188. read_config_param "PLEROMA_SECRET_KEY"
  189. if [ ${#PLEROMA_SECRET_KEY} -lt 50 ]; then
  190. PLEROMA_SECRET_KEY="$(create_password 30)$(create_password 30)"
  191. if [ ${#PLEROMA_SECRET_KEY} -lt 50 ]; then
  192. run_system_query_postgresql "ALTER USER pleroma NOSUPERUSER;"
  193. run_system_query_postgresql "ALTER USER pleroma NOCREATEDB;"
  194. echo $'Pleroma secret key not created'
  195. exit 6782352
  196. fi
  197. write_config_param "PLEROMA_SECRET_KEY" "$PLEROMA_SECRET_KEY"
  198. fi
  199. pleroma_secret=config/dev.secret.exs
  200. cp config/dev.exs $pleroma_secret
  201. sed -i "s|username:.*|username: \"pleroma\",|g" $pleroma_secret
  202. sed -i "s|password:.*|password: \"$PLEROMA_ADMIN_PASSWORD\",|g" $pleroma_secret
  203. sed -i "s|database:.*|database: \"pleroma\",|g" $pleroma_secret
  204. sed -i "/Pleroma.Web.Endpoint/a secret_key_base: \"$PLEROMA_SECRET_KEY\"," $pleroma_secret
  205. sed -i 's|secret_key_base: | secret_key_base: |g' $pleroma_secret
  206. sed -i "/Pleroma.Web.Endpoint/a pubsub: [name: Pleroma.Web.PubSub, adapter: Phoenix.PubSub.PG2]," $pleroma_secret
  207. sed -i 's|pubsub: | pubsub: |g' $pleroma_secret
  208. sed -i 's|watchers: []|watchers: [],|g' $pleroma_secret
  209. sed -i "/watchers: []/a url: [host: \"$PLEROMA_DOMAIN_NAME\", scheme: \"https\", port: 443]" $pleroma_secret
  210. sed -i 's|url: | url: |g' $pleroma_secret
  211. cd $PLEROMA_DIR
  212. chown -R pleroma:pleroma *
  213. sudo -u pleroma mix local.rebar --force
  214. if [ ! "$?" = "0" ]; then
  215. run_system_query_postgresql "ALTER USER pleroma NOSUPERUSER;"
  216. run_system_query_postgresql "ALTER USER pleroma NOCREATEDB;"
  217. echo $'mix local.rebar failed'
  218. exit 73528562
  219. fi
  220. sudo -u pleroma mix local.hex --force
  221. sudo -u pleroma mix deps.compile mimerl
  222. systemctl restart postgresql
  223. sudo -u pleroma mix ecto.create --force
  224. if [ ! "$?" = "0" ]; then
  225. run_system_query_postgresql "ALTER USER pleroma NOSUPERUSER;"
  226. run_system_query_postgresql "ALTER USER pleroma NOCREATEDB;"
  227. echo $'mix ecto.create failed'
  228. exit 83653582
  229. fi
  230. sudo -u pleroma mix ecto.migrate --force
  231. if [ ! "$?" = "0" ]; then
  232. run_system_query_postgresql "ALTER USER pleroma NOSUPERUSER;"
  233. run_system_query_postgresql "ALTER USER pleroma NOCREATEDB;"
  234. echo $'mix ecto.migrate failed'
  235. exit 73752573
  236. fi
  237. # revoke the ability to create databases for this user
  238. run_system_query_postgresql "ALTER USER pleroma NOSUPERUSER;"
  239. run_system_query_postgresql "ALTER USER pleroma NOCREATEDB;"
  240. }
  241. function reconfigure_pleroma {
  242. echo -n ''
  243. }
  244. function pleroma_set_background_image {
  245. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  246. data=$(tempfile 2>/dev/null)
  247. trap "rm -f $data" 0 1 2 5 15
  248. dialog --title $"Pleroma" \
  249. --backtitle $"Freedombone Control Panel" \
  250. --inputbox $'Set a background image URL' 10 60 2>$data
  251. sel=$?
  252. case $sel in
  253. 0)
  254. temp_background=$(<$data)
  255. if [ ${#temp_background} -gt 0 ]; then
  256. PLEROMA_BACKGROUND_IMAGE_URL="$temp_background"
  257. write_config_param "PLEROMA_BACKGROUND_IMAGE_URL" "$PLEROMA_BACKGROUND_IMAGE_URL"
  258. if [[ $(pleroma_set_background_image_from_url $PLEROMA_DIR "$PLEROMA_DOMAIN_NAME" "$PLEROMA_BACKGROUND_IMAGE_URL" "$PLEROMA_TITLE" | tail -n 1) == "0" ]]; then
  259. pleroma_recompile
  260. dialog --title $"Set Pleroma login background" \
  261. --msgbox $"The background image has been set" 6 60
  262. fi
  263. fi
  264. ;;
  265. esac
  266. rm $data
  267. }
  268. function pleroma_set_title {
  269. data=$(tempfile 2>/dev/null)
  270. trap "rm -f $data" 0 1 2 5 15
  271. dialog --title $"Pleroma" \
  272. --backtitle $"Freedombone Control Panel" \
  273. --inputbox $'Set a title' 10 60 2>$data
  274. sel=$?
  275. case $sel in
  276. 0)
  277. new_title=$(<$data)
  278. if [ ${#new_title} -gt 0 ]; then
  279. PLEROMA_TITLE="$new_title"
  280. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  281. write_config_param "PLEROMA_TITLE" "$PLEROMA_TITLE"
  282. sed -i "s|\"name\":.*|\"name\": \"${PLEROMA_TITLE}\",|g" $PLEROMA_DIR/static/config.json
  283. sed -i "s|\"name\":.*|\"name\": \"${PLEROMA_TITLE}\",|g" $PLEROMA_DIR/priv/static/static/config.json
  284. sed -i "s|name: .*|name: \"${PLEROMA_TITLE}\",|g" $PLEROMA_DIR/config/config.exs
  285. systemctl restart pleroma
  286. dialog --title $"Set Pleroma title" \
  287. --msgbox $"The title has been set" 6 60
  288. fi
  289. ;;
  290. esac
  291. rm $data
  292. }
  293. function pleroma_set_expire_months {
  294. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  295. read_config_param "PLEROMA_EXPIRE_MONTHS"
  296. data=$(tempfile 2>/dev/null)
  297. trap "rm -f $data" 0 1 2 5 15
  298. dialog --title $"Pleroma" \
  299. --backtitle $"Freedombone Control Panel" \
  300. --inputbox $'Set an expiry period for posts in months. Anything older will be deleted. Lower values help to keep the database size small and as fast as possible.' 12 60 "$PLEROMA_EXPIRE_MONTHS" 2>$data
  301. sel=$?
  302. case $sel in
  303. 0)
  304. new_expiry_months=$(<$data)
  305. if [ ${#new_expiry_months} -gt 0 ]; then
  306. # should contain no spaces
  307. if [[ "$new_expiry_months" == *" "* ]]; then
  308. return
  309. fi
  310. # should be a number
  311. re='^[0-9]+$'
  312. if ! [[ $new_expiry_months =~ $re ]] ; then
  313. return
  314. fi
  315. # set the new value
  316. PLEROMA_EXPIRE_MONTHS=$new_expiry_months
  317. write_config_param "PLEROMA_EXPIRE_MONTHS" "$PLEROMA_EXPIRE_MONTHS"
  318. # TODO
  319. dialog --title $"Set Pleroma post expiry period" \
  320. --msgbox $"Expiry period set to $PLEROMA_EXPIRE_MONTHS months" 6 60
  321. fi
  322. ;;
  323. esac
  324. rm $data
  325. }
  326. function pleroma_disable_registrations {
  327. dialog --title $"Disable new Pleroma user registrations" \
  328. --backtitle $"Freedombone Control Panel" \
  329. --yesno $"\nDo you wish to disable new registrations?" 10 60
  330. sel=$?
  331. case $sel in
  332. 0) sed -i 's|registrations_open:.*|registrations_open: false|g' $PLEROMA_DIR/config/config.exs
  333. sed -i 's|"registrationOpen":.*|"registrationOpen": false|g' $PLEROMA_DIR/priv/static/static/config.json
  334. ;;
  335. 1) sed -i 's|registrations_open:.*|registrations_open: true|g' $PLEROMA_DIR/config/config.exs
  336. sed -i 's|"registrationOpen":.*|"registrationOpen": true|g' $PLEROMA_DIR/priv/static/static/config.json
  337. ;;
  338. 255) return;;
  339. esac
  340. pleroma_recompile
  341. }
  342. function configure_interactive_pleroma {
  343. read_config_param PLEROMA_EXPIRE_MONTHS
  344. while true
  345. do
  346. data=$(tempfile 2>/dev/null)
  347. trap "rm -f $data" 0 1 2 5 15
  348. dialog --backtitle $"Freedombone Control Panel" \
  349. --title $"Pleroma" \
  350. --radiolist $"Choose an operation:" 14 70 5 \
  351. 1 $"Set a background image" off \
  352. 2 $"Set the title" off \
  353. 3 $"Disable new account registrations" off \
  354. 4 $"Set post expiry period (currently $PLEROMA_EXPIRE_MONTHS months)" off \
  355. 5 $"Exit" on 2> $data
  356. sel=$?
  357. case $sel in
  358. 1) return;;
  359. 255) return;;
  360. esac
  361. case $(cat $data) in
  362. 1) pleroma_set_background_image;;
  363. 2) pleroma_set_title;;
  364. 3) pleroma_disable_registrations;;
  365. 4) pleroma_set_expire_months;;
  366. 5) break;;
  367. esac
  368. rm $data
  369. done
  370. }
  371. function upgrade_pleroma {
  372. CURR_PLEROMA_COMMIT=$(get_completion_param "pleroma commit")
  373. if [[ "$CURR_PLEROMA_COMMIT" == "$PLEROMA_COMMIT" ]]; then
  374. return
  375. fi
  376. function_check set_repo_commit
  377. set_repo_commit $PLEROMA_DIR "pleroma commit" "$PLEROMA_COMMIT" $PLEROMA_REPO
  378. chown -R pleroma:pleroma $PLEROMA_DIR
  379. pleroma_recompile
  380. }
  381. function backup_local_pleroma {
  382. PLEROMA_DOMAIN_NAME='pleroma'
  383. if grep -q "pleroma domain" $COMPLETION_FILE; then
  384. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  385. fi
  386. function_check suspend_site
  387. suspend_site ${PLEROMA_DOMAIN_NAME}
  388. source_directory=$PLEROMA_DIR
  389. dest_directory=pleroma
  390. backup_directory_to_usb $source_directory $dest_directory
  391. USE_POSTGRESQL=1
  392. function_check backup_database_to_usb
  393. backup_database_to_usb pleroma
  394. function_check restart_site
  395. restart_site
  396. }
  397. function restore_local_pleroma {
  398. if ! grep -q "pleroma domain" $COMPLETION_FILE; then
  399. return
  400. fi
  401. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  402. if [ $PLEROMA_DOMAIN_NAME ]; then
  403. echo $"Restoring pleroma"
  404. temp_restore_dir=/root/temppleroma
  405. pleroma_dir=$PLEROMA_DIR
  406. function_check pleroma_create_database
  407. pleroma_create_database
  408. USE_POSTGRESQL=1
  409. restore_database pleroma
  410. if [ -d $temp_restore_dir ]; then
  411. rm -rf $temp_restore_dir
  412. fi
  413. function_check restore_directory_from_usb
  414. restore_directory_from_usb $temp_restore_dir pleroma
  415. if [ -d $temp_restore_dir ]; then
  416. chown -R pleroma:pleroma $pleroma_dir
  417. rm -rf $temp_restore_dir
  418. fi
  419. echo $"Restore of pleroma complete"
  420. fi
  421. }
  422. function backup_remote_pleroma {
  423. PLEROMA_DOMAIN_NAME='pleroma'
  424. if grep -q "pleroma domain" $COMPLETION_FILE; then
  425. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  426. fi
  427. function_check suspend_site
  428. suspend_site ${PLEROMA_DOMAIN_NAME}
  429. source_directory=$PLEROMA_DIR
  430. dest_directory=pleroma
  431. backup_directory_to_friend $source_directory $dest_directory
  432. USE_POSTGRESQL=1
  433. function_check backup_database_to_friend
  434. backup_database_to_friend pleroma
  435. function_check restart_site
  436. restart_site
  437. }
  438. function restore_remote_pleroma {
  439. if ! grep -q "pleroma domain" $COMPLETION_FILE; then
  440. return
  441. fi
  442. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  443. if [ $PLEROMA_DOMAIN_NAME ]; then
  444. echo $"Restoring pleroma"
  445. temp_restore_dir=/root/temppleroma
  446. pleroma_dir=$PLEROMA_DIR
  447. function_check pleroma_create_database
  448. pleroma_create_database
  449. USE_POSTGRESQL=1
  450. function_check restore_database_from_friend
  451. restore_database_from_friend pleroma
  452. if [ -d $temp_restore_dir ]; then
  453. rm -rf $temp_restore_dir
  454. fi
  455. function_check restore_directory_from_friend
  456. restore_directory_from_friend $temp_restore_dir pleroma
  457. if [ -d $temp_restore_dir ]; then
  458. chown -R pleroma:pleroma $pleroma_dir
  459. rm -rf $temp_restore_dir
  460. fi
  461. pleroma_update_after_restore pleroma ${PLEROMA_DOMAIN_NAME}
  462. echo $"Restore of pleroma complete"
  463. fi
  464. }
  465. function remove_pleroma {
  466. if [ ${#PLEROMA_DOMAIN_NAME} -eq 0 ]; then
  467. return
  468. fi
  469. systemctl stop pleroma
  470. systemctl disable pleroma
  471. rm /etc/systemd/system/pleroma.service
  472. userdel pleroma
  473. apt-get -yq remove esl-erlang elixir erlang-xmerl erlang-dev erlang-parsetools
  474. function_check remove_nodejs
  475. remove_nodejs pleroma-backend
  476. read_config_param "PLEROMA_DOMAIN_NAME"
  477. read_config_param "MY_USERNAME"
  478. echo "Removing $PLEROMA_DOMAIN_NAME"
  479. nginx_dissite $PLEROMA_DOMAIN_NAME
  480. remove_certs $PLEROMA_DOMAIN_NAME
  481. if [ -d /var/www/$PLEROMA_DOMAIN_NAME ]; then
  482. rm -rf /var/www/$PLEROMA_DOMAIN_NAME
  483. fi
  484. if [ -f /etc/nginx/sites-available/$PLEROMA_DOMAIN_NAME ]; then
  485. rm /etc/nginx/sites-available/$PLEROMA_DOMAIN_NAME
  486. fi
  487. if [ -d $PLEROMA_DIR ]; then
  488. rm -rf $PLEROMA_DIR
  489. fi
  490. function_check drop_database_postgresql
  491. drop_database_postgresql pleroma
  492. function_check remove_onion_service
  493. remove_onion_service pleroma ${PLEROMA_ONION_PORT}
  494. remove_app pleroma
  495. remove_completion_param install_pleroma
  496. sed -i '/pleroma domain/d' $COMPLETION_FILE
  497. sed -i '/pleroma commit/d' $COMPLETION_FILE
  498. function_check remove_ddns_domain
  499. remove_ddns_domain $PLEROMA_DOMAIN_NAME
  500. }
  501. function install_elixir {
  502. apt-get -yq install wget build-essential
  503. if [ ! -d $INSTALL_DIR ]; then
  504. mkdir -p $INSTALL_DIR
  505. fi
  506. cd $INSTALL_DIR
  507. erlang_package=erlang-solutions_1.0_all.deb
  508. wget https://packages.erlang-solutions.com/$erlang_package
  509. if [ ! -f $INSTALL_DIR/$erlang_package ]; then
  510. exit 72853
  511. fi
  512. dpkg -i $erlang_package
  513. apt-get -yq update
  514. apt-get -yq install esl-erlang
  515. apt-get -yq install elixir erlang-xmerl erlang-dev erlang-parsetools
  516. if [ ! -f /usr/local/bin/mix ]; then
  517. echo $'/usr/local/bin/mix not found after elixir installation'
  518. exit 629352
  519. fi
  520. }
  521. function install_pleroma {
  522. if [ ! $ONION_ONLY ]; then
  523. ONION_ONLY='no'
  524. fi
  525. # We need elixir 1.4+ here, so the debian repo package won't do
  526. install_elixir
  527. function_check install_nodejs
  528. install_nodejs pleroma-backend
  529. install_postgresql
  530. if [ ! -d /var/www/${PLEROMA_DOMAIN_NAME}/htdocs ]; then
  531. mkdir -p /var/www/${PLEROMA_DOMAIN_NAME}/htdocs
  532. fi
  533. if [ -d $PLEROMA_DIR ]; then
  534. rm -rf $PLEROMA_DIR
  535. fi
  536. # get the repo
  537. if [ -f /repos/pleroma/index.html ]; then
  538. mv /repos/pleroma /repos/pleroma-fe
  539. fi
  540. if [ -d /repos/pleroma ]; then
  541. mkdir -p $PLEROMA_DIR
  542. cp -r -p /repos/pleroma/. $PLEROMA_DIR
  543. cd $PLEROMA_DIR
  544. git pull
  545. else
  546. function_check git_clone
  547. git_clone $PLEROMA_REPO $PLEROMA_DIR
  548. fi
  549. if [ ! -d $PLEROMA_DIR ]; then
  550. echo $'Unable to clone pleroma backend repo'
  551. exit 783523
  552. fi
  553. # create user
  554. useradd -d $PLEROMA_DIR -s /bin/false pleroma
  555. # checkout the commit
  556. cd $PLEROMA_DIR
  557. git checkout $PLEROMA_COMMIT -b $PLEROMA_COMMIT
  558. set_completion_param "pleroma commit" "$PLEROMA_COMMIT"
  559. chown -R pleroma:pleroma $PLEROMA_DIR
  560. # web config
  561. function_check add_ddns_domain
  562. add_ddns_domain $PLEROMA_DOMAIN_NAME
  563. PLEROMA_ONION_HOSTNAME=$(add_onion_service pleroma 80 ${PLEROMA_ONION_PORT})
  564. pleroma_nginx_site=/etc/nginx/sites-available/$PLEROMA_DOMAIN_NAME
  565. if [[ $ONION_ONLY == "no" ]]; then
  566. function_check nginx_http_redirect
  567. nginx_http_redirect $PLEROMA_DOMAIN_NAME "index index.html"
  568. echo 'server {' >> $pleroma_nginx_site
  569. echo ' listen 443 ssl;' >> $pleroma_nginx_site
  570. echo ' listen [::]:443 ssl;' >> $pleroma_nginx_site
  571. echo " server_name $PLEROMA_DOMAIN_NAME;" >> $pleroma_nginx_site
  572. echo '' >> $pleroma_nginx_site
  573. function_check nginx_compress
  574. nginx_compress $PLEROMA_DOMAIN_NAME
  575. echo '' >> $pleroma_nginx_site
  576. echo ' # Security' >> $pleroma_nginx_site
  577. function_check nginx_ssl
  578. nginx_ssl $PLEROMA_DOMAIN_NAME
  579. function_check nginx_disable_sniffing
  580. nginx_disable_sniffing $PLEROMA_DOMAIN_NAME
  581. echo ' add_header Strict-Transport-Security max-age=15768000;' >> $pleroma_nginx_site
  582. echo '' >> $pleroma_nginx_site
  583. echo ' # Logs' >> $pleroma_nginx_site
  584. echo ' access_log /dev/null;' >> $pleroma_nginx_site
  585. echo ' error_log /dev/null;' >> $pleroma_nginx_site
  586. echo '' >> $pleroma_nginx_site
  587. echo " root $PLEROMA_DIR;" >> $pleroma_nginx_site
  588. echo '' >> $pleroma_nginx_site
  589. echo ' index index.html;' >> $pleroma_nginx_site
  590. echo ' location / {' >> $pleroma_nginx_site
  591. function_check nginx_limits
  592. nginx_limits $PLEROMA_DOMAIN_NAME '15m'
  593. echo " proxy_pass http://localhost:$PLEROMA_PORT;" >> $pleroma_nginx_site
  594. echo ' }' >> $pleroma_nginx_site
  595. echo ' # include snippets/well-known.conf;' >> $pleroma_nginx_site
  596. echo '}' >> $pleroma_nginx_site
  597. else
  598. echo -n '' > $pleroma_nginx_site
  599. fi
  600. echo 'server {' >> $pleroma_nginx_site
  601. echo " listen 127.0.0.1:$PLEROMA_ONION_PORT default_server;" >> $pleroma_nginx_site
  602. echo " server_name $PLEROMA_ONION_HOSTNAME;" >> $pleroma_nginx_site
  603. echo '' >> $pleroma_nginx_site
  604. function_check nginx_compress
  605. nginx_compress $PLEROMA_DOMAIN_NAME
  606. echo '' >> $pleroma_nginx_site
  607. function_check nginx_disable_sniffing
  608. nginx_disable_sniffing $PLEROMA_DOMAIN_NAME
  609. echo '' >> $pleroma_nginx_site
  610. echo ' # Logs' >> $pleroma_nginx_site
  611. echo ' access_log /dev/null;' >> $pleroma_nginx_site
  612. echo ' error_log /dev/null;' >> $pleroma_nginx_site
  613. echo '' >> $pleroma_nginx_site
  614. echo " root $PLEROMA_DIR;" >> $pleroma_nginx_site
  615. echo '' >> $pleroma_nginx_site
  616. echo ' index index.html;' >> $pleroma_nginx_site
  617. echo ' location / {' >> $pleroma_nginx_site
  618. function_check nginx_limits
  619. nginx_limits $PLEROMA_DOMAIN_NAME '15m'
  620. echo " proxy_pass http://localhost:$PLEROMA_PORT;" >> $pleroma_nginx_site
  621. echo ' }' >> $pleroma_nginx_site
  622. echo ' # include snippets/well-known.conf;' >> $pleroma_nginx_site
  623. echo '}' >> $pleroma_nginx_site
  624. # back end
  625. cd $PLEROMA_DIR
  626. chown -R pleroma:pleroma *
  627. sudo -u pleroma mix local.hex --force
  628. if [ ! "$?" = "0" ]; then
  629. echo $'mix local.hex failed'
  630. exit 1745673
  631. fi
  632. sudo -u pleroma mix deps.get --force
  633. if [ ! "$?" = "0" ]; then
  634. echo $'mix deps.get failed'
  635. exit 7325733
  636. fi
  637. function_check pleroma_create_database
  638. pleroma_create_database
  639. ${PROJECT_NAME}-pass -u $MY_USERNAME -a pleroma -p "$PLEROMA_ADMIN_PASSWORD"
  640. # NOTE: we don't need to install the frontend separately,
  641. # since the backend contains a precompiled version of it
  642. install_gnusocial_default_background "pleroma" "$PLEROMA_DOMAIN_NAME"
  643. if [ ! -f $PLEROMA_DIR/priv/static/static/config.json ]; then
  644. echo $"$PLEROMA_DIR/priv/static/static/config.json file missing"
  645. exit 323689
  646. fi
  647. sed -i 's|"theme":.*|"theme": "base16-summerfruit-dark.css",|g' $PLEROMA_DIR/priv/static/static/config.json
  648. if [ $PLEROMA_BACKGROUND_IMAGE_URL ]; then
  649. pleroma_set_background_image_from_url $PLEROMA_DIR/priv/static "$PLEROMA_DOMAIN_NAME" "$PLEROMA_BACKGROUND_IMAGE_URL" "$PLEROMA_TITLE"
  650. fi
  651. # Get certificate
  652. function_check create_site_certificate
  653. create_site_certificate $PLEROMA_DOMAIN_NAME 'yes'
  654. function_check nginx_ensite
  655. nginx_ensite $PLEROMA_DOMAIN_NAME
  656. systemctl restart postgresql
  657. systemctl restart nginx
  658. set_completion_param "pleroma domain" "$PLEROMA_DOMAIN_NAME"
  659. # daemon
  660. echo '[Unit]' > /etc/systemd/system/pleroma.service
  661. echo 'Description=Pleroma social network' >> /etc/systemd/system/pleroma.service
  662. echo 'After=network.target postgresql.service' >> /etc/systemd/system/pleroma.service
  663. echo '' >> /etc/systemd/system/pleroma.service
  664. echo '[Service]' >> /etc/systemd/system/pleroma.service
  665. echo 'User=pleroma' >> /etc/systemd/system/pleroma.service
  666. echo "WorkingDirectory=$PLEROMA_DIR" >> /etc/systemd/system/pleroma.service
  667. echo "Environment=\"HOME=$PLEROMA_DIR\"" >> /etc/systemd/system/pleroma.service
  668. echo 'ExecStart=/usr/local/bin/mix phx.server' >> /etc/systemd/system/pleroma.service
  669. echo 'ExecReload=/bin/kill $MAINPID' >> /etc/systemd/system/pleroma.service
  670. echo 'KillMode=process' >> /etc/systemd/system/pleroma.service
  671. echo 'Restart=on-failure' >> /etc/systemd/system/pleroma.service
  672. echo '' >> /etc/systemd/system/pleroma.service
  673. echo '[Install]' >> /etc/systemd/system/pleroma.service
  674. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/pleroma.service
  675. echo 'Alias=pleroma.service' >> /etc/systemd/system/pleroma.service
  676. systemctl daemon-reload
  677. systemctl enable pleroma
  678. systemctl start pleroma
  679. APP_INSTALLED=1
  680. }
  681. # NOTE: deliberately there is no "exit 0"