freedombone-app-pleroma 43KB

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