start.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/bash
  2. set -eu
  3. readonly WP="/app/code/wp --allow-root"
  4. readonly admin_password=$(pwgen -1y 16)
  5. readonly admin_email=${MAIL_SMTP_USERNAME}@${MAIL_DOMAIN}
  6. echo "Admin password is ${admin_password} and email is ${admin_email}"
  7. # Settings to be updated on every run. Regenerating salts means users have to relogin
  8. sed -e "s/##MYSQL_DATABASE/${MYSQL_DATABASE}/" \
  9. -e "s/##MYSQL_USERNAME/${MYSQL_USERNAME}/" \
  10. -e "s/##MYSQL_PASSWORD/${MYSQL_PASSWORD}/" \
  11. -e "s/##MYSQL_HOST/${MYSQL_HOST}:${MYSQL_PORT}/" \
  12. -e "s,##APP_ORIGIN,${APP_ORIGIN}," \
  13. -e "s/##AUTH_KEY/$(pwgen -1cns 64)/" \
  14. -e "s/##SECURE_AUTH_KEY/$(pwgen -1cns 64)/" \
  15. -e "s/##LOGGED_IN_KEY/$(pwgen -1cns 64)/" \
  16. -e "s/##NONCE_KEY/$(pwgen -1cns 64)/" \
  17. -e "s/##AUTH_SALT/$(pwgen -1cns 64)/" \
  18. -e "s/##SECURE_AUTH_SALT/$(pwgen -1cns 64)/" \
  19. -e "s/##LOGGED_IN_SALT/$(pwgen -1cns 64)/" \
  20. -e "s/##NONCE_SALT/$(pwgen -1cns 64)/" \
  21. /app/code/wp-config.php.template > /run/wordpress/wp-config.php # sed -i seems to destroy symlink
  22. if [[ ! -f "/app/data/.dbsetup" ]]; then
  23. echo "Copying wp-content files on first run"
  24. mkdir -p /app/data/wp-content/mu-plugins
  25. cp -r /app/code/wp-content-vanilla/* /app/data/wp-content/
  26. $WP --url="${APP_ORIGIN}" --require=/run/wordpress/disable-new-blog-notification.php core install \
  27. --url="${APP_ORIGIN}" \
  28. --title="My blog" \
  29. --admin_user=admin \
  30. --admin_password="${admin_password}" \
  31. --admin_email="${admin_email}"
  32. echo "WP is now installed"
  33. # install and backup the plugins. mu plugins are a "flat" structure"
  34. # sadly mu-plugins can still be re-configured, just not uninstallable
  35. echo "Unpacking plugins on first run"
  36. unzip -d /app/data/wp-content/mu-plugins/ /app/code/disable-wordpress-updates.zip
  37. mv /app/data/wp-content/mu-plugins/disable-wordpress-updates/* /app/data/wp-content/mu-plugins/
  38. rm -rf /app/data/wp-content/mu-plugins/disable-wordpress-updates/
  39. unzip -d /app/data/wp-content/mu-plugins/ /app/code/wp-mail-smtp.zip
  40. mv /app/data/wp-content/mu-plugins/wp-mail-smtp/* /app/data/wp-content/mu-plugins/
  41. rm -rf /app/data/wp-content/mu-plugins/wp-mail-smtp/
  42. unzip -d /app/data/wp-content/mu-plugins/ /app/code/authLdap.zip
  43. mv /app/data/wp-content/mu-plugins/authLdap-*/* /app/data/wp-content/mu-plugins/
  44. rm -rf /app/data/wp-content/mu-plugins/authLdap-*/
  45. touch "/app/data/.dbsetup"
  46. else
  47. # Update wordpress
  48. echo "Updating wordpress database"
  49. $WP core update-db
  50. fi
  51. # configure WP mail smtp plugin (smtp_user, smtp_pass can be set when supported)
  52. echo "Configuring smtp mail"
  53. $WP option update mailer smtp
  54. $WP option update mail_from ${MAIL_SMTP_USERNAME}@${MAIL_DOMAIN}
  55. $WP option update mail_from_name WordPress
  56. $WP option update smtp_host ${MAIL_SMTP_SERVER}
  57. $WP option update smtp_port ${MAIL_SMTP_PORT}
  58. $WP option update smtp_auth false
  59. # reset the admin password. do this after configuring smtp mail
  60. echo "Resetting admin password"
  61. $WP user update $($WP user get admin --field=ID) --user_pass="${admin_password}"
  62. $WP user update $($WP user get admin --field=ID) --user_email="${admin_email}"
  63. # configure LDAP
  64. # https://github.com/heiglandreas/authLdap/blob/master/authLdap.php#L644
  65. echo "Configuring LDAP"
  66. ldapConfig=$(cat <<EOF
  67. {
  68. "Enabled" : true,
  69. "CachePW" : false,
  70. "URI" : "ldap://${LDAP_SERVER}:${LDAP_PORT}/${LDAP_USERS_BASE_DN}",
  71. "Filter" : "(|(mail=%1\$s)(username=%1\$s))",
  72. "NameAttr" : "givenName",
  73. "SecName" : "sn",
  74. "UidAttr" : "username",
  75. "MailAttr" : "mail",
  76. "WebAttr" : "",
  77. "Groups" : { "administrator" : "cn=admins,${LDAP_GROUPS_BASE_DN}" },
  78. "Debug" : false,
  79. "GroupAttr" : "memberof",
  80. "GroupFilter" : "(|(mail=%1\$s)(username=%1\$s))",
  81. "DefaultRole" : "editor",
  82. "GroupEnable" : true,
  83. "GroupOverUser" : true,
  84. "Version" : 1
  85. }
  86. EOF
  87. )
  88. $WP --autoload=true --format=json option update authLDAPOptions "${ldapConfig}"
  89. touch /app/data/htaccess
  90. chown -R www-data:www-data /app/data /run/wordpress
  91. echo "Starting apache"
  92. APACHE_CONFDIR="" source /etc/apache2/envvars
  93. rm -f "${APACHE_PID_FILE}"
  94. exec /usr/sbin/apache2 -DFOREGROUND