package main // Exhibit 30. Any branch can be previewed on request (tools PLATFORM.md, "Preview"): its creator asks // (Preview on the branch's row at apps, or `ratcloud preview ` over ssh), root reads that // branch's ratcloud.conf, checks it out on https://-preview, runs the lint and the smoke test, and // the preview follows the branch from then on. Its data stays when it switches branches (Load copies // live's or staging's in, Empty empties it). A branch nobody asked for goes nowhere. // // The round, as the robot creator over ssh (creator.odin), on its own sample app zoo-broken: push three // branches (a, b, c), each adding public/e30.php, which names its branch and answers its own fresh // word; load a copy of this environment's data into the preview; ask for a, and from a write a note // into the preview's data (a POST to e30.php writes a's word); ask for b. Then b answers its word and // still finds a's note and the loaded copy, c's word answers nowhere (live, staging, preview), and // `ratcloud status` says the preview follows b and holds the copy. // // zoo-broken rather than zoo-scratch: zoo-broken is the robot's own app. Root keeps one preview per // app, which the live zoo and the staging zoo share here, so each starts rounds only in its own half // hour of the clock. import "core:encoding/json" import "core:fmt" import "core:strings" import "core:time" import "ratcloud:rc" PREVIEW_APP :: "zoo-broken" // Every 6 hours (an hour when red): each round puts three pushes into the sample's push log, which // keeps 200 and also holds exhibit 8's refused pushes, and redeploys its preview twice. Four rounds a // day in each environment keep a week of that log; a broken preview still shows the same day. E30_EVERY :: 6 * 3600 E30_RETRY :: 3600 // A round older than this means the rounds stopped (the job never reached its half hour). E30_STALE :: E30_EVERY + 2 * 3600 // This zoo's half hour: the live zoo starts rounds in minutes 0-24, the staging zoo in 30-54. A round // takes about a minute, so the two never share the preview at once. e30_slot :: proc() -> bool { _, m, _ := time.clock(time.now()) return m < 25 if env().env == "live" else m >= 30 && m < 55 } e30_branch :: proc(x: string) -> string {return fmt.tprintf("e30-%s-%s", env().env, x)} // public/e30.php on each branch (zoo-broken is a PHP app): which branch this is, its word, and what // the preview's data holds. A POST writes this branch's own word into the data as a note (nothing from // the request is kept). E30_PAGE :: ` BRANCH, "word" => WORD, "env" => getenv("RC_ENV"), "note" => $note === false ? null : $note, "files" => array_values(array_diff(scandir($d) ?: [], [".", ".."]))]), "\n"; ` e30_page :: proc(branch, word: string) -> string { b := strings.builder_make() rc.render_to(&b, E30_PAGE, {{"branch", branch}, {"word", word}}) return strings.to_string(b) } // e30.php at one of the app's three addresses. e30_url :: proc(env_: string) -> string { h_ := domain_host(rc.app_url(PREVIEW_APP)) dot := strings.index_byte(h_, '.') label := strings.trim_suffix(h_[:dot], "-staging") return fmt.tprintf("https://%s%s/e30.php", label if env_ == "live" else cat(label, "-", env_), h_[dot:]) } e30_get :: proc(env_: string, post_ := false) -> (int, json.Value, string) { url := e30_url(env_) r := go(new_browser(), "POST" if post_ else "GET", url) return r.status, r.j, url } Nowhere :: struct { url: string `json:"url"`, status: int `json:"status"`, word: json.Value `json:"word"`, } E30_Data :: struct { at: string `json:"at"`, app: string `json:"app"`, words: map[string]string `json:"words"`, branches: map[string]string `json:"branches"`, push: map[string]string `json:"push"`, load: string `json:"load"`, ask: map[string]string `json:"ask"`, seen: map[string]json.Value `json:"seen"`, nowhere: map[string]Nowhere `json:"nowhere"`, status: []string `json:"status"`, seconds: i64 `json:"seconds"`, } e30_round :: proc() -> Check { e := env().env app := PREVIEW_APP t0 := now() 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)} for x in ([]string{"a", "b", "c"}) {d.words[x] = fresh_word(); d.branches[x] = e30_branch(x)} dir := "e30" creator_git({op = "rm", dir = dir}) defer creator_git({op = "rm", dir = dir}) // 1. Three branches on top of main, each its own word; c is never asked for. 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) for x in ([]string{"a", "b", "c"}) { br := d.branches[x] creator_git({op = "checkout", dir = dir, branch = br, from = "main"}) creator_git({op = "write", dir = dir, path = "public/e30.php", data = e30_page(br, d.words[x])}) r := creator_git({op = "add", dir = dir, paths = {"public/e30.php"}}) 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])}) if r.code != 0 do return fail(fmt.tprintf("could not commit %s: %s", br, r.out), d) p := creator_git({op = "push", dir = dir, branch = br, force = true}) d.push[x] = clip(unremote(p.out), 400) if p.code != 0 do return fail(cat("the push of ", br, " was refused: ", clip(d.push[x], 200)), d) } 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) // 2. A copy of this environment's data into the preview. l := creator_ratcloud("load", app, e) d.load = clip(l.out, 300) if l.code != 0 do return fail(fmt.tprintf("ratcloud load %s %s was refused: %s", app, e, l.out), d) // 3. Ask for a; from a, write a note into the preview's data. a := creator_ratcloud("preview", app, d.branches["a"]) d.ask["a"] = clip(a.out, 600) 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) code, j, _ := e30_get("preview", true) d.seen["a"] = j 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) 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) // 4. Ask for b: its word, with a's note and the loaded copy still there. bb := creator_ratcloud("preview", app, d.branches["b"]) d.ask["b"] = clip(bb.out, 600) 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) code, j, _ = e30_get("preview") d.seen["b"] = j 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) 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) // 5. c answers nowhere: not live, not staging, not the preview. for x in ([]string{"live", "staging", "preview"}) { c, cj, url := e30_get(x) d.nowhere[x] = {url, c, jget(cj, "word")} 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) 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) } // 6. What root says: the preview follows b and holds the copy. st := creator_ratcloud("status", app) lines := make([dynamic]string) 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) d.status = lines[:] 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) 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) d.seconds = now() - t0 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) } e30 :: proc() -> Exhibit { return { n = 30, wing = "Deploy", title = "Preview a branch", 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.", block = "Preview on a branch's row at apps, or ssh @ ratcloud preview : root checks that branch's ratcloud.conf, puts it on https://-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.", src = {"e30_preview.odin", #load("e30_preview.odin", string)}, files = srcs({"creator.odin", #load("creator.odin", string)}), show = proc(me: ^rc.User) -> string { d, s, found := stored_data(30, E30_Data) ask := fmt.tprintf(`

