freedombone-app-pleroma 47KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Pleroma backend application
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2017-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. VARIANTS='full full-vim social'
  29. IN_DEFAULT_INSTALL=0
  30. SHOW_ON_ABOUT=1
  31. PLEROMA_DOMAIN_NAME=
  32. PLEROMA_CODE=
  33. PLEROMA_PORT=4000
  34. PLEROMA_ONION_PORT=8011
  35. PLEROMA_REPO="https://git.pleroma.social/pleroma/pleroma.git"
  36. PLEROMA_COMMIT='e153b364a71de431787db236c57114f229162ddf'
  37. PLEROMA_ADMIN_PASSWORD=
  38. PLEROMA_DIR=/etc/pleroma
  39. PLEROMA_SECRET_KEY=""
  40. pleroma_secret=$PLEROMA_DIR/config/dev.secret.exs
  41. PLEROMA_BACKGROUND_IMAGE_URL=
  42. PLEROMA_TITLE='Pleroma Server'
  43. # Number of months after which posts expire
  44. PLEROMA_EXPIRE_MONTHS=3
  45. pleroma_expire_posts_script=/usr/bin/pleroma-expire-posts
  46. blocking_script_file=/usr/bin/pleroma-blocking
  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_enable_chat {
  58. if [[ "$1" == 't'* || "$1" == 'y'* || "$1" == 'T'* || "$1" == 'Y'* ]]; then
  59. sed -i 's|"chatDisabled":.*|"chatDisabled": false,|g' $PLEROMA_DIR/priv/static/static/config.json
  60. sed -i 's|:chat, enabled:.*|:chat, enabled: true|g' $PLEROMA_DIR/config/config.exs
  61. else
  62. sed -i 's|"chatDisabled":.*|"chatDisabled": true,|g' $PLEROMA_DIR/priv/static/static/config.json
  63. sed -i 's|:chat, enabled:.*|:chat, enabled: false|g' $PLEROMA_DIR/config/config.exs
  64. fi
  65. pleroma_recompile
  66. }
  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. cd $PLEROMA_DIR || exit 252498
  246. mix rm_user "$remove_username"
  247. "${PROJECT_NAME}-pass" -u "$remove_username" --rmapp pleroma
  248. }
  249. function add_user_pleroma {
  250. new_username="$1"
  251. new_user_password="$2"
  252. cd $PLEROMA_DIR || exit 348346
  253. mix register_user "$new_username" "$new_username" "$new_username@$HOSTNAME" $"Your bio goes here" "$new_user_password"
  254. "${PROJECT_NAME}-pass" -u "$new_username" -a pleroma -p "$new_user_password"
  255. echo '0'
  256. }
  257. function install_interactive_pleroma {
  258. if [ ! "$ONION_ONLY" ]; then
  259. ONION_ONLY='no'
  260. fi
  261. if [[ $ONION_ONLY != "no" ]]; then
  262. PLEROMA_DOMAIN_NAME='pleroma.local'
  263. else
  264. PLEROMA_DETAILS_COMPLETE=
  265. while [ ! $PLEROMA_DETAILS_COMPLETE ]
  266. do
  267. data=$(mktemp 2>/dev/null)
  268. if [[ $DDNS_PROVIDER == "default@freedns.afraid.org" ]]; then
  269. dialog --backtitle $"Freedombone Configuration" \
  270. --title $"Pleroma Configuration" \
  271. --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 \
  272. $"Domain:" 1 1 "$(grep 'PLEROMA_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 25 33 40 \
  273. $"Title:" 2 1 "$(grep "$PLEROMA_TITLE" temp.cfg | awk -F '=' '{print $2}')" 2 25 255 255 \
  274. $"Background image URL:" 3 1 "$(grep "$PLEROMA_BACKGROUND_IMAGE_URL" temp.cfg | awk -F '=' '{print $2}')" 3 25 255 255 \
  275. $"Code:" 4 1 "$(grep 'PLEROMA_CODE' temp.cfg | awk -F '=' '{print $2}')" 4 25 33 255 \
  276. 2> "$data"
  277. else
  278. dialog --backtitle $"Freedombone Configuration" \
  279. --title $"Pleroma Configuration" \
  280. --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 \
  281. $"Domain:" 1 1 "$(grep 'PLEROMA_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 25 33 40 \
  282. $"Title:" 2 1 "$(grep "$PLEROMA_TITLE" temp.cfg | awk -F '=' '{print $2}')" 2 25 255 255 \
  283. $"Background image URL:" 3 1 "$(grep "$PLEROMA_BACKGROUND_IMAGE_URL" temp.cfg | awk -F '=' '{print $2}')" 3 25 255 255 \
  284. 2> "$data"
  285. fi
  286. sel=$?
  287. case $sel in
  288. 1) rm -f "$data"
  289. exit 1;;
  290. 255) rm -f "$data"
  291. exit 1;;
  292. esac
  293. PLEROMA_DOMAIN_NAME=$(sed -n 1p < "$data")
  294. title=$(sed -n 2p < "$data")
  295. if [ ${#title} -gt 1 ]; then
  296. PLEROMA_TITLE="$title"
  297. fi
  298. img_url=$(sed -n 3p < "$data")
  299. if [ ${#img_url} -gt 1 ]; then
  300. PLEROMA_BACKGROUND_IMAGE_URL=$img_url
  301. fi
  302. if [ "$PLEROMA_DOMAIN_NAME" ]; then
  303. if [[ $PLEROMA_DOMAIN_NAME == "$HUBZILLA_DOMAIN_NAME" ]]; then
  304. PLEROMA_DOMAIN_NAME=""
  305. fi
  306. TEST_DOMAIN_NAME=$PLEROMA_DOMAIN_NAME
  307. validate_domain_name
  308. if [[ "$TEST_DOMAIN_NAME" != "$PLEROMA_DOMAIN_NAME" ]]; then
  309. PLEROMA_DOMAIN_NAME=
  310. dialog --title $"Domain name validation" --msgbox "$TEST_DOMAIN_NAME" 15 50
  311. else
  312. if [[ $DDNS_PROVIDER == "default@freedns.afraid.org" ]]; then
  313. PLEROMA_CODE=$(sed -n 4p < "$data")
  314. validate_freedns_code "$PLEROMA_CODE"
  315. if [ ! "$VALID_CODE" ]; then
  316. PLEROMA_DOMAIN_NAME=
  317. fi
  318. fi
  319. fi
  320. fi
  321. if [ $PLEROMA_DOMAIN_NAME ]; then
  322. PLEROMA_DETAILS_COMPLETE="yes"
  323. fi
  324. rm -f "$data"
  325. done
  326. # remove any invalid characters
  327. if [ ${#PLEROMA_TITLE} -gt 0 ]; then
  328. new_title=${PLEROMA_TITLE//\'/}
  329. PLEROMA_TITLE="$new_title"
  330. fi
  331. # save the results in the config file
  332. write_config_param "PLEROMA_CODE" "$PLEROMA_CODE"
  333. write_config_param "PLEROMA_TITLE" "$PLEROMA_TITLE"
  334. write_config_param "PLEROMA_BACKGROUND_IMAGE_URL" "$PLEROMA_BACKGROUND_IMAGE_URL"
  335. fi
  336. write_config_param "PLEROMA_DOMAIN_NAME" "$PLEROMA_DOMAIN_NAME"
  337. APP_INSTALLED=1
  338. }
  339. function change_password_pleroma {
  340. # curr_username="$1"
  341. new_user_password="$2"
  342. #${PROJECT_NAME}-pass -u "$curr_username" -a pleroma -p "$new_user_password"
  343. }
  344. function pleroma_create_database_failed {
  345. run_system_query_postgresql "ALTER USER pleroma NOSUPERUSER;"
  346. run_system_query_postgresql "ALTER USER pleroma NOCREATEDB;"
  347. }
  348. function pleroma_create_database {
  349. if [ -f "$IMAGE_PASSWORD_FILE" ]; then
  350. PLEROMA_ADMIN_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
  351. else
  352. if [ ! "$PLEROMA_ADMIN_PASSWORD" ]; then
  353. PLEROMA_ADMIN_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
  354. fi
  355. fi
  356. if [ ! "$PLEROMA_ADMIN_PASSWORD" ]; then
  357. return
  358. fi
  359. systemctl restart postgresql
  360. add_postgresql_user pleroma "$PLEROMA_ADMIN_PASSWORD" encrypted
  361. run_system_query_postgresql "create database pleroma;"
  362. # temporarily allow the user to create databases
  363. run_system_query_postgresql "ALTER USER pleroma CREATEDB;"
  364. run_system_query_postgresql "ALTER USER pleroma SUPERUSER;"
  365. run_system_query_postgresql "GRANT ALL ON ALL tables IN SCHEMA public TO pleroma;"
  366. run_system_query_postgresql "GRANT ALL ON ALL sequences IN SCHEMA public TO pleroma;"
  367. run_system_query_postgresql "CREATE EXTENSION citext;"
  368. run_system_query_postgresql "set statement_timeout to 40000;"
  369. read_config_param "PLEROMA_SECRET_KEY"
  370. if [ ${#PLEROMA_SECRET_KEY} -lt 64 ]; then
  371. PLEROMA_SECRET_KEY="$(create_password 30)$(create_password 30)$(create_password 30)"
  372. if [ ${#PLEROMA_SECRET_KEY} -lt 64 ]; then
  373. pleroma_create_database_failed
  374. echo $'Pleroma secret key not created'
  375. exit 6782352
  376. fi
  377. write_config_param "PLEROMA_SECRET_KEY" "$PLEROMA_SECRET_KEY"
  378. fi
  379. if [ ! -d $PLEROMA_DIR/config ]; then
  380. echo $"Missing directory $PLEROMA_DIR/config"
  381. exit 7835393
  382. fi
  383. if [ ! -f $PLEROMA_DIR/config/dev.exs ]; then
  384. echo $"Did not find $PLEROMA_DIR/config/dev.exs"
  385. exit 78923528
  386. fi
  387. cp $PLEROMA_DIR/config/dev.exs $pleroma_secret
  388. sed -i "s|username:.*|username: \"pleroma\",|g" $pleroma_secret
  389. sed -i "s|password:.*|password: \"$PLEROMA_ADMIN_PASSWORD\",|g" $pleroma_secret
  390. sed -i "s|database:.*|database: \"pleroma\",|g" $pleroma_secret
  391. sed -i "/Pleroma.Web.Endpoint/a secret_key_base: \"$PLEROMA_SECRET_KEY\"," $pleroma_secret
  392. sed -i 's|secret_key_base: | secret_key_base: |g' $pleroma_secret
  393. sed -i "/Pleroma.Web.Endpoint/a pubsub: [name: Pleroma.Web.PubSub, adapter: Phoenix.PubSub.PG2]," $pleroma_secret
  394. sed -i 's|pubsub: | pubsub: |g' $pleroma_secret
  395. sed -i 's|watchers: []|watchers: [],|g' $pleroma_secret
  396. if [[ $ONION_ONLY == 'no' ]]; then
  397. sed -i "/watchers: []/a url: [host: \"$PLEROMA_DOMAIN_NAME\", scheme: \"https\", port: 443]" $pleroma_secret
  398. else
  399. sed -i "/watchers: []/a url: [host: \"$PLEROMA_ONION_HOSTNAME\", scheme: \"http\", port: 80]" $pleroma_secret
  400. fi
  401. sed -i 's|url: | url: |g' $pleroma_secret
  402. if ! grep -q "pbkdf2_rounds" $pleroma_secret; then
  403. sed -i '/config :logger/a config :comeonin, :pbkdf2_rounds, 1' $pleroma_secret
  404. else
  405. sed -i 's|pbkdf2_rounds.*|pbkdf2_rounds, 1|g' $pleroma_secret
  406. fi
  407. sed -i 's|import_config|# import_config|g' $pleroma_secret
  408. cd "$PLEROMA_DIR" || exit 678245245724
  409. chown -R pleroma:pleroma $PLEROMA_DIR/*
  410. if ! sudo -u pleroma mix local.rebar --force; then
  411. pleroma_create_database_failed
  412. echo $'mix local.rebar failed'
  413. exit 73528562
  414. fi
  415. sudo -u pleroma mix local.hex --force
  416. sudo -u pleroma mix deps.compile mimerl
  417. systemctl restart postgresql
  418. if ! sudo -u pleroma mix ecto.create --force; then
  419. pleroma_create_database_failed
  420. echo $'mix ecto.create failed'
  421. exit 83653582
  422. fi
  423. if ! sudo -u pleroma mix ecto.migrate --force; then
  424. pleroma_create_database_failed
  425. echo $'mix ecto.migrate failed'
  426. exit 73752573
  427. fi
  428. # revoke the ability to create databases for this user
  429. run_system_query_postgresql "ALTER USER pleroma NOSUPERUSER;"
  430. run_system_query_postgresql "ALTER USER pleroma NOCREATEDB;"
  431. }
  432. function reconfigure_pleroma {
  433. echo -n ''
  434. }
  435. function pleroma_set_background_image {
  436. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  437. data=$(mktemp 2>/dev/null)
  438. dialog --title $"Pleroma" \
  439. --backtitle $"Freedombone Control Panel" \
  440. --inputbox $'Set a background image URL' 10 60 2>"$data"
  441. sel=$?
  442. case $sel in
  443. 0)
  444. temp_background=$(<"$data")
  445. if [ ${#temp_background} -gt 0 ]; then
  446. PLEROMA_BACKGROUND_IMAGE_URL="$temp_background"
  447. write_config_param "PLEROMA_BACKGROUND_IMAGE_URL" "$PLEROMA_BACKGROUND_IMAGE_URL"
  448. if [[ $(pleroma_set_background_image_from_url $PLEROMA_DIR "$PLEROMA_DOMAIN_NAME" "$PLEROMA_BACKGROUND_IMAGE_URL" "$PLEROMA_TITLE" | tail -n 1) == "0" ]]; then
  449. pleroma_recompile
  450. dialog --title $"Set Pleroma login background" \
  451. --msgbox $"The background image has been set" 6 60
  452. fi
  453. fi
  454. ;;
  455. esac
  456. rm -f "$data"
  457. }
  458. function pleroma_set_title {
  459. data=$(mktemp 2>/dev/null)
  460. dialog --title $"Pleroma" \
  461. --backtitle $"Freedombone Control Panel" \
  462. --inputbox $'Set a title' 10 60 2>"$data"
  463. sel=$?
  464. case $sel in
  465. 0)
  466. new_title=$(<"$data")
  467. if [ ${#new_title} -gt 0 ]; then
  468. PLEROMA_TITLE="$new_title"
  469. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  470. write_config_param "PLEROMA_TITLE" "$PLEROMA_TITLE"
  471. sed -i "s|\"name\":.*|\"name\": \"${PLEROMA_TITLE}\",|g" $PLEROMA_DIR/static/config.json
  472. sed -i "s|\"name\":.*|\"name\": \"${PLEROMA_TITLE}\",|g" $PLEROMA_DIR/priv/static/static/config.json
  473. sed -i "s|name: .*|name: \"${PLEROMA_TITLE}\",|g" $PLEROMA_DIR/config/config.exs
  474. systemctl restart pleroma
  475. dialog --title $"Set Pleroma title" \
  476. --msgbox $"The title has been set" 6 60
  477. fi
  478. ;;
  479. esac
  480. rm -f "$data"
  481. }
  482. function pleroma_set_expire_months {
  483. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  484. read_config_param "PLEROMA_DOMAIN_NAME"
  485. read_config_param "PLEROMA_EXPIRE_MONTHS"
  486. data=$(mktemp 2>/dev/null)
  487. dialog --title $"Pleroma" \
  488. --backtitle $"Freedombone Control Panel" \
  489. --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"
  490. sel=$?
  491. case $sel in
  492. 0)
  493. new_expiry_months=$(<"$data")
  494. if [ ${#new_expiry_months} -gt 0 ]; then
  495. # should contain no spaces
  496. if [[ "$new_expiry_months" == *" "* ]]; then
  497. rm -f "$data"
  498. return
  499. fi
  500. # should be a number
  501. re='^[0-9]+$'
  502. if ! [[ $new_expiry_months =~ $re ]] ; then
  503. rm -f "$data"
  504. return
  505. fi
  506. # set the new value
  507. PLEROMA_EXPIRE_MONTHS=$new_expiry_months
  508. write_config_param "PLEROMA_EXPIRE_MONTHS" "$PLEROMA_EXPIRE_MONTHS"
  509. expire_pleroma_posts "$PLEROMA_DOMAIN_NAME" "$PLEROMA_EXPIRE_MONTHS"
  510. create_pleroma_blocklist
  511. dialog --title $"Set Pleroma post expiry period" \
  512. --msgbox $"Expiry period set to $PLEROMA_EXPIRE_MONTHS months" 6 60
  513. fi
  514. ;;
  515. esac
  516. rm -f "$data"
  517. }
  518. function pleroma_disable_registrations {
  519. dialog --title $"Disable new Pleroma user registrations" \
  520. --backtitle $"Freedombone Control Panel" \
  521. --yesno $"\\nDo you wish to disable new registrations?" 10 60
  522. sel=$?
  523. case $sel in
  524. 0) sed -i 's|registrations_open: true|registrations_open: false|g' $PLEROMA_DIR/config/config.exs
  525. sed -i 's|registrations_open: True|registrations_open: false|g' $PLEROMA_DIR/config/config.exs
  526. sed -i 's|"registrationOpen": true|"registrationOpen": false|g' $PLEROMA_DIR/priv/static/static/config.json
  527. sed -i 's|"registrationOpen": True|"registrationOpen": false|g' $PLEROMA_DIR/priv/static/static/config.json
  528. ;;
  529. 1) sed -i 's|registrations_open: false|registrations_open: true|g' $PLEROMA_DIR/config/config.exs
  530. sed -i 's|registrations_open: False|registrations_open: true|g' $PLEROMA_DIR/config/config.exs
  531. sed -i 's|"registrationOpen": false|"registrationOpen": true|g' $PLEROMA_DIR/priv/static/static/config.json
  532. sed -i 's|"registrationOpen": False|"registrationOpen": true|g' $PLEROMA_DIR/priv/static/static/config.json
  533. ;;
  534. 255) return;;
  535. esac
  536. pleroma_recompile
  537. }
  538. function pleroma_add_emoji {
  539. emoji_resolution='128x128'
  540. data=$(mktemp 2>/dev/null)
  541. dialog --backtitle $"Freedombone Control Panel" \
  542. --title $"Add Custom Emoji" \
  543. --form "\\n" 8 75 2 \
  544. $"Shortcode:" 1 1 "" 1 18 16 15 \
  545. $"ImageURL:" 2 1 "" 2 18 512 10000 \
  546. 2> "$data"
  547. sel=$?
  548. case $sel in
  549. 1) rm -f "$data"
  550. return;;
  551. 255) rm -f "$data"
  552. return;;
  553. esac
  554. shortcode=$(sed -n 1p < "$data")
  555. image_url=$(sed -n 2p < "$data")
  556. rm -f "$data"
  557. if [ ${#shortcode} -lt 2 ]; then
  558. return
  559. fi
  560. if [ ${#image_url} -lt 2 ]; then
  561. return
  562. fi
  563. if [[ "$image_url" != *'.'* ]]; then
  564. return
  565. fi
  566. if [[ "$image_url" != *'.png' && "$image_url" != *'.jpg' && "$image_url" != *'.jpeg' && "$image_url" != *'.gif' ]]; then
  567. dialog --title $"Add Custom Emoji" \
  568. --msgbox $"The image must be png/jpg/gif format" 6 60
  569. return
  570. fi
  571. if [[ "$shortcode" == *':'* || "$shortcode" == *' '* || "$shortcode" == *'.'* || "$shortcode" == *'!'* ]]; then
  572. dialog --title $"Add Custom Emoji" \
  573. --msgbox $"The shortcode contains invalid characters" 6 60
  574. return
  575. fi
  576. image_extension='png'
  577. if [[ "$image_url" == *'.jpg' || "$image_url" == *'.jpeg' ]]; then
  578. image_extension='jpg'
  579. fi
  580. if [[ "$image_url" == *'.gif' ]]; then
  581. image_extension='gif'
  582. fi
  583. if [ ! -d $PLEROMA_DIR/priv/static/emoji ]; then
  584. mkdir -p $PLEROMA_DIR/priv/static/emoji
  585. fi
  586. image_filename=$PLEROMA_DIR/priv/static/emoji/${shortcode}.${image_extension}
  587. wget "$image_url" -O "$image_filename"
  588. if [ ! -f "$image_filename" ]; then
  589. dialog --title $"Add Custom Emoji" \
  590. --msgbox $"Unable to download the image" 6 60
  591. return
  592. fi
  593. if [[ "$image_url" == *'.jpg' || "$image_url" == *'.jpeg' || "$image_url" == *'.gif' ]]; then
  594. convert "$image_filename" -resize "$emoji_resolution" "$PLEROMA_DIR/priv/static/emoji/${shortcode}.png"
  595. if [ ! -f "$PLEROMA_DIR/priv/static/emoji/${shortcode}.png" ]; then
  596. dialog --title $"Add Custom Emoji" \
  597. --msgbox $"Unable to convert empji image to png format" 6 60
  598. return
  599. fi
  600. # remove the original
  601. rm "$image_filename"
  602. image_extension='png'
  603. image_filename=$PLEROMA_DIR/priv/static/emoji/${shortcode}.${image_extension}
  604. else
  605. convert "$image_filename" -resize "$emoji_resolution" "$image_filename"
  606. fi
  607. if ! grep -q "${shortcode}," $PLEROMA_DIR/config/emoji.txt; then
  608. echo "${shortcode}, /emoji/${shortcode}.${image_extension}" >> $PLEROMA_DIR/config/emoji.txt
  609. else
  610. sed -i "s|${shortcode},.*|${shortcode}, /emoji/${shortcode}.${image_extension}|g" $PLEROMA_DIR/config/emoji.txt
  611. fi
  612. chown -R pleroma:pleroma $PLEROMA_DIR
  613. clear
  614. echo ''
  615. echo $'Recompiling Pleroma with the new emoji'
  616. systemctl stop pleroma
  617. pleroma_recompile
  618. dialog --title $"Add Custom Emoji" \
  619. --msgbox $"Custom emoji :${shortcode}: has been added" 6 70
  620. }
  621. function configure_interactive_pleroma {
  622. read_config_param PLEROMA_DOMAIN_NAME
  623. read_config_param PLEROMA_EXPIRE_MONTHS
  624. while true
  625. do
  626. chatenabled=
  627. enablechatstr=$'Enable chat system'
  628. if grep -q ':chat, enabled: true' $PLEROMA_DIR/config/config.exs; then
  629. chatenabled=1
  630. enablechatstr=$'Disable chat system'
  631. fi
  632. W=(1 $"Set a background image"
  633. 2 $"Set the title"
  634. 3 $"Disable new account registrations"
  635. 4 $"Add a custom emoji"
  636. 5 $"Set post expiry period (currently $PLEROMA_EXPIRE_MONTHS months)"
  637. 6 "$enablechatstr")
  638. # shellcheck disable=SC2068
  639. selection=$(dialog --backtitle $"Freedombone Administrator Control Panel" --title $"Pleroma" --menu $"Choose an operation, or ESC to exit:" 13 60 6 "${W[@]}" 3>&2 2>&1 1>&3)
  640. if [ ! "$selection" ]; then
  641. break
  642. fi
  643. case $selection in
  644. 1) pleroma_set_background_image;;
  645. 2) pleroma_set_title;;
  646. 3) pleroma_disable_registrations;;
  647. 4) pleroma_add_emoji;;
  648. 5) pleroma_set_expire_months;;
  649. 6) if [ $chatenabled ]; then
  650. pleroma_enable_chat false
  651. else
  652. pleroma_enable_chat true
  653. fi
  654. ;;
  655. esac
  656. done
  657. }
  658. function upgrade_pleroma {
  659. read_config_param PLEROMA_DOMAIN_NAME
  660. read_config_param PLEROMA_EXPIRE_MONTHS
  661. if ! grep -q "/media/" /etc/cron.daily/pleroma-expire; then
  662. rm $pleroma_expire_posts_script
  663. fi
  664. if [ ! -f $pleroma_expire_posts_script ]; then
  665. expire_pleroma_posts "$PLEROMA_DOMAIN_NAME" "$PLEROMA_EXPIRE_MONTHS"
  666. fi
  667. if [ ! -f $blocking_script_file ]; then
  668. create_pleroma_blocklist
  669. fi
  670. CURR_PLEROMA_COMMIT=$(get_completion_param "pleroma commit")
  671. if [[ "$CURR_PLEROMA_COMMIT" == "$PLEROMA_COMMIT" ]]; then
  672. return
  673. fi
  674. pleroma_registrations=open
  675. if grep -q 'registrations_open: false' $PLEROMA_DIR/config/config.exs; then
  676. pleroma_registrations=
  677. fi
  678. # make a copy of the configuration
  679. cp $PLEROMA_DIR/priv/static/static/config.json $PLEROMA_DIR/priv/static/static/config_prev.json
  680. if [ -f $PLEROMA_DIR/config/emoji.txt ]; then
  681. cp $PLEROMA_DIR/config/emoji.txt $PLEROMA_DIR/config/emoji_prev.txt
  682. fi
  683. apt-get -yq update
  684. apt-get -yq install --only-upgrade esl-erlang
  685. apt-get -yq install --only-upgrade elixir erlang-xmerl erlang-dev erlang-parsetools
  686. function_check set_repo_commit
  687. set_repo_commit $PLEROMA_DIR "pleroma commit" "$PLEROMA_COMMIT" $PLEROMA_REPO
  688. chown -R pleroma:pleroma $PLEROMA_DIR
  689. # restore the configuration
  690. cp $PLEROMA_DIR/priv/static/static/config_prev.json $PLEROMA_DIR/priv/static/static/config.json
  691. chown pleroma:pleroma $PLEROMA_DIR/priv/static/static/config.json
  692. if [ -f $PLEROMA_DIR/config/emoji_prev.txt ]; then
  693. cp $PLEROMA_DIR/config/emoji_prev.txt $PLEROMA_DIR/config/emoji.txt
  694. chown pleroma:pleroma $PLEROMA_DIR/config/emoji.txt
  695. rm cp $PLEROMA_DIR/config/emoji_prev.txt
  696. fi
  697. sudo -u pleroma mix deps.get
  698. if [ ! $pleroma_registrations ]; then
  699. sed -i 's|registrations_open: true|registrations_open: false|g' $PLEROMA_DIR/config/config.exs
  700. sed -i 's|registrations_open: True|registrations_open: false|g' $PLEROMA_DIR/config/config.exs
  701. fi
  702. pleroma_recompile
  703. # migrate database
  704. sudo -u pleroma mix deps.clean --build mime
  705. sudo -u pleroma mix ecto.migrate
  706. pleroma_custom_logo "$PLEROMA_DIR"
  707. expire_pleroma_posts "$PLEROMA_DOMAIN_NAME" "$PLEROMA_EXPIRE_MONTHS"
  708. create_pleroma_blocklist
  709. chown -R pleroma:pleroma $PLEROMA_DIR
  710. systemctl restart pleroma
  711. }
  712. function backup_local_pleroma {
  713. PLEROMA_DOMAIN_NAME='pleroma'
  714. if grep -q "pleroma domain" "$COMPLETION_FILE"; then
  715. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  716. fi
  717. systemctl stop pleroma
  718. function_check suspend_site
  719. suspend_site "${PLEROMA_DOMAIN_NAME}"
  720. source_directory=$PLEROMA_DIR
  721. dest_directory=pleroma
  722. backup_directory_to_usb $source_directory $dest_directory
  723. USE_POSTGRESQL=1
  724. function_check backup_database_to_usb
  725. backup_database_to_usb pleroma
  726. function_check restart_site
  727. restart_site
  728. systemctl restart pleroma
  729. }
  730. function restore_local_pleroma {
  731. if ! grep -q "pleroma domain" "$COMPLETION_FILE"; then
  732. return
  733. fi
  734. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  735. if [ "$PLEROMA_DOMAIN_NAME" ]; then
  736. echo $"Restoring pleroma"
  737. temp_restore_dir=/root/temppleroma
  738. pleroma_dir=$PLEROMA_DIR
  739. systemctl stop pleroma
  740. PLEROMA_ONION_HOSTNAME=$(cat /var/lib/tor/hidden_service_pleroma/hostname)
  741. function_check pleroma_create_database
  742. pleroma_create_database
  743. USE_POSTGRESQL=1
  744. restore_database pleroma
  745. if [ -d $temp_restore_dir ]; then
  746. rm -rf $temp_restore_dir
  747. fi
  748. function_check restore_directory_from_usb
  749. restore_directory_from_usb $temp_restore_dir pleroma
  750. if [ -d $temp_restore_dir ]; then
  751. chown -R pleroma:pleroma $pleroma_dir
  752. rm -rf $temp_restore_dir
  753. fi
  754. systemctl restart pleroma
  755. echo $"Restore of pleroma complete"
  756. fi
  757. }
  758. function backup_remote_pleroma {
  759. PLEROMA_DOMAIN_NAME='pleroma'
  760. if grep -q "pleroma domain" "$COMPLETION_FILE"; then
  761. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  762. fi
  763. systemctl stop pleroma
  764. function_check suspend_site
  765. suspend_site "${PLEROMA_DOMAIN_NAME}"
  766. source_directory=$PLEROMA_DIR
  767. dest_directory=pleroma
  768. backup_directory_to_friend $source_directory $dest_directory
  769. USE_POSTGRESQL=1
  770. function_check backup_database_to_friend
  771. backup_database_to_friend pleroma
  772. function_check restart_site
  773. restart_site
  774. systemctl restart pleroma
  775. }
  776. function restore_remote_pleroma {
  777. if ! grep -q "pleroma domain" "$COMPLETION_FILE"; then
  778. return
  779. fi
  780. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  781. if [ "$PLEROMA_DOMAIN_NAME" ]; then
  782. echo $"Restoring pleroma"
  783. temp_restore_dir=/root/temppleroma
  784. pleroma_dir=$PLEROMA_DIR
  785. systemctl stop pleroma
  786. PLEROMA_ONION_HOSTNAME=$(cat /var/lib/tor/hidden_service_pleroma/hostname)
  787. function_check pleroma_create_database
  788. pleroma_create_database
  789. # shellcheck disable=SC2034
  790. USE_POSTGRESQL=1
  791. function_check restore_database_from_friend
  792. restore_database_from_friend pleroma
  793. if [ -d $temp_restore_dir ]; then
  794. rm -rf $temp_restore_dir
  795. fi
  796. function_check restore_directory_from_friend
  797. restore_directory_from_friend $temp_restore_dir pleroma
  798. if [ -d $temp_restore_dir ]; then
  799. chown -R pleroma:pleroma $pleroma_dir
  800. rm -rf $temp_restore_dir
  801. fi
  802. systemctl restart pleroma
  803. echo $"Restore of pleroma complete"
  804. fi
  805. }
  806. function remove_pleroma {
  807. if [ ${#PLEROMA_DOMAIN_NAME} -eq 0 ]; then
  808. return
  809. fi
  810. systemctl stop pleroma
  811. systemctl disable pleroma
  812. rm /etc/systemd/system/pleroma.service
  813. userdel pleroma
  814. #remove_elixir
  815. function_check remove_nodejs
  816. remove_nodejs pleroma-backend
  817. read_config_param "PLEROMA_DOMAIN_NAME"
  818. read_config_param "MY_USERNAME"
  819. echo "Removing $PLEROMA_DOMAIN_NAME"
  820. nginx_dissite "$PLEROMA_DOMAIN_NAME"
  821. remove_certs "$PLEROMA_DOMAIN_NAME"
  822. if [ -d "/var/www/$PLEROMA_DOMAIN_NAME" ]; then
  823. rm -rf "/var/www/$PLEROMA_DOMAIN_NAME"
  824. fi
  825. if [ -f "/etc/nginx/sites-available/$PLEROMA_DOMAIN_NAME" ]; then
  826. rm "/etc/nginx/sites-available/$PLEROMA_DOMAIN_NAME"
  827. fi
  828. if [ -d $PLEROMA_DIR ]; then
  829. rm -rf $PLEROMA_DIR
  830. fi
  831. function_check drop_database_postgresql
  832. drop_database_postgresql pleroma
  833. function_check remove_onion_service
  834. remove_onion_service pleroma ${PLEROMA_ONION_PORT}
  835. remove_app pleroma
  836. remove_completion_param install_pleroma
  837. sed -i '/pleroma domain/d' "$COMPLETION_FILE"
  838. sed -i '/pleroma commit/d' "$COMPLETION_FILE"
  839. sed -i "/$blocking_script_file/d" /etc/crontab
  840. if [ -f /usr/bin/pleroma-blocking ]; then
  841. rm /usr/bin/pleroma-blocking
  842. fi
  843. function_check remove_ddns_domain
  844. remove_ddns_domain "$PLEROMA_DOMAIN_NAME"
  845. }
  846. function image_install_pleroma {
  847. if [[ "$SOCIALINSTANCE" != 'pleroma' ]]; then
  848. return
  849. fi
  850. # shellcheck disable=SC2154
  851. chroot "$rootdir" apt-get -yq install wget imagemagick
  852. image_install_elixir
  853. image_install_postgresql
  854. }
  855. function install_pleroma {
  856. if [ ! $ONION_ONLY ]; then
  857. ONION_ONLY='no'
  858. fi
  859. apt-get -yq install wget imagemagick
  860. # We need elixir 1.4+ here, so the debian repo package won't do
  861. install_elixir
  862. function_check install_nodejs
  863. install_nodejs pleroma-backend
  864. install_postgresql
  865. if [ ! -d "/var/www/${PLEROMA_DOMAIN_NAME}/htdocs" ]; then
  866. mkdir -p "/var/www/${PLEROMA_DOMAIN_NAME}/htdocs"
  867. fi
  868. if [ -d $PLEROMA_DIR ]; then
  869. rm -rf $PLEROMA_DIR
  870. fi
  871. # get the repo
  872. if [ -f /repos/pleroma/index.html ]; then
  873. mv /repos/pleroma /repos/pleroma-fe
  874. fi
  875. if [ -d /repos/pleroma ]; then
  876. mkdir -p $PLEROMA_DIR
  877. cp -r -p /repos/pleroma/. $PLEROMA_DIR
  878. cd "$PLEROMA_DIR" || exit 834537453
  879. git pull
  880. else
  881. function_check git_clone
  882. git_clone $PLEROMA_REPO $PLEROMA_DIR
  883. fi
  884. if [ ! -d $PLEROMA_DIR ]; then
  885. echo $'Unable to clone pleroma backend repo'
  886. exit 783523
  887. fi
  888. # create user
  889. useradd -d $PLEROMA_DIR -s /bin/false pleroma
  890. # checkout the commit
  891. cd "$PLEROMA_DIR" || exit 62452428
  892. git checkout $PLEROMA_COMMIT -b $PLEROMA_COMMIT
  893. set_completion_param "pleroma commit" "$PLEROMA_COMMIT"
  894. chown -R pleroma:pleroma $PLEROMA_DIR
  895. # web config
  896. function_check add_ddns_domain
  897. add_ddns_domain "$PLEROMA_DOMAIN_NAME"
  898. PLEROMA_ONION_HOSTNAME=$(add_onion_service pleroma 80 ${PLEROMA_ONION_PORT})
  899. pleroma_nginx_site=/etc/nginx/sites-available/$PLEROMA_DOMAIN_NAME
  900. if [[ $ONION_ONLY == "no" ]]; then
  901. function_check nginx_http_redirect
  902. nginx_http_redirect "$PLEROMA_DOMAIN_NAME" "index index.html"
  903. { echo '';
  904. 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;';
  905. echo '';
  906. echo 'server {';
  907. echo ' listen 443 ssl http2;';
  908. echo ' #listen [::]:443 ssl http2;';
  909. echo " server_name $PLEROMA_DOMAIN_NAME;";
  910. echo '';
  911. echo ' # Security'; } >> "$pleroma_nginx_site"
  912. function_check nginx_ssl
  913. nginx_ssl "$PLEROMA_DOMAIN_NAME"
  914. function_check nginx_security_options
  915. nginx_security_options "$PLEROMA_DOMAIN_NAME"
  916. { echo ' add_header Strict-Transport-Security max-age=0;';
  917. echo '';
  918. echo ' # Logs';
  919. echo ' access_log /dev/null;';
  920. echo ' error_log /dev/null;';
  921. echo '';
  922. echo " root $PLEROMA_DIR;";
  923. echo '';
  924. echo ' index index.html;';
  925. echo '';
  926. echo ' gzip_vary on;';
  927. echo ' gzip_proxied any;';
  928. echo ' gzip_comp_level 6;';
  929. echo ' gzip_buffers 16 8k;';
  930. echo ' gzip_http_version 1.1;';
  931. echo ' gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript application/activity+json application/atom+xml;';
  932. echo '';
  933. echo ' location / {';
  934. echo ' client_max_body_size 15m;';
  935. echo ' client_body_buffer_size 15m;';
  936. echo '';
  937. echo ' limit_conn conn_limit_per_ip 50;';
  938. echo ' limit_req zone=req_limit_per_ip burst=50 nodelay;';
  939. echo '';
  940. echo " add_header 'Access-Control-Allow-Origin' '*' always;";
  941. echo " add_header 'Access-Control-Allow-Methods' 'POST, GET, OPTIONS' always;";
  942. echo " add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type' always;";
  943. echo " if (\$request_method = OPTIONS) {";
  944. echo ' return 204;';
  945. echo ' }';
  946. echo '';
  947. echo ' proxy_http_version 1.1;';
  948. echo " proxy_set_header Upgrade \$http_upgrade;";
  949. echo ' proxy_set_header Connection "upgrade";';
  950. echo " proxy_set_header Host \$http_host;";
  951. echo '';
  952. echo " proxy_pass http://localhost:$PLEROMA_PORT;";
  953. echo ' }';
  954. echo '';
  955. echo ' location /proxy {';
  956. echo ' client_max_body_size 15m;';
  957. echo ' client_body_buffer_size 15m;';
  958. echo '';
  959. echo ' limit_conn conn_limit_per_ip 50;';
  960. echo ' limit_req zone=req_limit_per_ip burst=50 nodelay;';
  961. echo '';
  962. echo ' proxy_cache pleroma_media_cache;';
  963. echo ' proxy_cache_lock on;';
  964. echo " proxy_pass http://localhost:$PLEROMA_PORT;";
  965. echo ' }';
  966. echo ' # include snippets/well-known.conf;';
  967. echo '}'; } >> "$pleroma_nginx_site"
  968. else
  969. 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"
  970. echo '' >> "$pleroma_nginx_site"
  971. fi
  972. { echo 'server {';
  973. echo " listen 127.0.0.1:$PLEROMA_ONION_PORT default_server http2;";
  974. echo " server_name $PLEROMA_ONION_HOSTNAME;";
  975. echo ''; } >> "$pleroma_nginx_site"
  976. function_check nginx_security_options
  977. nginx_security_options "$PLEROMA_DOMAIN_NAME"
  978. { echo '';
  979. echo ' # Logs';
  980. echo ' access_log /dev/null;';
  981. echo ' error_log /dev/null;';
  982. echo '';
  983. echo " root $PLEROMA_DIR;";
  984. echo '';
  985. echo ' index index.html;';
  986. echo '';
  987. echo ' gzip_vary on;';
  988. echo ' gzip_proxied any;';
  989. echo ' gzip_comp_level 6;';
  990. echo ' gzip_buffers 16 8k;';
  991. echo ' gzip_http_version 1.1;';
  992. echo ' gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript application/activity+json application/atom+xml;';
  993. echo '';
  994. echo ' location / {';
  995. echo ' client_max_body_size 15m;';
  996. echo ' client_body_buffer_size 15m;';
  997. echo '';
  998. echo ' limit_conn conn_limit_per_ip 50;';
  999. echo ' limit_req zone=req_limit_per_ip burst=50 nodelay;';
  1000. echo '';
  1001. echo " add_header 'Access-Control-Allow-Origin' '*' always;";
  1002. echo " add_header 'Access-Control-Allow-Methods' 'POST, GET, OPTIONS' always;";
  1003. echo " add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type' always;";
  1004. echo " if (\$request_method = OPTIONS) {";
  1005. echo ' return 204;';
  1006. echo ' }';
  1007. echo '';
  1008. echo ' proxy_http_version 1.1;';
  1009. echo " proxy_set_header Upgrade \$http_upgrade;";
  1010. echo ' proxy_set_header Connection "upgrade";';
  1011. echo " proxy_set_header Host \$http_host;";
  1012. echo '';
  1013. echo " proxy_pass http://localhost:$PLEROMA_PORT;";
  1014. echo ' }';
  1015. echo '';
  1016. echo ' location /proxy {';
  1017. echo ' client_max_body_size 15m;';
  1018. echo ' client_body_buffer_size 15m;';
  1019. echo '';
  1020. echo ' limit_conn conn_limit_per_ip 50;';
  1021. echo ' limit_req zone=req_limit_per_ip burst=50 nodelay;';
  1022. echo '';
  1023. echo ' proxy_cache pleroma_media_cache;';
  1024. echo ' proxy_cache_lock on;';
  1025. echo " proxy_pass http://localhost:$PLEROMA_PORT;";
  1026. echo ' }';
  1027. echo ' # include snippets/well-known.conf;';
  1028. echo '}'; } >> "$pleroma_nginx_site"
  1029. # back end
  1030. cd "$PLEROMA_DIR" || exit 246824684
  1031. chown -R pleroma:pleroma "$PLEROMA_DIR/"*
  1032. if ! sudo -u pleroma mix local.hex --force; then
  1033. echo $'mix local.hex failed'
  1034. exit 1745673
  1035. fi
  1036. if ! sudo -u pleroma mix deps.get --force; then
  1037. echo $'mix deps.get failed'
  1038. exit 7325733
  1039. fi
  1040. function_check pleroma_create_database
  1041. pleroma_create_database
  1042. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a pleroma -p "$PLEROMA_ADMIN_PASSWORD"
  1043. # NOTE: we don't need to install the frontend separately,
  1044. # since the backend contains a precompiled version of it
  1045. install_gnusocial_default_background "pleroma" "$PLEROMA_DOMAIN_NAME"
  1046. if [ ! -f "$PLEROMA_DIR/priv/static/static/config.json" ]; then
  1047. echo $"$PLEROMA_DIR/priv/static/static/config.json file missing"
  1048. exit 323689
  1049. fi
  1050. sed -i 's|"theme":.*|"theme": "base16-summerfruit-dark.css",|g' "$PLEROMA_DIR/priv/static/static/config.json"
  1051. if [ "$PLEROMA_BACKGROUND_IMAGE_URL" ]; then
  1052. pleroma_set_background_image_from_url $PLEROMA_DIR/priv/static "$PLEROMA_DOMAIN_NAME" "$PLEROMA_BACKGROUND_IMAGE_URL" "$PLEROMA_TITLE"
  1053. fi
  1054. # Get certificate
  1055. function_check create_site_certificate
  1056. create_site_certificate "$PLEROMA_DOMAIN_NAME" 'yes'
  1057. function_check nginx_ensite
  1058. nginx_ensite "$PLEROMA_DOMAIN_NAME"
  1059. systemctl restart postgresql
  1060. systemctl restart nginx
  1061. set_completion_param "pleroma domain" "$PLEROMA_DOMAIN_NAME"
  1062. # We need to set up the url option again because it somehow gets
  1063. # lost during mix compile
  1064. if ! grep -q 'watchers: [],' $pleroma_secret; then
  1065. sed -i 's|watchers: \[\]|watchers: \[\],|g' $pleroma_secret
  1066. fi
  1067. if ! grep -q 'url:' $pleroma_secret; then
  1068. if [[ $ONION_ONLY == 'no' ]]; then
  1069. sed -i "/watchers: /a url: [host: \"$PLEROMA_DOMAIN_NAME\", scheme: \"https\", port: 443]" $pleroma_secret
  1070. else
  1071. sed -i "/watchers: /a url: [host: \"$PLEROMA_ONION_HOSTNAME\", scheme: \"http\", port: 80]" $pleroma_secret
  1072. fi
  1073. fi
  1074. create_pleroma_blocklist
  1075. # daemon
  1076. { echo '[Unit]';
  1077. echo 'Description=Pleroma social network';
  1078. echo 'After=network.target postgresql.service';
  1079. echo '';
  1080. echo '[Service]';
  1081. echo 'User=pleroma';
  1082. echo "WorkingDirectory=$PLEROMA_DIR";
  1083. echo "Environment=\"HOME=$PLEROMA_DIR\"";
  1084. echo 'ExecStart=/usr/local/bin/mix phx.server';
  1085. echo "ExecReload=/bin/kill \$MAINPID";
  1086. echo 'KillMode=process';
  1087. echo 'Restart=on-failure';
  1088. echo '';
  1089. echo '[Install]';
  1090. echo 'WantedBy=multi-user.target';
  1091. echo 'Alias=pleroma.service'; } > /etc/systemd/system/pleroma.service
  1092. # avoid mixed content warnings
  1093. sed -i '/config :pleroma, :media_proxy/!b;n;c####enabled: true,' $PLEROMA_DIR/config/config.exs
  1094. sed -i 's|####enabled| enabled|g' $PLEROMA_DIR/config/config.exs
  1095. sed -i 's|redirect_on_failure:.*|redirect_on_failure: false|g' $PLEROMA_DIR/config/config.exs
  1096. sed -i 's|:chat, enabled:.*|:chat, enabled: false|g' $PLEROMA_DIR/config/config.exs
  1097. # set registrations open initially
  1098. sed -i 's|registrations_open:.*|registrations_open: true,|g' $PLEROMA_DIR/config/config.exs
  1099. sed -i 's|"registrationOpen":.*|"registrationOpen": true,|g' $PLEROMA_DIR/priv/static/static/config.json
  1100. if ! grep -q "media_proxy" $PLEROMA_DIR/priv/static/static/config.json; then
  1101. sed -i '/"name":/a "media_proxy": true,' $PLEROMA_DIR/priv/static/static/config.json
  1102. sed -i 's|"media_proxy"| "media_proxy"|g' $PLEROMA_DIR/priv/static/static/config.json
  1103. else
  1104. sed -i 's|"media_proxy".*|"media_proxy": false,|g' $PLEROMA_DIR/priv/static/static/config.json
  1105. fi
  1106. sed -i 's|"chatDisabled":.*|"chatDisabled": true,|g' $PLEROMA_DIR/priv/static/static/config.json
  1107. systemctl daemon-reload
  1108. systemctl enable pleroma
  1109. systemctl start pleroma
  1110. cd $PLEROMA_DIR || exit 1935638
  1111. mix register_user "$MY_USERNAME" "$MY_USERNAME" "$MY_EMAIL_ADDRESS" $"Your bio goes here" "$PLEROMA_ADMIN_PASSWORD"
  1112. APP_INSTALLED=1
  1113. }
  1114. # NOTE: deliberately there is no "exit 0"