start.sh 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 [[ -z "$(ls -A /app/data)" ]]; 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. # create db tables
  27. $WP --url="${APP_ORIGIN}" 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. # install and backup the plugins
  34. $WP plugin install --activate --force /app/code/disable-wordpress-updates.zip
  35. mv /app/data/wp-content/plugins/disable-wordpress-updates /app/data/wp-content/mu-plugins/
  36. $WP plugin install --activate --force /app/code/wp-mail-smtp.zip
  37. mv /app/data/wp-content/plugins/wp-mail-smtp /app/data/wp-content/mu-plugins/
  38. $WP plugin install --activate --force /app/code/authLdap.zip
  39. else
  40. # Update wordpress
  41. $WP core update-db
  42. fi
  43. # reset the admin password
  44. $WP user update $($WP user get admin --field=ID) --user_pass="${admin_password}"
  45. $WP user update $($WP user get admin --field=ID) --user_email="${admin_email}"
  46. # configure WP mail smtp plugin (smtp_user, smtp_pass can be set when supported)
  47. $WP option update mailer smtp
  48. $WP option update mail_from ${MAIL_SMTP_USERNAME}@${MAIL_DOMAIN}
  49. $WP option update mail_from_name ${MAIL_SMTP_USERNAME}
  50. $WP option update smtp_host ${MAIL_SMTP_SERVER}
  51. $WP option update smtp_port ${MAIL_SMTP_PORT}
  52. $WP option update smtp_auth false
  53. # configure LDAP
  54. # https://github.com/heiglandreas/authLdap/blob/master/authLdap.php#L644
  55. ldapConfig=$(cat <<EOF
  56. [
  57. 'Enabled' => true,
  58. 'CachePW' => false,
  59. 'URI' => 'ldap://${LDAP_SERVER}:${LDAP_PORT}/${LDAP_USERS_BASE_DN}',
  60. 'Filter' => '(uid=%s)',
  61. 'NameAttr' => 'displayname',
  62. 'SecName' => '',
  63. 'UidAttr' => '', // 'uid'
  64. 'MailAttr' => '', // 'mail'
  65. 'WebAttr' => '',
  66. 'Groups' => array([ 'administrator' => 'cn=admins,${LDAP_GROUPS_BASE_DN}' ]),
  67. 'Debug' => false,
  68. 'GroupAttr' => 'memberof',
  69. 'GroupFilter' => '(&(objectClass=user)(uid=%s))',
  70. 'DefaultRole' => 'editor',
  71. 'GroupEnable' => true,
  72. 'GroupOverUser' => true,
  73. 'Version' => 1
  74. ]
  75. EOF
  76. )
  77. authLDAPOptions=$(echo "${ldapConfig}" | php5 -r 'echo serialize(eval("return " . file_get_contents("php://stdin") . ";"));')
  78. $WP option update authLDAPOptions "${authLDAPOptions}"
  79. chown -R www-data:www-data /app/data
  80. echo "Starting apache"
  81. APACHE_CONFDIR="" source /etc/apache2/envvars
  82. rm -f "${APACHE_PID_FILE}"
  83. exec /usr/sbin/apache2 -DFOREGROUND