From 996380f19d94a03b71972afbb84ed6adc1a53519 Mon Sep 17 00:00:00 2001 From: Oscar Arzola Date: Thu, 23 Jan 2025 14:38:19 -0500 Subject: [PATCH 1/2] feat: pbshare.sh --- deploying/pbshare.sh | 56 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 deploying/pbshare.sh diff --git a/deploying/pbshare.sh b/deploying/pbshare.sh new file mode 100644 index 0000000..ca2e367 --- /dev/null +++ b/deploying/pbshare.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +usage() { + echo "Usage: $0 []" + echo "If is provided, the script sets up the environment (ON mode)." + echo "If no arguments are provided, the script reverts the changes (OFF mode)." +} + +# Check if no arguments are provided (OFF mode) +if [ $# -eq 0 ]; then + echo "Reverting environment to default (OFF mode)..." + + # Step 1: Revert search-replace in WordPress database + echo "Reverting WP search-replace..." + CURRENT_SITE=$(grep -E '^DOMAIN_CURRENT_SITE=' .env | cut -d '=' -f2) + cd web && lando wp search-replace "$CURRENT_SITE" "pressbooks.test" --all-tables && cd .. + + # Step 2: Restore original .env values + echo "Restoring .env file..." + sed -i.bak -E "s|(WP_HOME=).*|\1https://pressbooks.test|g" .env + sed -i.bak -E "s|(DOMAIN_CURRENT_SITE=).*|\1pressbooks.test|g" .env + + # Step 3: Stop `ngrok` + echo "Stopping ngrok..." + pkill -f "ngrok http" + + echo "Environment reverted to default." + exit 0 +fi + +# If a domain is provided, execute ON mode +SHARED_URL=$1 +echo "Setting up environment with shared URL: $SHARED_URL" + +# Step 1: Search-replace in WordPress database +echo "Running WP search-replace..." +cd web && lando wp search-replace pressbooks.test "$SHARED_URL" --all-tables && cd .. + +# Step 2: Update .env with WP_HOME and DOMAIN_CURRENT_SITE +echo "Updating .env file..." +sed -i.bak -E "s|(WP_HOME=).*|\1https://$SHARED_URL|g" .env +sed -i.bak -E "s|(DOMAIN_CURRENT_SITE=).*|\1$SHARED_URL|g" .env + +# Step 3: Extract port from lando info +echo "Extracting localhost port from lando info..." +LANDO_INFO=$(lando info --format json) +# Extract port from the appserver_nginx service in lando info +LOCALHOST_PORT=$(echo "$LANDO_INFO" | jq -r '.[] | select(.service == "appserver_nginx") | .urls[] | select(startswith("http://localhost:"))' | awk -F':' '{print $3}' | awk -F'/' '{print $1}') + +if [ -z "$LOCALHOST_PORT" ]; then + echo "Error: Could not extract port from lando info." + exit 1 +fi +# Step 4: Run ngrok +echo "Starting ngrok..." +ngrok http "$LOCALHOST_PORT" --domain "$SHARED_URL" From 5c68fba33f763fe5dd9c1460293575df8a618be8 Mon Sep 17 00:00:00 2001 From: Oscar Arzola Date: Thu, 23 Jan 2025 14:44:54 -0500 Subject: [PATCH 2/2] Create README.md --- deploying/README.md | 78 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 deploying/README.md diff --git a/deploying/README.md b/deploying/README.md new file mode 100644 index 0000000..0d40736 --- /dev/null +++ b/deploying/README.md @@ -0,0 +1,78 @@ +# README + +## Script Overview +This script is a powerful and convenient tool to manage the dynamic setup and reversion of your WordPress development environment. It works with **Lando**, **ngrok**, and your `.env` file to ensure seamless transitions between local and public-facing development environments. The script offers two modes of operation: + +- **ON mode**: Dynamically configures your environment to use a provided ngrok subdomain. +- **OFF mode**: Reverts the environment to its original state. + +## Features +1. **Dynamic Domain Configuration**: Automatically updates WordPress URLs and `.env` values to match the provided ngrok subdomain. +2. **Reversion**: Reverts WordPress database and `.env` settings to their original values. +3. **Automatic Port Extraction**: Extracts the correct localhost port from the `appserver_nginx` service in Lando. +4. **Seamless ngrok Integration**: Starts ngrok with the appropriate configurations to expose your local environment. + +## Benefits +- **Automation**: Eliminates manual effort to update URLs, ports, and `.env` configurations. +- **Efficiency**: Quickly toggle between local and public environments. +- **Error Reduction**: Automates repetitive tasks, reducing the risk of human error. +- **Dynamic Reversion**: Reads the current domain from `.env` to ensure accurate reversion of changes. + +## Usage +### Prerequisites +Ensure the following tools are installed and configured: +- [Lando](https://docs.lando.dev/) +- [ngrok](https://ngrok.com/) +- `jq` for parsing JSON +- A valid `.env` file with the following keys: + - `WP_HOME` + - `DOMAIN_CURRENT_SITE` + +### Running the Script +#### ON Mode +To set up the environment with a specific ngrok subdomain: +```bash +./pbshare.sh +``` +- Replace `` with your desired ngrok subdomain (e.g., `app.ngrok.io`). + +#### OFF Mode +To revert the environment to its original state: +```bash +./pbshare.sh +``` + +## How It Works +### ON Mode +1. **WordPress Search-Replace**: Updates all database tables, replacing `pressbooks.test` with the provided ngrok subdomain. +2. **.env Update**: Updates `WP_HOME` and `DOMAIN_CURRENT_SITE` to use the ngrok subdomain. +3. **Port Extraction**: Dynamically retrieves the correct localhost port from the `appserver_nginx` service in Lando. +4. **ngrok Start**: Launches ngrok with the extracted port and configured domain. + +### OFF Mode +1. **Domain Extraction**: Reads the current `DOMAIN_CURRENT_SITE` from `.env`. +2. **WordPress Search-Replace**: Reverts all database tables, replacing the current domain with `pressbooks.test`. +3. **.env Reversion**: Restores `WP_HOME` and `DOMAIN_CURRENT_SITE` to their original values. +4. **ngrok Stop**: Stops any running ngrok processes. + +## Example +### Enable Environment +```bash +./pbshare.sh app.ngrok.io +``` +This sets up your environment to use the ngrok subdomain `app.ngrok.io`. + +### Revert Environment +```bash +./pbshare.sh +``` +This reverts the environment to its original state using `pressbooks.test`. + +## Troubleshooting +- Ensure `jq` is installed and accessible in your `$PATH`. +- Verify that the `.env` file contains the required keys (`WP_HOME` and `DOMAIN_CURRENT_SITE`). +- Check the output of `lando info --format json` to confirm the `appserver_nginx` service is correctly configured. + +## License +This script is open-source and provided under the MIT License. Feel free to modify and use it in your projects. +