freedombone-app-pleroma 44KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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. # Show stopper: This is dependent on https://placehold.it for avatar images,
  15. # so at present it's not usable until a first party placeholder image system
  16. # is included.
  17. #
  18. # There is also a possible issue with the chat system which uses an object called
  19. # "Agent" which may not be supported with the version of elixir within the
  20. # Debian package. This only applies if you're installing from the latest commit.
  21. #
  22. # License
  23. # =======
  24. #
  25. # Copyright (C) 2017-2018 Bob Mottram <bob@freedombone.net>
  26. #
  27. # This program is free software: you can redistribute it and/or modify
  28. # it under the terms of the GNU Affero General Public License as published by
  29. # the Free Software Foundation, either version 3 of the License, or
  30. # (at your option) any later version.
  31. #
  32. # This program is distributed in the hope that it will be useful,
  33. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  34. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35. # GNU Affero General Public License for more details.
  36. #
  37. # You should have received a copy of the GNU Affero General Public License
  38. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  39. VARIANTS='full full-vim social'
  40. IN_DEFAULT_INSTALL=0
  41. SHOW_ON_ABOUT=1
  42. PLEROMA_DOMAIN_NAME=
  43. PLEROMA_CODE=
  44. PLEROMA_PORT=4000
  45. PLEROMA_ONION_PORT=8011
  46. PLEROMA_REPO="https://git.pleroma.social/pleroma/pleroma.git"
  47. PLEROMA_COMMIT='b0d242cd9220c22c0af7c153182a51d8e40d34ba'
  48. PLEROMA_ADMIN_PASSWORD=
  49. PLEROMA_DIR=/etc/pleroma
  50. PLEROMA_SECRET_KEY=""
  51. PLEROMA_BACKGROUND_IMAGE_URL=
  52. PLEROMA_TITLE='Pleroma Server'
  53. # Number of months after which posts expire
  54. PLEROMA_EXPIRE_MONTHS=3
  55. pleroma_expire_posts_script=/usr/bin/pleroma-expire-posts
  56. blocking_script_file=/usr/bin/pleroma-blocking
  57. pleroma_variables=(ONION_ONLY
  58. PLEROMA_DOMAIN_NAME
  59. PLEROMA_CODE
  60. PLEROMA_WELCOME_MESSAGE
  61. PLEROMA_BACKGROUND_IMAGE_URL
  62. DDNS_PROVIDER
  63. PLEROMA_TITLE
  64. PLEROMA_EXPIRE_MONTHS
  65. MY_EMAIL_ADDRESS
  66. MY_USERNAME)
  67. function create_pleroma_blocklist {
  68. { echo '#!/bin/bash';
  69. echo "if [ ! -f /root/${PROJECT_NAME}-firewall-domains.cfg ]; then";
  70. echo ' exit 0';
  71. echo 'fi';
  72. echo 'if [ -f /tmp/pleroma-blocking.lock ]; then';
  73. echo ' cd /tmp';
  74. echo ' find ./pleroma*.lock -type f -mmin +5 -exec rm {} \;';
  75. echo ' if [ -f /tmp/pleroma-blocking.lock ]; then';
  76. echo ' exit 0';
  77. echo ' fi';
  78. echo 'fi';
  79. echo 'touch /tmp/pleroma-blocking.lock';
  80. echo 'objects_query="DELETE FROM objects WHERE"';
  81. echo 'objects_updated=';
  82. echo 'users_query="DELETE FROM users WHERE"';
  83. echo 'websub_server_subscriptions_query="DELETE FROM websub_server_subscriptions WHERE"';
  84. echo 'websub_server_subscriptions_updated=';
  85. echo 'while read blocked; do';
  86. echo " if [[ \"\$blocked\" == *\".\"* || \"\$blocked\" == *\"@\"* ]]; then";
  87. echo " if [ \${#blocked} -gt 4 ]; then";
  88. echo " if [ \$objects_updated ]; then";
  89. echo " objects_query=\"\${objects_query} or\"";
  90. echo " users_query=\"\${users_query} or\"";
  91. echo ' fi';
  92. echo " objects_query=\"\${objects_query} data->>'content' ilike '%\${blocked}%' or data->>'actor' ilike '%\${blocked}%' or data->>'to' ilike '%\${blocked}%' or data->>'id' ilike '%\${blocked}%' or data->>'external_url' ilike '%\${blocked}%'\"";
  93. echo " users_query=\"\${users_query} nickname ilike '%\${blocked}%'\"";
  94. echo ' objects_updated=1';
  95. echo " if [[ \"\$blocked\" != *\"@\"* ]]; then";
  96. echo " if ! grep -q \"127.0.0.1 \$blocked\" /etc/hosts; then";
  97. echo " echo \"127.0.0.1 \$blocked\" >> /etc/hosts";
  98. echo ' fi';
  99. echo " if [ \$websub_server_subscriptions_updated ]; then";
  100. echo " websub_server_subscriptions_query=\"\${websub_server_subscriptions_query} or\"";
  101. echo ' fi';
  102. echo " websub_server_subscriptions_query=\"\${websub_server_subscriptions_query} callback like '%\${blocked}%'\"";
  103. echo ' websub_server_subscriptions_updated=1';
  104. echo ' fi';
  105. echo ' fi';
  106. echo ' fi';
  107. echo 'done </root/freedombone-firewall-domains.cfg';
  108. echo '';
  109. echo 'cd /etc/postgresql';
  110. echo "if [ \$objects_updated ]; then";
  111. echo " sudo -u postgres psql -d pleroma -c \"\$objects_query\"";
  112. echo " sudo -u postgres psql -d pleroma -c \"\$users_query\"";
  113. echo 'fi';
  114. echo "if [ \$websub_server_subscriptions_updated ]; then";
  115. echo " sudo -u postgres psql -d pleroma -c \"\$websub_server_subscriptions_query\"";
  116. echo 'fi';
  117. echo 'rm /tmp/pleroma-blocking.lock'; } > $blocking_script_file
  118. chmod +x $blocking_script_file
  119. if ! grep -q "$blocking_script_file" /etc/crontab; then
  120. cron_add_mins 1 "$blocking_script_file 2> /dev/null"
  121. fi
  122. }
  123. function expire_pleroma_posts {
  124. domain_name=$1
  125. expire_months=$3
  126. if [ ! "$expire_months" ]; then
  127. expire_months=3
  128. fi
  129. #expire_days=$((expire_months * 30))
  130. # files are what take up most of the backup time, so don't keep them for very long
  131. expire_days_files=7
  132. # To prevent the database size from growing endlessly this script expires posts
  133. # after a number of months
  134. if [ ! -d /etc/pleroma ]; then
  135. return
  136. fi
  137. { echo '#!/bin/bash';
  138. echo "plmonths=\"$PLEROMA_EXPIRE_MONTHS\"";
  139. echo "if [ \${#plmonths} -eq 0 ]; then";
  140. echo ' exit 1';
  141. echo 'fi';
  142. echo "if [[ \"\$plmonths\" == \"0\" ]]; then";
  143. echo ' exit 2';
  144. echo 'fi';
  145. echo "oldate=\$(date +%Y-%m-%d --date=\"\$plmonths months ago\")";
  146. echo 'cd /etc/postgresql';
  147. echo "sudo -u postgres psql -d pleroma -c \"DELETE FROM notifications WHERE inserted_at <= '\$oldate 01:01:01'\"";
  148. echo "sudo -u postgres psql -d pleroma -c \"DELETE FROM objects WHERE inserted_at <= '\$oldate 01:01:01'\""; } > $pleroma_expire_posts_script
  149. chmod +x $pleroma_expire_posts_script
  150. pleroma_expire_script=/etc/cron.daily/pleroma-expire
  151. expire_days_files_threshold=$((expire_days_files - 1))
  152. { echo '#!/bin/bash';
  153. echo 'cd /etc/postgresql';
  154. echo 'if [ -d /etc/pleroma/tempfiles ]; then';
  155. echo ' rm -rf /etc/pleroma/tempfiles';
  156. echo 'fi';
  157. echo '';
  158. echo '# make directory to temporarily store local avatars';
  159. echo 'mkdir /etc/pleroma/tempfiles';
  160. echo '';
  161. echo '# get the local avatar files';
  162. echo "avatars=\$(sudo -u postgres psql -d pleroma -c \"select avatar->>'url' from users where avatar->>'url' like '%${domain_name}%'\")";
  163. echo '';
  164. echo '# copy the avatar files to a temporary directory';
  165. echo "arr=( \$avatars )";
  166. echo "for i in \${arr[@]}; do";
  167. echo " if [[ \"\$i\" == *'/media/'* ]]; then";
  168. echo " imagefile=/etc/pleroma/uploads/\$(echo \$i | sed 's|\"||g' | sed 's|,||g' | awk -F '/media/' '{print \$2}');";
  169. echo " if [ -f \$imagefile ]; then";
  170. echo " imagedir=/etc/pleroma/uploads/\$(echo \$i | sed 's|\"||g' | sed 's|,||g' | awk -F '/media/' '{print \$2}' | awk -F '/' '{print \$1}')";
  171. echo " nowdate=\$(date +%s)";
  172. echo " sinceepoch=\$(date +%s -r \$imagefile)";
  173. echo " daysold=\$(((\$nowdate - \$sinceepoch) / 86400))";
  174. echo " if [ \$daysold -ge ${expire_days_files_threshold} ]; then";
  175. echo " if [ ! -d \$imagedir ]; then";
  176. echo " mkdir \$imagedir";
  177. echo ' fi';
  178. echo " cp -rp \$imagefile \$imagedir";
  179. echo ' fi';
  180. echo ' fi';
  181. echo ' fi';
  182. echo 'done';
  183. echo '';
  184. echo '# get the local banner files';
  185. echo "banners=\$(sudo -u postgres psql -d pleroma -c \"select avatar->>'banner' from users where avatar->>'url' like '%${domain_name}%'\")";
  186. echo '';
  187. echo '# copy the banner files to a temporary directory';
  188. echo "arr=( \$banners )";
  189. echo "for i in \${arr[@]}; do";
  190. echo " if [[ \"\$i\" == *'/media/'* ]]; then";
  191. echo " imagefile=/etc/pleroma/uploads/\$(echo \$i | sed 's|\"||g' | sed 's|,||g' | awk -F '/media/' '{print \$2}');";
  192. echo " if [ -f \$imagefile ]; then";
  193. echo " imagedir=/etc/pleroma/uploads/\$(echo \$i | sed 's|\"||g' | sed 's|,||g' | awk -F '/media/' '{print \$2}' | awk -F '/' '{print \$1}')";
  194. echo " nowdate=\$(date +%s)";
  195. echo " sinceepoch=\$(date +%s -r \$imagefile)";
  196. echo " daysold=\$(((\$nowdate - \$sinceepoch) / 86400))";
  197. echo " if [ \$daysold -ge ${expire_days_files_threshold} ]; then";
  198. echo " if [ ! -d \$imagedir ]; then";
  199. echo " mkdir \$imagedir";
  200. echo ' fi';
  201. echo " cp -rp \$imagefile \$imagedir";
  202. echo ' fi';
  203. echo ' fi';
  204. echo ' fi';
  205. echo 'done';
  206. echo '';
  207. echo '# delete old files';
  208. echo "find /etc/pleroma/uploads/* -mtime +${expire_days_files} -exec rm -rf {} +";
  209. echo '';
  210. echo '# move avatar files back to uploads';
  211. echo 'chown -R pleroma:pleroma /etc/pleroma/tempfiles';
  212. echo 'mv /etc/pleroma/tempfiles/* /etc/pleroma/uploads';
  213. echo 'rm -rf /etc/pleroma/tempfiles';
  214. echo '';
  215. echo '/usr/bin/pleroma-expire-posts 2> /dev/null'; } > $pleroma_expire_script
  216. chmod +x $pleroma_expire_script
  217. # remove any old cron job
  218. if grep -q "pleroma-expire" /etc/crontab; then
  219. sed -i "/pleroma-expire/d" /etc/crontab
  220. rm /usr/bin/pleroma-expire
  221. fi
  222. # remove old expire script
  223. if [ -f /etc/cron.weekly/clear-pleroma-database ]; then
  224. rm /etc/cron.weekly/clear-pleroma-database
  225. fi
  226. }
  227. function pleroma_recompile {
  228. # necessary after parameter changes
  229. chown -R pleroma:pleroma $PLEROMA_DIR
  230. sudo -u pleroma mix clean
  231. sudo -u pleroma mix deps.compile
  232. sudo -u pleroma mix compile
  233. if [ -f /etc/systemd/system/pleroma.service ]; then
  234. systemctl restart pleroma
  235. fi
  236. }
  237. function logging_on_pleroma {
  238. echo -n ''
  239. }
  240. function logging_off_pleroma {
  241. echo -n ''
  242. }
  243. function remove_user_pleroma {
  244. remove_username="$1"
  245. "${PROJECT_NAME}-pass" -u "$remove_username" --rmapp pleroma
  246. }
  247. function add_user_pleroma {
  248. new_username="$1"
  249. new_user_password="$2"
  250. "${PROJECT_NAME}-pass" -u "$new_username" -a pleroma -p "$new_user_password"
  251. echo '0'
  252. }
  253. function install_interactive_pleroma {
  254. if [ ! "$ONION_ONLY" ]; then
  255. ONION_ONLY='no'
  256. fi
  257. if [[ $ONION_ONLY != "no" ]]; then
  258. PLEROMA_DOMAIN_NAME='pleroma.local'
  259. else
  260. PLEROMA_DETAILS_COMPLETE=
  261. while [ ! $PLEROMA_DETAILS_COMPLETE ]
  262. do
  263. data=$(mktemp 2>/dev/null)
  264. if [[ $DDNS_PROVIDER == "default@freedns.afraid.org" ]]; then
  265. dialog --backtitle $"Freedombone Configuration" \
  266. --title $"Pleroma Configuration" \
  267. --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 \
  268. $"Domain:" 1 1 "$(grep 'PLEROMA_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 25 33 40 \
  269. $"Title:" 2 1 "$(grep "$PLEROMA_TITLE" temp.cfg | awk -F '=' '{print $2}')" 2 25 255 255 \
  270. $"Background image URL:" 3 1 "$(grep "$PLEROMA_BACKGROUND_IMAGE_URL" temp.cfg | awk -F '=' '{print $2}')" 3 25 255 255 \
  271. $"Code:" 4 1 "$(grep 'PLEROMA_CODE' temp.cfg | awk -F '=' '{print $2}')" 4 25 33 255 \
  272. 2> "$data"
  273. else
  274. dialog --backtitle $"Freedombone Configuration" \
  275. --title $"Pleroma Configuration" \
  276. --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 \
  277. $"Domain:" 1 1 "$(grep 'PLEROMA_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 25 33 40 \
  278. $"Title:" 2 1 "$(grep "$PLEROMA_TITLE" temp.cfg | awk -F '=' '{print $2}')" 2 25 255 255 \
  279. $"Background image URL:" 3 1 "$(grep "$PLEROMA_BACKGROUND_IMAGE_URL" temp.cfg | awk -F '=' '{print $2}')" 3 25 255 255 \
  280. 2> "$data"
  281. fi
  282. sel=$?
  283. case $sel in
  284. 1) rm -f "$data"
  285. exit 1;;
  286. 255) rm -f "$data"
  287. exit 1;;
  288. esac
  289. PLEROMA_DOMAIN_NAME=$(sed -n 1p < "$data")
  290. title=$(sed -n 2p < "$data")
  291. if [ ${#title} -gt 1 ]; then
  292. PLEROMA_TITLE="$title"
  293. fi
  294. img_url=$(sed -n 3p < "$data")
  295. if [ ${#img_url} -gt 1 ]; then
  296. PLEROMA_BACKGROUND_IMAGE_URL=$img_url
  297. fi
  298. if [ "$PLEROMA_DOMAIN_NAME" ]; then
  299. if [[ $PLEROMA_DOMAIN_NAME == "$HUBZILLA_DOMAIN_NAME" ]]; then
  300. PLEROMA_DOMAIN_NAME=""
  301. fi
  302. TEST_DOMAIN_NAME=$PLEROMA_DOMAIN_NAME
  303. validate_domain_name
  304. if [[ "$TEST_DOMAIN_NAME" != "$PLEROMA_DOMAIN_NAME" ]]; then
  305. PLEROMA_DOMAIN_NAME=
  306. dialog --title $"Domain name validation" --msgbox "$TEST_DOMAIN_NAME" 15 50
  307. else
  308. if [[ $DDNS_PROVIDER == "default@freedns.afraid.org" ]]; then
  309. PLEROMA_CODE=$(sed -n 4p < "$data")
  310. validate_freedns_code "$PLEROMA_CODE"
  311. if [ ! "$VALID_CODE" ]; then
  312. PLEROMA_DOMAIN_NAME=
  313. fi
  314. fi
  315. fi
  316. fi
  317. if [ $PLEROMA_DOMAIN_NAME ]; then
  318. PLEROMA_DETAILS_COMPLETE="yes"
  319. fi
  320. rm -f "$data"
  321. done
  322. # remove any invalid characters
  323. if [ ${#PLEROMA_TITLE} -gt 0 ]; then
  324. new_title=${PLEROMA_TITLE//\'/}
  325. PLEROMA_TITLE="$new_title"
  326. fi
  327. # save the results in the config file
  328. write_config_param "PLEROMA_CODE" "$PLEROMA_CODE"
  329. write_config_param "PLEROMA_TITLE" "$PLEROMA_TITLE"
  330. write_config_param "PLEROMA_BACKGROUND_IMAGE_URL" "$PLEROMA_BACKGROUND_IMAGE_URL"
  331. fi
  332. write_config_param "PLEROMA_DOMAIN_NAME" "$PLEROMA_DOMAIN_NAME"
  333. APP_INSTALLED=1
  334. }
  335. function change_password_pleroma {
  336. # curr_username="$1"
  337. new_user_password="$2"
  338. #${PROJECT_NAME}-pass -u "$curr_username" -a pleroma -p "$new_user_password"
  339. }
  340. function pleroma_create_database_failed {
  341. run_system_query_postgresql "ALTER USER pleroma NOSUPERUSER;"
  342. run_system_query_postgresql "ALTER USER pleroma NOCREATEDB;"
  343. }
  344. function pleroma_create_database {
  345. if [ -f "$IMAGE_PASSWORD_FILE" ]; then
  346. PLEROMA_ADMIN_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
  347. else
  348. if [ ! "$PLEROMA_ADMIN_PASSWORD" ]; then
  349. PLEROMA_ADMIN_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
  350. fi
  351. fi
  352. if [ ! "$PLEROMA_ADMIN_PASSWORD" ]; then
  353. return
  354. fi
  355. systemctl restart postgresql
  356. add_postgresql_user pleroma "$PLEROMA_ADMIN_PASSWORD" encrypted
  357. run_system_query_postgresql "create database pleroma;"
  358. # temporarily allow the user to create databases
  359. run_system_query_postgresql "ALTER USER pleroma CREATEDB;"
  360. run_system_query_postgresql "ALTER USER pleroma SUPERUSER;"
  361. run_system_query_postgresql "GRANT ALL ON ALL tables IN SCHEMA public TO pleroma;"
  362. run_system_query_postgresql "GRANT ALL ON ALL sequences IN SCHEMA public TO pleroma;"
  363. run_system_query_postgresql "CREATE EXTENSION citext;"
  364. run_system_query_postgresql "set statement_timeout to 40000;"
  365. read_config_param "PLEROMA_SECRET_KEY"
  366. if [ ${#PLEROMA_SECRET_KEY} -lt 64 ]; then
  367. PLEROMA_SECRET_KEY="$(create_password 30)$(create_password 30)$(create_password 30)"
  368. if [ ${#PLEROMA_SECRET_KEY} -lt 64 ]; then
  369. pleroma_create_database_failed
  370. echo $'Pleroma secret key not created'
  371. exit 6782352
  372. fi
  373. write_config_param "PLEROMA_SECRET_KEY" "$PLEROMA_SECRET_KEY"
  374. fi
  375. if [ ! -d $PLEROMA_DIR/config ]; then
  376. echo $"Missing directory $PLEROMA_DIR/config"
  377. exit 7835393
  378. fi
  379. pleroma_secret=$PLEROMA_DIR/config/dev.secret.exs
  380. if [ ! -f $PLEROMA_DIR/config/dev.exs ]; then
  381. echo $"Did not find $PLEROMA_DIR/config/dev.exs"
  382. exit 78923528
  383. fi
  384. cp $PLEROMA_DIR/config/dev.exs $pleroma_secret
  385. sed -i "s|username:.*|username: \"pleroma\",|g" $pleroma_secret
  386. sed -i "s|password:.*|password: \"$PLEROMA_ADMIN_PASSWORD\",|g" $pleroma_secret
  387. sed -i "s|database:.*|database: \"pleroma\",|g" $pleroma_secret
  388. sed -i "/Pleroma.Web.Endpoint/a secret_key_base: \"$PLEROMA_SECRET_KEY\"," $pleroma_secret
  389. sed -i 's|secret_key_base: | secret_key_base: |g' $pleroma_secret
  390. sed -i "/Pleroma.Web.Endpoint/a pubsub: [name: Pleroma.Web.PubSub, adapter: Phoenix.PubSub.PG2]," $pleroma_secret
  391. sed -i 's|pubsub: | pubsub: |g' $pleroma_secret
  392. sed -i 's|watchers: []|watchers: [],|g' $pleroma_secret
  393. if [[ $ONION_ONLY == 'no' ]]; then
  394. sed -i "/watchers: []/a url: [host: \"$PLEROMA_DOMAIN_NAME\", scheme: \"https\", port: 443]" $pleroma_secret
  395. else
  396. sed -i "/watchers: []/a url: [host: \"$PLEROMA_ONION_HOSTNAME\", scheme: \"http\", port: 80]" $pleroma_secret
  397. fi
  398. sed -i 's|url: | url: |g' $pleroma_secret
  399. if ! grep -q "pbkdf2_rounds" $pleroma_secret; then
  400. sed -i '/config :logger/a config :comeonin, :pbkdf2_rounds, 1' $pleroma_secret
  401. else
  402. sed -i 's|pbkdf2_rounds.*|pbkdf2_rounds, 1|g' $pleroma_secret
  403. fi
  404. sed -i 's|import_config|# import_config|g' $pleroma_secret
  405. cd "$PLEROMA_DIR" || exit 678245245724
  406. chown -R pleroma:pleroma $PLEROMA_DIR/*
  407. if ! sudo -u pleroma mix local.rebar --force; then
  408. pleroma_create_database_failed
  409. echo $'mix local.rebar failed'
  410. exit 73528562
  411. fi
  412. sudo -u pleroma mix local.hex --force
  413. sudo -u pleroma mix deps.compile mimerl
  414. systemctl restart postgresql
  415. if ! sudo -u pleroma mix ecto.create --force; then
  416. pleroma_create_database_failed
  417. echo $'mix ecto.create failed'
  418. exit 83653582
  419. fi
  420. if ! sudo -u pleroma mix ecto.migrate --force; then
  421. pleroma_create_database_failed
  422. echo $'mix ecto.migrate failed'
  423. exit 73752573
  424. fi
  425. # revoke the ability to create databases for this user
  426. run_system_query_postgresql "ALTER USER pleroma NOSUPERUSER;"
  427. run_system_query_postgresql "ALTER USER pleroma NOCREATEDB;"
  428. }
  429. function reconfigure_pleroma {
  430. echo -n ''
  431. }
  432. function pleroma_set_background_image {
  433. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  434. data=$(mktemp 2>/dev/null)
  435. dialog --title $"Pleroma" \
  436. --backtitle $"Freedombone Control Panel" \
  437. --inputbox $'Set a background image URL' 10 60 2>"$data"
  438. sel=$?
  439. case $sel in
  440. 0)
  441. temp_background=$(<"$data")
  442. if [ ${#temp_background} -gt 0 ]; then
  443. PLEROMA_BACKGROUND_IMAGE_URL="$temp_background"
  444. write_config_param "PLEROMA_BACKGROUND_IMAGE_URL" "$PLEROMA_BACKGROUND_IMAGE_URL"
  445. if [[ $(pleroma_set_background_image_from_url $PLEROMA_DIR "$PLEROMA_DOMAIN_NAME" "$PLEROMA_BACKGROUND_IMAGE_URL" "$PLEROMA_TITLE" | tail -n 1) == "0" ]]; then
  446. pleroma_recompile
  447. dialog --title $"Set Pleroma login background" \
  448. --msgbox $"The background image has been set" 6 60
  449. fi
  450. fi
  451. ;;
  452. esac
  453. rm -f "$data"
  454. }
  455. function pleroma_set_title {
  456. data=$(mktemp 2>/dev/null)
  457. dialog --title $"Pleroma" \
  458. --backtitle $"Freedombone Control Panel" \
  459. --inputbox $'Set a title' 10 60 2>"$data"
  460. sel=$?
  461. case $sel in
  462. 0)
  463. new_title=$(<"$data")
  464. if [ ${#new_title} -gt 0 ]; then
  465. PLEROMA_TITLE="$new_title"
  466. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  467. write_config_param "PLEROMA_TITLE" "$PLEROMA_TITLE"
  468. sed -i "s|\"name\":.*|\"name\": \"${PLEROMA_TITLE}\",|g" $PLEROMA_DIR/static/config.json
  469. sed -i "s|\"name\":.*|\"name\": \"${PLEROMA_TITLE}\",|g" $PLEROMA_DIR/priv/static/static/config.json
  470. sed -i "s|name: .*|name: \"${PLEROMA_TITLE}\",|g" $PLEROMA_DIR/config/config.exs
  471. systemctl restart pleroma
  472. dialog --title $"Set Pleroma title" \
  473. --msgbox $"The title has been set" 6 60
  474. fi
  475. ;;
  476. esac
  477. rm -f "$data"
  478. }
  479. function pleroma_set_expire_months {
  480. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  481. read_config_param "PLEROMA_DOMAIN_NAME"
  482. read_config_param "PLEROMA_EXPIRE_MONTHS"
  483. data=$(mktemp 2>/dev/null)
  484. dialog --title $"Pleroma" \
  485. --backtitle $"Freedombone Control Panel" \
  486. --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"
  487. sel=$?
  488. case $sel in
  489. 0)
  490. new_expiry_months=$(<"$data")
  491. if [ ${#new_expiry_months} -gt 0 ]; then
  492. # should contain no spaces
  493. if [[ "$new_expiry_months" == *" "* ]]; then
  494. rm -f "$data"
  495. return
  496. fi
  497. # should be a number
  498. re='^[0-9]+$'
  499. if ! [[ $new_expiry_months =~ $re ]] ; then
  500. rm -f "$data"
  501. return
  502. fi
  503. # set the new value
  504. PLEROMA_EXPIRE_MONTHS=$new_expiry_months
  505. write_config_param "PLEROMA_EXPIRE_MONTHS" "$PLEROMA_EXPIRE_MONTHS"
  506. expire_pleroma_posts "$PLEROMA_DOMAIN_NAME" "$PLEROMA_EXPIRE_MONTHS"
  507. create_pleroma_blocklist
  508. dialog --title $"Set Pleroma post expiry period" \
  509. --msgbox $"Expiry period set to $PLEROMA_EXPIRE_MONTHS months" 6 60
  510. fi
  511. ;;
  512. esac
  513. rm -f "$data"
  514. }
  515. function pleroma_disable_registrations {
  516. dialog --title $"Disable new Pleroma user registrations" \
  517. --backtitle $"Freedombone Control Panel" \
  518. --yesno $"\\nDo you wish to disable new registrations?" 10 60
  519. sel=$?
  520. case $sel in
  521. 0) sed -i 's|registrations_open: true|registrations_open: false|g' $PLEROMA_DIR/config/config.exs
  522. sed -i 's|registrations_open: True|registrations_open: false|g' $PLEROMA_DIR/config/config.exs
  523. sed -i 's|"registrationOpen": true|"registrationOpen": false|g' $PLEROMA_DIR/priv/static/static/config.json
  524. sed -i 's|"registrationOpen": True|"registrationOpen": false|g' $PLEROMA_DIR/priv/static/static/config.json
  525. ;;
  526. 1) sed -i 's|registrations_open: false|registrations_open: true|g' $PLEROMA_DIR/config/config.exs
  527. sed -i 's|registrations_open: False|registrations_open: true|g' $PLEROMA_DIR/config/config.exs
  528. sed -i 's|"registrationOpen": false|"registrationOpen": true|g' $PLEROMA_DIR/priv/static/static/config.json
  529. sed -i 's|"registrationOpen": False|"registrationOpen": true|g' $PLEROMA_DIR/priv/static/static/config.json
  530. ;;
  531. 255) return;;
  532. esac
  533. pleroma_recompile
  534. }
  535. function pleroma_add_emoji {
  536. emoji_resolution='128x128'
  537. data=$(mktemp 2>/dev/null)
  538. dialog --backtitle $"Freedombone Control Panel" \
  539. --title $"Add Custom Emoji" \
  540. --form "\\n" 8 75 2 \
  541. $"Shortcode:" 1 1 "" 1 18 16 15 \
  542. $"ImageURL:" 2 1 "" 2 18 512 10000 \
  543. 2> "$data"
  544. sel=$?
  545. case $sel in
  546. 1) rm -f "$data"
  547. return;;
  548. 255) rm -f "$data"
  549. return;;
  550. esac
  551. shortcode=$(sed -n 1p < "$data")
  552. image_url=$(sed -n 2p < "$data")
  553. rm -f "$data"
  554. if [ ${#shortcode} -lt 2 ]; then
  555. return
  556. fi
  557. if [ ${#image_url} -lt 2 ]; then
  558. return
  559. fi
  560. if [[ "$image_url" != *'.'* ]]; then
  561. return
  562. fi
  563. if [[ "$image_url" != *'.png' && "$image_url" != *'.jpg' && "$image_url" != *'.jpeg' && "$image_url" != *'.gif' ]]; then
  564. dialog --title $"Add Custom Emoji" \
  565. --msgbox $"The image must be png/jpg/gif format" 6 60
  566. return
  567. fi
  568. if [[ "$shortcode" == *':'* || "$shortcode" == *' '* || "$shortcode" == *'.'* || "$shortcode" == *'!'* ]]; then
  569. dialog --title $"Add Custom Emoji" \
  570. --msgbox $"The shortcode contains invalid characters" 6 60
  571. return
  572. fi
  573. image_extension='png'
  574. if [[ "$image_url" == *'.jpg' || "$image_url" == *'.jpeg' ]]; then
  575. image_extension='jpg'
  576. fi
  577. if [[ "$image_url" == *'.gif' ]]; then
  578. image_extension='gif'
  579. fi
  580. if [ ! -d $PLEROMA_DIR/priv/static/emoji ]; then
  581. mkdir -p $PLEROMA_DIR/priv/static/emoji
  582. fi
  583. image_filename=$PLEROMA_DIR/priv/static/emoji/${shortcode}.${image_extension}
  584. wget "$image_url" -O "$image_filename"
  585. if [ ! -f "$image_filename" ]; then
  586. dialog --title $"Add Custom Emoji" \
  587. --msgbox $"Unable to download the image" 6 60
  588. return
  589. fi
  590. if [[ "$image_url" == *'.jpg' || "$image_url" == *'.jpeg' || "$image_url" == *'.gif' ]]; then
  591. convert "$image_filename" -resize "$emoji_resolution" "$PLEROMA_DIR/priv/static/emoji/${shortcode}.png"
  592. if [ ! -f "$PLEROMA_DIR/priv/static/emoji/${shortcode}.png" ]; then
  593. dialog --title $"Add Custom Emoji" \
  594. --msgbox $"Unable to convert empji image to png format" 6 60
  595. return
  596. fi
  597. # remove the original
  598. rm "$image_filename"
  599. image_extension='png'
  600. image_filename=$PLEROMA_DIR/priv/static/emoji/${shortcode}.${image_extension}
  601. else
  602. convert "$image_filename" -resize "$emoji_resolution" "$image_filename"
  603. fi
  604. if ! grep -q "${shortcode}," $PLEROMA_DIR/config/emoji.txt; then
  605. echo "${shortcode}, /emoji/${shortcode}.${image_extension}" >> $PLEROMA_DIR/config/emoji.txt
  606. else
  607. sed -i "s|${shortcode},.*|${shortcode}, /emoji/${shortcode}.${image_extension}|g" $PLEROMA_DIR/config/emoji.txt
  608. fi
  609. chown -R pleroma:pleroma $PLEROMA_DIR
  610. clear
  611. echo ''
  612. echo $'Recompiling Pleroma with the new emoji'
  613. systemctl stop pleroma
  614. pleroma_recompile
  615. dialog --title $"Add Custom Emoji" \
  616. --msgbox $"Custom emoji :${shortcode}: has been added" 6 70
  617. }
  618. function configure_interactive_pleroma {
  619. read_config_param PLEROMA_DOMAIN_NAME
  620. read_config_param PLEROMA_EXPIRE_MONTHS
  621. while true
  622. do
  623. data=$(mktemp 2>/dev/null)
  624. dialog --backtitle $"Freedombone Control Panel" \
  625. --title $"Pleroma" \
  626. --radiolist $"Choose an operation:" 15 70 6 \
  627. 1 $"Set a background image" off \
  628. 2 $"Set the title" off \
  629. 3 $"Disable new account registrations" off \
  630. 4 $"Add a custom emoji" off \
  631. 5 $"Set post expiry period (currently $PLEROMA_EXPIRE_MONTHS months)" off \
  632. 6 $"Exit" on 2> "$data"
  633. sel=$?
  634. case $sel in
  635. 1) rm -f "$data"
  636. return;;
  637. 255) rm -f "$data"
  638. return;;
  639. esac
  640. case $(cat "$data") in
  641. 1) pleroma_set_background_image;;
  642. 2) pleroma_set_title;;
  643. 3) pleroma_disable_registrations;;
  644. 4) pleroma_add_emoji;;
  645. 5) pleroma_set_expire_months;;
  646. 6) rm -f "$data"
  647. break;;
  648. esac
  649. rm -f "$data"
  650. done
  651. }
  652. function upgrade_pleroma {
  653. read_config_param PLEROMA_DOMAIN_NAME
  654. read_config_param PLEROMA_EXPIRE_MONTHS
  655. if ! grep -q "/media/" /etc/cron.daily/pleroma-expire; then
  656. rm $pleroma_expire_posts_script
  657. fi
  658. if [ ! -f $pleroma_expire_posts_script ]; then
  659. expire_pleroma_posts "$PLEROMA_DOMAIN_NAME" "$PLEROMA_EXPIRE_MONTHS"
  660. fi
  661. if [ ! -f $blocking_script_file ]; then
  662. create_pleroma_blocklist
  663. fi
  664. CURR_PLEROMA_COMMIT=$(get_completion_param "pleroma commit")
  665. if [[ "$CURR_PLEROMA_COMMIT" == "$PLEROMA_COMMIT" ]]; then
  666. return
  667. fi
  668. # make a copy of the configuration
  669. cp $PLEROMA_DIR/priv/static/static/config.json $PLEROMA_DIR/priv/static/static/config_prev.json
  670. if [ -f $PLEROMA_DIR/config/emoji.txt ]; then
  671. cp $PLEROMA_DIR/config/emoji.txt $PLEROMA_DIR/config/emoji_prev.txt
  672. fi
  673. apt-get -yq update
  674. apt-get -yq install --only-upgrade esl-erlang
  675. apt-get -yq install --only-upgrade elixir erlang-xmerl erlang-dev erlang-parsetools
  676. function_check set_repo_commit
  677. set_repo_commit $PLEROMA_DIR "pleroma commit" "$PLEROMA_COMMIT" $PLEROMA_REPO
  678. chown -R pleroma:pleroma $PLEROMA_DIR
  679. # restore the configuration
  680. cp $PLEROMA_DIR/priv/static/static/config_prev.json $PLEROMA_DIR/priv/static/static/config.json
  681. chown pleroma:pleroma $PLEROMA_DIR/priv/static/static/config.json
  682. if [ -f $PLEROMA_DIR/config/emoji_prev.txt ]; then
  683. cp $PLEROMA_DIR/config/emoji_prev.txt $PLEROMA_DIR/config/emoji.txt
  684. chown pleroma:pleroma $PLEROMA_DIR/config/emoji.txt
  685. rm cp $PLEROMA_DIR/config/emoji_prev.txt
  686. fi
  687. sudo -u pleroma mix deps.get
  688. pleroma_recompile
  689. # migrate database
  690. sudo -u pleroma mix deps.clean --build mime
  691. sudo -u pleroma mix ecto.migrate
  692. expire_pleroma_posts "$PLEROMA_DOMAIN_NAME" "$PLEROMA_EXPIRE_MONTHS"
  693. create_pleroma_blocklist
  694. systemctl restart pleroma
  695. }
  696. function backup_local_pleroma {
  697. PLEROMA_DOMAIN_NAME='pleroma'
  698. if grep -q "pleroma domain" "$COMPLETION_FILE"; then
  699. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  700. fi
  701. systemctl stop pleroma
  702. function_check suspend_site
  703. suspend_site "${PLEROMA_DOMAIN_NAME}"
  704. source_directory=$PLEROMA_DIR
  705. dest_directory=pleroma
  706. backup_directory_to_usb $source_directory $dest_directory
  707. USE_POSTGRESQL=1
  708. function_check backup_database_to_usb
  709. backup_database_to_usb pleroma
  710. function_check restart_site
  711. restart_site
  712. systemctl restart pleroma
  713. }
  714. function restore_local_pleroma {
  715. if ! grep -q "pleroma domain" "$COMPLETION_FILE"; then
  716. return
  717. fi
  718. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  719. if [ "$PLEROMA_DOMAIN_NAME" ]; then
  720. echo $"Restoring pleroma"
  721. temp_restore_dir=/root/temppleroma
  722. pleroma_dir=$PLEROMA_DIR
  723. systemctl stop pleroma
  724. PLEROMA_ONION_HOSTNAME=$(cat /var/lib/tor/hidden_service_pleroma/hostname)
  725. function_check pleroma_create_database
  726. pleroma_create_database
  727. USE_POSTGRESQL=1
  728. restore_database pleroma
  729. if [ -d $temp_restore_dir ]; then
  730. rm -rf $temp_restore_dir
  731. fi
  732. function_check restore_directory_from_usb
  733. restore_directory_from_usb $temp_restore_dir pleroma
  734. if [ -d $temp_restore_dir ]; then
  735. chown -R pleroma:pleroma $pleroma_dir
  736. rm -rf $temp_restore_dir
  737. fi
  738. systemctl restart pleroma
  739. echo $"Restore of pleroma complete"
  740. fi
  741. }
  742. function backup_remote_pleroma {
  743. PLEROMA_DOMAIN_NAME='pleroma'
  744. if grep -q "pleroma domain" "$COMPLETION_FILE"; then
  745. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  746. fi
  747. systemctl stop pleroma
  748. function_check suspend_site
  749. suspend_site "${PLEROMA_DOMAIN_NAME}"
  750. source_directory=$PLEROMA_DIR
  751. dest_directory=pleroma
  752. backup_directory_to_friend $source_directory $dest_directory
  753. USE_POSTGRESQL=1
  754. function_check backup_database_to_friend
  755. backup_database_to_friend pleroma
  756. function_check restart_site
  757. restart_site
  758. systemctl restart pleroma
  759. }
  760. function restore_remote_pleroma {
  761. if ! grep -q "pleroma domain" "$COMPLETION_FILE"; then
  762. return
  763. fi
  764. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  765. if [ "$PLEROMA_DOMAIN_NAME" ]; then
  766. echo $"Restoring pleroma"
  767. temp_restore_dir=/root/temppleroma
  768. pleroma_dir=$PLEROMA_DIR
  769. systemctl stop pleroma
  770. PLEROMA_ONION_HOSTNAME=$(cat /var/lib/tor/hidden_service_pleroma/hostname)
  771. function_check pleroma_create_database
  772. pleroma_create_database
  773. # shellcheck disable=SC2034
  774. USE_POSTGRESQL=1
  775. function_check restore_database_from_friend
  776. restore_database_from_friend pleroma
  777. if [ -d $temp_restore_dir ]; then
  778. rm -rf $temp_restore_dir
  779. fi
  780. function_check restore_directory_from_friend
  781. restore_directory_from_friend $temp_restore_dir pleroma
  782. if [ -d $temp_restore_dir ]; then
  783. chown -R pleroma:pleroma $pleroma_dir
  784. rm -rf $temp_restore_dir
  785. fi
  786. systemctl restart pleroma
  787. echo $"Restore of pleroma complete"
  788. fi
  789. }
  790. function remove_pleroma {
  791. if [ ${#PLEROMA_DOMAIN_NAME} -eq 0 ]; then
  792. return
  793. fi
  794. systemctl stop pleroma
  795. systemctl disable pleroma
  796. rm /etc/systemd/system/pleroma.service
  797. userdel pleroma
  798. #apt-get -yq remove esl-erlang elixir erlang-xmerl erlang-dev erlang-parsetools
  799. function_check remove_nodejs
  800. remove_nodejs pleroma-backend
  801. read_config_param "PLEROMA_DOMAIN_NAME"
  802. read_config_param "MY_USERNAME"
  803. echo "Removing $PLEROMA_DOMAIN_NAME"
  804. nginx_dissite "$PLEROMA_DOMAIN_NAME"
  805. remove_certs "$PLEROMA_DOMAIN_NAME"
  806. if [ -d "/var/www/$PLEROMA_DOMAIN_NAME" ]; then
  807. rm -rf "/var/www/$PLEROMA_DOMAIN_NAME"
  808. fi
  809. if [ -f "/etc/nginx/sites-available/$PLEROMA_DOMAIN_NAME" ]; then
  810. rm "/etc/nginx/sites-available/$PLEROMA_DOMAIN_NAME"
  811. fi
  812. if [ -d $PLEROMA_DIR ]; then
  813. rm -rf $PLEROMA_DIR
  814. fi
  815. function_check drop_database_postgresql
  816. drop_database_postgresql pleroma
  817. function_check remove_onion_service
  818. remove_onion_service pleroma ${PLEROMA_ONION_PORT}
  819. remove_app pleroma
  820. remove_completion_param install_pleroma
  821. sed -i '/pleroma domain/d' "$COMPLETION_FILE"
  822. sed -i '/pleroma commit/d' "$COMPLETION_FILE"
  823. sed -i "/$blocking_script_file/d" /etc/crontab
  824. function_check remove_ddns_domain
  825. remove_ddns_domain "$PLEROMA_DOMAIN_NAME"
  826. }
  827. function install_elixir {
  828. apt-get -yq install wget build-essential
  829. if [ ! -d "$INSTALL_DIR" ]; then
  830. mkdir -p "$INSTALL_DIR"
  831. fi
  832. cd "$INSTALL_DIR" || exit 768345274
  833. erlang_package=erlang-solutions_1.0_all.deb
  834. wget https://packages.erlang-solutions.com/$erlang_package
  835. if [ ! -f "$INSTALL_DIR/$erlang_package" ]; then
  836. exit 72853
  837. fi
  838. dpkg -i $erlang_package
  839. apt-get -yq update
  840. apt-get -yq install esl-erlang
  841. apt-get -yq install elixir erlang-xmerl erlang-dev erlang-parsetools
  842. if [ ! -f /usr/local/bin/mix ]; then
  843. echo $'/usr/local/bin/mix not found after elixir installation'
  844. exit 629352
  845. fi
  846. }
  847. function install_pleroma {
  848. if [ ! $ONION_ONLY ]; then
  849. ONION_ONLY='no'
  850. fi
  851. apt-get -yq install wget imagemagick
  852. # We need elixir 1.4+ here, so the debian repo package won't do
  853. install_elixir
  854. function_check install_nodejs
  855. install_nodejs pleroma-backend
  856. install_postgresql
  857. if [ ! -d "/var/www/${PLEROMA_DOMAIN_NAME}/htdocs" ]; then
  858. mkdir -p "/var/www/${PLEROMA_DOMAIN_NAME}/htdocs"
  859. fi
  860. if [ -d $PLEROMA_DIR ]; then
  861. rm -rf $PLEROMA_DIR
  862. fi
  863. # get the repo
  864. if [ -f /repos/pleroma/index.html ]; then
  865. mv /repos/pleroma /repos/pleroma-fe
  866. fi
  867. if [ -d /repos/pleroma ]; then
  868. mkdir -p $PLEROMA_DIR
  869. cp -r -p /repos/pleroma/. $PLEROMA_DIR
  870. cd "$PLEROMA_DIR" || exit 834537453
  871. git pull
  872. else
  873. function_check git_clone
  874. git_clone $PLEROMA_REPO $PLEROMA_DIR
  875. fi
  876. if [ ! -d $PLEROMA_DIR ]; then
  877. echo $'Unable to clone pleroma backend repo'
  878. exit 783523
  879. fi
  880. # create user
  881. useradd -d $PLEROMA_DIR -s /bin/false pleroma
  882. # checkout the commit
  883. cd "$PLEROMA_DIR" || exit 62452428
  884. git checkout $PLEROMA_COMMIT -b $PLEROMA_COMMIT
  885. set_completion_param "pleroma commit" "$PLEROMA_COMMIT"
  886. chown -R pleroma:pleroma $PLEROMA_DIR
  887. # web config
  888. function_check add_ddns_domain
  889. add_ddns_domain "$PLEROMA_DOMAIN_NAME"
  890. PLEROMA_ONION_HOSTNAME=$(add_onion_service pleroma 80 ${PLEROMA_ONION_PORT})
  891. pleroma_nginx_site=/etc/nginx/sites-available/$PLEROMA_DOMAIN_NAME
  892. if [[ $ONION_ONLY == "no" ]]; then
  893. function_check nginx_http_redirect
  894. nginx_http_redirect "$PLEROMA_DOMAIN_NAME" "index index.html"
  895. { echo '';
  896. echo 'proxy_cache_path /tmp/pleroma-media-cache levels=1:2 keys_zone=pleroma_media_cache:10m max_size=100m inactive=80m use_temp_path=off;';
  897. echo '';
  898. echo 'server {';
  899. echo ' listen 443 ssl;';
  900. echo ' #listen [::]:443 ssl;';
  901. echo " server_name $PLEROMA_DOMAIN_NAME;";
  902. echo ''; } >> "$pleroma_nginx_site"
  903. function_check nginx_compress
  904. nginx_compress "$PLEROMA_DOMAIN_NAME"
  905. echo '' >> "$pleroma_nginx_site"
  906. echo ' # Security' >> "$pleroma_nginx_site"
  907. function_check nginx_ssl
  908. nginx_ssl "$PLEROMA_DOMAIN_NAME"
  909. function_check nginx_security_options
  910. nginx_security_options "$PLEROMA_DOMAIN_NAME"
  911. { echo ' add_header Strict-Transport-Security max-age=15768000;';
  912. echo '';
  913. echo ' # Logs';
  914. echo ' access_log /dev/null;';
  915. echo ' error_log /dev/null;';
  916. echo '';
  917. echo " root $PLEROMA_DIR;";
  918. echo '';
  919. echo ' index index.html;';
  920. echo ' location / {'; } >> "$pleroma_nginx_site"
  921. function_check nginx_limits
  922. nginx_limits "$PLEROMA_DOMAIN_NAME" '15m'
  923. { echo " add_header 'Access-Control-Allow-Origin' '*';";
  924. echo ' proxy_http_version 1.1;';
  925. echo " proxy_set_header Upgrade \$http_upgrade;";
  926. echo ' proxy_set_header Connection "upgrade";';
  927. echo " proxy_set_header Host \$http_host;";
  928. echo '';
  929. echo " proxy_pass http://localhost:$PLEROMA_PORT;";
  930. echo ' }';
  931. echo '';
  932. echo ' location /proxy {'; } >> "$pleroma_nginx_site"
  933. nginx_limits "$PLEROMA_DOMAIN_NAME" '15m'
  934. { echo ' proxy_cache pleroma_media_cache;';
  935. echo ' proxy_cache_lock on;';
  936. echo " proxy_pass http://localhost:$PLEROMA_PORT;";
  937. echo ' }';
  938. echo ' # include snippets/well-known.conf;';
  939. echo '}'; } >> "$pleroma_nginx_site"
  940. else
  941. echo 'proxy_cache_path /tmp/pleroma-media-cache levels=1:2 keys_zone=pleroma_media_cache:10m max_size=100m inactive=80m use_temp_path=off;' > "$pleroma_nginx_site"
  942. echo '' >> "$pleroma_nginx_site"
  943. fi
  944. { echo 'server {';
  945. echo " listen 127.0.0.1:$PLEROMA_ONION_PORT default_server;";
  946. echo " server_name $PLEROMA_ONION_HOSTNAME;";
  947. echo ''; } >> "$pleroma_nginx_site"
  948. function_check nginx_compress
  949. nginx_compress "$PLEROMA_DOMAIN_NAME"
  950. echo '' >> "$pleroma_nginx_site"
  951. function_check nginx_security_options
  952. nginx_security_options "$PLEROMA_DOMAIN_NAME"
  953. { echo '';
  954. echo ' # Logs';
  955. echo ' access_log /dev/null;';
  956. echo ' error_log /dev/null;';
  957. echo '';
  958. echo " root $PLEROMA_DIR;";
  959. echo '';
  960. echo ' index index.html;';
  961. echo ' location / {'; } >> "$pleroma_nginx_site"
  962. function_check nginx_limits
  963. nginx_limits "$PLEROMA_DOMAIN_NAME" '15m'
  964. { echo " add_header 'Access-Control-Allow-Origin' '*';";
  965. echo ' proxy_http_version 1.1;';
  966. echo " proxy_set_header Upgrade \$http_upgrade;";
  967. echo ' proxy_set_header Connection "upgrade";';
  968. echo " proxy_set_header Host \$http_host;";
  969. echo '';
  970. echo " proxy_pass http://localhost:$PLEROMA_PORT;";
  971. echo ' }';
  972. echo '';
  973. echo ' location /proxy {'; } >> "$pleroma_nginx_site"
  974. nginx_limits "$PLEROMA_DOMAIN_NAME" '15m'
  975. { echo ' proxy_cache pleroma_media_cache;';
  976. echo ' proxy_cache_lock on;';
  977. echo " proxy_pass http://localhost:$PLEROMA_PORT;";
  978. echo ' }';
  979. echo ' # include snippets/well-known.conf;';
  980. echo '}'; } >> "$pleroma_nginx_site"
  981. # back end
  982. cd "$PLEROMA_DIR" || exit 246824684
  983. chown -R pleroma:pleroma "$PLEROMA_DIR/"*
  984. if ! sudo -u pleroma mix local.hex --force; then
  985. echo $'mix local.hex failed'
  986. exit 1745673
  987. fi
  988. if ! sudo -u pleroma mix deps.get --force; then
  989. echo $'mix deps.get failed'
  990. exit 7325733
  991. fi
  992. function_check pleroma_create_database
  993. pleroma_create_database
  994. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a pleroma -p "$PLEROMA_ADMIN_PASSWORD"
  995. # NOTE: we don't need to install the frontend separately,
  996. # since the backend contains a precompiled version of it
  997. install_gnusocial_default_background "pleroma" "$PLEROMA_DOMAIN_NAME"
  998. if [ ! -f "$PLEROMA_DIR/priv/static/static/config.json" ]; then
  999. echo $"$PLEROMA_DIR/priv/static/static/config.json file missing"
  1000. exit 323689
  1001. fi
  1002. sed -i 's|"theme":.*|"theme": "base16-summerfruit-dark.css",|g' "$PLEROMA_DIR/priv/static/static/config.json"
  1003. if [ "$PLEROMA_BACKGROUND_IMAGE_URL" ]; then
  1004. pleroma_set_background_image_from_url $PLEROMA_DIR/priv/static "$PLEROMA_DOMAIN_NAME" "$PLEROMA_BACKGROUND_IMAGE_URL" "$PLEROMA_TITLE"
  1005. fi
  1006. # Get certificate
  1007. function_check create_site_certificate
  1008. create_site_certificate "$PLEROMA_DOMAIN_NAME" 'yes'
  1009. function_check nginx_ensite
  1010. nginx_ensite "$PLEROMA_DOMAIN_NAME"
  1011. systemctl restart postgresql
  1012. systemctl restart nginx
  1013. set_completion_param "pleroma domain" "$PLEROMA_DOMAIN_NAME"
  1014. # We need to set up the url option again because it somehow gets
  1015. # lost during mix compile
  1016. pleroma_secret=$PLEROMA_DIR/config/dev.secret.exs
  1017. if ! grep -q 'watchers: [],' $pleroma_secret; then
  1018. sed -i 's|watchers: \[\]|watchers: \[\],|g' $pleroma_secret
  1019. fi
  1020. if ! grep -q 'url:' $pleroma_secret; then
  1021. if [[ $ONION_ONLY == 'no' ]]; then
  1022. sed -i "/watchers: /a url: [host: \"$PLEROMA_DOMAIN_NAME\", scheme: \"https\", port: 443]" $pleroma_secret
  1023. else
  1024. sed -i "/watchers: /a url: [host: \"$PLEROMA_ONION_HOSTNAME\", scheme: \"http\", port: 80]" $pleroma_secret
  1025. fi
  1026. fi
  1027. create_pleroma_blocklist
  1028. # daemon
  1029. { echo '[Unit]';
  1030. echo 'Description=Pleroma social network';
  1031. echo 'After=network.target postgresql.service';
  1032. echo '';
  1033. echo '[Service]';
  1034. echo 'User=pleroma';
  1035. echo "WorkingDirectory=$PLEROMA_DIR";
  1036. echo "Environment=\"HOME=$PLEROMA_DIR\"";
  1037. echo 'ExecStart=/usr/local/bin/mix phx.server';
  1038. echo "ExecReload=/bin/kill \$MAINPID";
  1039. echo 'KillMode=process';
  1040. echo 'Restart=on-failure';
  1041. echo '';
  1042. echo '[Install]';
  1043. echo 'WantedBy=multi-user.target';
  1044. echo 'Alias=pleroma.service'; } > /etc/systemd/system/pleroma.service
  1045. # set registrations open initially
  1046. sed -i 's|registrations_open:.*|registrations_open: true,|g' $PLEROMA_DIR/config/config.exs
  1047. sed -i 's|"registrationOpen":.*|"registrationOpen": true,|g' $PLEROMA_DIR/priv/static/static/config.json
  1048. systemctl daemon-reload
  1049. systemctl enable pleroma
  1050. systemctl start pleroma
  1051. APP_INSTALLED=1
  1052. }
  1053. # NOTE: deliberately there is no "exit 0"