Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

update.sh: an answer to its question reaches it, whatever docker compose exec does with standard input

milind-soni84b675a5e035
diff --git a/deploy/update.sh b/deploy/update.sh
index c356ec63be..254e8743ed 100755
--- a/deploy/update.sh
+++ b/deploy/update.sh
@@ -67,6 +67,9 @@ unset OMB_IMAGE
# A step interrupted while its output goes to the log still speaks to the terminal.
exec 3>&1
+# The answers to its questions are read from fd 4, and nothing else this script starts sees them: `docker compose exec`
+# forwards its standard input to the container and would swallow a `yes` typed or piped ahead of the question.
+exec 4<&0 </dev/null
say() {
printf '%s\n' "$*" >&3 2>/dev/null
if [ -d "$STATE" ]; then printf '%s %s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$*" >>"$LOG" 2>/dev/null; fi
@@ -80,9 +83,9 @@ refuse() {
ask() {
local answer=""
printf '%s' "$1" >&3 2>/dev/null
- read -r answer || answer=""
+ read -r answer <&4 || answer=""
# A terminal echoes the Enter that ends the answer; anything else does not.
- [ -t 0 ] || printf '\n' >&3 2>/dev/null
+ [ -t 4 ] || printf '\n' >&3 2>/dev/null
printf '%s' "$answer"
}
compose() { docker compose "$@"; }
diff --git a/scripts/testing/deploy-update.test.ts b/scripts/testing/deploy-update.test.ts
index a7a4885a88..a3d30314ac 100644
--- a/scripts/testing/deploy-update.test.ts
+++ b/scripts/testing/deploy-update.test.ts
@@ -248,6 +248,32 @@ describe.skipIf(!posix)("deploy/update.sh", () => {
expect(done.status, done.stdout + done.stderr).toBe(0);
});
+ // As the real-server check's step 6 runs it: killed while it waits for a version that never answers, then a `yes`
+ // piped in. Its health checks run `docker compose exec`, which forwards standard input to the container: the answer
+ // must still reach the question, and the old image, the data and .env (on a version tag) come back exactly.
+ it("after an update killed while it waits, a piped yes puts the old image, the data and .env back", () => {
+ const box = install({ behaviour: "silent" }, {
+ images: [{ ...OLD, refs: [...OLD.refs, `${REPO}:v0.1.150`] }, { ...NEW, behaviour: "silent" }],
+ signalOn: { match: "/api/health\"", signal: "SIGKILL" },
+ });
+ writeFileSync(join(box.deploy, ".env"), `${ENV}OMB_IMAGE=${REPO}:v0.1.150\n`, { mode: 0o600 });
+ const envBefore = box.env();
+ const before = tree(box.data);
+ expect(box.run().signal).toBe("SIGKILL");
+ expect(box.journal()).toBe(true);
+ expect(box.state().containers.omb.image).toBe(NEW.id);
+ const unanswered = box.run([], { input: "" });
+ expect(unanswered.status, unanswered.stdout).toBe(4);
+ expect(box.state().containers.omb.image).toBe(NEW.id);
+ const yes = box.run([], { input: "yes\n" });
+ expect(yes.said.at(-1), yes.stdout).toBe("Your server runs 0.1.150 again, with its data as it was before the update.");
+ expect(yes.status).toBe(1);
+ expect(box.state().containers.omb.image).toBe(OLD.id);
+ expect(box.env()).toBe(envBefore);
+ expect(tree(box.data)).toEqual({ ...before, "started-by.txt": "0.1.150\n0.1.150\n" });
+ expect(box.journal()).toBe(false);
+ });
+
it("asks before it undoes an update an earlier run left unhealthy, says what that loses, and changes nothing without a yes", () => {
const box = install({ behaviour: "silent" }, { signalOn: { step: "compose up", signal: "SIGKILL" } });
const before = tree(box.data);
@@ -262,7 +288,7 @@ describe.skipIf(!posix)("deploy/update.sh", () => {
// The new version has been running on the data since: nothing may take that away unasked.
const since = tree(box.data);
for (const unanswered of [box.run(), box.run([], { input: "no\n" }), box.run(["--rollback"])]) {
- expect(unanswered.status).toBe(4);
+ expect(unanswered.status, unanswered.stdout).toBe(4);
expect(unanswered.stdout).toMatch(/An earlier update to 0\.1\.151, started \S+Z, did not finish/);
expect(unanswered.stdout).toContain("Everything made on this server since then (chats, files, settings) is lost.");
expect(unanswered.said.at(-1)).toContain("Nothing was changed.");
diff --git a/scripts/testing/fake-compose-docker.mjs b/scripts/testing/fake-compose-docker.mjs
index 8a84ad24ca..21495dd2fa 100644
--- a/scripts/testing/fake-compose-docker.mjs
+++ b/scripts/testing/fake-compose-docker.mjs
@@ -62,6 +62,9 @@ function compose(rest) {
if (verb === "up") return composeUp();
if (verb === "logs") return out(image(omb?.image)?.lastWords ?? "");
if (verb === "exec") {
+ // As the real one does: `docker compose exec` forwards its standard input to the container, so it takes whatever is
+ // waiting there, an answer meant for update.sh's question included.
+ try { readFileSync(0); } catch { /* no standard input */ }
const serving = image(omb?.image);
if (omb?.status !== "running" || !serving) fail("service omb is not running");
// `doctor: false`: a hardened version, where doctor without a session is refused whatever its engines.