freedombone-app-pleroma 46KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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='7130e9ddb16286efd7d01088e816f05e82cfa2a1'
  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. W=(1 $"Set a background image"
  615. 2 $"Set the title"
  616. 3 $"Disable new account registrations"
  617. 4 $"Add a custom emoji"
  618. 5 $"Set post expiry period (currently $PLEROMA_EXPIRE_MONTHS months)")
  619. # shellcheck disable=SC2068
  620. 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)
  621. if [ ! "$selection" ]; then
  622. break
  623. fi
  624. case $selection in
  625. 1) pleroma_set_background_image;;
  626. 2) pleroma_set_title;;
  627. 3) pleroma_disable_registrations;;
  628. 4) pleroma_add_emoji;;
  629. 5) pleroma_set_expire_months;;
  630. esac
  631. done
  632. }
  633. function upgrade_pleroma {
  634. read_config_param PLEROMA_DOMAIN_NAME
  635. read_config_param PLEROMA_EXPIRE_MONTHS
  636. if ! grep -q "/media/" /etc/cron.daily/pleroma-expire; then
  637. rm $pleroma_expire_posts_script
  638. fi
  639. if [ ! -f $pleroma_expire_posts_script ]; then
  640. expire_pleroma_posts "$PLEROMA_DOMAIN_NAME" "$PLEROMA_EXPIRE_MONTHS"
  641. fi
  642. if [ ! -f $blocking_script_file ]; then
  643. create_pleroma_blocklist
  644. fi
  645. CURR_PLEROMA_COMMIT=$(get_completion_param "pleroma commit")
  646. if [[ "$CURR_PLEROMA_COMMIT" == "$PLEROMA_COMMIT" ]]; then
  647. return
  648. fi
  649. pleroma_registrations=open
  650. if grep -q 'registrations_open: false' $PLEROMA_DIR/config/config.exs; then
  651. pleroma_registrations=
  652. fi
  653. # make a copy of the configuration
  654. cp $PLEROMA_DIR/priv/static/static/config.json $PLEROMA_DIR/priv/static/static/config_prev.json
  655. if [ -f $PLEROMA_DIR/config/emoji.txt ]; then
  656. cp $PLEROMA_DIR/config/emoji.txt $PLEROMA_DIR/config/emoji_prev.txt
  657. fi
  658. apt-get -yq update
  659. apt-get -yq install --only-upgrade esl-erlang
  660. apt-get -yq install --only-upgrade elixir erlang-xmerl erlang-dev erlang-parsetools
  661. function_check set_repo_commit
  662. set_repo_commit $PLEROMA_DIR "pleroma commit" "$PLEROMA_COMMIT" $PLEROMA_REPO
  663. chown -R pleroma:pleroma $PLEROMA_DIR
  664. # restore the configuration
  665. cp $PLEROMA_DIR/priv/static/static/config_prev.json $PLEROMA_DIR/priv/static/static/config.json
  666. chown pleroma:pleroma $PLEROMA_DIR/priv/static/static/config.json
  667. if [ -f $PLEROMA_DIR/config/emoji_prev.txt ]; then
  668. cp $PLEROMA_DIR/config/emoji_prev.txt $PLEROMA_DIR/config/emoji.txt
  669. chown pleroma:pleroma $PLEROMA_DIR/config/emoji.txt
  670. rm cp $PLEROMA_DIR/config/emoji_prev.txt
  671. fi
  672. sudo -u pleroma mix deps.get
  673. if [ ! $pleroma_registrations ]; then
  674. sed -i 's|registrations_open: true|registrations_open: false|g' $PLEROMA_DIR/config/config.exs
  675. sed -i 's|registrations_open: True|registrations_open: false|g' $PLEROMA_DIR/config/config.exs
  676. fi
  677. pleroma_recompile
  678. # migrate database
  679. sudo -u pleroma mix deps.clean --build mime
  680. sudo -u pleroma mix ecto.migrate
  681. pleroma_custom_logo "$PLEROMA_DIR"
  682. expire_pleroma_posts "$PLEROMA_DOMAIN_NAME" "$PLEROMA_EXPIRE_MONTHS"
  683. create_pleroma_blocklist
  684. chown -R pleroma:pleroma $PLEROMA_DIR
  685. systemctl restart pleroma
  686. }
  687. function backup_local_pleroma {
  688. PLEROMA_DOMAIN_NAME='pleroma'
  689. if grep -q "pleroma domain" "$COMPLETION_FILE"; then
  690. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  691. fi
  692. systemctl stop pleroma
  693. function_check suspend_site
  694. suspend_site "${PLEROMA_DOMAIN_NAME}"
  695. source_directory=$PLEROMA_DIR
  696. dest_directory=pleroma
  697. backup_directory_to_usb $source_directory $dest_directory
  698. USE_POSTGRESQL=1
  699. function_check backup_database_to_usb
  700. backup_database_to_usb pleroma
  701. function_check restart_site
  702. restart_site
  703. systemctl restart pleroma
  704. }
  705. function restore_local_pleroma {
  706. if ! grep -q "pleroma domain" "$COMPLETION_FILE"; then
  707. return
  708. fi
  709. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  710. if [ "$PLEROMA_DOMAIN_NAME" ]; then
  711. echo $"Restoring pleroma"
  712. temp_restore_dir=/root/temppleroma
  713. pleroma_dir=$PLEROMA_DIR
  714. systemctl stop pleroma
  715. PLEROMA_ONION_HOSTNAME=$(cat /var/lib/tor/hidden_service_pleroma/hostname)
  716. function_check pleroma_create_database
  717. pleroma_create_database
  718. USE_POSTGRESQL=1
  719. restore_database pleroma
  720. if [ -d $temp_restore_dir ]; then
  721. rm -rf $temp_restore_dir
  722. fi
  723. function_check restore_directory_from_usb
  724. restore_directory_from_usb $temp_restore_dir pleroma
  725. if [ -d $temp_restore_dir ]; then
  726. chown -R pleroma:pleroma $pleroma_dir
  727. rm -rf $temp_restore_dir
  728. fi
  729. systemctl restart pleroma
  730. echo $"Restore of pleroma complete"
  731. fi
  732. }
  733. function backup_remote_pleroma {
  734. PLEROMA_DOMAIN_NAME='pleroma'
  735. if grep -q "pleroma domain" "$COMPLETION_FILE"; then
  736. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  737. fi
  738. systemctl stop pleroma
  739. function_check suspend_site
  740. suspend_site "${PLEROMA_DOMAIN_NAME}"
  741. source_directory=$PLEROMA_DIR
  742. dest_directory=pleroma
  743. backup_directory_to_friend $source_directory $dest_directory
  744. USE_POSTGRESQL=1
  745. function_check backup_database_to_friend
  746. backup_database_to_friend pleroma
  747. function_check restart_site
  748. restart_site
  749. systemctl restart pleroma
  750. }
  751. function restore_remote_pleroma {
  752. if ! grep -q "pleroma domain" "$COMPLETION_FILE"; then
  753. return
  754. fi
  755. PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
  756. if [ "$PLEROMA_DOMAIN_NAME" ]; then
  757. echo $"Restoring pleroma"
  758. temp_restore_dir=/root/temppleroma
  759. pleroma_dir=$PLEROMA_DIR
  760. systemctl stop pleroma
  761. PLEROMA_ONION_HOSTNAME=$(cat /var/lib/tor/hidden_service_pleroma/hostname)
  762. function_check pleroma_create_database
  763. pleroma_create_database
  764. # shellcheck disable=SC2034
  765. USE_POSTGRESQL=1
  766. function_check restore_database_from_friend
  767. restore_database_from_friend pleroma
  768. if [ -d $temp_restore_dir ]; then
  769. rm -rf $temp_restore_dir
  770. fi
  771. function_check restore_directory_from_friend
  772. restore_directory_from_friend $temp_restore_dir pleroma
  773. if [ -d $temp_restore_dir ]; then
  774. chown -R pleroma:pleroma $pleroma_dir
  775. rm -rf $temp_restore_dir
  776. fi
  777. systemctl restart pleroma
  778. echo $"Restore of pleroma complete"
  779. fi
  780. }
  781. function remove_pleroma {
  782. if [ ${#PLEROMA_DOMAIN_NAME} -eq 0 ]; then
  783. return
  784. fi
  785. systemctl stop pleroma
  786. systemctl disable pleroma
  787. rm /etc/systemd/system/pleroma.service
  788. userdel pleroma
  789. #remove_elixir
  790. function_check remove_nodejs
  791. remove_nodejs pleroma-backend
  792. read_config_param "PLEROMA_DOMAIN_NAME"
  793. read_config_param "MY_USERNAME"
  794. echo "Removing $PLEROMA_DOMAIN_NAME"
  795. nginx_dissite "$PLEROMA_DOMAIN_NAME"
  796. remove_certs "$PLEROMA_DOMAIN_NAME"
  797. if [ -d "/var/www/$PLEROMA_DOMAIN_NAME" ]; then
  798. rm -rf "/var/www/$PLEROMA_DOMAIN_NAME"
  799. fi
  800. if [ -f "/etc/nginx/sites-available/$PLEROMA_DOMAIN_NAME" ]; then
  801. rm "/etc/nginx/sites-available/$PLEROMA_DOMAIN_NAME"
  802. fi
  803. if [ -d $PLEROMA_DIR ]; then
  804. rm -rf $PLEROMA_DIR
  805. fi
  806. function_check drop_database_postgresql
  807. drop_database_postgresql pleroma
  808. function_check remove_onion_service
  809. remove_onion_service pleroma ${PLEROMA_ONION_PORT}
  810. remove_app pleroma
  811. remove_completion_param install_pleroma
  812. sed -i '/pleroma domain/d' "$COMPLETION_FILE"
  813. sed -i '/pleroma commit/d' "$COMPLETION_FILE"
  814. sed -i "/$blocking_script_file/d" /etc/crontab
  815. if [ -f /usr/bin/pleroma-blocking ]; then
  816. rm /usr/bin/pleroma-blocking
  817. fi
  818. function_check remove_ddns_domain
  819. remove_ddns_domain "$PLEROMA_DOMAIN_NAME"
  820. }
  821. function image_install_pleroma {
  822. if [[ "$SOCIALINSTANCE" != 'pleroma' ]]; then
  823. return
  824. fi
  825. # shellcheck disable=SC2154
  826. chroot "$rootdir" apt-get -yq install wget imagemagick
  827. image_install_elixir
  828. image_install_postgresql
  829. }
  830. function install_pleroma {
  831. if [ ! $ONION_ONLY ]; then
  832. ONION_ONLY='no'
  833. fi
  834. apt-get -yq install wget imagemagick
  835. # We need elixir 1.4+ here, so the debian repo package won't do
  836. install_elixir
  837. function_check install_nodejs
  838. install_nodejs pleroma-backend
  839. install_postgresql
  840. if [ ! -d "/var/www/${PLEROMA_DOMAIN_NAME}/htdocs" ]; then
  841. mkdir -p "/var/www/${PLEROMA_DOMAIN_NAME}/htdocs"
  842. fi
  843. if [ -d $PLEROMA_DIR ]; then
  844. rm -rf $PLEROMA_DIR
  845. fi
  846. # get the repo
  847. if [ -f /repos/pleroma/index.html ]; then
  848. mv /repos/pleroma /repos/pleroma-fe
  849. fi
  850. if [ -d /repos/pleroma ]; then
  851. mkdir -p $PLEROMA_DIR
  852. cp -r -p /repos/pleroma/. $PLEROMA_DIR
  853. cd "$PLEROMA_DIR" || exit 834537453
  854. git pull
  855. else
  856. function_check git_clone
  857. git_clone $PLEROMA_REPO $PLEROMA_DIR
  858. fi
  859. if [ ! -d $PLEROMA_DIR ]; then
  860. echo $'Unable to clone pleroma backend repo'
  861. exit 783523
  862. fi
  863. # create user
  864. useradd -d $PLEROMA_DIR -s /bin/false pleroma
  865. # checkout the commit
  866. cd "$PLEROMA_DIR" || exit 62452428
  867. git checkout $PLEROMA_COMMIT -b $PLEROMA_COMMIT
  868. set_completion_param "pleroma commit" "$PLEROMA_COMMIT"
  869. chown -R pleroma:pleroma $PLEROMA_DIR
  870. # web config
  871. function_check add_ddns_domain
  872. add_ddns_domain "$PLEROMA_DOMAIN_NAME"
  873. PLEROMA_ONION_HOSTNAME=$(add_onion_service pleroma 80 ${PLEROMA_ONION_PORT})
  874. pleroma_nginx_site=/etc/nginx/sites-available/$PLEROMA_DOMAIN_NAME
  875. if [[ $ONION_ONLY == "no" ]]; then
  876. function_check nginx_http_redirect
  877. nginx_http_redirect "$PLEROMA_DOMAIN_NAME" "index index.html"
  878. { echo '';
  879. 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;';
  880. echo '';
  881. echo 'server {';
  882. echo ' listen 443 ssl http2;';
  883. echo ' #listen [::]:443 ssl http2;';
  884. echo " server_name $PLEROMA_DOMAIN_NAME;";
  885. echo '';
  886. echo ' # Security'; } >> "$pleroma_nginx_site"
  887. function_check nginx_ssl
  888. nginx_ssl "$PLEROMA_DOMAIN_NAME"
  889. function_check nginx_security_options
  890. nginx_security_options "$PLEROMA_DOMAIN_NAME"
  891. { echo ' add_header Strict-Transport-Security max-age=0;';
  892. echo '';
  893. echo ' # Logs';
  894. echo ' access_log /dev/null;';
  895. echo ' error_log /dev/null;';
  896. echo '';
  897. echo " root $PLEROMA_DIR;";
  898. echo '';
  899. echo ' index index.html;';
  900. echo '';
  901. echo ' gzip_vary on;';
  902. echo ' gzip_proxied any;';
  903. echo ' gzip_comp_level 6;';
  904. echo ' gzip_buffers 16 8k;';
  905. echo ' gzip_http_version 1.1;';
  906. 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;';
  907. echo '';
  908. echo ' location / {';
  909. echo ' client_max_body_size 15m;';
  910. echo ' client_body_buffer_size 15m;';
  911. echo '';
  912. echo ' limit_conn conn_limit_per_ip 50;';
  913. echo ' limit_req zone=req_limit_per_ip burst=50 nodelay;';
  914. echo '';
  915. echo " add_header 'Access-Control-Allow-Origin' '*' always;";
  916. echo " add_header 'Access-Control-Allow-Methods' 'POST, GET, OPTIONS' always;";
  917. echo " add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type' always;";
  918. echo " if (\$request_method = OPTIONS) {";
  919. echo ' return 204;';
  920. echo ' }';
  921. echo '';
  922. echo ' proxy_http_version 1.1;';
  923. echo " proxy_set_header Upgrade \$http_upgrade;";
  924. echo ' proxy_set_header Connection "upgrade";';
  925. echo " proxy_set_header Host \$http_host;";
  926. echo '';
  927. echo " proxy_pass http://localhost:$PLEROMA_PORT;";
  928. echo ' }';
  929. echo '';
  930. echo ' location /proxy {';
  931. echo ' client_max_body_size 15m;';
  932. echo ' client_body_buffer_size 15m;';
  933. echo '';
  934. echo ' limit_conn conn_limit_per_ip 50;';
  935. echo ' limit_req zone=req_limit_per_ip burst=50 nodelay;';
  936. echo '';
  937. echo ' proxy_cache pleroma_media_cache;';
  938. echo ' proxy_cache_lock on;';
  939. echo " proxy_pass http://localhost:$PLEROMA_PORT;";
  940. echo ' }';
  941. echo ' # include snippets/well-known.conf;';
  942. echo '}'; } >> "$pleroma_nginx_site"
  943. else
  944. 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"
  945. echo '' >> "$pleroma_nginx_site"
  946. fi
  947. { echo 'server {';
  948. echo " listen 127.0.0.1:$PLEROMA_ONION_PORT default_server http2;";
  949. echo " server_name $PLEROMA_ONION_HOSTNAME;";
  950. echo ''; } >> "$pleroma_nginx_site"
  951. function_check nginx_security_options
  952. nginx_security_options "$PLEROMA_DOMAIN_NAME"
  953. { echo '';
  954. echo ' # Logs';
  955. echo ' access_log /dev/null;';
  956. echo ' error_log /dev/null;';
  957. echo '';
  958. echo " root $PLEROMA_DIR;";
  959. echo '';
  960. echo ' index index.html;';
  961. echo '';
  962. echo ' gzip_vary on;';
  963. echo ' gzip_proxied any;';
  964. echo ' gzip_comp_level 6;';
  965. echo ' gzip_buffers 16 8k;';
  966. echo ' gzip_http_version 1.1;';
  967. 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;';
  968. echo '';
  969. echo ' location / {';
  970. echo ' client_max_body_size 15m;';
  971. echo ' client_body_buffer_size 15m;';
  972. echo '';
  973. echo ' limit_conn conn_limit_per_ip 50;';
  974. echo ' limit_req zone=req_limit_per_ip burst=50 nodelay;';
  975. echo '';
  976. echo " add_header 'Access-Control-Allow-Origin' '*' always;";
  977. echo " add_header 'Access-Control-Allow-Methods' 'POST, GET, OPTIONS' always;";
  978. echo " add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type' always;";
  979. echo " if (\$request_method = OPTIONS) {";
  980. echo ' return 204;';
  981. echo ' }';
  982. echo '';
  983. echo ' proxy_http_version 1.1;';
  984. echo " proxy_set_header Upgrade \$http_upgrade;";
  985. echo ' proxy_set_header Connection "upgrade";';
  986. echo " proxy_set_header Host \$http_host;";
  987. echo '';
  988. echo " proxy_pass http://localhost:$PLEROMA_PORT;";
  989. echo ' }';
  990. echo '';
  991. echo ' location /proxy {';
  992. echo ' client_max_body_size 15m;';
  993. echo ' client_body_buffer_size 15m;';
  994. echo '';
  995. echo ' limit_conn conn_limit_per_ip 50;';
  996. echo ' limit_req zone=req_limit_per_ip burst=50 nodelay;';
  997. echo '';
  998. echo ' proxy_cache pleroma_media_cache;';
  999. echo ' proxy_cache_lock on;';
  1000. echo " proxy_pass http://localhost:$PLEROMA_PORT;";
  1001. echo ' }';
  1002. echo ' # include snippets/well-known.conf;';
  1003. echo '}'; } >> "$pleroma_nginx_site"
  1004. # back end
  1005. cd "$PLEROMA_DIR" || exit 246824684
  1006. chown -R pleroma:pleroma "$PLEROMA_DIR/"*
  1007. if ! sudo -u pleroma mix local.hex --force; then
  1008. echo $'mix local.hex failed'
  1009. exit 1745673
  1010. fi
  1011. if ! sudo -u pleroma mix deps.get --force; then
  1012. echo $'mix deps.get failed'
  1013. exit 7325733
  1014. fi
  1015. function_check pleroma_create_database
  1016. pleroma_create_database
  1017. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a pleroma -p "$PLEROMA_ADMIN_PASSWORD"
  1018. # NOTE: we don't need to install the frontend separately,
  1019. # since the backend contains a precompiled version of it
  1020. install_gnusocial_default_background "pleroma" "$PLEROMA_DOMAIN_NAME"
  1021. if [ ! -f "$PLEROMA_DIR/priv/static/static/config.json" ]; then
  1022. echo $"$PLEROMA_DIR/priv/static/static/config.json file missing"
  1023. exit 323689
  1024. fi
  1025. sed -i 's|"theme":.*|"theme": "base16-summerfruit-dark.css",|g' "$PLEROMA_DIR/priv/static/static/config.json"
  1026. if [ "$PLEROMA_BACKGROUND_IMAGE_URL" ]; then
  1027. pleroma_set_background_image_from_url $PLEROMA_DIR/priv/static "$PLEROMA_DOMAIN_NAME" "$PLEROMA_BACKGROUND_IMAGE_URL" "$PLEROMA_TITLE"
  1028. fi
  1029. # Get certificate
  1030. function_check create_site_certificate
  1031. create_site_certificate "$PLEROMA_DOMAIN_NAME" 'yes'
  1032. function_check nginx_ensite
  1033. nginx_ensite "$PLEROMA_DOMAIN_NAME"
  1034. systemctl restart postgresql
  1035. systemctl restart nginx
  1036. set_completion_param "pleroma domain" "$PLEROMA_DOMAIN_NAME"
  1037. # We need to set up the url option again because it somehow gets
  1038. # lost during mix compile
  1039. pleroma_secret=$PLEROMA_DIR/config/dev.secret.exs
  1040. if ! grep -q 'watchers: [],' $pleroma_secret; then
  1041. sed -i 's|watchers: \[\]|watchers: \[\],|g' $pleroma_secret
  1042. fi
  1043. if ! grep -q 'url:' $pleroma_secret; then
  1044. if [[ $ONION_ONLY == 'no' ]]; then
  1045. sed -i "/watchers: /a url: [host: \"$PLEROMA_DOMAIN_NAME\", scheme: \"https\", port: 443]" $pleroma_secret
  1046. else
  1047. sed -i "/watchers: /a url: [host: \"$PLEROMA_ONION_HOSTNAME\", scheme: \"http\", port: 80]" $pleroma_secret
  1048. fi
  1049. fi
  1050. create_pleroma_blocklist
  1051. # daemon
  1052. { echo '[Unit]';
  1053. echo 'Description=Pleroma social network';
  1054. echo 'After=network.target postgresql.service';
  1055. echo '';
  1056. echo '[Service]';
  1057. echo 'User=pleroma';
  1058. echo "WorkingDirectory=$PLEROMA_DIR";
  1059. echo "Environment=\"HOME=$PLEROMA_DIR\"";
  1060. echo 'ExecStart=/usr/local/bin/mix phx.server';
  1061. echo "ExecReload=/bin/kill \$MAINPID";
  1062. echo 'KillMode=process';
  1063. echo 'Restart=on-failure';
  1064. echo '';
  1065. echo '[Install]';
  1066. echo 'WantedBy=multi-user.target';
  1067. echo 'Alias=pleroma.service'; } > /etc/systemd/system/pleroma.service
  1068. # avoid mixed content warnings
  1069. sed -i '/config :pleroma, :media_proxy/!b;n;c####enabled: true,' $PLEROMA_DIR/config/config.exs
  1070. sed -i 's|####enabled| enabled|g' $PLEROMA_DIR/config/config.exs
  1071. sed -i 's|redirect_on_failure:.*|redirect_on_failure: false|g' $PLEROMA_DIR/config/config.exs
  1072. # set registrations open initially
  1073. sed -i 's|registrations_open:.*|registrations_open: true,|g' $PLEROMA_DIR/config/config.exs
  1074. sed -i 's|"registrationOpen":.*|"registrationOpen": true,|g' $PLEROMA_DIR/priv/static/static/config.json
  1075. if ! grep -q "media_proxy" $PLEROMA_DIR/priv/static/static/config.json; then
  1076. sed -i '/"name":/a "media_proxy": true,' $PLEROMA_DIR/priv/static/static/config.json
  1077. sed -i 's|"media_proxy"| "media_proxy"|g' $PLEROMA_DIR/priv/static/static/config.json
  1078. else
  1079. sed -i 's|"media_proxy".*|"media_proxy": false,|g' $PLEROMA_DIR/priv/static/static/config.json
  1080. fi
  1081. systemctl daemon-reload
  1082. systemctl enable pleroma
  1083. systemctl start pleroma
  1084. APP_INSTALLED=1
  1085. }
  1086. # NOTE: deliberately there is no "exit 0"