start.sh 4.9 KB

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