freedombone-app-pleroma 46KB

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