Zoo

← back to the zoo

7. The branch decides

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.

Push main for live, staging for https://<app>-staging. Any other branch goes nowhere until you ask for it (Preview on apps, or ratcloud preview <app> <branch>): then https://<app>-preview runs it, and later pushes to it redeploy it. Each environment has its own data: the same code, never the same data.

This is the code serving the zoo right now: the bytes this very program was built from (each file is embedded when the zoo is compiled), at commit 55c71513c5 (live). The zoo's own self-check fetches this page and compares it byte for byte with the file it runs.

e07_branch_decides.odin sha256 040432fcd9aa · raw

1package main
2
3// Exhibit 7. main goes live, staging goes to a twin with its own data, and any other branch goes
4// nowhere until its creator asks for it: a Preview request (Preview on the branch's row at apps, or
5// `ratcloud preview <app> <branch>` over ssh) puts that branch on <app>-preview, which follows it from
6// then on. The facts pushes.json and preview.json say where each push went and what the preview
7// runs. The staging zoo has its own data, so its own zoo.db and guestbook; the check compares the two
8// over HTTPS. Exhibit 30 asks for a preview and shows a branch nobody asked for going nowhere.
9
10import "core:encoding/json"
11import "core:fmt"
12import "core:slice"
13import "core:strings"
14import "ratcloud:rc"
15
16E07_Data :: struct {
17	mine:      int `json:"mine"`,
18	theirs:    int `json:"theirs"`,
19	other_env: string `json:"other_env"`,
20}
21
22e07 :: proc() -> Exhibit {
23	return {
24		n = 7, wing = "Deploy",
25		title = "The branch decides",
26		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.",
27		block = "Push main for live, staging for https://<app>-staging. Any other branch goes nowhere until you ask for it (Preview on apps, or ratcloud preview <app> <branch>): then https://<app>-preview runs it, and later pushes to it redeploy it. Each environment has its own data: the same code, never the same data.",
28		src = {"e07_branch_decides.odin", #load("e07_branch_decides.odin", string)},
29		show = proc(me: ^rc.User) -> string {
30			// The latest push to each place (live, staging, the preview, nowhere), then what the preview runs.
31			b := strings.builder_make()
32			strings.write_string(&b, `<ul class="list">`)
33			pushes := jarr(fact("pushes.json"))
34			for place in ([]string{"live", "staging", "preview", "nowhere"}) {
35				for p in pushes {
36					if jstr(p, "env") != place do continue
37					rc.render_to(&b, `<li><span class="tag {{1}}">{{2}}</span><span><code>{{3}}</code> {{4}} · {{5}} · {{6}}</span></li>`, {{"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"))))}})
38					break
39				}
40			}
41			if pv := fact("preview.json"); pv != nil {
42				rc.render_to(&b, `<li><span class="tag preview">preview</span><span>runs <code>{{1}}</code> {{2}}; its data: {{3}}</span></li>`, {{"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"))}})
43			}
44			strings.write_string(&b, "</ul>")
45			if d, s, found := stored_data(7, E07_Data); found && s.data != "null" {
46				rc.render_to(&b, `<p>This guestbook has {{1}} signature{{2}}; the <a href="https://{{3}}/#e4">{{4}} zoo's</a> has {{5}}, and they are different books.</p>`, {{"1", d.mine}, {"2", "" if d.mine == 1 else "s"}, {"3", other_env_host()}, {"4", d.other_env}, {"5", d.theirs}})
47			}
48			return strings.to_string(b)
49		},
50		check = proc() -> Check {
51			pushes := jarr(fact("pushes.json"))
52			// main goes live and staging to staging (each also redeploys the preview while the preview
53			// follows it); any other branch goes to the preview only as the branch someone asked for,
54			// else nowhere.
55			lives := make([dynamic]string)
56			for p in pushes {
57				ref, to := jstr(p, "ref"), jstr(p, "env")
58				allowed: []string
59				switch ref {
60				case "refs/heads/main": allowed = {"live", "preview"}
61				case "refs/heads/staging": allowed = {"staging", "preview"}
62				case: allowed = {"preview", "nowhere"}
63				}
64				if !slice.contains(allowed, to) do return fail(fmt.tprintf("%s was sent to %s", ref, to))
65				if to == "live" do append(&lives, jstr(p, "commit"))
66			}
67			live := jstr(fact("deploy.json"), "live", "commit")
68			for p in pushes {
69				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")))
70			}
71			// Ours against the other environment's, each read through its own web address.
72			mine := get(new_browser(), self_url("/api/4/list"))
73			theirs := get(new_browser(), fmt.tprintf("https://%s/api/4/list", other_env_host()))
74			if theirs.status != 200 || !jhas(theirs.j, "instance") do return fail(fmt.tprintf("the other environment's zoo did not answer (%d)", theirs.status))
75			if jstr(theirs.j, "instance") == jstr(mine.j, "instance") do return fail("live and staging are the same database")
76			book :: proc(j: json.Value) -> string {
77				b := strings.builder_make()
78				for g in jarr(j, "guestbook") {
79					strings.write_string(&b, jstr(g, "user", "username")); strings.write_byte(&b, 0)
80					strings.write_string(&b, jstr(g, "note")); strings.write_byte(&b, 0)
81					strings.write_string(&b, jstr(g, "at")); strings.write_byte(&b, '\n')
82				}
83				return strings.to_string(b)
84			}
85			if book(mine.j) == book(theirs.j) do return fail("the live and staging guestbooks match")
86			nowhere, preview := 0, 0
87			for p in pushes {
88				switch jstr(p, "env") {
89				case "nowhere": nowhere += 1
90				case "preview": preview += 1
91				}
92			}
93			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")),
94				E07_Data{len(jarr(mine.j, "guestbook")), len(jarr(theirs.j, "guestbook")), jstr(theirs.j, "env")})
95		},
96	}
97}