30. Preview a branch
Ask for a branch's preview and it runs there, with the data the preview already holds; a branch nobody asked for goes nowhere.
Preview on a branch's row at apps, or ssh <you>@<domain> ratcloud preview <app> <branch>: root checks that branch's ratcloud.conf, puts it on https://<app>-preview, lints it, runs its smoke test, and follows it. The preview's data stays across branches; Load copies live's or staging's in (ratcloud load), Empty empties it.
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.
e30_preview.odin sha256 1a1d58cba11a · raw
1package main 2 3// Exhibit 30. Any branch can be previewed on request (tools PLATFORM.md, "Preview"): its creator asks 4// (Preview on the branch's row at apps, or `ratcloud preview <app> <branch>` over ssh), root reads that 5// branch's ratcloud.conf, checks it out on https://<app>-preview, runs the lint and the smoke test, and 6// the preview follows the branch from then on. Its data stays when it switches branches (Load copies 7// live's or staging's in, Empty empties it). A branch nobody asked for goes nowhere. 8// 9// The round, as the robot creator over ssh (creator.odin), on its own sample app zoo-broken: push three 10// branches (a, b, c), each adding public/e30.php, which names its branch and answers its own fresh 11// word; load a copy of this environment's data into the preview; ask for a, and from a write a note 12// into the preview's data (a POST to e30.php writes a's word); ask for b. Then b answers its word and 13// still finds a's note and the loaded copy, c's word answers nowhere (live, staging, preview), and 14// `ratcloud status` says the preview follows b and holds the copy. 15// 16// zoo-broken rather than zoo-scratch: zoo-broken is the robot's own app. Root keeps one preview per 17// app, which the live zoo and the staging zoo share here, so each starts rounds only in its own half 18// hour of the clock. 19 20import "core:encoding/json" 21import "core:fmt" 22import "core:strings" 23import "core:time" 24import "ratcloud:rc" 25 26PREVIEW_APP :: "zoo-broken" 27// Every 6 hours (an hour when red): each round puts three pushes into the sample's push log, which 28// keeps 200 and also holds exhibit 8's refused pushes, and redeploys its preview twice. Four rounds a 29// day in each environment keep a week of that log; a broken preview still shows the same day. 30E30_EVERY :: 6 * 3600 31E30_RETRY :: 3600 32// A round older than this means the rounds stopped (the job never reached its half hour). 33E30_STALE :: E30_EVERY + 2 * 3600 34 35// This zoo's half hour: the live zoo starts rounds in minutes 0-24, the staging zoo in 30-54. A round 36// takes about a minute, so the two never share the preview at once. 37e30_slot :: proc() -> bool { 38 _, m, _ := time.clock(time.now()) 39 return m < 25 if env().env == "live" else m >= 30 && m < 55 40} 41e30_branch :: proc(x: string) -> string {return fmt.tprintf("e30-%s-%s", env().env, x)} 42 43// public/e30.php on each branch (zoo-broken is a PHP app): which branch this is, its word, and what 44// the preview's data holds. A POST writes this branch's own word into the data as a note (nothing from 45// the request is kept). 46E30_PAGE :: `<?php 47// The zoo's exhibit 30 (pushed by its robot): which branch this preview runs, and what its data 48// holds. A POST writes this branch's word into the data, so the next branch asked for can find it. 49const BRANCH = "{{branch}}"; 50const WORD = "{{word}}"; 51header("Content-Type: application/json"); 52header("Cache-Control: no-store"); 53$d = getenv("RC_DATA"); 54if (($_SERVER["REQUEST_METHOD"] ?? "GET") === "POST") file_put_contents("$d/e30-note.txt", BRANCH . " " . WORD); 55$note = @file_get_contents("$d/e30-note.txt"); 56echo json_encode(["branch" => BRANCH, "word" => WORD, "env" => getenv("RC_ENV"), "note" => $note === false ? null : $note, 57 "files" => array_values(array_diff(scandir($d) ?: [], [".", ".."]))]), "\n"; 58` 59 60e30_page :: proc(branch, word: string) -> string { 61 b := strings.builder_make() 62 rc.render_to(&b, E30_PAGE, {{"branch", branch}, {"word", word}}) 63 return strings.to_string(b) 64} 65 66// e30.php at one of the app's three addresses. 67e30_url :: proc(env_: string) -> string { 68 h_ := domain_host(rc.app_url(PREVIEW_APP)) 69 dot := strings.index_byte(h_, '.') 70 label := strings.trim_suffix(h_[:dot], "-staging") 71 return fmt.tprintf("https://%s%s/e30.php", label if env_ == "live" else cat(label, "-", env_), h_[dot:]) 72} 73e30_get :: proc(env_: string, post_ := false) -> (int, json.Value, string) { 74 url := e30_url(env_) 75 r := go(new_browser(), "POST" if post_ else "GET", url) 76 return r.status, r.j, url 77} 78 79Nowhere :: struct { 80 url: string `json:"url"`, 81 status: int `json:"status"`, 82 word: json.Value `json:"word"`, 83} 84E30_Data :: struct { 85 at: string `json:"at"`, 86 app: string `json:"app"`, 87 words: map[string]string `json:"words"`, 88 branches: map[string]string `json:"branches"`, 89 push: map[string]string `json:"push"`, 90 load: string `json:"load"`, 91 ask: map[string]string `json:"ask"`, 92 seen: map[string]json.Value `json:"seen"`, 93 nowhere: map[string]Nowhere `json:"nowhere"`, 94 status: []string `json:"status"`, 95 seconds: i64 `json:"seconds"`, 96} 97 98e30_round :: proc() -> Check { 99 e := env().env 100 app := PREVIEW_APP 101 t0 := now() 102 d := E30_Data{at = iso(t0), app = app, words = make(map[string]string), branches = make(map[string]string), push = make(map[string]string), ask = make(map[string]string), seen = make(map[string]json.Value), nowhere = make(map[string]Nowhere)} 103 for x in ([]string{"a", "b", "c"}) {d.words[x] = fresh_word(); d.branches[x] = e30_branch(x)} 104 dir := "e30" 105 creator_git({op = "rm", dir = dir}) 106 defer creator_git({op = "rm", dir = dir}) 107 // 1. Three branches on top of main, each its own word; c is never asked for. 108 if r := creator_git({op = "clone", repo = app, branch = "main", dir = dir}); r.code != 0 do return fail(fmt.tprintf("could not clone %s's main: %s", app, r.out), d) 109 for x in ([]string{"a", "b", "c"}) { 110 br := d.branches[x] 111 creator_git({op = "checkout", dir = dir, branch = br, from = "main"}) 112 creator_git({op = "write", dir = dir, path = "public/e30.php", data = e30_page(br, d.words[x])}) 113 r := creator_git({op = "add", dir = dir, paths = {"public/e30.php"}}) 114 if r.code == 0 do r = creator_git({op = "commit", dir = dir, message = fmt.tprintf("e30.php answers %s (the zoo's exhibit 30)", d.words[x])}) 115 if r.code != 0 do return fail(fmt.tprintf("could not commit %s: %s", br, r.out), d) 116 p := creator_git({op = "push", dir = dir, branch = br, force = true}) 117 d.push[x] = clip(unremote(p.out), 400) 118 if p.code != 0 do return fail(cat("the push of ", br, " was refused: ", clip(d.push[x], 200)), d) 119 } 120 if !strings.contains(d.push["c"], "goes nowhere") do return fail(fmt.tprintf("the push of %s, which nobody asked for, did not go nowhere: %s", d.branches["c"], d.push["c"]), d) 121 122 // 2. A copy of this environment's data into the preview. 123 l := creator_ratcloud("load", app, e) 124 d.load = clip(l.out, 300) 125 if l.code != 0 do return fail(fmt.tprintf("ratcloud load %s %s was refused: %s", app, e, l.out), d) 126 127 // 3. Ask for a; from a, write a note into the preview's data. 128 a := creator_ratcloud("preview", app, d.branches["a"]) 129 d.ask["a"] = clip(a.out, 600) 130 if a.code != 0 || !strings.contains(a.out, "lint ok") do return fail(fmt.tprintf("ratcloud preview %s %s: %s", app, d.branches["a"], a.out), d) 131 code, j, _ := e30_get("preview", true) 132 d.seen["a"] = j 133 if code != 200 || jstr(j, "word") != d.words["a"] do return fail(fmt.tprintf("after asking for %s, the preview answered %d %s, not its word %s", d.branches["a"], code, to_json(j), d.words["a"]), d) 134 if jstr(j, "note") != cat(d.branches["a"], " ", d.words["a"]) do return fail(cat("the preview could not keep a note in its data: ", to_json(j)), d) 135 136 // 4. Ask for b: its word, with a's note and the loaded copy still there. 137 bb := creator_ratcloud("preview", app, d.branches["b"]) 138 d.ask["b"] = clip(bb.out, 600) 139 if bb.code != 0 || !strings.contains(bb.out, "lint ok") do return fail(fmt.tprintf("ratcloud preview %s %s: %s", app, d.branches["b"], bb.out), d) 140 code, j, _ = e30_get("preview") 141 d.seen["b"] = j 142 if code != 200 || jstr(j, "word") != d.words["b"] do return fail(fmt.tprintf("after asking for %s, the preview answered %d %s, not its word %s", d.branches["b"], code, to_json(j), d.words["b"]), d) 143 if jstr(j, "note") != cat(d.branches["a"], " ", d.words["a"]) do return fail(fmt.tprintf("the preview's data did not survive the switch to %s: a's note is %s", d.branches["b"], to_json(jget(j, "note"))), d) 144 145 // 5. c answers nowhere: not live, not staging, not the preview. 146 for x in ([]string{"live", "staging", "preview"}) { 147 c, cj, url := e30_get(x) 148 d.nowhere[x] = {url, c, jget(cj, "word")} 149 if jstr(cj, "word") == d.words["c"] do return fail(fmt.tprintf("%s answers %s's word, a branch nobody asked for", url, d.branches["c"]), d) 150 if x != "preview" && c == 200 && jhas(cj, "word") do return fail(fmt.tprintf("%s answers an exhibit 30 branch's word: a branch reached %s", url, x), d) 151 } 152 153 // 6. What root says: the preview follows b and holds the copy. 154 st := creator_ratcloud("status", app) 155 lines := make([dynamic]string) 156 for line in strings.split_lines(st.out) do if strings.has_prefix(line, "preview") || strings.has_prefix(strings.trim_left_space(line), "data:") do append(&lines, line) 157 d.status = lines[:] 158 if st.code != 0 || !strings.contains(st.out, cat("follows ", d.branches["b"])) do return fail(fmt.tprintf("ratcloud status does not say the preview follows %s: %s", d.branches["b"], clip(st.out, 300)), d) 159 if !strings.contains(st.out, fmt.tprintf("data: a copy of %s's", e)) do return fail(cat("ratcloud status does not say the preview holds the copy of ", e, "'s data: ", strings.join(lines[:], " / ")), d) 160 d.seconds = now() - t0 161 return pass(fmt.tprintf("asked for %s then %s of %s over ssh: the preview answered each one's word, b still found a's note and the copy of %s's data, and %s, which nobody asked for, answered nowhere (%d s)", d.branches["a"], d.branches["b"], app, e, d.branches["c"], d.seconds), d) 162} 163 164e30 :: proc() -> Exhibit { 165 return { 166 n = 30, wing = "Deploy", 167 title = "Preview a branch", 168 promise = "Ask for a branch's preview and it runs there, with the data the preview already holds; a branch nobody asked for goes nowhere.", 169 block = "Preview on a branch's row at apps, or ssh <you>@<domain> ratcloud preview <app> <branch>: root checks that branch's ratcloud.conf, puts it on https://<app>-preview, lints it, runs its smoke test, and follows it. The preview's data stays across branches; Load copies live's or staging's in (ratcloud load), Empty empties it.", 170 src = {"e30_preview.odin", #load("e30_preview.odin", string)}, 171 files = srcs({"creator.odin", #load("creator.odin", string)}), 172 show = proc(me: ^rc.User) -> string { 173 d, s, found := stored_data(30, E30_Data) 174 ask := fmt.tprintf(`<p class="muted">Ask for one of yours: Preview on the branch's row on your app's page at <a href="%s/">apps</a>, or <code>ssh <you>@%s ratcloud preview <app> <branch></code>.</p>`, h(rc.app_url("apps")), h(env().domain)) 175 app := h(rc.app_url(PREVIEW_APP)) 176 if !found || len(d.words) == 0 do return cat(`<p>Every six hours the robot, as the creator of the sample app <a href="`, app, `/">`, PREVIEW_APP, `</a>, pushes three branches and asks for two of them. It has not run here yet.</p>`, ask) 177 b := strings.builder_make() 178 li :: proc(b: ^strings.Builder, tag: string, ok: bool, html: string) { 179 fmt.sbprintf(b, `<li><span class="tag %s">%s</span><span>%s</span></li>`, "ok" if ok else "bad", h(tag), html) 180 } 181 fmt.sbprintf(&b, `<p>The robot, as the creator of the sample app <a href="%s/">%s</a>, %s: pushed three branches, loaded a copy of %s's data into the preview, then asked for two of them over ssh.</p><ul class="list">`, app, PREVIEW_APP, ago(parse_time(d.at)), h(env().env)) 182 sb := d.seen["b"] 183 files := make([dynamic]string) 184 for f in jarr(sb, "files") do append(&files, jstr(f)) 185 li(&b, "asked", jhas(d.seen["a"], "word"), fmt.tprintf(`<code>%s</code>: the <a href="%s">preview</a> answered <code>%s</code> and wrote it into its data`, h(d.branches["a"]), h(e30_url("preview")), h(d.words["a"]))) 186 li(&b, "asked", jstr(sb, "word") == d.words["b"], fmt.tprintf("<code>%s</code> next: answered <code>%s</code>%s", h(d.branches["b"]), h(d.words["b"]), cat(", and still found ", h(jstr(sb, "note") or_else "no note"), " among ", h(strings.join(files[:], ", "))) if jhas(sb) else "")) 187 li(&b, "nowhere", s.ok && len(d.nowhere) > 0, fmt.tprintf("<code>%s</code>, never asked for: its push went nowhere, and no address answers <code>%s</code>", h(d.branches["c"]), h(d.words["c"]))) 188 for l in d.status do li(&b, "status", true, cat("<code>", h(strings.trim_space(l)), "</code>")) 189 strings.write_string(&b, "</ul>") 190 if !s.ok do fmt.sbprintf(&b, `<p class="muted">%s</p>`, h(s.detail)) 191 return cat(strings.to_string(b), ask) 192 }, 193 check = proc() -> Check { 194 d, s, found := stored_data(30, E30_Data) 195 last := parse_time(d.at) 196 due := last == 0 || now() - last >= (E30_EVERY if s.ok else E30_RETRY) 197 // Rounds run in the five-minute job, in this zoo's half hour; a "check now" reads the last one. 198 if due && g_in_job && e30_slot() do return e30_round() 199 if last == 0 do return fail(fmt.tprintf("no round yet: the five-minute job starts one in this zoo's half hour (%s of each hour)", "minutes 0-24" if env().env == "live" else "minutes 30-54")) 200 if now() - last > E30_STALE do return fail(fmt.tprintf("the last round is %.1f hours old: the rounds have stopped", f64(now() - last) / 3600), d) 201 _ = found 202 return {s.ok, s.detail, s.data} 203 }, 204 } 205}
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}