start.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. if [[ ! -f "/app/data/.dbsetup" ]]; then
  20. echo "Copying wp-content files on first run"
  21. mkdir -p /app/data/wp-content/mu-plugins
  22. cp -r /app/code/wp-content-vanilla/* /app/data/wp-content/
  23. readonly admin_password=$(pwgen -1y 16)
  24. readonly admin_email=${MAIL_SMTP_USERNAME}@${MAIL_DOMAIN}
  25. echo "Admin password is ${admin_password} and email is ${admin_email}"
  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. # configure LDAP
  71. # https://github.com/heiglandreas/authLdap/blob/master/authLdap.php#L644
  72. echo "Configuring LDAP"
  73. ldapConfig=$(cat <<EOF
  74. {
  75. "Enabled" : true,
  76. "CachePW" : false,
  77. "URI" : "ldap://${LDAP_SERVER}:${LDAP_PORT}/${LDAP_USERS_BASE_DN}",
  78. "Filter" : "(|(mail=%1\$s)(username=%1\$s))",
  79. "NameAttr" : "givenName",
  80. "SecName" : "sn",
  81. "UidAttr" : "username",
  82. "MailAttr" : "mail",
  83. "WebAttr" : "",
  84. "Groups" : { "administrator" : "cn=admins,${LDAP_GROUPS_BASE_DN}" },
  85. "GroupSeparator": ";",
  86. "Debug" : false,
  87. "GroupAttr" : "memberof",
  88. "GroupFilter" : "(|(mail=%1\$s)(username=%1\$s))",
  89. "DefaultRole" : "editor",
  90. "GroupEnable" : true,
  91. "GroupOverUser" : true,
  92. "Version" : 1
  93. }
  94. EOF
  95. )
  96. $WP --format=json option update authLDAPOptions "${ldapConfig}"
  97. touch /app/data/htaccess
  98. chown -R www-data:www-data /app/data /run/wordpress
  99. echo "Starting apache"
  100. APACHE_CONFDIR="" source /etc/apache2/envvars
  101. rm -f "${APACHE_PID_FILE}"
  102. exec /usr/sbin/apache2 -DFOREGROUND