freedombone-app-pleroma 43KB

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