diff --git a/README.md b/README.md index 5433957..606d8c6 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ The Varnish service inserts itself between ddev-router and the web container, so A `docker-compose.varnish_extras.yaml` file is generated on install which replaces the `VIRTUAL_HOST` variable of the web container with a sub-domain of the website URL. For example, `mysite.ddev.site`, would be accessible via Varnish on `mysite.ddev.site` and directly on `novarnish.mysite.ddev.site`. -If you use a `project_tld` other than `ddev.site` or `additional_fqdns` DDEV will help add hosts entries for the hostnames automagically; however, you'll need to add entries for the `novarnish.*` sub-domains yourself, e.g. `ddev hostname novarnish.testaddfqdn.random.tld 127.0.0.1`. +If you use a `project_tld` other than `ddev.site` or `additional_fqdns` DDEV will help add hosts entries for the hostnames automagically; however, you'll need to add entries for the `novarnish.*` sub-domains yourself, e.g. `ddev hostname novarnish.testaddfqdn.random.tld 127.0.0.1`. You can also use `ddev create-novarnish-hosts` which will call the `ddev hostname` command for all domains without a `novarnish.*` host entry. ## Helper Commands diff --git a/commands/host/create-novarnish-hosts b/commands/host/create-novarnish-hosts new file mode 100755 index 0000000..c465b1a --- /dev/null +++ b/commands/host/create-novarnish-hosts @@ -0,0 +1,40 @@ +#!/bin/bash + +## Description: Adds novarnish.* domains to hostfile if project uses a different project_tld or defines additional_fqdns +## Usage: create-novarnish-hosts +## Example: ddev create-novarnish-hosts + +NAME=$(ddev exec "yq e '.name' .ddev/config.yaml") +PROJECT_TLD=$(ddev exec "yq e '.project_tld' .ddev/config.yaml") +ADDITIONAL_HOSTNAMES=$(ddev exec "yq e '.additional_hostnames[]' .ddev/config.yaml") +ADDITIONAL_FQDNS=$(ddev exec "yq e '.additional_fqdns[]' .ddev/config.yaml") +declare -a HOSTNAMES=() + +if [ -n "${PROJECT_TLD}" ]; then + echo "Using custom project_tld: $PROJECT_TLD" + HOSTNAMES+=("${NAME}.${PROJECT_TLD}") + if [ -n "$ADDITIONAL_HOSTNAMES" ]; then + while IFS= read HOSTNAME; do + HOSTNAMES+=("${HOSTNAME}.${PROJECT_TLD}") + done < <(echo "$ADDITIONAL_HOSTNAMES") + fi +fi + +if [ -n "$ADDITIONAL_FQDNS" ]; then + while IFS= read FQDNS; do + HOSTNAMES+=("$FQDNS") + done < <(echo "$ADDITIONAL_FQDNS") +fi + +length=${#HOSTNAMES[@]} +for (( i = 0; i < length; i++ )); do + NOVARNISH_HOST="novarnish.${HOSTNAMES[$i]}" + echo "Hostname: $NOVARNISH_HOST" + if ddev hostname --check $NOVARNISH_HOST 127.0.0.1 ; then + echo "Already in hostfile. Skipping..." + else + echo "Not present in hostfile." + echo "Executing 'ddev hostname $NOVARNISH_HOST 127.0.0.1'" + sudo ddev hostname $NOVARNISH_HOST 127.0.0.1 + fi +done