Repository navigation
Add host-tools/setup.sh to create .env with prompts - #348
Conversation
Checks the required software, asks for the database credentials and the report API key, tests the database connection with mysqli, and writes .env from .env.default. WPT_TEST_DIR is set to WPT_PREPARE_DIR, which a local run requires. --non-interactive takes the values from the environment. Part of WordPress#257.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @iarif4u. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
ekamran
left a comment
There was a problem hiding this comment.
Thanks @iarif4u, this will save hosts a lot of time. I ran it on macOS (bash 3.2) and Linux (bash 5.2) against MariaDB 12.3. Correct and wrong credentials, a password with ', ", $, # and a space, an invalid host, PHP without mysqli, and missing software all work as described. The values come back the same after source .env.
The one change I think should be fixed before merge is about running the script again. It rebuilds .env from .env.default, so the settings it does not ask about go back to the defaults, and SSH hosts lose their SSH settings. Details and suggestions are inline. With all of them applied, a re-run with Enter at every prompt changes nothing, on a local host and on an SSH host.
Not blocking: the ${WPT_PHP_EXECUTABLE-php} line becomes 'php', so a cron entry that sets WPT_PHP_EXECUTABLE no longer wins. That is fine if no host relies on it.
- Build .env from the current .env, not .env.default, so the settings the script does not ask about (SSH, report URL, debug, ...) are kept, and append any asked-about variable the file does not have yet. - Set WPT_TEST_DIR to WPT_PREPARE_DIR after the prompts, so it also applies to --non-interactive, and keep it when WPT_SSH_CONNECT is set. - Skip the database check in SSH mode: the tests connect from the remote server. - Make the .env backup readable only by its owner. - Allow .env lines that use an unset variable.
SC2016 flags the single-quoted PHP code. The quotes are intentional.
|
@ekamran Thanks for the careful review. All suggestions on #346, #348 and #349 are applied: #348 e7efa2d (+ 41d62ea), #346 28be5d0, #349 4996e28. I ran both scripts through 36 checks, on macOS (bash 3.2.57, MySQL 9.6) and on Ubuntu 24.04 (bash 5.2.21, PHP 8.3, MySQL 8.0). Your cases are included: a re-run keeps the 8 custom settings and the SSH settings, On the non-blocking note: I left |
ekamran
left a comment
There was a problem hiding this comment.
Thanks @iarif4u. I re-checked 41d62ea as a whole on macOS and Linux. All 14 earlier cases now behave as intended, and so do a runner path with a space, a .env with a missing or non-exported variable, and repeated backups.
One case is left, details inline: a .env this user cannot read, or a runner folder it cannot write to. Happy to approve once that is in.
An unreadable .env (for example root-owned after a first run with sudo) was replaced without a backup, losing the settings the script does not ask about, and a read-only runner directory was reported as saved.
|
Thanks @ekamran for the approval, and for testing the last two cases on macOS and Linux! |
read without IFS= strips them before -r gets the line, so a password or report API key typed with a leading or trailing space loses it silently.
Part of #257 (the first idea: set up
.envwith prompts instead of vim). The one-step run script is in #346 for #260.Purpose
Hosts say that the runner install is tedious. Today, they clone the repo, copy
.env.default, edit it in vim, and find out on the first run whether the database credentials work. This script does those steps and checks them.What
host-tools/setup.shdoesphp git rsync wget unzip node npm.composeris optional. It prints the Node.js and npm versions and stops with a list if something is missing.wpt_runner_parse_db_host()andmysqli, usingWPT_PHP_EXECUTABLE. If the connection fails,.envis not changed..envfrom.env.default. It changes only the lines it asks about. It quotes each value safely, sets the file mode to600, and saves the old.envas.env.bak-<timestamp>.Also:
WPT_TEST_DIRtoWPT_PREPARE_DIR. With.env.defaultas it is, the two are different (wp-test-runnerand/tmp/wp-test-runner), so a local run stops incheck_required_env()with "WPT_TEST_DIR must be the same as WPT_PREPARE_DIR".--non-interactivetakes the values from the environment, for provisioning tools.--skip-checksand--skip-db-checkskip those steps.The README "Configuring the Runner" section now mentions the script first, before the manual steps.
Testing
Tested against a real MySQL 9.6 server with PHP 8.4:
--non-interactive, correct credentials.env(mode 600), exit 0source .env, thencheck_required_env().envunchanged, exit 1WPT_DB_HOST.envunchanged',#,$and spacessource .env.envnode,npmorwgetmissingContributed at WordCamp Contributor Day.