29. Real data to build with
An app's creator finds a copy of its live data in their storage, and can put data into staging or preview; live takes nothing that way.
storage shows each of your apps' data under Apps / <App> / data: live as a read-only copy root makes as the app, staging and preview with "Put a file in". Laptops: dev/ratcloud data pull | push in the app's folder (the same rules, over ssh). The preview also takes a whole copy of live's or staging's data: Load on the app's page at apps, or ratcloud load <app> live|staging.
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 (staging). The zoo's own self-check fetches this page and compares it byte for byte with the file it runs.
e29_real_data.odin sha256 6b6656f46151 · raw
1package main 2 3// Exhibit 29. An app's creator works with its data without a shell (tools PLATFORM.md, "Your app's 4// data"; storage README, "Apps / <App> / data"): storage shows a copy of each environment's data, live 5// read-only "as of" when root copied it, and a file put into staging or preview goes in. Storage 6// holds no authority here: it files the request with account's assertion for the person, root checks 7// they create the app, copies as the app (SQLite through its backup API) or refuses, and logs both. 8// 9// The robot creates the sample app zoo-broken, whose page keeps one SQLite table, who was here. The 10// check, as the robot: visit zoo-broken (its own row gets the visit's time), ask storage for a fresh 11// copy of live and put a file into live, wait for root's minute tick, then download the copy, open it 12// here (the zoo's own SQLite, on a copy in its data) and find that visit; the put must be refused. 13// 14// Live only: staging's storage shows no app's data on purpose (account-staging's usernames are 15// anyone's to register, so a name there proves nothing), and the staging zoo checks exactly that. 16 17import "core:encoding/json" 18import "core:fmt" 19import "core:strings" 20import "ratcloud:rc" 21 22DATA_APP :: "zoo-broken" 23DATA_DB :: "broken.db" 24// A new round (one copy, one put) at most once an hour: root logs every copy and every refusal (the 25// app's data.json, syslog), and an hourly round proves the door as well as one every five minutes 26// would. A red round is tried again at the next run. 27DATA_EVERY :: 3600 28// Root answers storage's requests on its minute tick, and a copy may take a while: the five-minute 29// job waits up to 110 s, a "check now" in the browser up to 20 (a request ends at 30). A round still 30// waiting is picked up by the next run. 31DATA_WAIT_JOB :: 110 32DATA_WAIT_WEB :: 20 33 34data_api :: proc(b: ^Browser, op: string, in_json := "{}") -> (int, json.Value) { 35 r := post(b, fmt.tprintf("%s/api/%s", rc.app_url("storage"), op), in_json) 36 if r.j == nil do return r.status, parse(fmt.tprintf(`{{"error":%s}}`, jq(clip(r.body, 160)))) 37 return r.status, r.j 38} 39 40// Staging: the robot (account-staging's zoo-robot, a name anyone could have taken) sees no data. 41data_staging :: proc() -> Check { 42 b, err := robot_at("storage") 43 if err != "" do return fail(err) 44 c1, all := data_api(b, "data") 45 c2, _ := data_api(b, "data", fmt.tprintf(`{{"app":%s}}`, jq(DATA_APP))) 46 c3, _ := data_api(b, "datafetch", fmt.tprintf(`{{"app":%s,"env":"live"}}`, jq(DATA_APP))) 47 data := fmt.tprintf(`{{"apps":%s,"one":%d,"fetch":%d}}`, to_json(jget(all, "apps")), c2, c3) 48 if c1 != 200 || to_json(jget(all, "apps")) != "[]" do return {false, cat("staging's storage shows the robot some app's data: ", to_json(all)), data} 49 if c2 != 404 || c3 != 404 do return {false, fmt.tprintf("staging's storage answered %d and %d for %s's data, not 404", c2, c3, DATA_APP), data} 50 return {true, "staging's storage shows no app's data and asks root for none, as it should (account-staging's names are anyone's to register); the live zoo shows the copy", data} 51} 52 53Pending :: struct { 54 seen: i64 `json:"seen"`, 55 asked: i64 `json:"asked"`, 56 copy: json.Value `json:"copy"`, 57 put: json.Value `json:"put"`, 58} 59 60E29_Data :: struct { 61 asked: string `json:"asked"`, 62 seen: string `json:"seen"`, 63 copy: json.Value `json:"copy"`, 64 put: json.Value `json:"put"`, 65 as_of: string `json:"as_of,omitempty"`, 66 after_s: i64 `json:"after_s"`, 67 file: json.Value `json:"file,omitempty"`, 68 quick_check: string `json:"quick_check,omitempty"`, 69 rows: i64 `json:"rows"`, 70} 71 72// One round on live: start it (or pick up the one waiting), wait up to `wait` seconds for root, judge 73// it. ok = false and detail "": root has not answered yet. 74data_round :: proc(wait: i64) -> (Check, bool) { 75 b, err := robot() 76 if err != "" do return fail(err), true 77 st := rc.app_url("storage") 78 pv, _ := meta("data.pending") 79 p: Pending 80 pending := pv != "" && json_into(pv, &p) 81 if pending && now() - p.asked > 300 { 82 meta_set("data.pending", "") 83 return fail(fmt.tprintf("root did not answer storage's requests within 5 minutes (asked %s UTC)", hms(p.asked)), p), true 84 } 85 if !pending { 86 r := visit(b, cat(rc.app_url(DATA_APP), "/")) 87 i := strings.index(r.body, `data-seen-at="`) 88 if r.status != 200 || i < 0 do return fail(fmt.tprintf("the robot's visit to %s was not noted in its data (%d)", DATA_APP, r.status)), true 89 seen_at := atoi(r.body[i + 14:]) 90 if s := visit(b, cat(st, "/")); s.status != 200 do return fail(fmt.tprintf("storage answered the robot with %d", s.status)), true 91 code, j := data_api(b, "data", fmt.tprintf(`{{"app":%s}}`, jq(DATA_APP))) 92 if code != 200 do return fail(fmt.tprintf("the robot's storage does not offer %s's data (%d): %s", DATA_APP, code, jstr(j, "error")), j), true 93 if w, isb := jget(j, "envs", "live", "writable").(json.Boolean); !isb || w do return fail("storage offers to write into live", jget(j, "envs")), true 94 asked := now() 95 code2, f := data_api(b, "datafetch", fmt.tprintf(`{{"app":%s,"env":"live"}}`, jq(DATA_APP))) 96 if code2 != 200 || !jhas(f, "id") do return fail(fmt.tprintf("storage would not ask root for a copy of live (%d): %s", code2, jstr(f, "error")), f), true 97 hs := make(map[string]string) 98 hs["X-RC"] = "1" 99 u := go(b, "POST", fmt.tprintf("%s/api/dataput?app=%s&env=live&path=zoo-was-here.txt", st, DATA_APP), "The zoo's robot tried to put this into live (exhibit 29). Root must refuse it.\n", "application/octet-stream", hs) 100 if u.status != 200 || !jhas(u.j, "id") do return fail(fmt.tprintf("storage would not pass a put into live on to root (%d)", u.status), u.j), true 101 p = {seen_at, asked, jget(f, "id"), jget(u.j, "id")} 102 meta_set("data.pending", to_json(p)) 103 } else { 104 visit(b, cat(st, "/")) // this instance's robot, signed in at storage 105 } 106 end := now() + wait 107 c, u: json.Value 108 for { 109 _, c = data_api(b, "datastate", fmt.tprintf(`{{"id":%s}}`, to_json(p.copy))) 110 _, u = data_api(b, "datastate", fmt.tprintf(`{{"id":%s}}`, to_json(p.put))) 111 cr, ur := jstr(c, "result") or_else "waiting", jstr(u, "result") or_else "waiting" 112 if cr != "waiting" && ur != "waiting" do break 113 if now() >= end do return {}, false 114 rc.sleep(3000) 115 } 116 meta_set("data.pending", "") 117 return data_judge(b, p, c, u), true 118} 119 120data_judge :: proc(b: ^Browser, p: Pending, c, u: json.Value) -> Check { 121 st := rc.app_url("storage") 122 data := E29_Data{asked = iso(p.asked), seen = iso(p.seen), copy = c, put = u} 123 if jstr(c, "result") != "done" do return fail(fmt.tprintf("root did not copy %s's live data: %s %s", DATA_APP, jstr(c, "result") or_else "?", jstr(c, "message")), data) 124 _, j := data_api(b, "data", fmt.tprintf(`{{"app":%s}}`, jq(DATA_APP))) 125 cp := jget(j, "envs", "live", "copy") 126 at := parse_time(jstr(cp, "at")) 127 for f in jarr(cp, "files") do if jstr(f, "path") == DATA_DB do data.file = f 128 data.as_of, data.after_s = jstr(cp, "at"), at - p.asked 129 if data.file == nil do return fail(cat("the copy of live has no ", DATA_DB), data) 130 // Within a tick: root's minute tick, plus the copy itself. 131 if at < p.asked - 5 || at - p.asked > 120 do return fail(fmt.tprintf("the copy is as of %s, not within a tick of asking at %s", jstr(cp, "at") or_else "never", hms(p.asked)), data) 132 r := get(b, fmt.tprintf("%s/data/%s/live/%s", st, DATA_APP, DATA_DB)) 133 if r.status != 200 || !strings.has_prefix(r.body, "SQLite format 3\x00") do return fail(fmt.tprintf("%s did not download as a SQLite file (%d)", DATA_DB, r.status), data) 134 // Open it here: a copy in the zoo's own data, opened with the zoo's own SQLite, then removed. 135 name := cat("copy-", random_hex(4)) 136 if rc.write_file(cat(name, ".db"), transmute([]u8)r.body) != 0 do return fail("could not keep the copy to open it", data) 137 defer for x in ([]string{".db", ".db-journal", ".db-wal", ".db-shm"}) do rc.delete_file(cat(name, x)) 138 d, ok := rc.db(name) 139 if !ok do return fail(cat("the copy of ", DATA_DB, " does not open"), data) 140 q := rc.query_rows(d, "PRAGMA quick_check") 141 for rc.next(&q) do data.quick_check = strings.clone(rc.col_text(&q, 0)) 142 if q.err != "" do return fail(cat("the copy of ", DATA_DB, " does not open: ", q.err), data) 143 n := rc.query_rows(d, "SELECT count(*) FROM seen") 144 for rc.next(&n) do data.rows = rc.col_int(&n, 0) 145 visit_at: i64 = 0 146 v := rc.query_rows(d, "SELECT at FROM seen WHERE user_id = ?", robot_user().id) 147 for rc.next(&v) do visit_at = rc.col_int(&v, 0) 148 if data.quick_check != "ok" do return fail(cat("the copy of ", DATA_DB, " fails SQLite's quick_check: ", data.quick_check), data) 149 // The visit noted before asking, or a later one of the robot's (another run's), never an older one. 150 if visit_at < p.seen || visit_at > at do return fail(fmt.tprintf("the copy lacks the robot's visit at %s UTC%s", hms(p.seen), fmt.tprintf(" (it holds %s)", hms(visit_at)) if visit_at > 0 else ""), data) 151 if jstr(u, "result") != "refused" do return fail(fmt.tprintf("a put into live was %s: live must take nothing that way", jstr(u, "result") or_else "?"), data) 152 return pass(fmt.tprintf("root copied %s's live data into the robot's storage %d s after it asked (as of %s UTC); %s opened here with the robot's visit in it; a put into live was refused: %s", DATA_APP, at - p.asked, hms(at), DATA_DB, jstr(u, "message")), data) 153} 154 155// The preview's data, as root says it, from exhibit 30's last round, which loads a copy of live's data 156// into the robot's preview of zoo-broken. 157data_preview_line :: proc() -> string { 158 d, _, _ := stored_data(30, E30_Data) 159 for l in d.status { 160 t := strings.trim_space(l) 161 if strings.has_prefix(t, "data:") do return fmt.tprintf(`<li><span class="tag ok">preview</span><span>%s (exhibit <a href="#e30">30</a> loads it)</span></li>`, h(strings.trim_space(t[5:]))) 162 } 163 return "" 164} 165 166e29 :: proc() -> Exhibit { 167 return { 168 n = 29, wing = "Building", 169 title = "Real data to build with", 170 promise = "An app's creator finds a copy of its live data in their storage, and can put data into staging or preview; live takes nothing that way.", 171 block = "storage shows each of your apps' data under Apps / <App> / data: live as a read-only copy root makes as the app, staging and preview with \"Put a file in\". Laptops: dev/ratcloud data pull | push in the app's folder (the same rules, over ssh). The preview also takes a whole copy of live's or staging's data: Load on the app's page at apps, or ratcloud load <app> live|staging.", 172 src = {"e29_real_data.odin", #load("e29_real_data.odin", string)}, 173 show = proc(me: ^rc.User) -> string { 174 st := rc.app_url("storage") 175 mine := fmt.tprintf(`<p class="muted">Your own apps' data is in <a href="%s/#data" target="_blank" rel="noopener">your storage, under Apps</a>, if you create one.</p>`, h(st)) 176 if env().env != "live" do return cat(`<p>Staging's storage shows no app's data: account-staging's usernames are anyone's to register. The live zoo shows the copy.</p>`, mine) 177 d, s, found := stored_data(29, E29_Data) 178 app := h(rc.app_url(DATA_APP)) 179 if !found || !jhas(d.file) do return cat(`<p>The robot creates the sample app <a href="`, app, `/">`, DATA_APP, `</a>; its copy has not been checked yet.</p>`, mine) 180 tables := make([dynamic]string) 181 if t, ok := jget(d.file, "tables").(json.Object); ok do for k, v in t do append(&tables, fmt.tprintf("%s: %d row%s", h(k), jint(v), "" if jint(v) == 1 else "s")) 182 b := strings.builder_make() 183 rc.render_to(&b, `<p>The robot creates the sample app <a href="{{1}}/">{{2}}</a>, which notes who was here. In the robot's storage, Apps / Zoo-broken / data / live:</p><ul class="list">`, {{"1", rc.app_url(DATA_APP)}, {"2", DATA_APP}}) 184 rc.render_to(&b, `<li><span class="tag {{1}}">copy</span><span><code>{{2}}</code>, {{3}} bytes{{4}}, as of {{5}}, {{6}} s after the robot asked</span></li>`, {{"1", "ok" if s.ok else "bad"}, {"2", DATA_DB}, {"3", jint(d.file, "size")}, {"4", rc.raw(cat(" (", strings.join(tables[:], ", "), ")") if len(tables) > 0 else "")}, {"5", rc.raw(ago(parse_time(d.as_of)))}, {"6", d.after_s}}) 185 rc.render_to(&b, `<li><span class="tag {{1}}">opened</span><span>here, with SQLite: the robot's visit at {{2}} UTC is in it</span></li>`, {{"1", "ok" if d.rows > 0 else "bad"}, {"2", hms(parse_time(d.seen))}}) 186 rc.render_to(&b, `<li><span class="tag {{1}}">put into live</span><span>{{2}}: {{3}}</span></li>`, {{"1", "ok" if jstr(d.put, "result") == "refused" else "bad"}, {"2", jstr(d.put, "result") or_else "?"}, {"3", jstr(d.put, "message")}}) 187 strings.write_string(&b, data_preview_line()) 188 strings.write_string(&b, "</ul>") 189 return cat(strings.to_string(b), mine) 190 }, 191 check = proc() -> Check { 192 if env().env != "live" do return data_staging() 193 d, s, found := stored_data(29, E29_Data) 194 round := parse_time(d.asked) 195 pv, _ := meta("data.pending") 196 if found && s.ok && pv == "" && now() - round < DATA_EVERY do return {true, s.detail, s.data} 197 c, answered := data_round(DATA_WAIT_JOB if g_in_job else DATA_WAIT_WEB) 198 if answered do return c 199 if found && d.asked != "" do return {s.ok, s.detail, s.data} // the last round stands until root answers this one 200 return fail(fmt.tprintf("asked root for a copy of %s's live data; no answer yet (root answers on its minute tick)", DATA_APP)) 201 }, 202 } 203}