freedombone-app-pleroma 43KB

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