mirror of
https://github.com/hasso5703/alzeo.git
synced 2026-12-06 12:54:19 +01:00
--no-nemotron-super and --with-nemotron-super, the lane at boot kept on an update, a Nemotron lane begun only by its own first Load (the image the two share is not enough), its units on every plain run, whole with --all-weights or by name and proved when new, its opencode provider and the help's sizes (77 GB beside the Nemotron lane, 102 alone). The image and video lanes' installers read the units' cache and a path's nearest folder through lanes.sh, and every lane installer's --help prints its whole header (fixed line ranges had drifted). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
226 lines
12 KiB
Python
226 lines
12 KiB
Python
#!/usr/bin/env python3
|
|
"""install-image.sh finds the cache it serves from, measures space where things land, and
|
|
does not leave a box without an engine when it removes the lane that booted.
|
|
|
|
Found in review, 2026-09-24:
|
|
- HF_CACHE came from the environment or the default, never from the installed unit
|
|
(Environment=HF_HOME=), so an update re-downloaded 31 GB into ~/.cache and rewrote
|
|
HF_HOME on a box whose lane lived on another disk;
|
|
- free space was measured under $HOME, not where the checkpoint and the runtime go, and
|
|
"cached" meant the folder existed, which huggingface_hub creates before the first byte;
|
|
- --uninstall on a box whose boot lane was the image left no engine enabled at all.
|
|
The script's own lines run here with its /etc paths moved to a temporary directory.
|
|
"""
|
|
import pathlib
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
REPO = pathlib.Path(__file__).resolve().parents[1]
|
|
TEXT = (REPO / "install-image.sh").read_text()
|
|
|
|
|
|
def block(start, end, inclusive=True):
|
|
i = TEXT.index(start)
|
|
j = TEXT.index(end, i)
|
|
return TEXT[i:TEXT.index("\n", j) + 1] if inclusive else TEXT[i:j]
|
|
|
|
|
|
def bash(script, env, path_dirs=()):
|
|
path = ":".join([*map(str, path_dirs), "/usr/bin", "/bin"])
|
|
r = subprocess.run(["bash", "-c", script], capture_output=True, text=True, timeout=30,
|
|
env={"PATH": path, **env})
|
|
return r.returncode, r.stdout + r.stderr
|
|
|
|
|
|
class TheCacheItServesFrom(unittest.TestCase):
|
|
CACHE = block('installed_env(){', 'HF_CACHE="${HF_CACHE:-$HOME/.cache/huggingface}"')
|
|
|
|
def run_cache(self, image_unit=None, text_unit=None, env_cache=None):
|
|
d = pathlib.Path(tempfile.mkdtemp(prefix="img-cache-"))
|
|
(d / "image.service").write_text(image_unit or "")
|
|
(d / "text.service").write_text(text_unit or "")
|
|
env = {"HOME": str(d)}
|
|
if env_cache:
|
|
env["HF_CACHE"] = env_cache
|
|
# lanes.sh, which the installer sources: the unit's cache is read through it (unit_hf_cache)
|
|
script = (f'set -euo pipefail\nINSTALLED="{d}/image.service"; CONFIG_DIR="{d}"; TEXT_UNITS="{d}/text.service"\n'
|
|
+ f'LANES_UNIT_DIR="{d}"; . "{REPO}/lanes.sh"\n' + self.CACHE + 'echo "CACHE=$HF_CACHE"\n')
|
|
rc, out = bash(script, env)
|
|
return next(ln[6:] for ln in out.splitlines() if ln.startswith("CACHE="))
|
|
|
|
def test_the_installed_unit_wins_over_the_default(self):
|
|
self.assertEqual(self.run_cache(image_unit="[Service]\nEnvironment=PYTHONUNBUFFERED=1\n"
|
|
"Environment=HF_HOME=/data/hf\n"), "/data/hf")
|
|
|
|
def test_a_first_install_follows_the_text_lane(self):
|
|
self.assertEqual(self.run_cache(text_unit="ExecStart=docker run -v /mnt/hf:/root/.cache/huggingface x\n"),
|
|
"/mnt/hf")
|
|
|
|
def test_the_environment_still_wins(self):
|
|
self.assertEqual(self.run_cache(image_unit="Environment=HF_HOME=/data/hf\n", env_cache="/elsewhere"),
|
|
"/elsewhere")
|
|
|
|
|
|
class TheSpaceWhereItLands(unittest.TestCase):
|
|
# from lanes.sh's existing, which the block uses (the installer sources lanes.sh), to the line it says
|
|
SPACE = block("# existing: lanes.sh's", 'echo "OK: $(uname -m), key present, checkpoint')
|
|
|
|
def run_space(self, have_gib, free_w, free_r=None, mode="full"):
|
|
d = pathlib.Path(tempfile.mkdtemp(prefix="img-space-"))
|
|
blobs = d / "hf/hub/models--Qwen--Qwen-Image-2.1/blobs"
|
|
blobs.mkdir(parents=True)
|
|
with open(blobs / "b", "wb") as f:
|
|
f.truncate(int(have_gib * 1024 ** 3))
|
|
stub = d / "bin"
|
|
stub.mkdir()
|
|
two = free_r is not None
|
|
(stub / "df").write_text("#!/bin/sh\ncase \"$*\" in *lane*) echo Avail; echo %sG;; *) echo Avail; echo %sG;; esac\n"
|
|
% (free_r if two else free_w, free_w))
|
|
(stub / "stat").write_text("#!/bin/sh\ncase \"$*\" in *lane*) echo %s;; *) echo 1;; esac\n" % (2 if two else 1))
|
|
for f in stub.iterdir():
|
|
f.chmod(0o755)
|
|
(d / "lane").mkdir()
|
|
script = ('set -euo pipefail\ndie(){ echo "DIE: $*"; exit 1; }\n'
|
|
f'HF_CACHE="{d}/hf"; LANE_DIR="{d}/lane"; MODEL=Qwen/Qwen-Image-2.1; WEIGHTS_GB=31; RUNTIME_GB=11; MODE={mode}\n'
|
|
'step(){ echo "STEP $*"; }\ninstall_service(){ echo SERVICE; }\n'
|
|
+ f'. "{REPO}/lanes.sh"\n' + self.SPACE)
|
|
return bash(script, {"HOME": str(d)}, [stub])
|
|
|
|
def test_a_download_that_just_started_needs_the_whole_checkpoint(self):
|
|
rc, out = self.run_space(have_gib=0.000001, free_w=20)
|
|
self.assertEqual(rc, 1, out)
|
|
self.assertIn("DIE:", out)
|
|
|
|
def test_a_whole_checkpoint_needs_the_runtime_only(self):
|
|
rc, out = self.run_space(have_gib=30.86, free_w=20)
|
|
self.assertEqual(rc, 0, out)
|
|
self.assertIn("already cached", out)
|
|
|
|
def test_a_prepare_needs_room_for_the_runtime_only(self):
|
|
# the checkpoint is the Load's switch's to fetch, and to measure (on demand, 2026-10-14)
|
|
rc, out = self.run_space(have_gib=0.0, free_w=20, mode="prepare")
|
|
self.assertEqual(rc, 0, out)
|
|
rc, out = self.run_space(have_gib=0.0, free_w=5, mode="prepare")
|
|
self.assertEqual(rc, 1, out)
|
|
|
|
def test_units_only_measures_nothing_and_installs_the_unit(self):
|
|
rc, out = self.run_space(have_gib=0.0, free_w=0, mode="units")
|
|
self.assertEqual(rc, 0, out)
|
|
self.assertIn("SERVICE", out)
|
|
self.assertNotIn("DIE:", out)
|
|
|
|
def test_each_disk_is_measured_for_its_own_part(self):
|
|
rc, out = self.run_space(have_gib=0.0, free_w=500, free_r=5)
|
|
self.assertEqual(rc, 1, out)
|
|
self.assertIn("runtime", out)
|
|
|
|
|
|
class RemovingTheLaneThatBooted(unittest.TestCase):
|
|
UNINSTALL = block('if [ "$ACTION" = uninstall ]; then', ' exit 0')
|
|
|
|
def run_uninstall(self, image_enabled, text_enabled, before="qwen38-sglang.service", cached=(), note=None):
|
|
d = pathlib.Path(tempfile.mkdtemp(prefix="img-uninst-"))
|
|
if note:
|
|
(d / "image-model-before-turbo").write_text(note + "\n")
|
|
for repo in cached:
|
|
(d / "hf/hub" / ("models--" + repo.replace("/", "--"))).mkdir(parents=True)
|
|
units = d / "etc"
|
|
units.mkdir()
|
|
(units / "qwen38-image.service").write_text("[Service]\n")
|
|
(units / "qwen38-sglang.service").write_text("[Service]\n")
|
|
(d / "lane-before-image").write_text(before + "\n")
|
|
stub = d / "bin"
|
|
stub.mkdir()
|
|
enabled = []
|
|
if image_enabled:
|
|
enabled.append("qwen38-image.service")
|
|
if text_enabled:
|
|
enabled.append("qwen38-sglang.service")
|
|
(stub / "systemctl").write_text(
|
|
"#!/bin/sh\ncase \"$1\" in is-enabled) for u in %s; do [ \"$3\" = \"$u\" ] && exit 0; done; exit 1;; esac\n"
|
|
"echo \"systemctl $*\" >> \"%s/calls\"\n" % (" ".join(enabled) or "none", d))
|
|
(stub / "sudo").write_text("#!/bin/sh\n\"$@\"\n")
|
|
for f in stub.iterdir():
|
|
f.chmod(0o755)
|
|
text = self.UNINSTALL.replace("/etc/systemd/system", str(units))
|
|
script = ('set -euo pipefail\nstep(){ :; }\nACTION=uninstall; UNIT=qwen38-image.service\n'
|
|
f'LANES_UNIT_DIR="{units}"; . "{REPO}/lanes.sh"\n' # the library the script sources
|
|
f'INSTALLED="{units}/qwen38-image.service"; CONFIG_DIR="{d}"; LANE_DIR="{d}/lane"; '
|
|
f'VENV="{d}/lane/venv"; SRC="{d}/lane/sglang"; HF_CACHE="{d}/hf"; MODEL=Qwen/Qwen-Image-2.1\n'
|
|
'IMAGE_MODEL_PIN=Qwen/Qwen-Image-2.1; IMAGE_TURBO_PIN=Qwen/Qwen-Image-2.1-Turbo\n'
|
|
+ text + "\nfi\n")
|
|
rc, out = bash(script, {"HOME": str(d)}, [stub])
|
|
calls = (d / "calls").read_text() if (d / "calls").exists() else ""
|
|
self.dir = d
|
|
return rc, out, calls
|
|
|
|
def test_the_lane_before_images_is_enabled_again(self):
|
|
rc, out, calls = self.run_uninstall(image_enabled=True, text_enabled=False)
|
|
self.assertEqual(rc, 0, out)
|
|
self.assertIn("systemctl enable qwen38-sglang.service", calls)
|
|
self.assertIn("sudo systemctl start qwen38-sglang.service", out)
|
|
|
|
def test_a_box_whose_text_lane_is_enabled_is_left_as_it_is(self):
|
|
rc, out, calls = self.run_uninstall(image_enabled=True, text_enabled=True)
|
|
self.assertNotIn("systemctl enable qwen38-sglang.service", calls)
|
|
|
|
def test_an_image_lane_that_was_not_the_boot_lane_changes_nothing_else(self):
|
|
rc, out, calls = self.run_uninstall(image_enabled=False, text_enabled=True)
|
|
self.assertNotIn("systemctl enable", calls)
|
|
|
|
def test_every_checkpoint_of_the_lane_in_the_cache_is_named(self):
|
|
"""A box that tried the Turbo holds both; the message named the unit's alone."""
|
|
rc, out, calls = self.run_uninstall(image_enabled=False, text_enabled=True,
|
|
cached=("Qwen/Qwen-Image-2.1", "Qwen/Qwen-Image-2.1-Turbo"))
|
|
self.assertEqual(rc, 0, out)
|
|
self.assertIn(f"rm -rf {self.dir}/hf/hub/models--Qwen--Qwen-Image-2.1\n", out)
|
|
self.assertIn(f"rm -rf {self.dir}/hf/hub/models--Qwen--Qwen-Image-2.1-Turbo\n", out)
|
|
self.assertEqual(out.count("rm -rf "), 2, out)
|
|
|
|
def test_the_checkpoint_the_switch_note_names_is_named_too(self):
|
|
"""A unit installed with IMAGE_MODEL= and switched to the Turbo: the note names the
|
|
checkpoint of the unit's own, 31 GB in the cache the message must not forget."""
|
|
rc, out, calls = self.run_uninstall(image_enabled=False, text_enabled=True, note="someone/finetune",
|
|
cached=("Qwen/Qwen-Image-2.1-Turbo", "someone/finetune"))
|
|
self.assertEqual(rc, 0, out)
|
|
self.assertIn(f"rm -rf {self.dir}/hf/hub/models--someone--finetune\n", out)
|
|
self.assertIn(f"rm -rf {self.dir}/hf/hub/models--Qwen--Qwen-Image-2.1-Turbo\n", out)
|
|
|
|
def test_nothing_in_the_cache_nothing_to_name_and_the_switch_note_goes(self):
|
|
rc, out, calls = self.run_uninstall(image_enabled=False, text_enabled=True, note="someone/finetune")
|
|
self.assertEqual(rc, 0, out)
|
|
self.assertNotIn("rm -rf", out)
|
|
self.assertFalse((self.dir / "image-model-before-turbo").exists(), "the switch note outlived the lane")
|
|
|
|
|
|
class TheSwitchNoteAndAnInstall(unittest.TestCase):
|
|
"""./switch-model.sh image-turbo writes down the checkpoint of the unit's own it leaves,
|
|
for `image` to bring back. An install that points the unit at another checkpoint makes
|
|
that note a stale one: `image` would then bring back a checkpoint from before it."""
|
|
BLOCK = block("# A unit installed for another checkpoint", ' fi\n UNIT_PUT="$(unit_put "$RENDER" "$INSTALLED")"')
|
|
|
|
def run_block(self, installed_model, model):
|
|
d = pathlib.Path(tempfile.mkdtemp(prefix="img-note-"))
|
|
(d / "unit").write_text(f"ExecStart=/x/sglang serve --model-path {installed_model} \\\n --port 30020\n")
|
|
(d / "image-model-before-turbo").write_text("someone/finetune\n")
|
|
unit_flag = next(ln for ln in TEXT.splitlines() if ln.startswith("unit_flag(){"))
|
|
script = (f'set -euo pipefail\nINSTALLED="{d}/unit"; CONFIG_DIR="{d}"; MODEL="{model}"\n'
|
|
+ unit_flag + "\n" + self.BLOCK)
|
|
rc, out = bash(script, {"HOME": str(d)})
|
|
return rc, out, (d / "image-model-before-turbo").exists()
|
|
|
|
def test_another_checkpoint_drops_the_note(self):
|
|
rc, out, kept = self.run_block("Qwen/Qwen-Image-2.1-Turbo", "Qwen/Qwen-Image-2.1")
|
|
self.assertEqual(rc, 0, out)
|
|
self.assertFalse(kept)
|
|
|
|
def test_an_update_of_the_same_checkpoint_keeps_it(self):
|
|
rc, out, kept = self.run_block("Qwen/Qwen-Image-2.1-Turbo", "Qwen/Qwen-Image-2.1-Turbo")
|
|
self.assertEqual(rc, 0, out)
|
|
self.assertTrue(kept)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main(verbosity=2)
|