Zoo

← back to the zoo

8. A bad manifest is shown, not applied

A mistake in an app's settings file (ratcloud.conf) leaves the old settings running and says what was wrong.

ratcloud.conf is checked before anything is applied. A bad one refuses the push, in the pusher's terminal, in the fact manifest.json and on the app's page at apps; the site keeps running. A wasm app that does not compile is refused the same way.

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.

e08_bad_manifest.odin sha256 264ee7303e84 · raw

1package main
2
3// Exhibit 8. A push whose ratcloud.conf is wrong is refused whole: the old code and settings keep
4// running and the error lands in the app's facts (manifest.json). The sample app `zoo-broken` has a
5// deliberate typo pushed to it. The zoo reads only its own facts, so zoo-broken publishes its own
6// manifest fact at /manifest.json, from its last good version, and the zoo fetches that over HTTPS.
7// apps shows the same error on the app's page, to its creator: the check reads it there as the robot,
8// zoo-broken's creator. Any valid push to zoo-broken clears the error, so the zoo's nightly job puts
9// the typo back when it is gone (broken.odin). On wasm, a build that does not compile is refused the
10// same way: the sample zoo-wasm keeps a preview of its branch broken-build refused, with the compiler's
11// lines, and publishes its manifest fact as zoo-broken does.
12
13import "core:encoding/json"
14import "core:fmt"
15import "core:strings"
16import "ratcloud:rc"
17
18E08_Data :: struct {
19	ok:     json.Value `json:"ok"`,
20	error:  string `json:"error"`,
21	at:     string `json:"at"`,
22	commit: string `json:"commit"`,
23	apps:   string `json:"apps,omitempty"`,
24	wasm:   json.Value `json:"wasm,omitempty"`,
25}
26
27e08 :: proc() -> Exhibit {
28	return {
29		n = 8, wing = "Deploy",
30		title = "A bad manifest is shown, not applied",
31		promise = "A mistake in an app's settings file (ratcloud.conf) leaves the old settings running and says what was wrong.",
32		block = "ratcloud.conf is checked before anything is applied. A bad one refuses the push, in the pusher's terminal, in the fact manifest.json and on the app's page at apps; the site keeps running. A wasm app that does not compile is refused the same way.",
33		src = {"e08_bad_manifest.odin", #load("e08_bad_manifest.odin", string)},
34		files = srcs({"broken.odin", #load("broken.odin", string)}, {"creator.odin", #load("creator.odin", string)}),
35		show = proc(me: ^rc.User) -> string {
36			url := rc.app_url(BROKEN_APP)
37			b := strings.builder_make()
38			night := ""
39			if v, ok := meta("broken.nightly"); ok {
40				n: Broken
41				json.unmarshal_string(v, &n)
42				night = fmt.tprintf(`<p class="muted small">The nightly job, %s: %s%s.</p>`, ago(n.at), h(n.result), " (a valid push had cleared the error)" if n.result == "put back" else "")
43			}
44			d, s, found := stored_data(8, E08_Data)
45			if !found || s.data == "null" {
46				rc.render_to(&b, `<p>The sample app <a href="{{1}}/">zoo-broken</a> has not been checked yet.</p>{{2}}`, {{"1", url}, {"2", rc.raw(night)}})
47				return strings.to_string(b)
48			}
49			rc.render_to(&b, `<p>The sample app <a href="{{1}}/">{{2}}</a> is up, and its last push was refused:</p><pre class="log">{{3}}</pre>`, {{"1", url}, {"2", domain_host(url)}, {"3", d.error}})
50			rc.render_to(&b, `<p class="muted">Refused {{1}}, commit {{2}}. It still serves its last good version.`, {{"1", rc.raw(ago(parse_time(d.at)))}, {"2", short(d.commit, 7)}})
51			if d.apps != "" do rc.render_to(&b, ` Its creator sees the same on <a href="{{1}}/{{2}}">its page at apps</a>: {{3}}.`, {{"1", rc.app_url("apps")}, {"2", BROKEN_APP}, {"3", d.apps}})
52			strings.write_string(&b, "</p>")
53			if ok, isb := jget(d.wasm, "ok").(json.Boolean); isb && !ok {
54				rc.render_to(&b, `<p>On wasm, a build that does not compile is refused the same way: the preview of <a href="{{1}}/8">zoo-wasm</a>'s branch <code>broken-build</code> keeps what it had, and says why:</p><pre class="log">{{2}}</pre>`, {{"1", rc.app_url("zoo-wasm")}, {"2", jstr(d.wasm, "error")}})
55			}
56			strings.write_string(&b, night)
57			return strings.to_string(b)
58		},
59		check = proc() -> Check {
60			url := rc.app_url(BROKEN_APP)
61			if r := get(new_browser(), cat(url, "/")); r.status != 200 do return fail(fmt.tprintf("the sample app is down (%d): a bad manifest must not take it down", r.status))
62			mr := get(new_browser(), cat(url, "/manifest.json"))
63			m: E08_Data
64			if mr.status != 200 || json.unmarshal_string(mr.body, &m) != nil do return fail(fmt.tprintf("the sample app's manifest fact did not load (%d)", mr.status))
65			if ok, isb := m.ok.(json.Boolean); !isb || ok do return fail("the sample app's typo was accepted, or is gone: no error to show (a valid push to zoo-broken clears it; the zoo's nightly job puts it back)")
66			if strings.trim_space(m.error) == "" do return fail("the push was refused but no error was recorded")
67			// Where its creator looks: the app's page at apps, read as the robot (zoo-broken's creator).
68			b, err := robot_at("apps")
69			if err != "" do return fail(err, m)
70			a := get(b, fmt.tprintf("%s/api/app/%s", rc.app_url("apps"), BROKEN_APP))
71			e := env().env
72			if a.status != 200 || a.j == nil do return fail(fmt.tprintf("apps does not show zoo-broken's page to its creator (%d)", a.status), m)
73			if ok, isb := jget(a.j, "manifest", e, "ok").(json.Boolean); !isb || ok || jstr(a.j, "manifest", e, "error") != m.error {
74				return fail(fmt.tprintf("apps's page for zoo-broken does not show the refused manifest on %s: %s", e, to_json(jget(a.j, "manifest", e))), m)
75			}
76			att := make([dynamic]string)
77			for x in jarr(a.j, "attention") do if s, ok := x.(json.String); ok do append(&att, string(s))
78			m.apps = strings.join(att[:], ", ") if len(att) > 0 else "the refused manifest"
79			// The wasm line: zoo-wasm's preview of broken-build, refused with the compiler's lines.
80			if wr := get(new_browser(), cat(rc.app_url("zoo-wasm"), "/manifest.json")); wr.status == 200 do m.wasm = jget(wr.j, "build", "preview")
81			return pass(fmt.tprintf("zoo-broken is up on its last good version; its typo was refused with: %s (apps shows it on the app's page: %s)", m.error, m.apps), m)
82		},
83	}
84}

broken.odin sha256 0c3b4c24da0a · raw

1package main
2
3// Exhibit 8's upkeep. zoo-broken shows a refused manifest only until someone pushes it a valid one
4// (any change to its page does), so every night the job `nightly` reads zoo-broken's manifest fact for
5// this zoo's environment (the live zoo looks after `main`, the staging zoo after `staging`) and, if a
6// valid manifest has replaced the typo, puts it back: one commit on top of that branch's head that
7// adds `chanels = updates`, pushed as zoo-broken's creator. Root refuses the push, as it must, the
8// branch stays where it was, and the error is back in zoo-broken's facts.
9//
10// The key: zoo-broken's creator is the zoo's robot, `zoo-robot`, a robot creator whose only app it is,
11// so its key can push nothing but zoo-broken (creator.odin). Without the capability this says so and
12// pushes nothing.
13
14import "core:encoding/json"
15import "core:fmt"
16import "core:strings"
17import "ratcloud:rc"
18
19BROKEN_APP :: "zoo-broken"
20BROKEN_TYPO :: "chanels = updates" // `channels`, misspelt: root refuses a key it does not know
21
22Broken :: struct {
23	result: string `json:"result"`, // still refused, put back, no key, failed, ACCEPTED
24	branch: string `json:"branch"`,
25	detail: string `json:"detail"`,
26	commit: string `json:"commit,omitempty"`,
27	output: string `json:"output,omitempty"`,
28	at:     i64 `json:"at"`,
29}
30
31// zoo-broken's manifest fact for this environment, as zoo-broken itself publishes it over HTTPS.
32broken_fact :: proc() -> json.Value {
33	r := get(new_browser(), cat(rc.app_url(BROKEN_APP), "/manifest.json"))
34	if r.status != 200 do return nil
35	_, isobj := r.j.(json.Object)
36	return r.j if isobj else nil
37}
38
39// Put the typo back if it is gone.
40broken_reassert :: proc() -> Broken {
41	branch := "main" if env().env == "live" else "staging"
42	m := broken_fact()
43	if m == nil do return {result = "failed", branch = branch, detail = "zoo-broken's /manifest.json did not load"}
44	if ok, isb := jget(m, "ok").(json.Boolean); isb && !ok {
45		return {result = "still refused", branch = branch, detail = fmt.tprintf("its typo is still refused (commit %s, %s); nothing to do", short(jstr(m, "commit"), 7), jstr(m, "at"))}
46	}
47	dir := "broken"
48	creator_git({op = "rm", dir = dir}) // a clone left behind by a run that was killed goes first
49	defer creator_git({op = "rm", dir = dir})
50	r := creator_git({op = "clone", repo = BROKEN_APP, branch = branch, dir = dir})
51	if r.code == -1 && strings.has_prefix(r.out, "refused") do return {result = "no key", branch = branch, detail = cat("a valid manifest replaced the typo, and this zoo may not run git as ", CREATOR, " (", r.out, ")")}
52	if r.code != 0 do return {result = "failed", branch = branch, detail = fmt.tprintf("could not clone zoo-broken's %s: %s", branch, r.out)}
53	conf := creator_git({op = "read", dir = dir, path = "ratcloud.conf"})
54	if conf.code != 0 do return {result = "failed", branch = branch, detail = cat("could not read its ratcloud.conf: ", conf.out)}
55	for line in strings.split_lines(conf.out) {
56		if strings.has_prefix(strings.trim_space(line), "chanels") do return {result = "ACCEPTED", branch = branch, detail = fmt.tprintf("zoo-broken's %s already holds the typo, yet its manifest fact says ok: root took a manifest it must refuse", branch)}
57	}
58	if w := creator_git({op = "write", dir = dir, path = "ratcloud.conf", data = cat(strings.trim_right(conf.out, "\n"), "\n", BROKEN_TYPO, "\n")}); w.code != 0 do return {result = "failed", branch = branch, detail = cat("could not write the typo: ", w.out)}
59	if c := creator_git({op = "commit", dir = dir, all = true, message = "zoo-broken: the deliberate typo again (exhibit 8), put back by the zoo's nightly job"}); c.code != 0 do return {result = "failed", branch = branch, detail = cat("could not commit the typo: ", c.out)}
60	commit := creator_git({op = "rev-parse", dir = dir, rev = "HEAD"}).out
61	p := creator_git({op = "push", dir = dir, branch = branch})
62	out := unremote(p.out)
63	if p.code == 0 do return {result = "ACCEPTED", branch = branch, commit = commit, output = out, detail = "root ACCEPTED a push whose ratcloud.conf has an unknown key"}
64	m = broken_fact()
65	if ok, isb := jget(m, "ok").(json.Boolean); !isb || ok || jstr(m, "commit") != commit {
66		return {result = "failed", branch = branch, commit = commit, output = out, detail = cat("the push was refused, but not for its manifest: ", clip(out, 200))}
67	}
68	return {result = "put back", branch = branch, commit = commit, output = out, detail = fmt.tprintf("a valid manifest had replaced the typo; put it back on top of %s as %s and root refused it: %s", branch, short(commit, 7), jstr(m, "error"))}
69}

creator.odin sha256 1924fc3d7c2d · raw

1package main
2
3// The zoo's robot as a creator: git and `ratcloud` over ssh, as `zoo-robot`, the robot creator whose
4// apps are the zoo's samples it looks after (zoo-broken). A wasm app runs no process of its own:
5// git and ssh are the platform's named capabilities (`capabilities = git, ssh` in ratcloud.conf,
6// granted by root with `rc cap zoo git ssh --as zoo-robot`). Each call is one fixed shape (rc.git,
7// rc.ssh: clone, checkout, write, read, add, commit, push, rev-parse, rm; keygen, ratcloud), run as
8// the zoo's own unix user in its own workspace, with the robot creator's key from the zoo's secrets,
9// which never enters this program. It logs in exactly as a creator's laptop does: git, `ratcloud`,
10// nothing that is a shell.
11//
12// Exhibit 8's nightly (broken.odin), 18 (a machine key of its own for a minute) and 30 (asking for a
13// preview) use it.
14
15import "core:strings"
16import "ratcloud:rc"
17
18CREATOR :: "zoo-robot"
19
20Ran_Tool :: struct {
21	code: int, // the tool's exit code; -1: it did not run (out says why)
22	out:  string,
23}
24
25// git as the robot creator: its exit code and output (git's messages included).
26creator_git :: proc(t: rc.Tool) -> Ran_Tool {
27	r := rc.git(t)
28	if r.code < 0 do return {-1, r.err}
29	return {r.code, strings.trim_space(string(r.out))}
30}
31
32// `ratcloud <verb> <app> ...` over ssh, the verbs a creator's laptop has (tools PLATFORM.md).
33creator_ratcloud :: proc(words: ..string) -> Ran_Tool {
34	ws := make([]string, len(words))
35	copy(ws, words)
36	r := rc.ssh({op = "ratcloud", words = ws})
37	if r.code < 0 do return {-1, r.err}
38	return {r.code, strings.trim_space(string(r.out))}
39}
40
41// git's "remote: " prefixes off, as a person reads a push's answer.
42unremote :: proc(s: string) -> string {
43	b := strings.builder_make()
44	for line, i in strings.split_lines(s) {
45		if i > 0 do strings.write_byte(&b, '\n')
46		strings.write_string(&b, strings.trim_left_space(strings.trim_prefix(line, "remote:")) if strings.has_prefix(line, "remote:") else line)
47	}
48	return strings.trim_space(strings.to_string(b))
49}