diff --git a/.vortex/docs/.utils/variables/variables.excluded.txt b/.vortex/docs/.utils/variables/variables.excluded.txt index d3f82c5164..de67d4dcbf 100755 --- a/.vortex/docs/.utils/variables/variables.excluded.txt +++ b/.vortex/docs/.utils/variables/variables.excluded.txt @@ -7,7 +7,6 @@ IFS PATH SCRIPT_DIR SRC_TMPDIR -TARGET_ENV_REMAP TEST_PACKAGE_TOKEN VORTEX_DOCTOR_CHECK_PREFLIGHT VORTEX_PROVISION_LOG_ACTIVE diff --git a/.vortex/docs/content/development/variables.mdx b/.vortex/docs/content/development/variables.mdx index e20e7ddd79..2aa32ef220 100644 --- a/.vortex/docs/content/development/variables.mdx +++ b/.vortex/docs/content/development/variables.mdx @@ -576,7 +576,6 @@ The list below is automatically generated with [Shellvar](https://github.com/ale | `VORTEX_TASK_CUSTOM_LAGOON_SSH_FINGERPRINT` | SSH key fingerprint used to connect to a remote. | `UNDEFINED` | `.vortex/tooling/src/vortex-task-custom-lagoon` | | `VORTEX_TASK_PLATFORM` | Hosting platform to run the operation for. | `${VORTEX_PLATFORM}` | `.vortex/tooling/src/vortex-task` | | `VORTEX_TASK_PURGE_CACHE_ACQUIA_APP_NAME` | Application name. Used to discover UUID. | `${VORTEX_ACQUIA_APP_NAME}` | `.vortex/tooling/src/vortex-task-purge-cache-acquia` | -| `VORTEX_TASK_PURGE_CACHE_ACQUIA_DOMAINS_FILE` | File with a list of domains that should be purged. | `domains.txt` | `.vortex/tooling/src/vortex-task-purge-cache-acquia` | | `VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV` | An environment name to purge cache for. | `UNDEFINED` | `.vortex/tooling/src/vortex-task-purge-cache-acquia` | | `VORTEX_TASK_PURGE_CACHE_ACQUIA_KEY` | Acquia Cloud API key. | `${VORTEX_ACQUIA_KEY}` | `.vortex/tooling/src/vortex-task-purge-cache-acquia` | | `VORTEX_TASK_PURGE_CACHE_ACQUIA_SECRET` | Acquia Cloud API secret. | `${VORTEX_ACQUIA_SECRET}` | `.vortex/tooling/src/vortex-task-purge-cache-acquia` | diff --git a/.vortex/docs/content/hosting/acquia.mdx b/.vortex/docs/content/hosting/acquia.mdx index 14ade92717..15154adb69 100644 --- a/.vortex/docs/content/hosting/acquia.mdx +++ b/.vortex/docs/content/hosting/acquia.mdx @@ -95,13 +95,11 @@ blank field in the Acquia UI counts as empty. `purge-cache.sh` and both copy hooks require `VORTEX_ACQUIA_KEY` and `VORTEX_ACQUIA_SECRET`, so set both in the Acquia UI before the first deployment. -`purge-cache.sh` purges the domains listed in `hooks/library/domains.txt`. -The installer writes your domain there as `$TARGET_ENV_REMAP.`. Add -a line for every other domain the site serves. - -`$TARGET_ENV_REMAP` expands to `dev` on `dev`, `stage` on `test` and -`test2` on `test2`. On `prod`, the prefix and its dot are removed. Any -other environment, including on-demand ones, purges nothing. +`purge-cache.sh` purges every domain attached to the environment, on-demand +environments included. It reads the list from the Acquia Cloud API, so it +covers the Acquia domain and every custom domain or sub-domain. It skips +wildcard domains such as `*.example.com`. Each purge clears Varnish and the +Platform CDN when it's enabled. `notify-deployment.sh` builds the notification URL from Acquia's `AH_SITE_NAME` and `AH_REALM` variables: @@ -217,10 +215,7 @@ default environments, so set them for a manual run: (the database name). - `copy-files`: `VORTEX_TASK_COPY_FILES_ACQUIA_SRC` and `VORTEX_TASK_COPY_FILES_ACQUIA_DST`. -- `purge-cache`: `VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV`, plus - `VORTEX_TASK_PURGE_CACHE_ACQUIA_DOMAINS_FILE` when the domain list isn't - `domains.txt` in the current directory (the hook sets it to - `hooks/library/domains.txt`). +- `purge-cache`: `VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV`. For example, to copy the production database into `dev`, run the following command from the project root: diff --git a/.vortex/installer/tests/Fixtures/handler_process/hosting_acquia/hooks/library/domains.txt b/.vortex/installer/tests/Fixtures/handler_process/hosting_acquia/hooks/library/domains.txt deleted file mode 100644 index 7d47091d34..0000000000 --- a/.vortex/installer/tests/Fixtures/handler_process/hosting_acquia/hooks/library/domains.txt +++ /dev/null @@ -1 +0,0 @@ -$TARGET_ENV_REMAP.star-wars.com diff --git a/.vortex/installer/tests/Fixtures/handler_process/hosting_acquia/hooks/library/purge-cache.sh b/.vortex/installer/tests/Fixtures/handler_process/hosting_acquia/hooks/library/purge-cache.sh index f2537f599b..080ec2eb18 100755 --- a/.vortex/installer/tests/Fixtures/handler_process/hosting_acquia/hooks/library/purge-cache.sh +++ b/.vortex/installer/tests/Fixtures/handler_process/hosting_acquia/hooks/library/purge-cache.sh @@ -17,7 +17,6 @@ export VORTEX_ACQUIA_KEY="${VORTEX_ACQUIA_KEY:?Missing required value.}" export VORTEX_ACQUIA_SECRET="${VORTEX_ACQUIA_SECRET:?Missing required value.}" export VORTEX_ACQUIA_APP_NAME="${VORTEX_ACQUIA_APP_NAME:-${site}}" export VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV="${VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV:-${target_env}}" -export VORTEX_TASK_PURGE_CACHE_ACQUIA_DOMAINS_FILE="${VORTEX_TASK_PURGE_CACHE_ACQUIA_DOMAINS_FILE:-"/var/www/html/${site}.${target_env}/hooks/library/domains.txt"}" pushd "/var/www/html/${site}.${target_env}" >/dev/null || exit 1 diff --git a/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___acquia/hooks/library/domains.txt b/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___acquia/hooks/library/domains.txt deleted file mode 100644 index 7d47091d34..0000000000 --- a/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___acquia/hooks/library/domains.txt +++ /dev/null @@ -1 +0,0 @@ -$TARGET_ENV_REMAP.star-wars.com diff --git a/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___acquia/hooks/library/purge-cache.sh b/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___acquia/hooks/library/purge-cache.sh index f2537f599b..080ec2eb18 100755 --- a/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___acquia/hooks/library/purge-cache.sh +++ b/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___acquia/hooks/library/purge-cache.sh @@ -17,7 +17,6 @@ export VORTEX_ACQUIA_KEY="${VORTEX_ACQUIA_KEY:?Missing required value.}" export VORTEX_ACQUIA_SECRET="${VORTEX_ACQUIA_SECRET:?Missing required value.}" export VORTEX_ACQUIA_APP_NAME="${VORTEX_ACQUIA_APP_NAME:-${site}}" export VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV="${VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV:-${target_env}}" -export VORTEX_TASK_PURGE_CACHE_ACQUIA_DOMAINS_FILE="${VORTEX_TASK_PURGE_CACHE_ACQUIA_DOMAINS_FILE:-"/var/www/html/${site}.${target_env}/hooks/library/domains.txt"}" pushd "/var/www/html/${site}.${target_env}" >/dev/null || exit 1 diff --git a/.vortex/tooling/src/vortex-task-purge-cache-acquia b/.vortex/tooling/src/vortex-task-purge-cache-acquia index 5d5c941f41..830a041deb 100755 --- a/.vortex/tooling/src/vortex-task-purge-cache-acquia +++ b/.vortex/tooling/src/vortex-task-purge-cache-acquia @@ -9,7 +9,7 @@ # To create a Cloud API token, go to # Acquia Cloud UI -> Account -> API tokens -> Create Token # -# @see https://cloudapi-docs.acquia.com/#/Environments/postEnvironmentsDomainsClearVarnish +# @see https://cloudapi-docs.acquia.com/#/Environments/postEnvironmentsClearCaches # # shellcheck disable=SC1090,SC1091 @@ -30,9 +30,6 @@ VORTEX_TASK_PURGE_CACHE_ACQUIA_APP_NAME="${VORTEX_TASK_PURGE_CACHE_ACQUIA_APP_NA # An environment name to purge cache for. VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV="${VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV:-}" -# File with a list of domains that should be purged. -VORTEX_TASK_PURGE_CACHE_ACQUIA_DOMAINS_FILE="${VORTEX_TASK_PURGE_CACHE_ACQUIA_DOMAINS_FILE:-domains.txt}" - # Number of status retrieval retries. If this limit reached and task has not # yet finished, the task is considered failed. VORTEX_TASK_PURGE_CACHE_ACQUIA_STATUS_RETRIES="${VORTEX_TASK_PURGE_CACHE_ACQUIA_STATUS_RETRIES:-600}" @@ -60,6 +57,19 @@ extract_json_value() { php -r "\$data=json_decode(file_get_contents('php://stdin'), TRUE); isset(\$data[\"${key}\"]) ? print trim(json_encode(\$data[\"${key}\"], JSON_UNESCAPED_SLASHES), '\"') : exit(1);" } +# Wildcard entries are not hostnames, so the purge endpoint cannot take them. +extract_json_hostnames() { + php -r "\$data=json_decode(file_get_contents('php://stdin'), TRUE); foreach (\$data['_embedded']['items'] ?? [] as \$item) { if (!empty(\$item['hostname']) && empty(\$item['flags']['wildcard']) && strpos(\$item['hostname'], '*') === FALSE) { print \$item['hostname'] . PHP_EOL; } }" +} + +extract_json_items_count() { + php -r "\$data=json_decode(file_get_contents('php://stdin'), TRUE); is_array(\$data['_embedded']['items'] ?? NULL) ? print count(\$data['_embedded']['items']) : exit(1);" +} + +extract_json_pagination_total() { + php -r "\$data=json_decode(file_get_contents('php://stdin'), TRUE); isset(\$data['pagination']['total']) ? print (int) \$data['pagination']['total'] : exit(1);" +} + for cmd in php curl; do command -v "${cmd}" >/dev/null || fail "Command ${cmd} is not available."; done info "Started cache purging in Acquia." @@ -68,7 +78,6 @@ info "Started cache purging in Acquia." [ -z "${VORTEX_TASK_PURGE_CACHE_ACQUIA_SECRET}" ] && fail "Missing required value for VORTEX_TASK_PURGE_CACHE_ACQUIA_SECRET or VORTEX_ACQUIA_SECRET." [ -z "${VORTEX_TASK_PURGE_CACHE_ACQUIA_APP_NAME}" ] && fail "Missing required value for VORTEX_TASK_PURGE_CACHE_ACQUIA_APP_NAME or VORTEX_ACQUIA_APP_NAME." [ -z "${VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV}" ] && fail "Missing required value for VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV." -[ -z "${VORTEX_TASK_PURGE_CACHE_ACQUIA_DOMAINS_FILE}" ] && fail "Missing required value for VORTEX_TASK_PURGE_CACHE_ACQUIA_DOMAINS_FILE." [ -z "${VORTEX_TASK_PURGE_CACHE_ACQUIA_STATUS_RETRIES}" ] && fail "Missing required value for VORTEX_TASK_PURGE_CACHE_ACQUIA_STATUS_RETRIES." [ -z "${VORTEX_TASK_PURGE_CACHE_ACQUIA_STATUS_INTERVAL}" ] && fail "Missing required value for VORTEX_TASK_PURGE_CACHE_ACQUIA_STATUS_INTERVAL." @@ -111,53 +120,60 @@ env_id=$(echo "${envs_json}" | extract_json_value "_embedded" | extract_json_val [ -z "${env_id}" ] && fail "Unable to retrieve environment ID for \"${VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV}\". API response: ${envs_json}" pass "Retrieved environment ID." -task "Compiling a list of domains." - -target_env="${VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV}" +task "Retrieving domains attached to ${VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV} environment." domain_list=() -while read -r domain; do - # TARGET_ENV_REMAP maps the target env to the sub-domain prefix based on - # the UI name. - TARGET_ENV_REMAP="${target_env}" - # Strip placeholder for PROD environment. - if [ "${target_env}" = "prod" ]; then - domain="${domain//\$TARGET_ENV_REMAP./}" - domain="${domain//\$TARGET_ENV./}" +domains_limit=100 +domains_offset=0 +previous_domains_json="" +while true; do + domains_json=$(curl -s -L -H 'Accept: application/json, version=2' -H "Authorization: Bearer ${token}" "https://cloud.acquia.com/api/environments/${env_id}/domains?limit=${domains_limit}&offset=${domains_offset}") + [ "${VORTEX_DEBUG-}" = "1" ] && note "Domains API response: ${domains_json}" + + page_count=$(echo "${domains_json}" | extract_json_items_count) || page_count="" + if echo "${domains_json}" | grep -q '"error"' || [ -z "${page_count}" ]; then + fail "Unable to retrieve domains attached to ${VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV} environment. API response: ${domains_json}" fi - # Re-map 'test' to 'stage' as seen in UI. - if [ "${target_env}" = "test" ]; then - TARGET_ENV_REMAP=stage + # Stops an API that ignores the offset from returning the same page forever. + if [ "${page_count}" -eq 0 ] || [ "${domains_json}" = "${previous_domains_json}" ]; then + break fi + previous_domains_json="${domains_json}" - # Disable replacement for unknown environments. - if [ "${target_env}" != "dev" ] && [ "${target_env}" != "test" ] && [ "${target_env}" != "test2" ] && [ "${target_env}" != "prod" ]; then - TARGET_ENV_REMAP="" - fi + while IFS= read -r domain; do + if [ -n "${domain}" ]; then + domain_list+=("${domain}") + fi + done < <(echo "${domains_json}" | extract_json_hostnames) - if [ -n "${TARGET_ENV_REMAP}" ]; then - # Interpolate variables in domain name. - domain="$(eval echo "${domain}")" - domain_list+=("${domain}") - fi -done <"${VORTEX_TASK_PURGE_CACHE_ACQUIA_DOMAINS_FILE}" + domains_offset=$((domains_offset + page_count)) + domains_total=$(echo "${domains_json}" | extract_json_pagination_total) || domains_total="" -pass "Compiled a list of ${#domain_list[@]} domains." + # The API can cap a page below the requested limit, so a short page marks + # the end only when the response reports no total. + if [ -n "${domains_total}" ]; then + if [ "${domains_offset}" -ge "${domains_total}" ]; then + break + fi + elif [ "${page_count}" -lt "${domains_limit}" ]; then + break + fi +done +pass "Retrieved ${#domain_list[@]} domains attached to ${VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV} environment." if [ "${#domain_list[@]}" -gt 0 ]; then # The Acquia API aborts a batched purge when any domain fails, so caches - # are cleared per domain and a missing domain is not a failure. + # are cleared per domain. for domain in "${domain_list[@]}"; do task "Purging cache for ${VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV} environment domain ${domain}." - task_status_json=$(curl -X POST -s -L -H 'Accept: application/json, version=2' -H "Authorization: Bearer ${token}" -H "Content-Type: application/json" -d "{\"domains\":[\"${domain}\"]}" "https://cloud.acquia.com/api/environments/${env_id}/domains/actions/clear-varnish") + task_status_json=$(curl -X POST -s -L -H 'Accept: application/json, version=2' -H "Authorization: Bearer ${token}" -H "Content-Type: application/json" -d "{\"domains\":[\"${domain}\"]}" "https://cloud.acquia.com/api/environments/${env_id}/actions/clear-caches") notification_url=$(echo "${task_status_json}" | extract_json_value "_links" | extract_json_value "notification" | extract_json_value "href") || true - # A missing domain yields an empty notification URL and is skipped - # without a failure. + # A rejected purge yields an empty notification URL. if [ -z "${notification_url}" ]; then - note "Warning: Unable to purge cache for ${VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV} environment domain ${domain} as it does not exist." + note "Warning: Unable to purge cache for ${VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV} environment domain ${domain}. API response: ${task_status_json}" pass "Completed cache purging for ${VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV} environment domain ${domain}." - break + continue fi printf " Checking task status: " diff --git a/.vortex/tooling/tests/unit/task-purge-cache-acquia.bats b/.vortex/tooling/tests/unit/task-purge-cache-acquia.bats index 16671d5791..2c432dd4e0 100644 --- a/.vortex/tooling/tests/unit/task-purge-cache-acquia.bats +++ b/.vortex/tooling/tests/unit/task-purge-cache-acquia.bats @@ -6,22 +6,17 @@ load ../_helper.bash -# Runs the task for one environment and asserts the domain it purges. -# -# The Acquia API replies without a notification link, which the task reports as -# a missing domain and skips, so the run stops before the status polling. +# Appends the steps that resolve the token, application, environment and the +# first page of domains attached to the environment. # # Arguments: # 1. env_name: Environment to purge the cache for. -# 2. domain: Domain the placeholder is expected to resolve to. -# 3. domains_file: File to read the domain list from. Optional, defaults to -# the domain list shipped with the template. -assert_purge_domain() { +# 2. domains_json: Response of the environment domains API. +steps_lookup() { local env_name="${1}" - local domain="${2}" - local domains_file="${3:-hooks/library/domains.txt}" + local domains_json="${2}" - declare -a STEPS=( + STEPS+=( "[INFO] Started cache purging in Acquia." "Retrieving authentication token." @@ -36,157 +31,320 @@ assert_purge_domain() { "@curl -s -L -H Accept: application/json, version=2 -H Authorization: Bearer test-token https://cloud.acquia.com/api/applications/app-uuid-123/environments?filter=name%3D${env_name} # {\"_embedded\":{\"items\":[{\"id\":\"env-id-456\"}]}}" "[ OK ] Retrieved environment ID." - "Compiling a list of domains." - "[ OK ] Compiled a list of 1 domains." + "Retrieving domains attached to ${env_name} environment." + "@curl -s -L -H Accept: application/json, version=2 -H Authorization: Bearer test-token https://cloud.acquia.com/api/environments/env-id-456/domains?limit=100&offset=0 # ${domains_json}" + ) +} + +# Appends the request for a further page of the environment domains. +# +# Arguments: +# 1. offset: Offset the page is requested from. +# 2. domains_json: Response of the environment domains API. +steps_lookup_page() { + local offset="${1}" + local domains_json="${2}" + + STEPS+=( + "@curl -s -L -H Accept: application/json, version=2 -H Authorization: Bearer test-token https://cloud.acquia.com/api/environments/env-id-456/domains?limit=100&offset=${offset} # ${domains_json}" + ) +} + +# Appends the steps of a purge that completes for one domain. +# +# Arguments: +# 1. env_name: Environment to purge the cache for. +# 2. domain: Domain to purge. +steps_purge() { + local env_name="${1}" + local domain="${2}" + STEPS+=( "Purging cache for ${env_name} environment domain ${domain}." - "@curl -X POST -s -L -H Accept: application/json, version=2 -H Authorization: Bearer test-token -H Content-Type: application/json -d {\"domains\":[\"${domain}\"]} https://cloud.acquia.com/api/environments/env-id-456/domains/actions/clear-varnish # {}" - "Warning: Unable to purge cache for ${env_name} environment domain ${domain} as it does not exist." + "@curl -X POST -s -L -H Accept: application/json, version=2 -H Authorization: Bearer test-token -H Content-Type: application/json -d {\"domains\":[\"${domain}\"]} https://cloud.acquia.com/api/environments/env-id-456/actions/clear-caches # {\"_links\":{\"notification\":{\"href\":\"https://cloud.acquia.com/api/notifications/notif-123\"}}}" + '@curl -s -L -H Accept: application/json, version=2 -H Authorization: Bearer test-token https://cloud.acquia.com/api/notifications/notif-123 # {"status":"completed"}' + "Purged cache for ${env_name} environment domain ${domain}." "[ OK ] Completed cache purging for ${env_name} environment domain ${domain}." - - "[ OK ] Finished cache purging in Acquia." ) +} + +# Builds a domains API response from a list of hostnames. +# +# A hostname starting with '*' is flagged as a wildcard. +# +# Arguments: +# 1. --total=: Total reported in the pagination block. Optional, the +# block is left out when omitted. +# @: Hostnames attached to the environment. +domains_json() { + local pagination="" + if [ "${1#--total=}" != "${1:-}" ]; then + pagination=",\"pagination\":{\"total\":${1#--total=},\"limit\":100,\"offset\":0}" + shift + fi + + local items=() + local hostname + local wildcard + for hostname in "$@"; do + wildcard="false" + if [ "${hostname:0:1}" = "*" ]; then + wildcard="true" + fi + items+=("{\"hostname\":\"${hostname}\",\"flags\":{\"default\":false,\"active\":true,\"wildcard\":${wildcard}}}") + done + + local IFS="," + echo "{\"total\":${#items[@]}${pagination},\"_embedded\":{\"items\":[${items[*]}]}}" +} + +# Prints wildcard hostnames, which fill a page without adding purges. +# +# Arguments: +# 1. count: Number of hostnames to print. +wildcard_hostnames() { + local count="${1}" + local i + for ((i = 1; i <= count; i++)); do + echo "*.d${i}.example.com" + done +} + +# Runs the task for one environment with the steps in STEPS. +# +# Arguments: +# 1. env_name: Environment to purge the cache for. +run_purge() { + local env_name="${1}" export VORTEX_TASK_PURGE_CACHE_ACQUIA_KEY="test-key" export VORTEX_TASK_PURGE_CACHE_ACQUIA_SECRET="test-secret" export VORTEX_TASK_PURGE_CACHE_ACQUIA_APP_NAME="testapp" export VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV="${env_name}" - export VORTEX_TASK_PURGE_CACHE_ACQUIA_DOMAINS_FILE="${domains_file}" + export VORTEX_TASK_PURGE_CACHE_ACQUIA_STATUS_RETRIES="1" + export VORTEX_TASK_PURGE_CACHE_ACQUIA_STATUS_INTERVAL="0" mocks="$(steps_run "setup")" run .vortex/tooling/src/vortex-task-purge-cache-acquia steps_run "assert" "${mocks[@]}" +} + +@test "task-purge-cache-acquia: purges every domain attached to the environment" { + pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 + + declare -a STEPS=() + steps_lookup "prod" "$(domains_json "siteprod.prod.acquia-sites.com" "example.com" "www.example.com" "*.example.com")" + STEPS+=( + "[ OK ] Retrieved 3 domains attached to prod environment." + ) + steps_purge "prod" "siteprod.prod.acquia-sites.com" + steps_purge "prod" "example.com" + steps_purge "prod" "www.example.com" + STEPS+=( + "- *.example.com" + "[ OK ] Finished cache purging in Acquia." + ) + + run_purge "prod" assert_success + + popd >/dev/null || exit 1 } -@test "task-purge-cache-acquia: prod strips the placeholder from the shipped domain list" { +@test "task-purge-cache-acquia: purges every domain attached to an on-demand environment" { pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 - assert_purge_domain "prod" "your-site-domain.example" + declare -a STEPS=() + steps_lookup "ode1" "$(domains_json "siteode1.prod.acquia-sites.com")" + STEPS+=( + "[ OK ] Retrieved 1 domains attached to ode1 environment." + ) + steps_purge "ode1" "siteode1.prod.acquia-sites.com" + STEPS+=( + "[ OK ] Finished cache purging in Acquia." + ) + + run_purge "ode1" - # A placeholder that survives the strip is interpolated into the environment - # name, which is the prefix production must not carry. - assert_output_not_contains "prod.your-site-domain.example" + assert_success popd >/dev/null || exit 1 } -@test "task-purge-cache-acquia: prod strips the legacy placeholder" { +@test "task-purge-cache-acquia: environment without domains purges nothing" { pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 - # shellcheck disable=SC2016 - echo '$TARGET_ENV.your-site-domain.example' >domains-legacy.txt + declare -a STEPS=() + steps_lookup "dev" '{"total":0,"_embedded":{"items":[]}}' + STEPS+=( + "[ OK ] Retrieved 0 domains attached to dev environment." + "Unable to find domains to purge cache for dev environment." + "- Purging cache for" + "[ OK ] Finished cache purging in Acquia." + ) - assert_purge_domain "prod" "your-site-domain.example" "domains-legacy.txt" + run_purge "dev" - assert_output_not_contains "prod.your-site-domain.example" + assert_success popd >/dev/null || exit 1 } -@test "task-purge-cache-acquia: dev prefixes the domain with the environment name" { +@test "task-purge-cache-acquia: continues with the next domain when a purge is rejected" { pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 - assert_purge_domain "dev" "dev.your-site-domain.example" + declare -a STEPS=() + steps_lookup "dev" "$(domains_json "dev.example.com" "dev.other.example")" + STEPS+=( + "[ OK ] Retrieved 2 domains attached to dev environment." + + "Purging cache for dev environment domain dev.example.com." + '@curl -X POST -s -L -H Accept: application/json, version=2 -H Authorization: Bearer test-token -H Content-Type: application/json -d {"domains":["dev.example.com"]} https://cloud.acquia.com/api/environments/env-id-456/actions/clear-caches # {"error":"forbidden"}' + 'Warning: Unable to purge cache for dev environment domain dev.example.com. API response: {"error":"forbidden"}' + "[ OK ] Completed cache purging for dev environment domain dev.example.com." + ) + steps_purge "dev" "dev.other.example" + STEPS+=( + "[ OK ] Finished cache purging in Acquia." + ) + + run_purge "dev" + + assert_success popd >/dev/null || exit 1 } -@test "task-purge-cache-acquia: test prefixes the domain with the stage name used in the UI" { +@test "task-purge-cache-acquia: continues with the next domain when the status checks run out" { pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 - assert_purge_domain "test" "stage.your-site-domain.example" + declare -a STEPS=() + steps_lookup "dev" "$(domains_json "dev.example.com" "dev.other.example")" + STEPS+=( + "[ OK ] Retrieved 2 domains attached to dev environment." + + "Purging cache for dev environment domain dev.example.com." + '@curl -X POST -s -L -H Accept: application/json, version=2 -H Authorization: Bearer test-token -H Content-Type: application/json -d {"domains":["dev.example.com"]} https://cloud.acquia.com/api/environments/env-id-456/actions/clear-caches # {"_links":{"notification":{"href":"https://cloud.acquia.com/api/notifications/notif-123"}}}' + '@curl -s -L -H Accept: application/json, version=2 -H Authorization: Bearer test-token https://cloud.acquia.com/api/notifications/notif-123 # {"status":"in-progress"}' + '@curl -s -L https://accounts.acquia.com/api/auth/oauth/token --data-urlencode client_id=test-key --data-urlencode client_secret=test-secret --data-urlencode grant_type=client_credentials # {"access_token":"test-token"}' + "Refreshed authentication token." + "Warning: Unable to purge cache for dev environment domain dev.example.com." + "[ OK ] Completed cache purging for dev environment domain dev.example.com." + ) + steps_purge "dev" "dev.other.example" + STEPS+=( + "[ OK ] Finished cache purging in Acquia." + ) + + run_purge "dev" + + assert_success popd >/dev/null || exit 1 } -@test "task-purge-cache-acquia: test2 prefixes the domain with the environment name" { +@test "task-purge-cache-acquia: reports the failed domain lookup" { pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 - assert_purge_domain "test2" "test2.your-site-domain.example" + declare -a STEPS=() + steps_lookup "dev" '{"error":"not_found","message":"Domain names do not exist on non-hosted applications."}' + STEPS+=( + '[FAIL] Unable to retrieve domains attached to dev environment. API response: {"error":"not_found","message":"Domain names do not exist on non-hosted applications."}' + "- Purging cache for" + ) + + run_purge "dev" + + assert_failure popd >/dev/null || exit 1 } -@test "task-purge-cache-acquia: unknown environment compiles no domains" { +@test "task-purge-cache-acquia: reports a domain lookup response without domains" { pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 - declare -a STEPS=( - "[INFO] Started cache purging in Acquia." + declare -a STEPS=() + steps_lookup "dev" 'Bad Gateway' + STEPS+=( + '[FAIL] Unable to retrieve domains attached to dev environment. API response: Bad Gateway' + "- Purging cache for" + ) - "Retrieving authentication token." - '@curl -s -L https://accounts.acquia.com/api/auth/oauth/token --data-urlencode client_id=test-key --data-urlencode client_secret=test-secret --data-urlencode grant_type=client_credentials # {"access_token":"test-token"}' + run_purge "dev" - "Retrieving testapp application UUID." - '@curl -s -L -H Accept: application/json, version=2 -H Authorization: Bearer test-token https://cloud.acquia.com/api/applications?filter=name%3Dtestapp # {"_embedded":{"items":[{"uuid":"app-uuid-123"}]}}' + assert_failure - "Retrieving environment ID." - '@curl -s -L -H Accept: application/json, version=2 -H Authorization: Bearer test-token https://cloud.acquia.com/api/applications/app-uuid-123/environments?filter=name%3Dunknownenv # {"_embedded":{"items":[{"id":"env-id-456"}]}}' + popd >/dev/null || exit 1 +} - "[ OK ] Compiled a list of 0 domains." - "Unable to find domains to purge cache for unknownenv environment." +@test "task-purge-cache-acquia: requests the next page of domains after a full page" { + pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 + + mapfile -t first_page < <(wildcard_hostnames 100) + declare -a STEPS=() + steps_lookup "dev" "$(domains_json "${first_page[@]}")" + steps_lookup_page 100 "$(domains_json "dev.example.com")" + STEPS+=( + "[ OK ] Retrieved 1 domains attached to dev environment." + ) + steps_purge "dev" "dev.example.com" + STEPS+=( "[ OK ] Finished cache purging in Acquia." ) - export VORTEX_TASK_PURGE_CACHE_ACQUIA_KEY="test-key" - export VORTEX_TASK_PURGE_CACHE_ACQUIA_SECRET="test-secret" - export VORTEX_TASK_PURGE_CACHE_ACQUIA_APP_NAME="testapp" - export VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV="unknownenv" - export VORTEX_TASK_PURGE_CACHE_ACQUIA_DOMAINS_FILE="hooks/library/domains.txt" - - mocks="$(steps_run "setup")" - run .vortex/tooling/src/vortex-task-purge-cache-acquia - steps_run "assert" "${mocks[@]}" + run_purge "dev" assert_success popd >/dev/null || exit 1 } -@test "task-purge-cache-acquia: prod purge completes for an existing domain" { +@test "task-purge-cache-acquia: requests the next page of domains until the reported total" { pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 - declare -a STEPS=( - "[INFO] Started cache purging in Acquia." + declare -a STEPS=() + steps_lookup "dev" "$(domains_json --total=3 "dev.example.com" "dev.other.example")" + steps_lookup_page 2 "$(domains_json --total=3 "dev.third.example")" + STEPS+=( + "[ OK ] Retrieved 3 domains attached to dev environment." + ) + steps_purge "dev" "dev.example.com" + steps_purge "dev" "dev.other.example" + steps_purge "dev" "dev.third.example" + STEPS+=( + "[ OK ] Finished cache purging in Acquia." + ) - "Retrieving authentication token." - '@curl -s -L https://accounts.acquia.com/api/auth/oauth/token --data-urlencode client_id=test-key --data-urlencode client_secret=test-secret --data-urlencode grant_type=client_credentials # {"access_token":"test-token"}' + run_purge "dev" - "Retrieving testapp application UUID." - '@curl -s -L -H Accept: application/json, version=2 -H Authorization: Bearer test-token https://cloud.acquia.com/api/applications?filter=name%3Dtestapp # {"_embedded":{"items":[{"uuid":"app-uuid-123"}]}}' + assert_success - "Retrieving environment ID." - '@curl -s -L -H Accept: application/json, version=2 -H Authorization: Bearer test-token https://cloud.acquia.com/api/applications/app-uuid-123/environments?filter=name%3Dprod # {"_embedded":{"items":[{"id":"env-id-456"}]}}' + popd >/dev/null || exit 1 +} - "[ OK ] Compiled a list of 1 domains." +@test "task-purge-cache-acquia: stops paging when the API repeats a page" { + pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 - "Purging cache for prod environment domain your-site-domain.example." - '@curl -X POST -s -L -H Accept: application/json, version=2 -H Authorization: Bearer test-token -H Content-Type: application/json -d {"domains":["your-site-domain.example"]} https://cloud.acquia.com/api/environments/env-id-456/domains/actions/clear-varnish # {"_links":{"notification":{"href":"https://cloud.acquia.com/api/notifications/notif-123"}}}' - '@curl -s -L -H Accept: application/json, version=2 -H Authorization: Bearer test-token https://cloud.acquia.com/api/notifications/notif-123 # {"status":"completed"}' - "Purged cache for prod environment domain your-site-domain.example." - "[ OK ] Completed cache purging for prod environment domain your-site-domain.example." + mapfile -t page < <(wildcard_hostnames 99) + page_json="$(domains_json "${page[@]}" "dev.example.com")" + declare -a STEPS=() + steps_lookup "dev" "${page_json}" + steps_lookup_page 100 "${page_json}" + STEPS+=( + "[ OK ] Retrieved 1 domains attached to dev environment." + ) + steps_purge "dev" "dev.example.com" + STEPS+=( "[ OK ] Finished cache purging in Acquia." ) - export VORTEX_TASK_PURGE_CACHE_ACQUIA_KEY="test-key" - export VORTEX_TASK_PURGE_CACHE_ACQUIA_SECRET="test-secret" - export VORTEX_TASK_PURGE_CACHE_ACQUIA_APP_NAME="testapp" - export VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV="prod" - export VORTEX_TASK_PURGE_CACHE_ACQUIA_DOMAINS_FILE="hooks/library/domains.txt" - export VORTEX_TASK_PURGE_CACHE_ACQUIA_STATUS_RETRIES="1" - export VORTEX_TASK_PURGE_CACHE_ACQUIA_STATUS_INTERVAL="0" - - mocks="$(steps_run "setup")" - run .vortex/tooling/src/vortex-task-purge-cache-acquia - steps_run "assert" "${mocks[@]}" + run_purge "dev" assert_success - assert_output_not_contains "prod.your-site-domain.example" - popd >/dev/null || exit 1 } diff --git a/hooks/library/domains.txt b/hooks/library/domains.txt deleted file mode 100644 index 0e31e9f2ea..0000000000 --- a/hooks/library/domains.txt +++ /dev/null @@ -1 +0,0 @@ -$TARGET_ENV_REMAP.your-site-domain.example diff --git a/hooks/library/purge-cache.sh b/hooks/library/purge-cache.sh index f2537f599b..080ec2eb18 100755 --- a/hooks/library/purge-cache.sh +++ b/hooks/library/purge-cache.sh @@ -17,7 +17,6 @@ export VORTEX_ACQUIA_KEY="${VORTEX_ACQUIA_KEY:?Missing required value.}" export VORTEX_ACQUIA_SECRET="${VORTEX_ACQUIA_SECRET:?Missing required value.}" export VORTEX_ACQUIA_APP_NAME="${VORTEX_ACQUIA_APP_NAME:-${site}}" export VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV="${VORTEX_TASK_PURGE_CACHE_ACQUIA_ENV:-${target_env}}" -export VORTEX_TASK_PURGE_CACHE_ACQUIA_DOMAINS_FILE="${VORTEX_TASK_PURGE_CACHE_ACQUIA_DOMAINS_FILE:-"/var/www/html/${site}.${target_env}/hooks/library/domains.txt"}" pushd "/var/www/html/${site}.${target_env}" >/dev/null || exit 1