configure 17 KB

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