Ask for one of yours: Preview on the branch's row on your app's page at apps, or ssh <you>@%s ratcloud preview <app> <branch>.

`, h(rc.app_url("apps")), h(env().domain)) app := h(rc.app_url(PREVIEW_APP)) if !found || len(d.words) == 0 do return cat(`

Every six hours the robot, as the creator of the sample app `, PREVIEW_APP, `, pushes three branches and asks for two of them. It has not run here yet.

`, ask) b := strings.builder_make() li :: proc(b: ^strings.Builder, tag: string, ok: bool, html: string) { fmt.sbprintf(b, `
  • %s%s
  • `, "ok" if ok else "bad", h(tag), html) } fmt.sbprintf(&b, `

    The robot, as the creator of the sample app %s, %s: pushed three branches, loaded a copy of %s's data into the preview, then asked for two of them over ssh.

      `, app, PREVIEW_APP, ago(parse_time(d.at)), h(env().env)) sb := d.seen["b"] files := make([dynamic]string) for f in jarr(sb, "files") do append(&files, jstr(f)) li(&b, "asked", jhas(d.seen["a"], "word"), fmt.tprintf(`%s: the preview answered %s and wrote it into its data`, h(d.branches["a"]), h(e30_url("preview")), h(d.words["a"]))) li(&b, "asked", jstr(sb, "word") == d.words["b"], fmt.tprintf("%s next: answered %s%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 "")) li(&b, "nowhere", s.ok && len(d.nowhere) > 0, fmt.tprintf("%s, never asked for: its push went nowhere, and no address answers %s", h(d.branches["c"]), h(d.words["c"]))) for l in d.status do li(&b, "status", true, cat("", h(strings.trim_space(l)), "")) strings.write_string(&b, "
    ") if !s.ok do fmt.sbprintf(&b, `

    %s

    `, h(s.detail)) return cat(strings.to_string(b), ask) }, check = proc() -> Check { d, s, found := stored_data(30, E30_Data) last := parse_time(d.at) due := last == 0 || now() - last >= (E30_EVERY if s.ok else E30_RETRY) // Rounds run in the five-minute job, in this zoo's half hour; a "check now" reads the last one. if due && g_in_job && e30_slot() do return e30_round() 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")) 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) _ = found return {s.ok, s.detail, s.data} }, } }