diff --git a/README.md b/README.md index 5d8a8f5..1ddfcf4 100644 --- a/README.md +++ b/README.md @@ -519,6 +519,45 @@ summarised below. Two things become required with it: Neither is needed when the database already holds DDEV hostnames. +### Which side the database holds — `--holds` + +A committed production-canonical file describes the sites, not the database +in front of you. A worktree's data comes from somewhere: a pristine production +dump holds each site's `canonical`, `ddev hostshift copy-db` from a parent whose +pull search-replaced to `.ddev.site` holds each site's `base`, and a staging +pull holds its staging alias. Mapped from the wrong side, the app is handed a +hostname its data does not name, and one that checks the Host redirects every +page to the one it does — a copy-db worktree of a production-canonical repo +301s to the parent. + +So say which side, and keep the one file: + +``` +ddev hostshift init --holds base # copy-db, or any search-replaced pull +ddev hostshift init --holds alias # a staging pull +ddev hostshift init --holds canonical # a production dump; the default +``` + +`base` and `canonical` are the fields of that name; `alias` is the one alias a +site declares besides its base, and refuses a site with none or several rather +than guess. The variant does not move — it is always derived from `base` — so +the preview URL is the same whichever side the data is on. + +The side is recorded as `HOSTSHIFT_HOLDS` in `.ddev/.env`, the way a chosen +slug is, so `check`, the post-start hook and a later bare `init` resolve the +same map; `--holds canonical` goes back and records nothing. `copy-db` says so +when it has just put the bases into a worktree mapped on its canonicals. + +Off the canonical side the proxy gets a flat `--from`/`--to` map — the held +origin of each site against its variant — rather than reading the mounted file, +because the published image predates the flag. A flat map carries no aliases, +so a URL in the copy naming one of the site's other origins passes through +unrewritten, as it did before the repo committed the file. `--holds base` +without a `hostshift.yaml` is accepted and changes nothing: the map is the +parent's DDEV hostnames already. The binary takes the same flag +(`hostshift map --holds base --pairs`), and there it keeps the other origins as +aliases. + ## Reference ``` @@ -542,8 +581,9 @@ ddev hostshift env print what init would write, without writing ddev hostshift wp-cli emit wp-cli.local.yml on stdout ``` -`--slug NAME` overrides the slug derived from the git branch. `--dry-run` shows -what `init` would write without writing it. +`--slug NAME` overrides the slug derived from the git branch. `--holds SIDE` +says which of `hostshift.yaml`'s origins the database holds (above). `--dry-run` +shows what `init` would write without writing it. Flags worth knowing: diff --git a/cmd/hostshift/main.go b/cmd/hostshift/main.go index 7576e8d..54c5aad 100644 --- a/cmd/hostshift/main.go +++ b/cmd/hostshift/main.go @@ -201,6 +201,7 @@ type common struct { file string slug, upstream string from, to repeatable + holds string anyVariant bool } @@ -212,6 +213,9 @@ func (c *common) register(fs *flag.FlagSet) { fs.Var(&c.from, "from", "canonical origin, repeatable, index-aligned with --to") fs.Var(&c.to, "to", "variant origin, repeatable, index-aligned with --from") fs.Var(pairFlag{&c.from, &c.to}, "map", "canonical=variant, repeatable") + fs.StringVar(&c.holds, "holds", "", + "which of each hostshift.yaml site's origins the database holds:\n"+ + "canonical (the default), base, or alias (its one alias besides base)") } func (c *common) load() (*config.Resolved, error) { @@ -221,6 +225,7 @@ func (c *common) load() (*config.Resolved, error) { From: c.from, To: c.to, File: c.file, + Holds: c.holds, VariantOptional: c.anyVariant, }) } diff --git a/cmd/hostshift/map_json_test.go b/cmd/hostshift/map_json_test.go index 6e64d0f..3cbf535 100644 --- a/cmd/hostshift/map_json_test.go +++ b/cmd/hostshift/map_json_test.go @@ -99,6 +99,59 @@ func TestMapFileOverridesTheProjects(t *testing.T) { } } +// --holds picks which declared origin of each site is the canonical side: the +// same file maps a production dump, a copy of the main checkout's database and +// a staging pull, and the variant stays where it is. +func TestMapHolds(t *testing.T) { + dir := t.TempDir() + writeFile(t, dir, "hostshift.yaml", prodYAML) + pairs := func(holds string) (int, string, string) { + return run(t, "", cmdMap, "-C", dir, "--slug", "wt-a", "--pairs", "--holds", holds) + } + + for holds, want := range map[string]string{ + "canonical": "https://www.example.com=https://wt-a--acme.ddev.site\nhttps://shop.example.com=https://wt-a--shop.acme.ddev.site\n", + "base": "https://acme.ddev.site=https://wt-a--acme.ddev.site\nhttps://shop.acme.ddev.site=https://wt-a--shop.acme.ddev.site\n", + } { + if code, out, errOut := pairs(holds); code != exitOK || out != want { + t.Errorf("--holds %s: exit %d: %q %s", holds, code, out, errOut) + } + } + + // The origin it replaces stays in the map as an alias, so a production URL + // left in a copied database still rewrites to the variant. + _, out, _ := run(t, "", cmdMap, "-C", dir, "--json", "--holds", "base") + s := decodeMap(t, out).Sites[0] + if s.Canonical != "https://acme.ddev.site" || s.Base != "https://acme.ddev.site" || + strings.Join(s.Aliases, " ") != "https://www.example.com https://acme.staging.example.net" { + t.Errorf("--holds base: %+v", s) + } + + // An alias only where a site declares exactly one: the shop declares none. + if code, _, errOut := pairs("alias"); code != exitConfig || !strings.Contains(errOut, `site "shop": --holds alias, but it declares no alias`) { + t.Errorf("alias, none: exit %d: %s", code, errOut) + } + two := strings.Replace(prodYAML, "aliases: [https://acme.staging.example.net]", "aliases: [https://acme.staging.example.net, https://acme.test.example.net]", 1) + staged := two + " aliases: [https://shop.staging.example.net]\n" + writeFile(t, dir, "hostshift.yaml", staged) + if code, _, errOut := pairs("alias"); code != exitConfig || !strings.Contains(errOut, "ambiguous") { + t.Errorf("alias, two: exit %d: %s", code, errOut) + } + writeFile(t, dir, "hostshift.yaml", prodYAML+" aliases: [https://shop.staging.example.net]\n") + want := "https://acme.staging.example.net=https://wt-a--acme.ddev.site\nhttps://shop.staging.example.net=https://wt-a--shop.acme.ddev.site\n" + if code, out, errOut := pairs("alias"); code != exitOK || out != want { + t.Errorf("--holds alias: exit %d: %q %s", code, out, errOut) + } + + // No file, no aliases to hold; and a side that is not one is refused. + if code, _, errOut := run(t, "", cmdMap, "--slug", "wt-a", "--from", "https://a.ddev.site", "--holds", "alias"); code != exitConfig || !strings.Contains(errOut, "declares no aliases") { + t.Errorf("flags: exit %d: %s", code, errOut) + } + if code, _, errOut := pairs("staging"); code != exitConfig || !strings.Contains(errOut, "canonical, base or alias") { + t.Errorf("bad side: exit %d: %s", code, errOut) + } +} + // The layer says which of the three the map came from. func TestMapJSONLayer(t *testing.T) { dir := t.TempDir() diff --git a/ddev/commands/host/hostshift b/ddev/commands/host/hostshift index e9cee5a..44c3570 100755 --- a/ddev/commands/host/hostshift +++ b/ddev/commands/host/hostshift @@ -57,7 +57,9 @@ usage: ddev hostshift [flags] --slug NAME name this worktree's hostnames; recorded, and it sticks --slug-from-branch forget a recorded slug and follow the branch again - --dry-run, -n say what would happen, write nothing + --holds SIDE which of hostshift.yaml's origins the database holds: + canonical (the default), base or alias; recorded + --dry-run, -n say what would happen, write nothing --no-restart init: write the file and stop, for a pre-start hook --force copy-db: overwrite a database that has tables --quiet, -q say nothing when there is nothing to check @@ -76,6 +78,7 @@ fi slug="" slug_explicit="" forget_slug="" +holds="" branch="" dry_run="" no_restart="" @@ -97,6 +100,11 @@ while [ $# -gt 0 ]; do # recording it — so without this the only way to return to branch-derived # naming is to hand-edit .ddev/.env. --slug-from-branch) forget_slug=1; shift ;; + # Both spellings, for the reason --slug gives above. + --holds) holds="${2:?--holds needs a value}"; shift 2 ;; + --holds=*) holds="${1#*=}" + [ -n "$holds" ] || { echo "--holds needs a value" >&2; exit 2; } + shift ;; --dry-run|-n) dry_run=1; shift ;; --no-restart) no_restart=1; shift ;; --quiet|-q) quiet=1; shift ;; @@ -308,6 +316,41 @@ case "$slug" in exit 2 ;; esac +# Which side of hostshift.yaml the database holds, recorded like a chosen slug. +# +# The file declares each site once — canonical, base, aliases — and which of +# those a database holds depends on where *that copy* came from, not on the +# site: a pristine production dump holds the canonicals, `copy-db` from a +# search-replaced parent holds the bases, a staging pull holds the aliases. With +# the file mounted, the proxy maps the canonicals and nothing else, so a copy of +# the parent's database 301'd every page to the parent's hostname — an app that +# checks the Host it is handed against the hostname its data names redirects. +# +# Recorded in .ddev/.env, for the reason HOSTSHIFT_SLUG_CHOSEN is: `check`, the +# post-start hook and a later bare `init` must resolve the same side the +# database was deployed with, or they call a correct deployment stale and +# "fix" it back onto the wrong side. `--holds canonical` is the way back, and +# records nothing because it is the default. +if [ -z "$holds" ] && [ "$cmd" != "copy-db" ]; then + holds="$(sed -n '/^HOSTSHIFT_HOLDS=/{s///p;q;}' .ddev/.env 2>/dev/null | tr -d '\r' || true)" +fi +case "${holds:-canonical}" in + canonical|base|alias) ;; + *) echo "hostshift: --holds $holds: say canonical, base or alias" >&2; exit 2 ;; +esac +[ "$holds" = canonical ] && holds="" +# Without a hostshift.yaml the map already is the base side — the parent's DDEV +# hostnames, which is what the database holds after `copy-db` — so `base` is +# accepted as a no-op, and a caller that knows only where its data came from can +# say so without first asking whether the repo has the file. The other two name +# origins only the file declares. +if [ ! -f hostshift.yaml ] && [ -n "$holds" ] && [ "$holds" != base ] && [ "$cmd" != "copy-db" ]; then + echo "hostshift: --holds $holds needs a hostshift.yaml to say which hostnames those" >&2 + echo " are, and this checkout has none. Without one the map is the parent's" >&2 + echo " DDEV hostnames, which is --holds base." >&2 + exit 2 +fi + # What this project answers to. `hostshift hosts` reads the DDEV config with # its config.*.yaml override merging — parsing that here would be the place the # careful parts get lost. @@ -673,6 +716,15 @@ if [ "$cmd" = "copy-db" ]; then echo " or re-run the copy once $from is up, which replaces it outright." >&2 fi exit 1; } + # Not "nothing else to do" when a hostshift.yaml is here. The file's canonical + # side is usually production, and the copy holds $from's DDEV hostnames — its + # bases — so a map left on the canonicals 301s every page to the parent. + if [ -f hostshift.yaml ] && ! grep -q '^HOSTSHIFT_HOLDS=base$' .ddev/.env 2>/dev/null; then + echo "hostshift: copied. It holds $from's hostnames — hostshift.yaml's" >&2 + echo " \`base:\` side — so map that side:" >&2 + echo " ddev hostshift init --holds base" >&2 + exit 0 + fi echo "hostshift: copied. It still holds $from's hostnames, which is what" >&2 echo " hostshift maps — nothing else to do." >&2 exit 0 @@ -683,6 +735,37 @@ fi # other environments, or production ones the database was never search-replaced # away from. Passing --from would override it. map_args=() +# ...which is exactly what --holds wants, and the only way to ask for it. +# +# The proxy image is :latest and older than this flag, and the compose file's +# rule is that nothing may ask it for a flag it does not have. --from/--to has +# worked since the first release, so the side is resolved here, on the host, and +# handed over as the flat map the no-file path already uses: the held origin of +# each site against its variant. `flat` routes everything below that keys on +# "is there a hostshift.yaml?" for the *proxy's* sake onto that path. +# +# The cost is the aliases. A flat list cannot carry them, so a URL in the copy +# naming one of the site's other origins — production, say, in a copy-db of a +# search-replaced database — passes through unrewritten, exactly as it did +# before the repo committed its hostshift.yaml. +flat="" +if [ -f hostshift.yaml ] && [ -n "$holds" ] && [ "$cmd" != "copy-db" ]; then + if ! held="$(hostshift map --slug "$slug" --holds "$holds" --pairs 2>&1)"; then + case "$held" in + *"flag provided but not defined: -holds"*) + echo "hostshift: --holds needs the hostshift CLI v0.4.0 or later on PATH." >&2 + echo " https://github.com/generoi/hostshift/releases" >&2 ;; + *) printf '%s\n' "$held" >&2 ;; + esac + exit 2 + fi + while IFS='=' read -r c v; do + [ -n "$c" ] || continue + map_args+=(--from "$c" --to "$v") + done <<<"$held" + [ "${#map_args[@]}" -gt 0 ] || { echo "hostshift: --holds $holds resolved no sites" >&2; exit 2; } + flat=1 +fi # The parent declares canonical hostnames this worktree cannot see. # # `hostshift hosts -C ` reads the parent's *DDEV* config and nothing else, @@ -1216,9 +1299,9 @@ done <<<"$hosts" # So it is resolved here, on the host, where the parent checkout is readable, and # handed over. Only when there is no hostshift.yaml: that file *is* mounted, it # carries aliases a canonical=variant list cannot express, and the container -# reads it correctly. +# reads it correctly — for the canonical side. Any other side is flat too. hsmap="" -if [ ! -f hostshift.yaml ]; then +if [ ! -f hostshift.yaml ] || [ -n "$flat" ]; then # Repeated --from/--to, not `--map c=v,c=v`. Comma-separated --map is a flag # form this branch added, and the image is :latest: the published binary splits # the whole string on its first `=`, so a two-site map came out as one site @@ -1368,7 +1451,8 @@ fi # slug when run as printed, which in the case that motivates --slug at all is # back onto the colliding name the collision warning told you to move off. env_out="#ddev-silent-no-warn${slug_explicit:+ -HOSTSHIFT_SLUG_CHOSEN=$slug} +HOSTSHIFT_SLUG_CHOSEN=$slug}${holds:+ +HOSTSHIFT_HOLDS=$holds} HOSTSHIFT_ARGS=$args HOSTSHIFT_VARIANTS=$variants HOSTSHIFT_WEB_HOSTS=$web" @@ -1422,7 +1506,7 @@ if [ "$cmd" = "check" ]; then # variable of the developer's own sharing the prefix — and the compose file # reads HOSTSHIFT_* names, so that is how someone arrives at one. deployed="$(grep -e '^HOSTSHIFT_ARGS=' -e '^HOSTSHIFT_VARIANTS=' -e '^HOSTSHIFT_WEB_HOSTS=' \ - -e '^HOSTSHIFT_SLUG_CHOSEN=' -e '^#ddev-silent-no-warn' .ddev/.env | tr -d '\r' || true)" + -e '^HOSTSHIFT_SLUG_CHOSEN=' -e '^HOSTSHIFT_HOLDS=' -e '^#ddev-silent-no-warn' .ddev/.env | tr -d '\r' || true)" if [ "$deployed" != "$env_out" ]; then echo "hostshift: .ddev/.env is out of date. It says" >&2 printf '%s\n' "${deployed:- nothing}" >&2 @@ -2902,7 +2986,7 @@ trap 'rm -f "$tmp"' EXIT # it meant it. cp -p .ddev/.env "$tmp" { grep -v -e '^HOSTSHIFT_ARGS=' -e '^HOSTSHIFT_VARIANTS=' -e '^HOSTSHIFT_WEB_HOSTS=' \ - -e '^HOSTSHIFT_SLUG_CHOSEN=' -e '^#ddev-silent-no-warn' .ddev/.env || true + -e '^HOSTSHIFT_SLUG_CHOSEN=' -e '^HOSTSHIFT_HOLDS=' -e '^#ddev-silent-no-warn' .ddev/.env || true printf '%s\n' "$env_out"; } > "$tmp" mv "$tmp" .ddev/.env diff --git a/internal/config/config.go b/internal/config/config.go index faa7907..5e7982b 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -15,6 +15,7 @@ import ( "net/url" "os" "path/filepath" + "slices" "strings" "gopkg.in/yaml.v3" @@ -64,8 +65,35 @@ type Flags struct { // a variant from; those sites keep a zero Variant and the Map is then a // listing that was never validated as a map, for `map --json` only. VariantOptional bool + // Holds says which of each hostshift.yaml site's declared origins the + // database holds: "canonical" (the default, also ""), "base", or "alias", + // the one alias a site declares besides its base. See Holds. + Holds string } +// The sides a database can hold, for Flags.Holds. +// +// A hostshift.yaml declares each site once, with every hostname its content is +// known by: canonical (usually production), base (the main checkout's DDEV +// host), aliases (staging). Which of them a given database holds is not a +// property of the site — it is a property of where that copy came from. A +// pristine production dump holds the canonicals, a copy of the developer's own +// search-replaced database holds the bases, a staging pull holds the staging +// aliases. Writing a second hostshift.yaml per source would be two declarations +// of one set of sites drifting apart; this names which origin is the map's +// canonical side and leaves the file alone. +// +// The held origin becomes the canonical — what the request direction writes +// back, and so the Host the app sees — and every other origin +// the site declares becomes an alias, so a residual URL from any environment +// still rewrites to the one variant. The variant does not move: it is always +// derived from base. +const ( + HoldsCanonical = "canonical" + HoldsBase = "base" + HoldsAlias = "alias" +) + // Resolved is everything the proxy needs. type Resolved struct { Map *origin.Map @@ -135,8 +163,27 @@ func validateUpstream(raw string) error { } func resolve(dir string, f Flags) (*Resolved, error) { + switch f.Holds { + case "", HoldsCanonical, HoldsBase, HoldsAlias: + default: + return nil, fmt.Errorf("--holds %q: say canonical, base or alias", f.Holds) + } + // Only a hostshift.yaml declares more than one origin per site. Under the + // other layers canonical and base are the same origin, so either answer is + // the map as it stands; an alias is something only the file can name, and + // quietly mapping the canonical instead would be the wrong side, silently. + fileLayerOnly := func(layer string) error { + if f.Holds == HoldsAlias { + return fmt.Errorf("--holds alias: the map comes from %s, which declares no aliases — only a hostshift.yaml does", layer) + } + return nil + } + // Layer 3 first when it is total: an explicit --from/--to map needs no files. if len(f.From) > 0 || len(f.To) > 0 { + if err := fileLayerOnly("--from/--to"); err != nil { + return nil, err + } m, err := mapFromFlags(f) if err != nil { return nil, err @@ -187,7 +234,13 @@ func resolve(dir string, f Flags) (*Resolved, error) { if sites, err = sitesFromFile(hf, pattern, f.Slug); err != nil { return nil, err } + if sites, err = holdSide(sites, hf, f.Holds); err != nil { + return nil, err + } default: + if err := fileLayerOnly("the DDEV config"); err != nil { + return nil, err + } // Layer 1. For a single-environment site with no extra aliases this is // sufficient on its own and no hostshift.yaml is needed at all. if proj == nil { @@ -456,6 +509,54 @@ func sitesFromFile(f *File, pattern, slug string) ([]origin.Site, error) { return sites, nil } +// holdSide makes each site's held origin its canonical (Flags.Holds), and the +// canonical it replaces one of its aliases. sites are sitesFromFile's, in the +// file's order. +func holdSide(sites []origin.Site, f *File, holds string) ([]origin.Site, error) { + if holds == "" || holds == HoldsCanonical { + return sites, nil + } + out := make([]origin.Site, 0, len(sites)) + for i, s := range sites { + held := s.Base + if holds == HoldsAlias { + // The aliases as declared, not s.Aliases, which has base folded in. + var cands []origin.Origin + for _, raw := range f.Sites[i].Aliases { + o, _ := origin.Parse(raw) // parsed by sitesFromFile + if !o.Equal(s.Base) && !o.Equal(s.Canonical) && + !slices.ContainsFunc(cands, o.Equal) { + cands = append(cands, o) + } + } + // Exactly one, or the answer is a guess. A site with none has no + // staging hostname to hold; a site with two could hold either, and + // picking the first maps the other's rows to nothing. + switch len(cands) { + case 1: + held = cands[0] + case 0: + return nil, fmt.Errorf("site %q: --holds alias, but it declares no alias besides its base", s.Name) + default: + return nil, fmt.Errorf("site %q: --holds alias, but it declares %d aliases besides its base, so which one the database holds is ambiguous", s.Name, len(cands)) + } + } + if held.Equal(s.Canonical) { + out = append(out, s) + continue + } + aliases := []origin.Origin{s.Canonical} + for _, a := range s.Aliases { + if !a.Equal(held) { + aliases = append(aliases, a) + } + } + s.Canonical, s.Aliases = held, aliases + out = append(out, s) + } + return out, nil +} + // deriveVariant applies the pattern to the leftmost label of base. An explicit // variant: on the site wins; base falls back to canonical when absent. With no // slug it returns the zero Origin, which resolve refuses unless the caller asked diff --git a/test/addon-command.sh b/test/addon-command.sh index da672df..93eb168 100755 --- a/test/addon-command.sh +++ b/test/addon-command.sh @@ -3021,6 +3021,82 @@ args="$(sed -n 's/^HOSTSHIFT_ARGS=//p' "$wtk/.ddev/.env")" n="$(printf '%s\n' "$args" | grep -o -- '--max-body' | wc -l | tr -d ' ')" check "a second init does not duplicate it" "1" "$n" +echo "== --holds: which of hostshift.yaml's origins the database holds" + +# One committed production-canonical file, three databases: a production dump +# holds the canonicals, `copy-db` from the search-replaced parent holds the +# bases, a staging pull holds the aliases. With the file mounted the proxy maps +# the canonicals only, so a copy-db worktree 301'd every page to the parent's +# hostname and every e2e test failed. +hm="$work/holdsm"; newproject "$hm" 'name: holdsm\nadditional_hostnames:\n - shop.holdsm' +hw="$work/holdsm-wt" +git -C "$hm" worktree add -q -b wt-h "$hw" +mkdir -p "$hw/.ddev"; printf 'name: holdsm-wt\n' > "$hw/.ddev/config.worktree.local.yaml" +printf '%s\n' 'version: 1' 'sites:' \ + ' - name: main' ' canonical: https://www.example.com' ' base: https://holdsm.ddev.site' \ + ' aliases: [https://holdsm.staging.example.net]' \ + ' - name: shop' ' canonical: https://shop.example.com' ' base: https://shop.holdsm.ddev.site' \ + ' aliases: [https://shop.staging.example.net]' > "$hw/hostshift.yaml" +held_base="HOSTSHIFT_ARGS=--from https://holdsm.ddev.site --to https://wt-h--holdsm.ddev.site --from https://shop.holdsm.ddev.site --to https://wt-h--shop.holdsm.ddev.site" +variants_h="HOSTSHIFT_VARIANTS=wt-h--holdsm.ddev.site,wt-h--shop.holdsm.ddev.site" + +out="$(cd "$hw" && "$cmd" env --slug wt-h 2>/dev/null || true)" +check "by default the mounted file maps its canonicals" \ + "HOSTSHIFT_ARGS=--slug wt-h" "$(printf '%s\n' "$out" | sed -n '/^HOSTSHIFT_ARGS=/p')" +case "$out" in *HOSTSHIFT_HOLDS*) fail "and records no side" "$out" ;; *) pass "and records no side" ;; esac + +# The proxy image predates the flag, so the side goes over as --from/--to. +out="$(cd "$hw" && "$cmd" env --slug wt-h --holds base 2>/dev/null || true)" +check "--holds base hands the proxy the bases, flat" \ + "$held_base" "$(printf '%s\n' "$out" | sed -n '/^HOSTSHIFT_ARGS=/p')" +check "and the variants do not move" "$variants_h" "$(printf '%s\n' "$out" | sed -n '/^HOSTSHIFT_VARIANTS=/p')" +contains "and the side is recorded" "HOSTSHIFT_HOLDS=base" "$out" + +out="$(cd "$hw" && "$cmd" env --slug wt-h --holds=alias 2>/dev/null || true)" +contains "--holds alias maps the staging hostnames" \ + "HOSTSHIFT_ARGS=--from https://holdsm.staging.example.net --to https://wt-h--holdsm.ddev.site --from https://shop.staging.example.net --to" "$out" + +# Recorded, so a bare init, `check` and the post-start hook agree with it rather +# than calling the deployment stale and "fixing" it back to the canonicals. +(cd "$hw" && "$cmd" init --slug wt-h --holds base >/dev/null 2>&1) || fail "init --holds base exited non-zero" "" +contains "init writes the side" "HOSTSHIFT_HOLDS=base" "$(cat "$hw/.ddev/.env")" +out="$(cd "$hw" && "$cmd" env --slug wt-h 2>/dev/null || true)" +check "a later run without the flag keeps it" \ + "$held_base" "$(printf '%s\n' "$out" | sed -n '/^HOSTSHIFT_ARGS=/p')" +contains "wp-cli's url is the held side too" "url: https://holdsm.ddev.site" \ + "$(cd "$hw" && "$cmd" wp-cli --slug wt-h 2>/dev/null || true)" + +env_args="$(sed -n 's/^HOSTSHIFT_ARGS=//p' "$hw/.ddev/.env")" +env_variants="$(sed -n 's/^HOSTSHIFT_VARIANTS=//p' "$hw/.ddev/.env")" +env_web="$(sed -n 's/^HOSTSHIFT_WEB_HOSTS=//p' "$hw/.ddev/.env")" +writefake +out="$(cd "$hw" && PATH="$fakecurl:$fakebin:$PATH" "$cmd" check --slug wt-h 2>&1)" \ + && pass "check agrees with a recorded side" \ + || fail "check agrees with a recorded side" "$out" +case "$out" in *"out of date"*) fail "and does not call it stale" "$out" ;; *) pass "and does not call it stale" ;; esac +# ...and a proxy still running the canonicals is a different map, not healthy. +printf 'true\nproxy --slug wt-h \nVIRTUAL_HOST=%s\n' "$env_variants" > "$HS_FAKE_DIR/hostshift-state" +out="$(cd "$hw" && PATH="$fakecurl:$fakebin:$PATH" "$cmd" check --slug wt-h 2>&1 || true)" +contains "a proxy on the other side is caught" "different map" "$out" +(cd "$hw" && "$cmd" init --slug wt-h --holds canonical >/dev/null 2>&1) || fail "init --holds canonical exited non-zero" "" +check "--holds canonical is the way back, and records nothing" \ + "" "$(sed -n '/^HOSTSHIFT_HOLDS=/p' "$hw/.ddev/.env")" +check "and the file is mounted again" "HOSTSHIFT_ARGS=--slug wt-h" "$(sed -n '/^HOSTSHIFT_ARGS=/p' "$hw/.ddev/.env")" + +out="$(cd "$hw" && "$cmd" env --slug wt-h --holds staging 2>&1 || true)" +contains "a side that is not one is refused" "say canonical, base or alias" "$out" +# Without the file the map already is the bases, so `base` is a no-op a caller +# can always pass; the others name origins only the file declares. +mv "$hw/hostshift.yaml" "$work/holds.hold" +out="$(cd "$hw" && "$cmd" env --slug wt-h --holds base 2>/dev/null || true)" +contains "without a file --holds base is the parent's hostnames" "$held_base" "$out" +if out="$(cd "$hw" && "$cmd" env --slug wt-h --holds alias 2>&1)"; then + fail "without a file --holds alias is refused" "exited 0: $out" +else + contains "without a file --holds alias is refused" "needs a hostshift.yaml" "$out" +fi +mv "$work/holds.hold" "$hw/hostshift.yaml" + echo "== the proxy dials its own web container, not whichever answers" # The service sits on ddev_default as well as its own network — it must, so the