freedombone-app-pleroma 46KB

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