start.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. # --skip-email is part of 0.23.0 (https://github.com/wp-cli/wp-cli/pull/2345)
  27. $WP --url="${APP_ORIGIN}" --skip-email core install \
  28. --url="${APP_ORIGIN}" \
  29. --title="My blog" \
  30. --admin_user=admin \
  31. --admin_password="${admin_password}" \
  32. --admin_email="${admin_email}"
  33. echo "WP is now installed"
  34. # install and backup the plugins. mu plugins are a "flat" structure"
  35. # sadly mu-plugins can still be re-configured, just not uninstallable
  36. echo "Unpacking plugins on first run"
  37. unzip -d /app/data/wp-content/mu-plugins/ /app/code/disable-wordpress-updates.zip
  38. mv /app/data/wp-content/mu-plugins/disable-wordpress-updates/* /app/data/wp-content/mu-plugins/
  39. rm -rf /app/data/wp-content/mu-plugins/disable-wordpress-updates/
  40. unzip -d /app/data/wp-content/mu-plugins/ /app/code/wp-mail-smtp.zip
  41. mv /app/data/wp-content/mu-plugins/wp-mail-smtp/* /app/data/wp-content/mu-plugins/
  42. rm -rf /app/data/wp-content/mu-plugins/wp-mail-smtp/
  43. unzip -d /app/data/wp-content/mu-plugins/ /app/code/authLdap.zip
  44. mv /app/data/wp-content/mu-plugins/authLdap-*/* /app/data/wp-content/mu-plugins/
  45. rm -rf /app/data/wp-content/mu-plugins/authLdap-*/
  46. touch "/app/data/.dbsetup"
  47. else
  48. # Update wordpress
  49. echo "Updating wordpress database"
  50. $WP core update-db
  51. fi
  52. # configure WP mail smtp plugin (smtp_user, smtp_pass can be set when supported)
  53. echo "Configuring smtp mail"
  54. $WP option update mailer smtp
  55. $WP option update mail_from ${MAIL_SMTP_USERNAME}@${MAIL_DOMAIN}
  56. $WP option update mail_from_name WordPress
  57. $WP option update smtp_host ${MAIL_SMTP_SERVER}
  58. $WP option update smtp_port ${MAIL_SMTP_PORT}
  59. $WP option update smtp_auth false
  60. # reset the admin password. do this after configuring smtp mail
  61. echo "Resetting admin password"
  62. $WP user update $($WP user get admin --field=ID) --user_pass="${admin_password}"
  63. $WP user update $($WP user get admin --field=ID) --user_email="${admin_email}"
  64. # configure LDAP
  65. # https://github.com/heiglandreas/authLdap/blob/master/authLdap.php#L644
  66. echo "Configuring LDAP"
  67. ldapConfig=$(cat <<EOF
  68. {
  69. "Enabled" : true,
  70. "CachePW" : false,
  71. "URI" : "ldap://${LDAP_SERVER}:${LDAP_PORT}/${LDAP_USERS_BASE_DN}",
  72. "Filter" : "(|(mail=%1\$s)(username=%1\$s))",
  73. "NameAttr" : "givenName",
  74. "SecName" : "sn",
  75. "UidAttr" : "username",
  76. "MailAttr" : "mail",
  77. "WebAttr" : "",
  78. "Groups" : { "administrator" : "cn=admins,${LDAP_GROUPS_BASE_DN}" },
  79. "GroupSeparator": ";",
  80. "Debug" : false,
  81. "GroupAttr" : "memberof",
  82. "GroupFilter" : "(|(mail=%1\$s)(username=%1\$s))",
  83. "DefaultRole" : "editor",
  84. "GroupEnable" : true,
  85. "GroupOverUser" : true,
  86. "Version" : 1
  87. }
  88. EOF
  89. )
  90. $WP --autoload=true --format=json option update authLDAPOptions "${ldapConfig}"
  91. touch /app/data/htaccess
  92. chown -R www-data:www-data /app/data /run/wordpress
  93. echo "Starting apache"
  94. APACHE_CONFDIR="" source /etc/apache2/envvars
  95. rm -f "${APACHE_PID_FILE}"
  96. exec /usr/sbin/apache2 -DFOREGROUND