start.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 and https://github.com/wp-cli/wp-cli/issues/1164
  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. touch "/app/data/.dbsetup"
  35. else
  36. # Update wordpress
  37. echo "Updating wordpress database"
  38. $WP core update-db
  39. fi
  40. # install and backup the plugins. mu plugins are a "flat" structure
  41. # sadly mu-plugins can still be re-configured, just not uninstallable
  42. # We have to do this on every run to get plugin updates
  43. if [[ ! -f "/run/wordpress/plugins_unpacked" ]]; then
  44. echo "Unpacking plugins"
  45. # clear the directory, otherwise unzip/mv have to be forced
  46. rm -rf /app/data/wp-content/mu-plugins/*
  47. unzip -d /app/data/wp-content/mu-plugins/ /app/code/disable-wordpress-updates.zip
  48. mv /app/data/wp-content/mu-plugins/disable-wordpress-updates/* /app/data/wp-content/mu-plugins/
  49. rm -rf /app/data/wp-content/mu-plugins/disable-wordpress-updates/
  50. unzip -d /app/data/wp-content/mu-plugins/ /app/code/wp-mail-smtp.zip
  51. mv /app/data/wp-content/mu-plugins/wp-mail-smtp/* /app/data/wp-content/mu-plugins/
  52. rm -rf /app/data/wp-content/mu-plugins/wp-mail-smtp/
  53. unzip -d /app/data/wp-content/mu-plugins/ /app/code/authLdap.zip
  54. mv /app/data/wp-content/mu-plugins/authLdap-*/* /app/data/wp-content/mu-plugins/
  55. rm -rf /app/data/wp-content/mu-plugins/authLdap-*/
  56. touch /run/wordpress/plugins_unpacked
  57. else
  58. echo "Plugins already unpacked from previous run" # restarts
  59. fi
  60. # configure WP mail smtp plugin (smtp_user, smtp_pass can be set when supported)
  61. echo "Configuring smtp mail"
  62. $WP option update mailer smtp
  63. $WP option update mail_from "${MAIL_FROM}"
  64. $WP option update mail_from_name WordPress
  65. $WP option update smtp_host ${MAIL_SMTP_SERVER}
  66. $WP option update smtp_port ${MAIL_SMTP_PORT}
  67. $WP option update smtp_auth true
  68. $WP option update smtp_user ${MAIL_SMTP_USERNAME}
  69. $WP option update smtp_pass "${MAIL_SMTP_PASSWORD}"
  70. # reset the admin password. do this after configuring smtp mail
  71. echo "Resetting admin password"
  72. # see http://wp-cli.org/docs/internal-api/wp-cli-add-hook/ and https://codex.wordpress.org/Plugin_API/Filter_Reference/send_password_change_email
  73. echo "<?php WP_CLI::add_hook('after_wp_load', function () { add_filter( 'send_password_change_email', '__return_false' ); }); ?>" > /run/wordpress/skip-email.php
  74. $WP --require=/run/wordpress/skip-email.php user update $($WP user get admin --field=ID) --user_pass="${admin_password}"
  75. $WP --require=/run/wordpress/skip-email.php user update $($WP user get admin --field=ID) --user_email="${admin_email}"
  76. # configure LDAP
  77. # https://github.com/heiglandreas/authLdap/blob/master/authLdap.php#L644
  78. echo "Configuring LDAP"
  79. ldapConfig=$(cat <<EOF
  80. {
  81. "Enabled" : true,
  82. "CachePW" : false,
  83. "URI" : "ldap://${LDAP_SERVER}:${LDAP_PORT}/${LDAP_USERS_BASE_DN}",
  84. "Filter" : "(|(mail=%1\$s)(username=%1\$s))",
  85. "NameAttr" : "givenName",
  86. "SecName" : "sn",
  87. "UidAttr" : "username",
  88. "MailAttr" : "mail",
  89. "WebAttr" : "",
  90. "Groups" : { "administrator" : "cn=admins,${LDAP_GROUPS_BASE_DN}" },
  91. "GroupSeparator": ";",
  92. "Debug" : false,
  93. "GroupAttr" : "memberof",
  94. "GroupFilter" : "(|(mail=%1\$s)(username=%1\$s))",
  95. "DefaultRole" : "editor",
  96. "GroupEnable" : true,
  97. "GroupOverUser" : true,
  98. "Version" : 1
  99. }
  100. EOF
  101. )
  102. $WP --autoload=true --format=json option update authLDAPOptions "${ldapConfig}"
  103. touch /app/data/htaccess
  104. chown -R www-data:www-data /app/data /run/wordpress
  105. echo "Starting apache"
  106. APACHE_CONFDIR="" source /etc/apache2/envvars
  107. rm -f "${APACHE_PID_FILE}"
  108. exec /usr/sbin/apache2 -DFOREGROUND