configure 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. #!/bin/sh
  2. # ------------ Find the root folder where social is installed --------------
  3. INSTALL_DIR="${PWD}"
  4. while true; do
  5. if [ ! -f "${INSTALL_DIR}/social.yaml" ]; then
  6. INSTALL_DIR="$(dirname "${INSTALL_DIR}")"
  7. elif [ "${INSTALL_DIR}" = '/' ]; then
  8. echo "The current folder and it's parents don't seem to contain a valid GNU social installation, exiting"
  9. exit 1
  10. else
  11. break
  12. fi
  13. done
  14. cd "${INSTALL_DIR}" || exit 1
  15. # --------------------------------------------------------------------------
  16. # ------------ Check whether the system has whiptail or dialog -------------
  17. if command -v whiptail > /dev/null 2>&1; then
  18. WHIPTAIL=whiptail
  19. elif command -v dialog > /dev/null 2>&1; then
  20. WHIPTAIL=dialog
  21. else
  22. echo "whiptail/dialog are not available, can't proceed"
  23. exit 1
  24. fi
  25. # whiptail/dialog exits with 1 when cancelling through the UI, or 255 on ^C
  26. validate_exit () {
  27. case $1 in
  28. 1|255) printf "Canceling...\n" && exit 2 ;;
  29. esac
  30. }
  31. # --------------------------------------------------------------------------
  32. # TODO Add suport for other webservers
  33. # ------------ Pick which services to configure through docker-compose and which to configure externally --------------
  34. SERVICES=$(${WHIPTAIL} --title 'GNU social' --clear --backtitle 'GNU social' \
  35. --menu "\nWelcome to the GNU social configurator. This program will help configure your GNU social node.\n\n\
  36. Choose whether you prefer social to handle all the services it needs though docker,\nor if you'd rather use and configure your own:" 0 0 0 \
  37. docker 'Docker service configuration' \
  38. mixed 'Mixed docker/external service configuration' \
  39. external 'External service configuration' \
  40. 3>&1 1>&2 2>&3)
  41. validate_exit $?
  42. case ${SERVICES} in
  43. 'docker') DOCKER='"nginx" "certbot" "php" "db" "redis" "worker"' ;; # TODO enable and configure "mail"
  44. 'mixed')
  45. DOCKER=$(${WHIPTAIL} --title 'GNU social Docker services' --clear --backtitle 'GNU social' \
  46. --checklist "\nPick which of the following services you'd like to add to docker-compose.\n* indicates a service that has extra configuration" 0 0 0 \
  47. nginx 'Configure NGINX' on \
  48. certbot "Configure CertBot (automatic certificate renewing)" on \
  49. php 'Configure PHP' on \
  50. db 'Configure a DBMS*' on \
  51. redis 'Configure Redis (optional, recommended)' on \
  52. mail 'Confugure a mail server*' on \
  53. worker 'Confugure container with worker queues' on \
  54. 3>&1 1>&2 2>&3)
  55. validate_exit $?
  56. ;;
  57. 'external') DOCKER='' ;;
  58. esac
  59. # --------------------------------------------------------------------------
  60. # ------------ If the user requested the use of docker services, ensure we have `docker` and `docker-compose` --------------
  61. case ${SERVICES} in
  62. 'mixed'|'docker')
  63. if ! (command -v docker > /dev/null 2>&1 && command -v docker-compose > /dev/null 2>&1); then
  64. echo "docker/docker-compose are not available, can't proceed"
  65. exit 1
  66. fi
  67. ;;
  68. esac
  69. # --------------------------------------------------------------------------
  70. # ------------ Regarless of whether using a docker container for the DBMS or not, we need to know which we're using, and it's settings --------------
  71. DBMS=$(${WHIPTAIL} --title 'GNU social DBMS' --clear --backtitle 'GNU social' \
  72. --radiolist "\nPick which DBMS you'd like to use" 0 0 0 \
  73. postgres 'Use PostgreSQL' on \
  74. mariadb 'Use MariaDB' off \
  75. 3>&1 1>&2 2>&3)
  76. validate_exit $?
  77. while true; do
  78. DB_NAME=$(${WHIPTAIL} --title 'GNU social DB name' --clear --backtitle 'GNU social' \
  79. --inputbox "\nEnter a name for the database to be used by social" 0 0 "social" \
  80. 3>&1 1>&2 2>&3)
  81. validate_exit $?
  82. if [ -n "${DB_NAME}" ]; then break; fi
  83. done
  84. if [ "${DBMS}" = 'postgres' ]; then DB_USER="postgres"; else DB_USER="social"; fi
  85. if echo "${DOCKER}" | grep -Fvq '"db"'; then
  86. while true; do
  87. DB_USER=$(${WHIPTAIL} --title 'GNU social DB user' --clear --backtitle 'GNU social' \
  88. --inputbox "\nEnter a user name for social to connect to the database under" 0 0 "${DB_USER}" \
  89. 3>&1 1>&2 2>&3)
  90. validate_exit $?
  91. if [ -n "${DB_USER}" ]; then break; fi
  92. done
  93. fi
  94. while true; do
  95. DB_PASSWORD=$(${WHIPTAIL} --title 'GNU social DB password' --clear --backtitle 'GNU social' \
  96. --passwordbox "\nEnter a password for social to connect to the database with" 0 0 \
  97. 3>&1 1>&2 2>&3)
  98. validate_exit $?
  99. if [ -n "${DB_PASSWORD}" ]; then break; fi
  100. done
  101. if [ "${DBMS}" = 'postgres' ]; then DB_DSN="postgresql://${DB_USER}:${DB_PASSWORD}@db:5432/${DB_NAME}";
  102. else DB_DSN="mysql://${DB_USER}:${DB_PASSWORD}@db:3306/${DB_NAME}"; fi
  103. if echo "${DOCKER}" | grep -Fvq '"db"'; then
  104. while true; do
  105. DB_DSN=$(${WHIPTAIL} --title 'GNU social DB DSN' --clear --backtitle 'GNU social' \
  106. --inputbox "\nEnter the DSN/URL for social to connect to the database with" 0 0 \
  107. 3>&1 1>&2 2>&3)
  108. validate_exit $?
  109. if [ -n "${DB_DSN}" ]; then break; fi
  110. done
  111. fi
  112. if [ "${DBMS}" != 'postgres' ] && echo "${DOCKER}" | grep -Fq '"db"'; then
  113. while true; do
  114. DB_ROOT_PASSWORD=$(${WHIPTAIL} --title 'GNU social DB root user password' --clear --backtitle 'GNU social' \
  115. --passwordbox "\nEnter a password for the database root user" 0 0 \
  116. 3>&1 1>&2 2>&3)
  117. validate_exit $?
  118. if [ -n "${DB_ROOT_PASSWORD}" ]; then break; fi
  119. done
  120. fi
  121. # --------------------------------------------------------------------------
  122. # -------------------------------- PHP -------------------------------------
  123. if echo "${DOCKER}" | grep -Fq '"php"'; then
  124. ${WHIPTAIL} --title "Build PHP container locally?" --clear --backtitle 'GNU social' \
  125. --yesno "\nDo you want to compile the needed PHP extensions and build the container locally? (May provide better performance but requires more than 1GiB of RAM)" 0 0 \
  126. --defaultno \
  127. 3>&1 1>&2 2>&3
  128. BUILD_PHP=$((1-$?)) # Invert output
  129. fi
  130. # --------------------------------------------------------------------------
  131. # ------------------------ Network configuration ----------------------------
  132. while true; do
  133. DOMAIN_ROOT=$(${WHIPTAIL} --title 'GNU social domain root' --clear --backtitle 'GNU social' \
  134. --inputbox "\nEnter the root domain from where social will be served" 0 0 \
  135. 3>&1 1>&2 2>&3)
  136. validate_exit $?
  137. if [ -n "${DOMAIN_ROOT}" ]; then break; fi
  138. done
  139. # Subdomain is optional
  140. SUBDOMAIN=$(${WHIPTAIL} --title 'GNU social subdomain' --clear --backtitle 'GNU social' \
  141. --inputbox "\nEnter the subdomain from where social will be served, if any" 0 0 \
  142. 3>&1 1>&2 2>&3)
  143. validate_exit $?
  144. if [ -z "${SUBDOMAIN}" ]; then
  145. DOMAIN="${DOMAIN_ROOT}"
  146. else
  147. DOMAIN="${SUBDOMAIN}.${DOMAIN_ROOT}"
  148. fi
  149. ${WHIPTAIL} --title "Use Let's Encrypt certificate?" --clear --backtitle 'GNU social' \
  150. --yesno "\nDo you want to use a certificate signed by Let's Encrypt? A self signed certificate will be created, \
  151. as one is required, but you may provide your own" 0 0 \
  152. 3>&1 1>&2 2>&3
  153. LE_CERT=$((1-$?)) # Invert output
  154. if [ $LE_CERT -ne 0 ]; then
  155. while true; do
  156. EMAIL=$(${WHIPTAIL} --title 'GNU social admin email' --clear --backtitle 'GNU social' \
  157. --inputbox "\nEnter the email to register the admin user under" 0 0 \
  158. 3>&1 1>&2 2>&3)
  159. validate_exit $?
  160. if [ -n "${EMAIL}" ]; then break; fi
  161. done
  162. fi
  163. while true; do
  164. NODE_NAME=$(${WHIPTAIL} --title 'GNU social node name' --clear --backtitle 'GNU social' \
  165. --inputbox "\nEnter the name for this GNU social node" 0 0 \
  166. 3>&1 1>&2 2>&3)
  167. validate_exit $?
  168. if [ -n "${NODE_NAME}" ]; then break; fi
  169. done
  170. while true; do
  171. NGINX_HTTP_PORT=$(${WHIPTAIL} --title 'GNU social HTTP port' --clear --backtitle 'GNU social' \
  172. --inputbox "\nWhich port should NGINX use for HTTP traffic ('host:port' is also valid)" 0 0 "80" \
  173. 3>&1 1>&2 2>&3)
  174. validate_exit $?
  175. if [ -n "${NGINX_HTTP_PORT}" ]; then break; fi
  176. done
  177. while true; do
  178. NGINX_HTTPS_PORT=$(${WHIPTAIL} --title 'GNU social HTTPS port' --clear --backtitle 'GNU social' \
  179. --inputbox "\nWhich port should NGINX use for HTTPS traffic ('host:port' is also valid)" 0 0 "443" \
  180. 3>&1 1>&2 2>&3)
  181. validate_exit $?
  182. if [ -n "${NGINX_HTTPS_PORT}" ]; then break; fi
  183. done
  184. PHP_PORT=9000
  185. if echo "${DOCKER}" | grep -Fvq '"php"'; then
  186. while true; do
  187. PHP_PORT=$(${WHIPTAIL} --title 'GNU social PHP service port' --clear --backtitle 'GNU social' \
  188. --inputbox "\nWhich port should be used for PHP" 0 0 "9000" \
  189. 3>&1 1>&2 2>&3)
  190. validate_exit $?
  191. if [ -n "${PHP_PORT}" ]; then break; fi
  192. done
  193. fi
  194. # --------------------------------------------------------------------------
  195. PROFILE=$(${WHIPTAIL} --title 'GNU social site profile' --clear --backtitle 'GNU social' \
  196. --menu "\nPick one of the following node visibility presets:" 0 0 0 \
  197. public 'Make this node publicly accessible, with open registration' \
  198. community 'Make this node publicly accessible, but with invite-only registration' \
  199. isolated 'Make this node publicly accessible, with open registration but do not federate' \
  200. private 'Make this node publicly accessible, but with invite-only registration, only registered users can see feeds' \
  201. single_user 'Like public, but only allows registering one user' \
  202. 3>&1 1>&2 2>&3)
  203. validate_exit $?
  204. # ------------ Mail server --------------
  205. MAILER_DSN='sendmail://localhost'
  206. if false; then
  207. if echo "${DOCKER}" | grep -Fvq '"mail"'; then
  208. while true; do
  209. MAILER_DSN=$(${WHIPTAIL} --title 'GNU social mail server DSN' --clear --backtitle 'GNU social' \
  210. --inputbox "\nEnter a DSN/URL social will use to connect to the mail server" 0 0 "${MAILER_DSN}" \
  211. 3>&1 1>&2 2>&3)
  212. validate_exit $?
  213. if [ -n "${MAILER_DSN}" ]; then break; fi
  214. done
  215. while true; do
  216. MAIL_DOMAIN=$(${WHIPTAIL} --title 'GNU social mail server domain' --clear --backtitle 'GNU social' \
  217. --inputbox "\nEnter the domain social will use to serve mail" 0 0 "${DOMAIN_ROOT}" \
  218. 3>&1 1>&2 2>&3)
  219. validate_exit $?
  220. if [ -n "${MAIL_DOMAIN}" ]; then break; fi
  221. done
  222. fi
  223. if echo "${DOCKER}" | grep -Fq '"mail"'; then
  224. while true; do
  225. MAIL_DOMAIN_ROOT=$(${WHIPTAIL} --title 'GNU social mail server domain' --clear --backtitle 'GNU social' \
  226. --inputbox "\nEnter the root domain social will use to serve mail" 0 0 "${DOMAIN_ROOT}" \
  227. 3>&1 1>&2 2>&3)
  228. validate_exit $?
  229. if [ -n "${MAIL_DOMAIN_ROOT}" ]; then break; fi
  230. done
  231. MAIL_SUBDOMAIN=$(${WHIPTAIL} --title 'GNU social mail server subdomain' --clear --backtitle 'GNU social' \
  232. --inputbox "\nEnter a subdomain social will send email from (optional, can be empty)" 0 0 \
  233. 3>&1 1>&2 2>&3)
  234. validate_exit $?
  235. if [ -z "${MAIL_SUBDOMAIN}" ]; then
  236. MAIL_DOMAIN="${MAIL_DOMAIN_ROOT}"
  237. else
  238. MAIL_DOMAIN="${MAIL_SUBDOMAIN}.${MAIL_DOMAIN_ROOT}"
  239. fi
  240. while true; do
  241. MAIL_SENDER_USER=$(${WHIPTAIL} --title 'GNU social mail sender user' --clear --backtitle 'GNU social' \
  242. --inputbox "\nEnter the user emails should be sent from (email without @domain)" 0 0 \
  243. 3>&1 1>&2 2>&3)
  244. validate_exit $?
  245. if [ -n "${MAIL_SENDER_USER}" ]; then break; fi
  246. done
  247. while true; do
  248. MAIL_SENDER_NAME=$(${WHIPTAIL} --title 'GNU social mail sender name' --clear --backtitle 'GNU social' \
  249. --inputbox "\nEnter the name emails should be sent from" 0 0 "${NODE_NAME}" \
  250. 3>&1 1>&2 2>&3)
  251. validate_exit $?
  252. if [ -n "${MAIL_SENDER_NAME}" ]; then break; fi
  253. done
  254. while true; do
  255. MAIL_PASSWORD=$(${WHIPTAIL} --title 'GNU social mail password' --clear --backtitle 'GNU social' \
  256. --passwordbox "\nEnter a password for the user in the mail server" 0 0 \
  257. 3>&1 1>&2 2>&3)
  258. validate_exit $?
  259. if [ -n "${MAIL_PASSWORD}" ]; then break; fi
  260. done
  261. fi
  262. fi
  263. # --------------------------------------------------------------------------
  264. # --------------- Ensure we have the needed certificates -------------------
  265. mkdir -p "${INSTALL_DIR}/docker/bootstrap"
  266. cat > "${INSTALL_DIR}/docker/bootstrap/bootstrap.env" <<EOF
  267. #!/bin/sh
  268. DOMAIN_ROOT=${DOMAIN_ROOT}
  269. WEB_DOMAIN=${DOMAIN}
  270. MAIL_DOMAIN=${MAIL_DOMAIN}
  271. SIGNED=${LE_CERT}
  272. EOF
  273. [ -n "${EMAIL}" ] && echo EMAIL="${EMAIL}" >> "${INSTALL_DIR}/docker/bootstrap/bootstrap.env"
  274. chmod +x ./docker/bootstrap/bootstrap.env
  275. docker-compose -f docker/bootstrap/bootstrap.yaml up
  276. validate_exit $?
  277. # --------------------------------------------------------------------------
  278. # ------------ Configure parameters for the creation of docker containers --------------
  279. mkdir -p "${INSTALL_DIR}/docker/db"
  280. if [ "${DBMS}" = 'postgres' ]; then
  281. cat > "${INSTALL_DIR}/docker/db/db.env" <<EOF
  282. DBMS=${DBMS}
  283. POSTGRES_USER=postgres
  284. POSTGRES_PASSWORD=${DB_PASSWORD}
  285. EOF
  286. else
  287. cat > "${INSTALL_DIR}/docker/db/db.env" <<EOF
  288. DBMS=${DBMS}
  289. MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
  290. MYSQL_DATABASE=${DB_NAME}
  291. MYSQL_USER=${DB_USER}
  292. MYSQL_PASSWORD=${DB_PASSWORD}
  293. EOF
  294. fi
  295. touch .env.local
  296. sed -ri 's/DATABASE_URL=.*//' .env.local
  297. echo "DATABASE_URL=${DB_DSN}" >> .env.local
  298. sed -ri 's/MAILER_DSN=.*//' .env.local
  299. echo "MAILER_DSN=${MAILER_DSN}" >> .env.local
  300. mkdir -p "${INSTALL_DIR}/docker/social"
  301. cat > "${INSTALL_DIR}/docker/social/social.env" <<EOF
  302. SOCIAL_DBMS=${DBMS}
  303. SOCIAL_DB=${DB_NAME}
  304. SOCIAL_USER=${DB_USER}
  305. SOCIAL_PASSWORD=${DB_PASSWORD}
  306. SOCIAL_DOMAIN=${DOMAIN}
  307. SOCIAL_NODE_NAME=${NODE_NAME}
  308. SOCIAL_ADMIN_EMAIL=${EMAIL}
  309. SOCIAL_SITE_PROFILE=${PROFILE}
  310. MAILER_DSN=${MAILER_DSN}
  311. EOF
  312. # --------------------------------------------------------------------------
  313. # TODO create admin user
  314. #SOCIAL_ADMIN_NICK="${ADMIN_NICK}"
  315. #SOCIAL_ADMIN_PASSWORD="${ADMIN_PASSWORD}"
  316. # --------------- Write mail configuration, and setup ----------------------
  317. mkdir -p "${INSTALL_DIR}/docker/mail"
  318. HASHED_PASSWORD="{SHA512-CRYPT}"$(echo "${MAIL_PASSWORD}" | openssl passwd -6 -in -)
  319. cat > "${INSTALL_DIR}/docker/mail/mail.env" <<EOF
  320. MAIL_DOMAIN=${MAIL_DOMAIN}
  321. MAIL_DOMAIN_ROOT=${MAIL_DOMAIN_ROOT}
  322. MAIL_USER=${MAIL_SENDER_USER}
  323. MAIL_NAME=${MAIL_SENDER_NAME}
  324. MAIL_ADDRESS=${MAIL_SENDER_USER}@${MAIL_DOMAIN}
  325. SSL_CERT=/etc/letsencrypt/live/${MAIL_DOMAIN}/fullchain.pem
  326. SSL_KEY=/etc/letsencrypt/live/${MAIL_DOMAIN}/privkey.pem
  327. HASHED_PASSWORD=${HASHED_PASSWORD}
  328. EOF
  329. # --------------------------------------------------------------------------
  330. # ------------------- Write docker-compose config file ---------------------
  331. cat > "${INSTALL_DIR}/docker-compose.yaml" <<EOF
  332. version: '3'
  333. services:
  334. EOF
  335. export DOCKER="${DOCKER}"
  336. export NGINX_HTTP_PORT="${NGINX_HTTP_PORT}"
  337. export NGINX_HTTPS_PORT="${NGINX_HTTPS_PORT}"
  338. export PHP_PORT="${PHP_PORT}"
  339. export DBMS="${DBMS}"
  340. export BUILD_PHP="${BUILD_PHP}"
  341. export LE_CERT="${LE_CERT}"
  342. for SERV in ${DOCKER}; do
  343. SERV=$(echo "${SERV}" | sed -r 's/"([^"]*)"/\1/')
  344. sh "${INSTALL_DIR}/docker/${SERV}/docker-compose.fragment.sh" >> "${INSTALL_DIR}/docker-compose.yaml"
  345. done
  346. if echo "${DOCKER}" | grep -Fq '"db"'; then
  347. cat >> "${INSTALL_DIR}/docker-compose.yaml" <<EOF
  348. volumes:
  349. database:
  350. EOF
  351. fi
  352. # --------------------------------------------------------------------------
  353. cd "${OLDPWD}" || exit 1