start.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. unzip -d /app/data/wp-content/mu-plugins/ /app/code/disable-wordpress-updates.zip
  36. mv /app/data/wp-content/mu-plugins/disable-wordpress-updates/* /app/data/wp-content/mu-plugins/
  37. rm -rf /app/data/wp-content/mu-plugins/disable-wordpress-updates/
  38. unzip -d /app/data/wp-content/mu-plugins/ /app/code/wp-mail-smtp.zip
  39. mv /app/data/wp-content/mu-plugins/wp-mail-smtp/* /app/data/wp-content/mu-plugins/
  40. rm -rf /app/data/wp-content/mu-plugins/wp-mail-smtp/
  41. unzip -d /app/data/wp-content/mu-plugins/ /app/code/authLdap.zip
  42. mv /app/data/wp-content/mu-plugins/authLdap-*/* /app/data/wp-content/mu-plugins/
  43. rm -rf /app/data/wp-content/mu-plugins/authLdap-*/
  44. touch "/app/data/.dbsetup"
  45. else
  46. # Update wordpress
  47. echo "Updating wordpress database"
  48. $WP core update-db
  49. fi
  50. # configure WP mail smtp plugin (smtp_user, smtp_pass can be set when supported)
  51. echo "Configuring smtp mail"
  52. $WP option update mailer smtp
  53. $WP option update mail_from ${MAIL_SMTP_USERNAME}@${MAIL_DOMAIN}
  54. $WP option update mail_from_name ${MAIL_SMTP_USERNAME}
  55. $WP option update smtp_host ${MAIL_SMTP_SERVER}
  56. $WP option update smtp_port ${MAIL_SMTP_PORT}
  57. $WP option update smtp_auth false
  58. # reset the admin password. do this after configuring smtp mail
  59. echo "Resetting admin password"
  60. $WP user update $($WP user get admin --field=ID) --user_pass="${admin_password}"
  61. $WP user update $($WP user get admin --field=ID) --user_email="${admin_email}"
  62. # configure LDAP
  63. # https://github.com/heiglandreas/authLdap/blob/master/authLdap.php#L644
  64. echo "Configuring LDAP"
  65. ldapConfig=$(cat <<EOF
  66. {
  67. "Enabled" : true,
  68. "CachePW" : false,
  69. "URI" : "ldap://${LDAP_SERVER}:${LDAP_PORT}/${LDAP_USERS_BASE_DN}",
  70. "Filter" : "(uid=%s)",
  71. "NameAttr" : "displayname",
  72. "SecName" : "",
  73. "UidAttr" : "uid",
  74. "MailAttr" : "mail",
  75. "WebAttr" : "",
  76. "Groups" : { "administrator" : "cn=admins,${LDAP_GROUPS_BASE_DN}" },
  77. "Debug" : false,
  78. "GroupAttr" : "memberof",
  79. "GroupFilter" : "(&(objectClass=user)(uid=%s))",
  80. "DefaultRole" : "editor",
  81. "GroupEnable" : true,
  82. "GroupOverUser" : true,
  83. "Version" : 1
  84. }
  85. EOF
  86. )
  87. $WP --autoload=true --format=json option update authLDAPOptions "${ldapConfig}"
  88. chown -R www-data:www-data /app/data /run/wordpress
  89. echo "Starting apache"
  90. APACHE_CONFDIR="" source /etc/apache2/envvars
  91. rm -f "${APACHE_PID_FILE}"
  92. exec /usr/sbin/apache2 -DFOREGROUND