start.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. readonly admin_password=$(pwgen -1y 16)
  26. readonly admin_email=${MAIL_SMTP_USERNAME}@${MAIL_DOMAIN}
  27. echo "Admin password is ${admin_password} and email is ${admin_email}"
  28. # --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
  29. $WP --url="${APP_ORIGIN}" --skip-email core install \
  30. --url="${APP_ORIGIN}" \
  31. --title="My blog" \
  32. --admin_user=admin \
  33. --admin_password="${admin_password}" \
  34. --admin_email="${admin_email}"
  35. echo "WP is now installed"
  36. # Set default post structure to what most people want
  37. # Curiously, installing some plugins prevents .htaccess getting written
  38. $WP rewrite structure '/%postname%/'
  39. touch "/app/data/.dbsetup"
  40. else
  41. # Update wordpress
  42. echo "Updating wordpress database"
  43. $WP core update-db
  44. fi
  45. # install and backup the plugins. mu plugins are a "flat" structure
  46. # sadly mu-plugins can still be re-configured, just not uninstallable
  47. # We have to do this on every run to get plugin updates
  48. if [[ ! -f "/run/wordpress/plugins_unpacked" ]]; then
  49. echo "Unpacking plugins"
  50. # clear the directory, otherwise unzip/mv have to be forced
  51. rm -rf /app/data/wp-content/mu-plugins/*
  52. unzip -d /app/data/wp-content/mu-plugins/ /app/code/disable-wordpress-updates.zip
  53. mv /app/data/wp-content/mu-plugins/disable-wordpress-updates/* /app/data/wp-content/mu-plugins/
  54. rm -rf /app/data/wp-content/mu-plugins/disable-wordpress-updates/
  55. unzip -d /app/data/wp-content/mu-plugins/ /app/code/wp-mail-smtp.zip
  56. mv /app/data/wp-content/mu-plugins/wp-mail-smtp/* /app/data/wp-content/mu-plugins/
  57. rm -rf /app/data/wp-content/mu-plugins/wp-mail-smtp/
  58. unzip -d /app/data/wp-content/mu-plugins/ /app/code/authLdap.zip
  59. mv /app/data/wp-content/mu-plugins/authLdap-*/* /app/data/wp-content/mu-plugins/
  60. rm -rf /app/data/wp-content/mu-plugins/authLdap-*/
  61. touch /run/wordpress/plugins_unpacked
  62. else
  63. echo "Plugins already unpacked from previous run" # restarts
  64. fi
  65. # configure WP mail smtp plugin (smtp_user, smtp_pass can be set when supported)
  66. echo "Configuring smtp mail"
  67. $WP option update mailer smtp
  68. $WP option update mail_from "${MAIL_FROM}"
  69. $WP option update mail_from_name WordPress
  70. $WP option update smtp_host ${MAIL_SMTP_SERVER}
  71. $WP option update smtp_port ${MAIL_SMTP_PORT}
  72. $WP option update smtp_auth true
  73. $WP option update smtp_user ${MAIL_SMTP_USERNAME}
  74. $WP option update smtp_pass "${MAIL_SMTP_PASSWORD}"
  75. # configure LDAP
  76. # https://github.com/heiglandreas/authLdap/blob/master/authLdap.php#L644
  77. echo "Configuring LDAP"
  78. ldapConfig=$(cat <<EOF
  79. {
  80. "Enabled" : true,
  81. "CachePW" : false,
  82. "URI" : "ldap://${LDAP_SERVER}:${LDAP_PORT}/${LDAP_USERS_BASE_DN}",
  83. "Filter" : "(|(mail=%1\$s)(username=%1\$s))",
  84. "NameAttr" : "givenName",
  85. "SecName" : "sn",
  86. "UidAttr" : "username",
  87. "MailAttr" : "mail",
  88. "WebAttr" : "",
  89. "Groups" : { "administrator" : "cn=admins,${LDAP_GROUPS_BASE_DN}" },
  90. "GroupSeparator": ";",
  91. "Debug" : false,
  92. "GroupAttr" : "memberof",
  93. "GroupFilter" : "(|(mail=%1\$s)(username=%1\$s))",
  94. "DefaultRole" : "editor",
  95. "GroupEnable" : true,
  96. "GroupOverUser" : true,
  97. "Version" : 1
  98. }
  99. EOF
  100. )
  101. $WP --format=json option update authLDAPOptions "${ldapConfig}"
  102. chown -R www-data:www-data /app/data /run/wordpress
  103. echo "Starting apache"
  104. APACHE_CONFDIR="" source /etc/apache2/envvars
  105. rm -f "${APACHE_PID_FILE}"
  106. exec /usr/sbin/apache2 -DFOREGROUND