Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Self-hosting fixes: a rollback puts .env back exactly, and a backup archive is 600 in a 700 folder
milind-sonicec1d2258ae0
diff --git a/apps/docs/content/docs/self-hosting/deploy-vps.mdx b/apps/docs/content/docs/self-hosting/deploy-vps.mdxindex 989c9f5307..9c775c3758 100644--- a/apps/docs/content/docs/self-hosting/deploy-vps.mdx+++ b/apps/docs/content/docs/self-hosting/deploy-vps.mdx@@ -417,10 +417,11 @@ every file's owner and mode either way. The script then:without it), and Caddy runs again.5. If any of that fails: puts back the data from the copy, `.env` and the oldimage, starts the old version, and tells you what went wrong. Your server- is as it was before step 1, except that `.env` now names the old version by- its digest: a tag such as the installer's `stable` may have moved since,- and a later run of the installer would otherwise follow it. `--rollback`- does the same. The copy is put into a new folder inside the+ is as it was before step 1, `.env` included, byte for byte. Only when `.env`+ named a tag that moves (the installer's `stable`, `latest`, or no image at+ all) does it now name the old version by its digest instead: that tag may+ have moved since, and a later run of the installer would otherwise follow+ it. `--rollback` does the same. The copy is put into a new folder inside thedata and checked there before it takes the data's place, so a restore thatfails (a full disk, say) leaves both the data and the copy as they were:the server stays stopped, the update's journal is kept, and@@ -494,7 +495,7 @@ the administrator shell, replacing `X.Y.Z`:```shsudo systemctl stop openmausbot-sudo -iu maus sh -c 'umask 077; tar czf ~/openmausbot-before-update.tgz -C ~ .openmausbot'+sudo -iu maus sh -c 'umask 077; tar czf ~/openmausbot-before-update.tgz -C ~ .openmausbot && chmod 600 ~/openmausbot-before-update.tgz'sudo -iu maus npm install --global --prefix /home/maus/.local [email protected]sudo systemctl start openmausbotcurl -sf http://127.0.0.1:8799/api/health && sudo -iu maus openmausbot doctor@@ -536,6 +537,7 @@ credentials, and paired sessions). Run it from the service account's shell:umask 077backup_dir=$(mktemp -d "$PWD/openmausbot-backup.XXXXXX")tar czf "$backup_dir/openmausbot-data.tgz" -C "$HOME" .openmausbot+chmod 600 "$backup_dir/openmausbot-data.tgz"```A full backup also needs your engine credential/configuration paths, such as@@ -552,7 +554,7 @@ volume name if you changed the Compose project:```shdocker compose stopbackup_dir=$(mktemp -d "$PWD/openmausbot-backup.XXXXXX")-docker run --rm -v deploy_data:/data:ro -v "$backup_dir":/b alpine sh -c 'umask 077; tar czf /b/openmausbot-data.tgz -C /data .'+docker run --rm -v deploy_data:/data:ro -v "$backup_dir":/b alpine sh -c 'umask 077; tar czf /b/openmausbot-data.tgz -C /data . && chmod 600 /b/openmausbot-data.tgz'docker compose start```@@ -566,24 +568,26 @@ docker compose start```With the installer, the data is the folder `/opt/openmausbot/data`. This backs-it up, with `.env`, into a private archive in `/opt/openmausbot`, the whole-project stopped and started again around it:+it up, with `.env`, into an archive in `/opt/openmausbot/backups`, the whole+project stopped and started again around it. The folder is `700` and the archive+`600` whatever the server's umask or the folder's default ACLs, since the archive+holds every chat, key and session:```sh-cd /opt/openmausbot && sudo docker compose stop && sudo sh -c 'umask 077; tar --warning=no-file-ignored -czf "backup-$(date -u +%Y%m%dT%H%M%SZ).tgz" data .env'; sudo docker compose start+cd /opt/openmausbot && sudo docker compose stop && sudo sh -c 'set -e; install -d -m 700 backups; f="backups/backup-$(date -u +%Y%m%dT%H%M%SZ).tgz"; (umask 077; tar --warning=no-file-ignored -czf "$f" data .env); chmod 600 "$f"'; sudo docker compose start```Copy the archive to another machine and keep it private.`--warning=no-file-ignored` keeps tar from listing the sockets in the datafolder, which are not data: the server makes them again when it starts.-To restore an archive, replace `backup-20261110T153000Z.tgz` with its name.+To restore an archive, replace `backups/backup-20261110T153000Z.tgz` with its name.The data as it is now is moved aside to `data.before-restore`, and the archive'sdata takes its place, with its owner and permissions. An archive without a`data/` folder changes nothing:```sh-cd /opt/openmausbot && sudo docker compose stop && sudo sh -c 'set -e; tar -tzf backup-20261110T153000Z.tgz data/ >/dev/null; rm -rf data.before-restore; mv data data.before-restore; tar -xzpf backup-20261110T153000Z.tgz data'; sudo docker compose start+cd /opt/openmausbot && sudo docker compose stop && sudo sh -c 'set -e; tar -tzf backups/backup-20261110T153000Z.tgz data/ >/dev/null; rm -rf data.before-restore; mv data data.before-restore; tar -xzpf backups/backup-20261110T153000Z.tgz data'; sudo docker compose start```Once MausBot runs on the restored data, remove the old copy:diff --git a/deploy/e2e/run.sh b/deploy/e2e/run.shindex a12754dfd7..7a10b485f6 100755--- a/deploy/e2e/run.sh+++ b/deploy/e2e/run.sh@@ -155,11 +155,12 @@ healthy_again && through_caddy "kill -9 of the server process"step "the documented backup and restore, each with the whole project stopped and started"echo before-backup | sudo tee "$DIR/data/e2e-backup" >/dev/null# docs/deploy-vps.md, "Back up", with the installer: exactly as written there.-(cd /opt/openmausbot && sudo docker compose stop && sudo sh -c 'umask 077; tar --warning=no-file-ignored -czf "backup-$(date -u +%Y%m%dT%H%M%SZ).tgz" data .env'; sudo docker compose start) >"$OUT/backup.txt" 2>&1-archive=$(cd "$DIR" && sudo sh -c 'ls backup-*.tgz' | head -n 1)+(cd /opt/openmausbot && sudo docker compose stop && sudo sh -c 'set -e; install -d -m 700 backups; f="backups/backup-$(date -u +%Y%m%dT%H%M%SZ).tgz"; (umask 077; tar --warning=no-file-ignored -czf "$f" data .env); chmod 600 "$f"'; sudo docker compose start) >"$OUT/backup.txt" 2>&1+archive=$(cd "$DIR" && sudo sh -c 'ls backups/backup-*.tgz' | head -n 1)[ -n "$archive" ] || fail "the backup made no archive: $(cat "$OUT/backup.txt")"grep -q "socket ignored" "$OUT/backup.txt" && fail "tar listed sockets: $(cat "$OUT/backup.txt")"[ "$(sudo stat -c %a "$DIR/$archive")" = 600 ] || fail "the archive is readable by others"+[ "$(sudo stat -c %a "$DIR/backups")" = 700 ] || fail "the backups folder is open to others"healthy_again && through_caddy "the backup"echo after-backup | sudo tee "$DIR/data/e2e-backup" >/dev/null(cd /opt/openmausbot && sudo docker compose stop && sudo sh -c "set -e; tar -tzf $archive data/ >/dev/null; rm -rf data.before-restore; mv data data.before-restore; tar -xzpf $archive data"; sudo docker compose start) >"$OUT/restore.txt" 2>&1diff --git a/deploy/update.sh b/deploy/update.shindex f0b39ca4f9..c356ec63be 100755--- a/deploy/update.sh+++ b/deploy/update.sh@@ -274,11 +274,19 @@ room_for_copy() { # data backup image → 0 when there is room where the copy go[ "$FREE_MB" -ge "$NEEDED_MB" ]}-# After a rollback the version that ran before stays pinned by its digest. A tag .env named before (the installer's-# `stable`) may have moved since, and a later run of the installer or `docker compose pull` would follow it and undo-# the rollback. A version built on this machine has no digest to pin.+# After a rollback .env is exactly as it was, unless it named a tag that moves: the installer's `stable`, `latest`, or no+# image at all (the Compose file's `latest`). Such a tag may have moved since, and a later run of the installer or+# `docker compose pull` would follow it and undo the rollback, so the version that ran before is pinned by its digest+# instead. A digest or a released version's own tag (`:v0.1.150`) never moves, and stays as it was. A version built on+# this machine has no digest to pin.pin_previous() {- case "$J_previous" in "$REPO@sha256:"*) pin_image "$J_previous" ;; esac+ local named+ case "$J_previous" in "$REPO@sha256:"*) ;; *) return 0 ;; esac+ named=$(sed -n 's/^OMB_IMAGE=//p' .env 2>/dev/null | tail -n 1)+ case "$named" in+ *@sha256:* | *:v[0-9]*.[0-9]*.[0-9]*) return 0 ;;+ esac+ pin_image "$J_previous"}pin_image() { # writes OMB_IMAGE=<ref> into .env, keeping every other line and the file's modelocal tmp="$DIR/.env.update.tmp"diff --git a/docs/deploy-vps.md b/docs/deploy-vps.mdindex 5714a85ac8..151a58e42f 100644--- a/docs/deploy-vps.md+++ b/docs/deploy-vps.md@@ -397,10 +397,11 @@ every file's owner and mode either way. The script then:without it), and Caddy runs again.5. If any of that fails: puts back the data from the copy, `.env` and the oldimage, starts the old version, and tells you what went wrong. Your server- is as it was before step 1, except that `.env` now names the old version by- its digest: a tag such as the installer's `stable` may have moved since,- and a later run of the installer would otherwise follow it. `--rollback`- does the same. The copy is put into a new folder inside the+ is as it was before step 1, `.env` included, byte for byte. Only when `.env`+ named a tag that moves (the installer's `stable`, `latest`, or no image at+ all) does it now name the old version by its digest instead: that tag may+ have moved since, and a later run of the installer would otherwise follow+ it. `--rollback` does the same. The copy is put into a new folder inside thedata and checked there before it takes the data's place, so a restore thatfails (a full disk, say) leaves both the data and the copy as they were:the server stays stopped, the update's journal is kept, and@@ -474,7 +475,7 @@ the administrator shell, replacing `X.Y.Z`:```shsudo systemctl stop openmausbot-sudo -iu maus sh -c 'umask 077; tar czf ~/openmausbot-before-update.tgz -C ~ .openmausbot'+sudo -iu maus sh -c 'umask 077; tar czf ~/openmausbot-before-update.tgz -C ~ .openmausbot && chmod 600 ~/openmausbot-before-update.tgz'sudo -iu maus npm install --global --prefix /home/maus/.local [email protected]sudo systemctl start openmausbotcurl -sf http://127.0.0.1:8799/api/health && sudo -iu maus openmausbot doctor@@ -516,6 +517,7 @@ credentials, and paired sessions). Run it from the service account's shell:umask 077backup_dir=$(mktemp -d "$PWD/openmausbot-backup.XXXXXX")tar czf "$backup_dir/openmausbot-data.tgz" -C "$HOME" .openmausbot+chmod 600 "$backup_dir/openmausbot-data.tgz"```A full backup also needs your engine credential/configuration paths, such as@@ -532,7 +534,7 @@ volume name if you changed the Compose project:```shdocker compose stopbackup_dir=$(mktemp -d "$PWD/openmausbot-backup.XXXXXX")-docker run --rm -v deploy_data:/data:ro -v "$backup_dir":/b alpine sh -c 'umask 077; tar czf /b/openmausbot-data.tgz -C /data .'+docker run --rm -v deploy_data:/data:ro -v "$backup_dir":/b alpine sh -c 'umask 077; tar czf /b/openmausbot-data.tgz -C /data . && chmod 600 /b/openmausbot-data.tgz'docker compose start```@@ -546,24 +548,26 @@ docker compose start```With the installer, the data is the folder `/opt/openmausbot/data`. This backs-it up, with `.env`, into a private archive in `/opt/openmausbot`, the whole-project stopped and started again around it:+it up, with `.env`, into an archive in `/opt/openmausbot/backups`, the whole+project stopped and started again around it. The folder is `700` and the archive+`600` whatever the server's umask or the folder's default ACLs, since the archive+holds every chat, key and session:```sh-cd /opt/openmausbot && sudo docker compose stop && sudo sh -c 'umask 077; tar --warning=no-file-ignored -czf "backup-$(date -u +%Y%m%dT%H%M%SZ).tgz" data .env'; sudo docker compose start+cd /opt/openmausbot && sudo docker compose stop && sudo sh -c 'set -e; install -d -m 700 backups; f="backups/backup-$(date -u +%Y%m%dT%H%M%SZ).tgz"; (umask 077; tar --warning=no-file-ignored -czf "$f" data .env); chmod 600 "$f"'; sudo docker compose start```Copy the archive to another machine and keep it private.`--warning=no-file-ignored` keeps tar from listing the sockets in the datafolder, which are not data: the server makes them again when it starts.-To restore an archive, replace `backup-20261110T153000Z.tgz` with its name.+To restore an archive, replace `backups/backup-20261110T153000Z.tgz` with its name.The data as it is now is moved aside to `data.before-restore`, and the archive'sdata takes its place, with its owner and permissions. An archive without a`data/` folder changes nothing:```sh-cd /opt/openmausbot && sudo docker compose stop && sudo sh -c 'set -e; tar -tzf backup-20261110T153000Z.tgz data/ >/dev/null; rm -rf data.before-restore; mv data data.before-restore; tar -xzpf backup-20261110T153000Z.tgz data'; sudo docker compose start+cd /opt/openmausbot && sudo docker compose stop && sudo sh -c 'set -e; tar -tzf backups/backup-20261110T153000Z.tgz data/ >/dev/null; rm -rf data.before-restore; mv data data.before-restore; tar -xzpf backups/backup-20261110T153000Z.tgz data'; sudo docker compose start```Once MausBot runs on the restored data, remove the old copy:diff --git a/scripts/testing/deploy-update.test.ts b/scripts/testing/deploy-update.test.tsindex f6994940d7..a7a4885a88 100644--- a/scripts/testing/deploy-update.test.ts+++ b/scripts/testing/deploy-update.test.ts@@ -325,6 +325,26 @@ describe.skipIf(!posix)("deploy/update.sh", () => {expect(box.run(["--rollback", "--yes"]).stdout).toContain("There is no earlier update to go back from.");});+ // A released version's own tag, or a digest, never moves: .env comes back byte for byte. Only a moving tag (`stable`,+ // `latest`, or none) is replaced by the digest of the version that ran.+ it("puts .env back exactly when it named a tag that does not move", () => {+ for (const named of [`${REPO}:v0.1.150`, OLD.refs[1]!]) {+ const box = install({}, { images: [{ ...OLD, refs: [...OLD.refs, `${REPO}:v0.1.150`] }, NEW] });+ writeFileSync(join(box.deploy, ".env"), `${ENV}OMB_IMAGE=${named}\n`, { mode: 0o600 });+ const before = box.env();+ expect(box.run().status).toBe(0);+ expect(box.env()).not.toBe(before);+ const back = box.run(["--rollback", "--yes"]);+ expect(back.status, back.stdout).toBe(0);+ expect(box.env(), named).toBe(before);+ }+ const moving = install();+ writeFileSync(join(moving.deploy, ".env"), `${ENV}OMB_IMAGE=${REPO}:stable\n`);+ expect(moving.run().status).toBe(0);+ expect(moving.run(["--rollback", "--yes"]).status).toBe(0);+ expect(moving.env()).toBe(`${ENV}OMB_IMAGE=${OLD.refs[1]}\n`);+ });+// A hardened server answers `openmausbot doctor` without a session with a refusal, so before the server's own count// the check that a bot can still start never ran there.it("checks a hardened version's engines with the server's own count, where doctor is refused", () => {