freedombone-app-pleroma 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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. add_postgresql_user pleroma "$PLEROMA_ADMIN_PASSWORD" encrypted
  179. run_system_query_postgresql "create database pleroma;"
  180. # temporarily allow the user to create databases
  181. run_system_query_postgresql "ALTER USER pleroma CREATEDB;"
  182. run_system_query_postgresql "ALTER USER pleroma SUPERUSER;"
  183. run_system_query_postgresql "GRANT ALL ON ALL tables IN SCHEMA public TO pleroma;"
  184. run_system_query_postgresql "GRANT ALL ON ALL sequences IN SCHEMA public TO pleroma;"
  185. run_system_query_postgresql "CREATE EXTENSION citext;"
  186. read_config_param "PLEROMA_SECRET_KEY"
  187. if [ ${#PLEROMA_SECRET_KEY} -lt 50 ]; then
  188. PLEROMA_SECRET_KEY="$(create_password 30)$(create_password 30)"
  189. if [ ${#PLEROMA_SECRET_KEY} -lt 50 ]; then
  190. run_system_query_postgresql "ALTER USER pleroma NOSUPERUSER;"
  191. run_system_query_postgresql "ALTER USER pleroma NOCREATEDB;"
  192. echo $'Pleroma secret key not created'
  193. exit 6782352
  194. fi
  195. write_config_param "PLEROMA_SECRET_KEY" "$PLEROMA_SECRET_KEY"
  196. fi
  197. pleroma_secret=config/dev.secret.exs
  198. cp config/dev.exs $pleroma_secret
  199. sed -i "s|username:.*|username: \"pleroma\",|g" $pleroma_secret
  200. sed -i "s|password:.*|password: \"$PLEROMA_ADMIN_PASSWORD\",|g" $pleroma_secret
  201. sed -i "s|database:.*|database: \"pleroma\",|g" $pleroma_secret
  202. sed -i "/Pleroma.Web.Endpoint/a secret_key_base: \"$PLEROMA_SECRET_KEY\"," $pleroma_secret
  203. sed -i 's|secret_key_base: | secret_key_base: |g' $pleroma_secret
  204. sed -i "/Pleroma.Web.Endpoint/a pubsub: [name: Pleroma.Web.PubSub, adapter: Phoenix.PubSub.PG2]," $pleroma_secret
  205. sed -i 's|pubsub: | pubsub: |g' $pleroma_secret
  206. sed -i 's|watchers: []|watchers: [],|g' $pleroma_secret
  207. sed -i "/watchers: []/a url: [host: \"$PLEROMA_DOMAIN_NAME\", scheme: \"https\", port: 443]" $pleroma_secret
  208. sed -i 's|url: | url: |g' $pleroma_secret
  209. cd $PLEROMA_DIR
  210. chown -R pleroma:pleroma *
  211. sudo -u pleroma mix local.rebar --force
  212. if [ ! "$?" = "0" ]; then
  213. run_system_query_postgresql "ALTER USER pleroma NOSUPERUSER;"
  214. run_system_query_postgresql "ALTER USER pleroma NOCREATEDB;"
  215. echo $'mix local.rebar failed'
  216. exit 73528562
  217. fi
  218. sudo -u pleroma mix local.hex --force
  219. sudo -u pleroma mix deps.compile mimerl
  220. sudo -u pleroma mix ecto.create --force
  221. if [ ! "$?" = "0" ]; then
  222. run_system_query_postgresql "ALTER USER pleroma NOSUPERUSER;"
  223. run_system_query_postgresql "ALTER USER pleroma NOCREATEDB;"
  224. echo $'mix ecto.create failed'
  225. exit 83653582
  226. fi
  227. sudo -u pleroma mix ecto.migrate --force
  228. if [ ! "$?" = "0" ]; then
  229. run_system_query_postgresql "ALTER USER pleroma NOSUPERUSER;"
  230. run_system_query_postgresql "ALTER USER pleroma NOCREATEDB;"
  231. echo $'mix ecto.migrate failed'
  232. exit 73752573
  233. fi
  234. # revoke the ability to create databases for this user
  235. run_system_query_postgresql "ALTER USER pleroma NOSUPERUSER;"
  236. run_system_query_postgresql "ALTER USER pleroma NOCREATEDB;"
  237. }
  238. function reconfigure_pleroma {
  239. echo -n ''
  240. }
  241. function pleroma_set_background_image {
  242. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  243. data=$(tempfile 2>/dev/null)
  244. trap "rm -f $data" 0 1 2 5 15
  245. dialog --title $"Pleroma" \
  246. --backtitle $"Freedombone Control Panel" \
  247. --inputbox $'Set a background image URL' 10 60 2>$data
  248. sel=$?
  249. case $sel in
  250. 0)
  251. temp_background=$(<$data)
  252. if [ ${#temp_background} -gt 0 ]; then
  253. PLEROMA_BACKGROUND_IMAGE_URL="$temp_background"
  254. write_config_param "PLEROMA_BACKGROUND_IMAGE_URL" "$PLEROMA_BACKGROUND_IMAGE_URL"
  255. if [[ $(pleroma_set_background_image_from_url $PLEROMA_DIR "$PLEROMA_DOMAIN_NAME" "$PLEROMA_BACKGROUND_IMAGE_URL" "$PLEROMA_TITLE" | tail -n 1) == "0" ]]; then
  256. pleroma_recompile
  257. dialog --title $"Set Pleroma login background" \
  258. --msgbox $"The background image has been set" 6 60
  259. fi
  260. fi
  261. ;;
  262. esac
  263. rm $data
  264. }
  265. function pleroma_set_title {
  266. data=$(tempfile 2>/dev/null)
  267. trap "rm -f $data" 0 1 2 5 15
  268. dialog --title $"Pleroma" \
  269. --backtitle $"Freedombone Control Panel" \
  270. --inputbox $'Set a title' 10 60 2>$data
  271. sel=$?
  272. case $sel in
  273. 0)
  274. new_title=$(<$data)
  275. if [ ${#new_title} -gt 0 ]; then
  276. PLEROMA_TITLE="$new_title"
  277. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  278. write_config_param "PLEROMA_TITLE" "$PLEROMA_TITLE"
  279. sed -i "s|\"name\":.*|\"name\": \"${PLEROMA_TITLE}\",|g" $PLEROMA_DIR/static/config.json
  280. sed -i "s|\"name\":.*|\"name\": \"${PLEROMA_TITLE}\",|g" $PLEROMA_DIR/priv/static/static/config.json
  281. sed -i "s|name: .*|name: \"${PLEROMA_TITLE}\",|g" $PLEROMA_DIR/config/config.exs
  282. systemctl restart pleroma
  283. dialog --title $"Set Pleroma title" \
  284. --msgbox $"The title has been set" 6 60
  285. fi
  286. ;;
  287. esac
  288. rm $data
  289. }
  290. function pleroma_set_expire_months {
  291. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  292. read_config_param "PLEROMA_EXPIRE_MONTHS"
  293. data=$(tempfile 2>/dev/null)
  294. trap "rm -f $data" 0 1 2 5 15
  295. dialog --title $"Pleroma" \
  296. --backtitle $"Freedombone Control Panel" \
  297. --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
  298. sel=$?
  299. case $sel in
  300. 0)
  301. new_expiry_months=$(<$data)
  302. if [ ${#new_expiry_months} -gt 0 ]; then
  303. # should contain no spaces
  304. if [[ "$new_expiry_months" == *" "* ]]; then
  305. return
  306. fi
  307. # should be a number
  308. re='^[0-9]+$'
  309. if ! [[ $new_expiry_months =~ $re ]] ; then
  310. return
  311. fi
  312. # set the new value
  313. PLEROMA_EXPIRE_MONTHS=$new_expiry_months
  314. write_config_param "PLEROMA_EXPIRE_MONTHS" "$PLEROMA_EXPIRE_MONTHS"
  315. # TODO
  316. dialog --title $"Set Pleroma post expiry period" \
  317. --msgbox $"Expiry period set to $PLEROMA_EXPIRE_MONTHS months" 6 60
  318. fi
  319. ;;
  320. esac
  321. rm $data
  322. }
  323. function pleroma_disable_registrations {
  324. dialog --title $"Disable new Pleroma user registrations" \
  325. --backtitle $"Freedombone Control Panel" \
  326. --yesno $"\nDo you wish to disable new registrations?" 10 60
  327. sel=$?
  328. case $sel in
  329. 0) sed -i 's|registrations_open:.*|registrations_open: false|g' $PLEROMA_DIR/config/config.exs
  330. sed -i 's|"registrationOpen":.*|"registrationOpen": false|g' $PLEROMA_DIR/priv/static/static/config.json
  331. ;;
  332. 1) sed -i 's|registrations_open:.*|registrations_open: true|g' $PLEROMA_DIR/config/config.exs
  333. sed -i 's|"registrationOpen":.*|"registrationOpen": true|g' $PLEROMA_DIR/priv/static/static/config.json
  334. ;;
  335. 255) return;;
  336. esac
  337. pleroma_recompile
  338. }
  339. function configure_interactive_pleroma {
  340. read_config_param PLEROMA_EXPIRE_MONTHS
  341. while true
  342. do
  343. data=$(tempfile 2>/dev/null)
  344. trap "rm -f $data" 0 1 2 5 15
  345. dialog --backtitle $"Freedombone Control Panel" \
  346. --title $"Pleroma" \
  347. --radiolist $"Choose an operation:" 14 70 5 \
  348. 1 $"Set a background image" off \
  349. 2 $"Set the title" off \
  350. 3 $"Disable new account registrations" off \
  351. 4 $"Set post expiry period (currently $PLEROMA_EXPIRE_MONTHS months)" off \
  352. 5 $"Exit" on 2> $data
  353. sel=$?
  354. case $sel in
  355. 1) return;;
  356. 255) return;;
  357. esac
  358. case $(cat $data) in
  359. 1) pleroma_set_background_image;;
  360. 2) pleroma_set_title;;
  361. 3) pleroma_disable_registrations;;
  362. 4) pleroma_set_expire_months;;
  363. 5) break;;
  364. esac
  365. rm $data
  366. done
  367. }
  368. function upgrade_pleroma {
  369. CURR_PLEROMA_COMMIT=$(get_completion_param "pleroma commit")
  370. if [[ "$CURR_PLEROMA_COMMIT" == "$PLEROMA_COMMIT" ]]; then
  371. return
  372. fi
  373. function_check set_repo_commit
  374. set_repo_commit $PLEROMA_DIR "pleroma commit" "$PLEROMA_COMMIT" $PLEROMA_REPO
  375. chown -R pleroma:pleroma $PLEROMA_DIR
  376. pleroma_recompile
  377. }
  378. function backup_local_pleroma {
  379. PLEROMA_DOMAIN_NAME='pleroma'
  380. if grep -q "pleroma domain" $COMPLETION_FILE; then
  381. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  382. fi
  383. function_check suspend_site
  384. suspend_site ${PLEROMA_DOMAIN_NAME}
  385. source_directory=$PLEROMA_DIR
  386. dest_directory=pleroma
  387. backup_directory_to_usb $source_directory $dest_directory
  388. USE_POSTGRESQL=1
  389. function_check backup_database_to_usb
  390. backup_database_to_usb pleroma
  391. function_check restart_site
  392. restart_site
  393. }
  394. function restore_local_pleroma {
  395. if ! grep -q "pleroma domain" $COMPLETION_FILE; then
  396. return
  397. fi
  398. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  399. if [ $PLEROMA_DOMAIN_NAME ]; then
  400. echo $"Restoring pleroma"
  401. temp_restore_dir=/root/temppleroma
  402. pleroma_dir=$PLEROMA_DIR
  403. function_check pleroma_create_database
  404. pleroma_create_database
  405. USE_POSTGRESQL=1
  406. restore_database pleroma
  407. if [ -d $temp_restore_dir ]; then
  408. rm -rf $temp_restore_dir
  409. fi
  410. function_check restore_directory_from_usb
  411. restore_directory_from_usb $temp_restore_dir pleroma
  412. if [ -d $temp_restore_dir ]; then
  413. chown -R pleroma:pleroma $pleroma_dir
  414. rm -rf $temp_restore_dir
  415. fi
  416. echo $"Restore of pleroma complete"
  417. fi
  418. }
  419. function backup_remote_pleroma {
  420. PLEROMA_DOMAIN_NAME='pleroma'
  421. if grep -q "pleroma domain" $COMPLETION_FILE; then
  422. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  423. fi
  424. function_check suspend_site
  425. suspend_site ${PLEROMA_DOMAIN_NAME}
  426. source_directory=$PLEROMA_DIR
  427. dest_directory=pleroma
  428. backup_directory_to_friend $source_directory $dest_directory
  429. USE_POSTGRESQL=1
  430. function_check backup_database_to_friend
  431. backup_database_to_friend pleroma
  432. function_check restart_site
  433. restart_site
  434. }
  435. function restore_remote_pleroma {
  436. if ! grep -q "pleroma domain" $COMPLETION_FILE; then
  437. return
  438. fi
  439. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  440. if [ $PLEROMA_DOMAIN_NAME ]; then
  441. echo $"Restoring pleroma"
  442. temp_restore_dir=/root/temppleroma
  443. pleroma_dir=$PLEROMA_DIR
  444. function_check pleroma_create_database
  445. pleroma_create_database
  446. USE_POSTGRESQL=1
  447. function_check restore_database_from_friend
  448. restore_database_from_friend pleroma
  449. if [ -d $temp_restore_dir ]; then
  450. rm -rf $temp_restore_dir
  451. fi
  452. function_check restore_directory_from_friend
  453. restore_directory_from_friend $temp_restore_dir pleroma
  454. if [ -d $temp_restore_dir ]; then
  455. chown -R pleroma:pleroma $pleroma_dir
  456. rm -rf $temp_restore_dir
  457. fi
  458. pleroma_update_after_restore pleroma ${PLEROMA_DOMAIN_NAME}
  459. echo $"Restore of pleroma complete"
  460. fi
  461. }
  462. function remove_pleroma {
  463. if [ ${#PLEROMA_DOMAIN_NAME} -eq 0 ]; then
  464. return
  465. fi
  466. systemctl stop pleroma
  467. systemctl disable pleroma
  468. rm /etc/systemd/system/pleroma.service
  469. userdel pleroma
  470. apt-get -yq remove esl-erlang elixir
  471. function_check remove_nodejs
  472. remove_nodejs pleroma-backend
  473. read_config_param "PLEROMA_DOMAIN_NAME"
  474. read_config_param "MY_USERNAME"
  475. echo "Removing $PLEROMA_DOMAIN_NAME"
  476. nginx_dissite $PLEROMA_DOMAIN_NAME
  477. remove_certs $PLEROMA_DOMAIN_NAME
  478. if [ -d /var/www/$PLEROMA_DOMAIN_NAME ]; then
  479. rm -rf /var/www/$PLEROMA_DOMAIN_NAME
  480. fi
  481. if [ -f /etc/nginx/sites-available/$PLEROMA_DOMAIN_NAME ]; then
  482. rm /etc/nginx/sites-available/$PLEROMA_DOMAIN_NAME
  483. fi
  484. if [ -d $PLEROMA_DIR ]; then
  485. rm -rf $PLEROMA_DIR
  486. fi
  487. function_check drop_database_postgresql
  488. drop_database_postgresql
  489. function_check remove_onion_service
  490. remove_onion_service pleroma ${PLEROMA_ONION_PORT}
  491. remove_app pleroma
  492. remove_completion_param install_pleroma
  493. sed -i '/pleroma domain/d' $COMPLETION_FILE
  494. sed -i '/pleroma commit/d' $COMPLETION_FILE
  495. function_check remove_ddns_domain
  496. remove_ddns_domain $PLEROMA_DOMAIN_NAME
  497. }
  498. function install_elixir {
  499. apt-get -yq install wget build-essential
  500. if [ ! -d $INSTALL_DIR ]; then
  501. mkdir -p $INSTALL_DIR
  502. fi
  503. cd $INSTALL_DIR
  504. erlang_package=erlang-solutions_1.0_all.deb
  505. wget https://packages.erlang-solutions.com/$erlang_package
  506. if [ ! -f $INSTALL_DIR/$erlang_package ]; then
  507. exit 72853
  508. fi
  509. dpkg -i $erlang_package
  510. apt-get -yq update
  511. apt-get -yq install esl-erlang
  512. apt-get -yq install elixir erlang-xmerl erlang-dev erlang-parsetools
  513. if [ ! -f /usr/local/bin/mix ]; then
  514. echo $'/usr/local/bin/mix not found after elixir installation'
  515. exit 629352
  516. fi
  517. }
  518. function install_pleroma {
  519. if [ ! $ONION_ONLY ]; then
  520. ONION_ONLY='no'
  521. fi
  522. # We need elixir 1.4+ here, so the debian repo package won't do
  523. install_elixir
  524. function_check install_nodejs
  525. install_nodejs pleroma-backend
  526. install_postgresql
  527. if [ ! -d /var/www/${PLEROMA_DOMAIN_NAME}/htdocs ]; then
  528. mkdir -p /var/www/${PLEROMA_DOMAIN_NAME}/htdocs
  529. fi
  530. if [ -d $PLEROMA_DIR ]; then
  531. rm -rf $PLEROMA_DIR
  532. fi
  533. # get the repo
  534. if [ -f /repos/pleroma/index.html ]; then
  535. mv /repos/pleroma /repos/pleroma-fe
  536. fi
  537. if [ -d /repos/pleroma ]; then
  538. mkdir -p $PLEROMA_DIR
  539. cp -r -p /repos/pleroma/. $PLEROMA_DIR
  540. cd $PLEROMA_DIR
  541. git pull
  542. else
  543. function_check git_clone
  544. git_clone $PLEROMA_REPO $PLEROMA_DIR
  545. fi
  546. if [ ! -d $PLEROMA_DIR ]; then
  547. echo $'Unable to clone pleroma backend repo'
  548. exit 783523
  549. fi
  550. # create user
  551. useradd -d $PLEROMA_DIR -s /bin/false pleroma
  552. # checkout the commit
  553. cd $PLEROMA_DIR
  554. git checkout $PLEROMA_COMMIT -b $PLEROMA_COMMIT
  555. set_completion_param "pleroma commit" "$PLEROMA_COMMIT"
  556. chown -R pleroma:pleroma $PLEROMA_DIR
  557. # web config
  558. function_check add_ddns_domain
  559. add_ddns_domain $PLEROMA_DOMAIN_NAME
  560. PLEROMA_ONION_HOSTNAME=$(add_onion_service pleroma 80 ${PLEROMA_ONION_PORT})
  561. pleroma_nginx_site=/etc/nginx/sites-available/$PLEROMA_DOMAIN_NAME
  562. if [[ $ONION_ONLY == "no" ]]; then
  563. function_check nginx_http_redirect
  564. nginx_http_redirect $PLEROMA_DOMAIN_NAME "index index.html"
  565. echo 'server {' >> $pleroma_nginx_site
  566. echo ' listen 443 ssl;' >> $pleroma_nginx_site
  567. echo ' listen [::]:443 ssl;' >> $pleroma_nginx_site
  568. echo " server_name $PLEROMA_DOMAIN_NAME;" >> $pleroma_nginx_site
  569. echo '' >> $pleroma_nginx_site
  570. function_check nginx_compress
  571. nginx_compress $PLEROMA_DOMAIN_NAME
  572. echo '' >> $pleroma_nginx_site
  573. echo ' # Security' >> $pleroma_nginx_site
  574. function_check nginx_ssl
  575. nginx_ssl $PLEROMA_DOMAIN_NAME
  576. function_check nginx_disable_sniffing
  577. nginx_disable_sniffing $PLEROMA_DOMAIN_NAME
  578. echo ' add_header Strict-Transport-Security max-age=15768000;' >> $pleroma_nginx_site
  579. echo '' >> $pleroma_nginx_site
  580. echo ' # Logs' >> $pleroma_nginx_site
  581. echo ' access_log /dev/null;' >> $pleroma_nginx_site
  582. echo ' error_log /dev/null;' >> $pleroma_nginx_site
  583. echo '' >> $pleroma_nginx_site
  584. echo " root $PLEROMA_DIR;" >> $pleroma_nginx_site
  585. echo '' >> $pleroma_nginx_site
  586. echo ' index index.html;' >> $pleroma_nginx_site
  587. echo ' location / {' >> $pleroma_nginx_site
  588. function_check nginx_limits
  589. nginx_limits $PLEROMA_DOMAIN_NAME '15m'
  590. echo " proxy_pass http://localhost:$PLEROMA_PORT;" >> $pleroma_nginx_site
  591. echo ' }' >> $pleroma_nginx_site
  592. echo ' # include snippets/well-known.conf;' >> $pleroma_nginx_site
  593. echo '}' >> $pleroma_nginx_site
  594. else
  595. echo -n '' > $pleroma_nginx_site
  596. fi
  597. echo 'server {' >> $pleroma_nginx_site
  598. echo " listen 127.0.0.1:$PLEROMA_ONION_PORT default_server;" >> $pleroma_nginx_site
  599. echo " server_name $PLEROMA_ONION_HOSTNAME;" >> $pleroma_nginx_site
  600. echo '' >> $pleroma_nginx_site
  601. function_check nginx_compress
  602. nginx_compress $PLEROMA_DOMAIN_NAME
  603. echo '' >> $pleroma_nginx_site
  604. function_check nginx_disable_sniffing
  605. nginx_disable_sniffing $PLEROMA_DOMAIN_NAME
  606. echo '' >> $pleroma_nginx_site
  607. echo ' # Logs' >> $pleroma_nginx_site
  608. echo ' access_log /dev/null;' >> $pleroma_nginx_site
  609. echo ' error_log /dev/null;' >> $pleroma_nginx_site
  610. echo '' >> $pleroma_nginx_site
  611. echo " root $PLEROMA_DIR;" >> $pleroma_nginx_site
  612. echo '' >> $pleroma_nginx_site
  613. echo ' index index.html;' >> $pleroma_nginx_site
  614. echo ' location / {' >> $pleroma_nginx_site
  615. function_check nginx_limits
  616. nginx_limits $PLEROMA_DOMAIN_NAME '15m'
  617. echo " proxy_pass http://localhost:$PLEROMA_PORT;" >> $pleroma_nginx_site
  618. echo ' }' >> $pleroma_nginx_site
  619. echo ' # include snippets/well-known.conf;' >> $pleroma_nginx_site
  620. echo '}' >> $pleroma_nginx_site
  621. # back end
  622. cd $PLEROMA_DIR
  623. chown -R pleroma:pleroma *
  624. sudo -u pleroma mix local.hex --force
  625. if [ ! "$?" = "0" ]; then
  626. echo $'mix local.hex failed'
  627. exit 1745673
  628. fi
  629. sudo -u pleroma mix deps.get --force
  630. if [ ! "$?" = "0" ]; then
  631. echo $'mix deps.get failed'
  632. exit 7325733
  633. fi
  634. function_check pleroma_create_database
  635. pleroma_create_database
  636. ${PROJECT_NAME}-pass -u $MY_USERNAME -a pleroma -p "$PLEROMA_ADMIN_PASSWORD"
  637. # front end
  638. install_pleroma_front_end "pleroma" "$PLEROMA_DOMAIN_NAME" "$PLEROMA_BACKGROUND_IMAGE_URL" "$PLEROMA_TITLE"
  639. install_gnusocial_default_background "gnusocial" "$PLEROMA_DOMAIN_NAME"
  640. if [ ! -f $PLEROMA_DIR/static/config.json ]; then
  641. echo $"$PLEROMA_DIR/static/config.json file missing"
  642. exit 323689
  643. fi
  644. sed -i 's|"theme":.*|"theme": "base16-apathy.css",|g' $PLEROMA_DIR/static/config.json
  645. if [ $PLEROMA_BACKGROUND_IMAGE_URL ]; then
  646. pleroma_set_background_image_from_url $PLEROMA_DIR "$PLEROMA_DOMAIN_NAME" "$PLEROMA_BACKGROUND_IMAGE_URL" "$PLEROMA_TITLE"
  647. fi
  648. if [ ! -f $PLEROMA_DIR/index.html ]; then
  649. echo $'Pleroma index file missing'
  650. exit 7825393
  651. fi
  652. # Get certificate
  653. function_check create_site_certificate
  654. create_site_certificate $PLEROMA_DOMAIN_NAME 'yes'
  655. function_check nginx_ensite
  656. nginx_ensite $PLEROMA_DOMAIN_NAME
  657. systemctl restart postgresql
  658. systemctl restart nginx
  659. set_completion_param "pleroma domain" "$PLEROMA_DOMAIN_NAME"
  660. # daemon
  661. echo '[Unit]' > /etc/systemd/system/pleroma.service
  662. echo 'Description=Pleroma social network' >> /etc/systemd/system/pleroma.service
  663. echo 'After=network.target postgresql.service' >> /etc/systemd/system/pleroma.service
  664. echo '' >> /etc/systemd/system/pleroma.service
  665. echo '[Service]' >> /etc/systemd/system/pleroma.service
  666. echo 'User=pleroma' >> /etc/systemd/system/pleroma.service
  667. echo "WorkingDirectory=$PLEROMA_DIR" >> /etc/systemd/system/pleroma.service
  668. echo "Environment=\"HOME=$PLEROMA_DIR\"" >> /etc/systemd/system/pleroma.service
  669. echo 'ExecStart=/usr/local/bin/mix phx.server' >> /etc/systemd/system/pleroma.service
  670. echo 'ExecReload=/bin/kill $MAINPID' >> /etc/systemd/system/pleroma.service
  671. echo 'KillMode=process' >> /etc/systemd/system/pleroma.service
  672. echo 'Restart=on-failure' >> /etc/systemd/system/pleroma.service
  673. echo '' >> /etc/systemd/system/pleroma.service
  674. echo '[Install]' >> /etc/systemd/system/pleroma.service
  675. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/pleroma.service
  676. echo 'Alias=pleroma.service' >> /etc/systemd/system/pleroma.service
  677. systemctl daemon-reload
  678. systemctl enable pleroma
  679. systemctl start pleroma
  680. APP_INSTALLED=1
  681. }
  682. # NOTE: deliberately there is no "exit 0"