package main // Exhibit 7. main goes live, staging goes to a twin with its own data, and any other branch goes // nowhere until its creator asks for it: a Preview request (Preview on the branch's row at apps, or // `ratcloud preview ` over ssh) puts that branch on -preview, which follows it from // then on. The facts pushes.json and preview.json say where each push went and what the preview // runs. The staging zoo has its own data, so its own zoo.db and guestbook; the check compares the two // over HTTPS. Exhibit 30 asks for a preview and shows a branch nobody asked for going nowhere. import "core:encoding/json" import "core:fmt" import "core:slice" import "core:strings" import "ratcloud:rc" E07_Data :: struct { mine: int `json:"mine"`, theirs: int `json:"theirs"`, other_env: string `json:"other_env"`, } e07 :: proc() -> Exhibit { return { n = 7, wing = "Deploy", title = "The branch decides", promise = "main goes live, staging goes to a twin with its own data, and any other branch goes nowhere until you ask: a Preview request puts it on the app's preview address, which then follows it.", block = "Push main for live, staging for https://-staging. Any other branch goes nowhere until you ask for it (Preview on apps, or ratcloud preview ): then https://-preview runs it, and later pushes to it redeploy it. Each environment has its own data: the same code, never the same data.", src = {"e07_branch_decides.odin", #load("e07_branch_decides.odin", string)}, show = proc(me: ^rc.User) -> string { // The latest push to each place (live, staging, the preview, nowhere), then what the preview runs. b := strings.builder_make() strings.write_string(&b, `
    `) pushes := jarr(fact("pushes.json")) for place in ([]string{"live", "staging", "preview", "nowhere"}) { for p in pushes { if jstr(p, "env") != place do continue rc.render_to(&b, `
  • {{2}}{{3}} {{4}} · {{5}} · {{6}}
  • `, {{"1", place}, {"2", place}, {"3", strings.trim_prefix(jstr(p, "ref"), "refs/heads/")}, {"4", short(jstr(p, "commit"), 7)}, {"5", jstr(p, "result")}, {"6", rc.raw(ago(parse_time(jstr(p, "at"))))}}) break } } if pv := fact("preview.json"); pv != nil { rc.render_to(&b, `
  • previewruns {{1}} {{2}}; its data: {{3}}
  • `, {{"1", jstr(pv, "branch") or_else "?"}, {"2", cat(short(jstr(pv, "commit"), 7), ", following it" if jbool(pv, "follows") else ", following no branch: nobody asked for one since")}, {"3", preview_data_words(jget(pv, "data"))}}) } strings.write_string(&b, "
") if d, s, found := stored_data(7, E07_Data); found && s.data != "null" { rc.render_to(&b, `

This guestbook has {{1}} signature{{2}}; the {{4}} zoo's has {{5}}, and they are different books.

`, {{"1", d.mine}, {"2", "" if d.mine == 1 else "s"}, {"3", other_env_host()}, {"4", d.other_env}, {"5", d.theirs}}) } return strings.to_string(b) }, check = proc() -> Check { pushes := jarr(fact("pushes.json")) // main goes live and staging to staging (each also redeploys the preview while the preview // follows it); any other branch goes to the preview only as the branch someone asked for, // else nowhere. lives := make([dynamic]string) for p in pushes { ref, to := jstr(p, "ref"), jstr(p, "env") allowed: []string switch ref { case "refs/heads/main": allowed = {"live", "preview"} case "refs/heads/staging": allowed = {"staging", "preview"} case: allowed = {"preview", "nowhere"} } if !slice.contains(allowed, to) do return fail(fmt.tprintf("%s was sent to %s", ref, to)) if to == "live" do append(&lives, jstr(p, "commit")) } live := jstr(fact("deploy.json"), "live", "commit") for p in pushes { if jstr(p, "env") == "nowhere" && jstr(p, "commit") == live && !slice.contains(lives[:], live) do return fail(fmt.tprintf("a work-in-progress commit (%s) is what live runs", jstr(p, "ref"))) } // Ours against the other environment's, each read through its own web address. mine := get(new_browser(), self_url("/api/4/list")) theirs := get(new_browser(), fmt.tprintf("https://%s/api/4/list", other_env_host())) if theirs.status != 200 || !jhas(theirs.j, "instance") do return fail(fmt.tprintf("the other environment's zoo did not answer (%d)", theirs.status)) if jstr(theirs.j, "instance") == jstr(mine.j, "instance") do return fail("live and staging are the same database") book :: proc(j: json.Value) -> string { b := strings.builder_make() for g in jarr(j, "guestbook") { strings.write_string(&b, jstr(g, "user", "username")); strings.write_byte(&b, 0) strings.write_string(&b, jstr(g, "note")); strings.write_byte(&b, 0) strings.write_string(&b, jstr(g, "at")); strings.write_byte(&b, '\n') } return strings.to_string(b) } if book(mine.j) == book(theirs.j) do return fail("the live and staging guestbooks match") nowhere, preview := 0, 0 for p in pushes { switch jstr(p, "env") { case "nowhere": nowhere += 1 case "preview": preview += 1 } } return pass(fmt.tprintf("%d pushes each went where its branch says (%d nowhere, %d to the preview it was asked for); %s and %s keep different guestbooks", len(pushes), nowhere, preview, env().env, jstr(theirs.j, "env")), E07_Data{len(jarr(mine.j, "guestbook")), len(jarr(theirs.j, "guestbook")), jstr(theirs.j, "env")}) }, } }