diff --git a/scripts/local-dev-entrypoint.sh b/scripts/local-dev-entrypoint.sh index 025175874..7858b39f6 100755 --- a/scripts/local-dev-entrypoint.sh +++ b/scripts/local-dev-entrypoint.sh @@ -6,40 +6,51 @@ set -e cd /var/www/html -# http serves a single offer, whereas https serves multiple. we only want one -LATEST_WORDPRESS_VERSION=$( php -r 'echo @json_decode(file_get_contents("http://api.wordpress.org/core/version-check/1.7/"), true)["offers"][0]["version"];' ); -if [[ -z "$LATEST_WORDPRESS_VERSION" ]]; then - echo "Latest WordPress version could not be found" - exit 1 +if [[ "$1" = "bash" ]]; then + "$@" + exit $? fi -if [[ "$WORDPRESS_VERSION" = "latest" || -z "$WORDPRESS_VERSION" ]]; then - WORDPRESS_VERSION="$LATEST_WORDPRESS_VERSION" -fi -WORDPRESS_FOLDER=${WORDPRESS_FOLDER:-$WORDPRESS_VERSION} +function export_global() { + # http serves a single offer, whereas https serves multiple. we only want one + export LATEST_WORDPRESS_VERSION=$( php -r 'echo @json_decode(file_get_contents("http://api.wordpress.org/core/version-check/1.7/"), true)["offers"][0]["version"];' ); + if [[ -z "$LATEST_WORDPRESS_VERSION" ]]; then + echo "Latest WordPress version could not be found" + exit 1 + fi -if [[ "$MULTISITE" = "1" ]]; then - WORDPRESS_FOLDER="$WORDPRESS_FOLDER-multi" -fi + if [[ "$WORDPRESS_VERSION" = "latest" || -z "$WORDPRESS_VERSION" ]]; then + export WORDPRESS_VERSION="$LATEST_WORDPRESS_VERSION" + fi -export WP_DB_NAME=$(echo "wp_matomo_$WORDPRESS_FOLDER" | sed 's/\./_/g' | sed 's/-/_/g') + export WORDPRESS_FOLDER=${WORDPRESS_FOLDER:-$WORDPRESS_VERSION} +} -export WP_TESTS_DIR=/var/www/html/$WORDPRESS_FOLDER/wp-test # used for setting up for tests and running phpunit +function export_install_dependent() { + ARG_MULTISITE="$1" + if [[ "$ARG_MULTISITE" = "1" ]]; then + export WORDPRESS_FOLDER="$WORDPRESS_FOLDER-multi" + fi -echo "Using WordPress install $WORDPRESS_FOLDER." -echo + export WP_DB_NAME=$(echo "wp_matomo_$WORDPRESS_FOLDER" | sed 's/\./_/g' | sed 's/-/_/g') -echo " /var/www/html/matomo.wpload_dir.php || true + export WP_TESTS_DIR=/var/www/html/$WORDPRESS_FOLDER/wp-test # used for setting up for tests and running phpunit +} -if [[ "$1" = "bash" ]]; then - "$@" - exit $? -fi +function init_wpload_dir_file() { + echo " /var/www/html/matomo.wpload_dir.php || true +} -if [[ "$EXECUTE_CLI" = "1" ]]; then +function handle_cli_command() { EXECUTE_TARGET="$1" EXECUTE_ARGS="${@:2}" + export_install_dependent $MULTISITE + init_wpload_dir_file + + echo "Using WordPress install $WORDPRESS_FOLDER." + echo + if [[ "$EXECUTE_TARGET" = "wp" ]]; then /var/www/html/wp-cli.phar --path=/var/www/html/$WORDPRESS_FOLDER $EXECUTE_ARGS exit $? @@ -61,75 +72,78 @@ if [[ "$EXECUTE_CLI" = "1" ]]; then "$EXECUTE_TARGET" $EXECUTE_ARGS exit $? fi -fi +} -a2enmod rewrite || true +function install_wordpress() { + MULTISITE="$1" -# install wp-cli.phar -if [ ! -f "/var/www/html/wp-cli.phar" ]; then - curl https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -o /var/www/html/wp-cli.phar -fi -chmod +x /var/www/html/wp-cli.phar + export_install_dependent $MULTISITE -# TODO: switch download to use wp-cli instead of just curling (also can use wp db create instead of raw php) -# install wordpress if not present -if [ ! -d "/var/www/html/$WORDPRESS_FOLDER" ]; then - WORDPRESS_URL="https://wordpress.org/wordpress-$WORDPRESS_VERSION.zip" - if [ "$WORDPRESS_VERSION" = "trunk" ]; then - WORDPRESS_URL="https://wordpress.org/nightly-builds/wordpress-latest.zip" + # install wp-cli.phar + if [ ! -f "/var/www/html/wp-cli.phar" ]; then + curl https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -o /var/www/html/wp-cli.phar fi + chmod +x /var/www/html/wp-cli.phar + + # TODO: switch download to use wp-cli instead of just curling (also can use wp db create instead of raw php) + # install wordpress if not present + if [ ! -d "/var/www/html/$WORDPRESS_FOLDER" ]; then + WORDPRESS_URL="https://wordpress.org/wordpress-$WORDPRESS_VERSION.zip" + if [ "$WORDPRESS_VERSION" = "trunk" ]; then + WORDPRESS_URL="https://wordpress.org/nightly-builds/wordpress-latest.zip" + fi - echo "installing wordpress $WORDPRESS_VERSION from $WORDPRESS_URL to /var/www/html/$WORDPRESS_FOLDER/..." - - curl "$WORDPRESS_URL" > "wordpress-$WORDPRESS_VERSION.zip" - - rm -rf wordpress - unzip -q "wordpress-$WORDPRESS_VERSION.zip" - mv wordpress "$WORDPRESS_FOLDER" + echo "installing wordpress $WORDPRESS_VERSION from $WORDPRESS_URL to /var/www/html/$WORDPRESS_FOLDER/..." - echo "wordpress installed!" -else - echo "wordpress $WORDPRESS_VERSION already installed at /var/www/html/$WORDPRESS_FOLDER/." -fi + curl "$WORDPRESS_URL" > "wordpress-$WORDPRESS_VERSION.zip" -echo "waiting for database..." -sleep 5 # wait for database -echo "done." + rm -rf wordpress + unzip -q "wordpress-$WORDPRESS_VERSION.zip" + mv wordpress "$WORDPRESS_FOLDER" -# if requested, drop the database for a clean install (used mainly for automated tests) -if [[ ! -z "$RESET_DATABASE" ]]; then - echo "dropping existing database..." + echo "wordpress installed!" + else + echo "wordpress $WORDPRESS_VERSION already installed at /var/www/html/$WORDPRESS_FOLDER/." + fi - php -r "\$pdo = new PDO('mysql:host=$WP_DB_HOST', 'root', 'pass'); - \$pdo->exec('DROP DATABASE IF EXISTS \`$WP_DB_NAME\`');" + echo "waiting for database..." + sleep 5 # wait for database + echo "done." - rm /var/www/html/$WORDPRESS_FOLDER/wp-content/uploads/matomo/config/config.ini.php || true - rm /var/www/html/$WORDPRESS_FOLDER/apppassword || true -fi + # if requested, drop the database for a clean install (used mainly for automated tests) + if [[ ! -z "$RESET_DATABASE" ]]; then + echo "dropping existing database..." -# create database if it does not already exist -php -r "\$pdo = new PDO('mysql:host=$WP_DB_HOST', 'root', 'pass'); -\$pdo->exec('CREATE DATABASE IF NOT EXISTS \`$WP_DB_NAME\`');\ -\$pdo->exec('GRANT ALL PRIVILEGES ON $WP_DB_NAME.* TO \'root\'@\'%\' IDENTIFIED BY \'pass\'');" + php -r "\$pdo = new PDO('mysql:host=$WP_DB_HOST', 'root', 'pass'); + \$pdo->exec('DROP DATABASE IF EXISTS \`$WP_DB_NAME\`');" -# setup wordpress config if not done so -if [ ! -f "/var/www/html/$WORDPRESS_FOLDER/wp-config.php" ]; then - if [[ "$MULTISITE" = "1" ]]; then - MULTISITE_CONFIG=" -define( 'WP_ALLOW_MULTISITE', true ); -define( 'MULTISITE', true ); -define( 'SUBDOMAIN_INSTALL', false ); -define( 'DOMAIN_CURRENT_SITE', 'localhost' . ($PORT === 80 ? '' : ':$PORT') ); -define( 'PATH_CURRENT_SITE', '/$WORDPRESS_FOLDER/' ); -define( 'SITE_ID_CURRENT_SITE', 1 ); -define( 'BLOG_ID_CURRENT_SITE', 1 ); -" + rm /var/www/html/$WORDPRESS_FOLDER/wp-content/uploads/matomo/config/config.ini.php || true + rm /var/www/html/$WORDPRESS_FOLDER/apppassword || true fi - WP_DEBUG="${WP_DEBUG:-false}" - WP_DEBUG_LOG="${WP_DEBUG_LOG:-false}" - WP_DEBUG_DISPLAY="${WP_DEBUG_DISPLAY:-true}" - cat > "/var/www/html/$WORDPRESS_FOLDER/wp-config.php" <exec('CREATE DATABASE IF NOT EXISTS \`$WP_DB_NAME\`');\ + \$pdo->exec('GRANT ALL PRIVILEGES ON $WP_DB_NAME.* TO \'root\'@\'%\' IDENTIFIED BY \'pass\'');" + + # setup wordpress config if not done so + if [ ! -f "/var/www/html/$WORDPRESS_FOLDER/wp-config.php" ]; then + if [[ "$MULTISITE" = "1" ]]; then + MULTISITE_CONFIG=" + define( 'WP_ALLOW_MULTISITE', true ); + define( 'MULTISITE', true ); + define( 'SUBDOMAIN_INSTALL', false ); + define( 'DOMAIN_CURRENT_SITE', 'localhost' . ($PORT === 80 ? '' : ':$PORT') ); + define( 'PATH_CURRENT_SITE', '/$WORDPRESS_FOLDER/' ); + define( 'SITE_ID_CURRENT_SITE', 1 ); + define( 'BLOG_ID_CURRENT_SITE', 1 ); + " + fi + + WP_DEBUG="${WP_DEBUG:-false}" + WP_DEBUG_LOG="${WP_DEBUG_LOG:-false}" + WP_DEBUG_DISPLAY="${WP_DEBUG_DISPLAY:-true}" + cat > "/var/www/html/$WORDPRESS_FOLDER/wp-config.php" < "/var/www/html/$WORDPRESS_FOLDER/.htaccess" < "/var/www/html/$WORDPRESS_FOLDER/.htaccess" <exec('UPDATE \`${WP_DB_NAME}\`.wp_matomo_site SET ts_created = \"2023-01-01 00:00:00\"');" -fi + # update site created date for e2e tests + php -r "\$pdo = new PDO('mysql:host=$WP_DB_HOST', 'root', 'pass'); + \$pdo->exec('UPDATE \`${WP_DB_NAME}\`.wp_matomo_site SET ts_created = \"2023-01-01 00:00:00\"');" + fi -# add index.php file listing available installs to root /var/www/html -if [ ! -f "/var/www/html/index.php" ]; then - cat > "/var/www/html/index.php" < "/var/www/html/index.php" <