Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Format the machine boot script and its test, which main's format check rejects (#746)

Abel Dantasfe5832ee270f
diff --git a/deploy/hosted/machine/switch_machine_boot.py b/deploy/hosted/machine/switch_machine_boot.py
index f5cd6f001..59214a01f 100644
--- a/deploy/hosted/machine/switch_machine_boot.py
+++ b/deploy/hosted/machine/switch_machine_boot.py
@@ -149,7 +149,14 @@ def parse_bundle(raw: str) -> Bundle:
) from None
value = _strict(
value,
- {"version", "installationId", "machineId", "dataVolumeId", "apiEndpoint", "controller"},
+ {
+ "version",
+ "installationId",
+ "machineId",
+ "dataVolumeId",
+ "apiEndpoint",
+ "controller",
+ },
"machine bundle",
)
if value["version"] != 4:
@@ -176,7 +183,9 @@ def parse_bundle(raw: str) -> Bundle:
):
raise BootError("The machine bundle's controller is invalid.")
elif not isinstance(code, str) or not ENROLLMENT_CODE_RE.fullmatch(code):
- raise BootError("The machine bundle holds neither a controller nor an enrollment code.")
+ raise BootError(
+ "The machine bundle holds neither a controller nor an enrollment code."
+ )
return Bundle(
installation_id=_identifier(value["installationId"], "installation id"),
machine_id=machine_id,
@@ -195,7 +204,9 @@ def _user_data(opener: OpenerDirector) -> str:
)
with opener.open(token_request, timeout=5) as response:
token = response.read().decode()
- request = Request(f"{METADATA_URL}/user-data", headers={"X-aws-ec2-metadata-token": token})
+ request = Request(
+ f"{METADATA_URL}/user-data", headers={"X-aws-ec2-metadata-token": token}
+ )
try:
with opener.open(request, timeout=5) as response:
raw = response.read(MAX_JSON_BYTES + 1)
@@ -229,7 +240,9 @@ def read_bundle(
raise
except Exception as error:
if time.monotonic() > deadline:
- raise BootError(f"The instance's user data cannot be read: {error}") from None
+ raise BootError(
+ f"The instance's user data cannot be read: {error}"
+ ) from None
logger.warning("Waiting for instance metadata: %s", error)
sleep(5)
return parse_bundle(raw)
@@ -316,22 +329,35 @@ def prepare_storage(
if signatures or device.get("uuid"):
raise BootError("The data volume is not blank, but holds no filesystem.")
logger.info("Formatting the blank data volume %s", volume_id)
- commands.run(["/usr/sbin/mkfs.ext4", "-q", "-m", "0", "-L", "switch-data", path])
+ commands.run(
+ ["/usr/sbin/mkfs.ext4", "-q", "-m", "0", "-L", "switch-data", path]
+ )
commands.run(["/usr/bin/udevadm", "settle", "--timeout=30"])
device = _data_device(commands, volume_id)
if device is None:
raise BootError("The data volume went away while it was formatted.")
if device.get("fstype") != "ext4":
- raise BootError("The data volume's filesystem is not ext4; refusing to mount it.")
+ raise BootError(
+ "The data volume's filesystem is not ext4; refusing to mount it."
+ )
DATA_MOUNT.mkdir(mode=0o755, exist_ok=True)
mounted = commands.result(
- ["/usr/bin/findmnt", "--mountpoint", str(DATA_MOUNT), "--noheadings", "--output", "SOURCE"]
+ [
+ "/usr/bin/findmnt",
+ "--mountpoint",
+ str(DATA_MOUNT),
+ "--noheadings",
+ "--output",
+ "SOURCE",
+ ]
)
if mounted.returncode == 0:
if os.path.realpath(mounted.stdout.strip()) != os.path.realpath(path):
raise BootError("/data is another device's mountpoint.")
return
- commands.run([SYSTEMD_MOUNT, "--type=ext4", "--options=nodev,nosuid", path, str(DATA_MOUNT)])
+ commands.run(
+ [SYSTEMD_MOUNT, "--type=ext4", "--options=nodev,nosuid", path, str(DATA_MOUNT)]
+ )
def reconcile_marker(data: Path, bundle: Bundle) -> None:
@@ -348,7 +374,9 @@ def reconcile_marker(data: Path, bundle: Bundle) -> None:
if marker.is_symlink():
raise BootError("The data volume's marker is a link.")
if json.loads(marker.read_text()) != wanted:
- raise BootError("The data volume belongs to another machine; refusing to use it.")
+ raise BootError(
+ "The data volume belongs to another machine; refusing to use it."
+ )
return
temporary = data / f"{MARKER_NAME}.new"
temporary.write_text(json.dumps(wanted))
@@ -408,11 +436,14 @@ def start_controller(
# machine: whatever this directory holds was revoked there.
aside = DATA_MOUNT / f"{CONTROLLER_DIR_NAME}.replaced-{int(now())}"
logger.warning(
- "Switch gave a new code; setting the old enrollment aside in %s", aside
+ "Switch gave a new code; setting the old enrollment aside in %s",
+ aside,
)
os.replace(data_dir, aside)
data_dir.mkdir(mode=0o700)
- commands.run(["/usr/bin/chown", f"{config.controller_user}:", str(data_dir)])
+ commands.run(
+ ["/usr/bin/chown", f"{config.controller_user}:", str(data_dir)]
+ )
if code is not None:
# Recorded first: a crash right after enrolling must not leave an
# enrollment the next attempt would take for a revoked one.
diff --git a/deploy/hosted/machine/tests/test_boot.py b/deploy/hosted/machine/tests/test_boot.py
index 887c03ecd..c5570ab0c 100644
--- a/deploy/hosted/machine/tests/test_boot.py
+++ b/deploy/hosted/machine/tests/test_boot.py
@@ -189,10 +189,15 @@ def test_a_blank_volume_is_formatted_and_mounted(monkeypatch, tmp_path):
)
boot.prepare_storage(commands, VOLUME_ID, sleep=lambda _: None)
assert commands.ran("/usr/sbin/mkfs.ext4")[0][-1] == "/dev/nvme1n1"
- assert commands.ran(boot.SYSTEMD_MOUNT)[0][-2:] == ["/dev/nvme1n1", str(tmp_path / "data")]
+ assert commands.ran(boot.SYSTEMD_MOUNT)[0][-2:] == [
+ "/dev/nvme1n1",
+ str(tmp_path / "data"),
+ ]
-def test_a_volume_attached_late_is_waited_for_and_a_formatted_one_kept(monkeypatch, tmp_path):
+def test_a_volume_attached_late_is_waited_for_and_a_formatted_one_kept(
+ monkeypatch, tmp_path
+):
monkeypatch.setattr(boot, "DATA_MOUNT", tmp_path / "data")
commands = FakeCommands(
{
@@ -208,7 +213,10 @@ def test_a_volume_attached_late_is_waited_for_and_a_formatted_one_kept(monkeypat
def test_a_disk_with_unknown_contents_is_never_formatted(monkeypatch, tmp_path):
monkeypatch.setattr(boot, "DATA_MOUNT", tmp_path / "data")
commands = FakeCommands(
- {"lsblk": [(0, LSBLK_BLANK)], "wipefs": [(0, '{"signatures": [{"type": "xfs"}]}')]}
+ {
+ "lsblk": [(0, LSBLK_BLANK)],
+ "wipefs": [(0, '{"signatures": [{"type": "xfs"}]}')],
+ }
)
with pytest.raises(boot.BootError, match="not blank"):
boot.prepare_storage(commands, VOLUME_ID, sleep=lambda _: None)
@@ -225,7 +233,9 @@ def test_the_volume_marker_refuses_another_machines_disk(tmp_path):
boot.reconcile_marker(tmp_path, boot.parse_bundle(other))
-def test_a_fresh_machine_enrolls_then_runs_its_agents_as_users_of_their_own(monkeypatch, tmp_path):
+def test_a_fresh_machine_enrolls_then_runs_its_agents_as_users_of_their_own(
+ monkeypatch, tmp_path
+):
monkeypatch.setattr(boot, "DATA_MOUNT", tmp_path)
commands = FakeCommands({" status ": [(1, "Not enrolled.")]})
boot.start_controller(commands, CONFIG, boot.parse_bundle(bundle()))
@@ -239,7 +249,9 @@ def test_a_fresh_machine_enrolls_then_runs_its_agents_as_users_of_their_own(monk
"/opt/switch/controller/bin/switch-agent-controller",
]
assert enroll[enroll.index("--code") + 1] == CODE
- assert enroll[enroll.index("--data-dir") + 1] == str(tmp_path / ".switch-controller")
+ assert enroll[enroll.index("--data-dir") + 1] == str(
+ tmp_path / ".switch-controller"
+ )
[setup] = commands.ran("install-service")
assert setup[:2] == [
"/opt/switch/node/bin/node",
@@ -268,10 +280,14 @@ def test_a_new_code_sets_a_revoked_enrollment_aside(monkeypatch, tmp_path):
monkeypatch.setattr(boot, "DATA_MOUNT", tmp_path)
(tmp_path / ".switch-controller").mkdir()
(tmp_path / ".switch-controller" / "controller.db").write_text("old")
- (tmp_path / ".switch-controller-code").write_text(hashlib.sha256(b"swce_old").hexdigest())
+ (tmp_path / ".switch-controller-code").write_text(
+ hashlib.sha256(b"swce_old").hexdigest()
+ )
commands = FakeCommands({" status ": [(0, "Controller: old")]})
boot.start_controller(commands, CONFIG, boot.parse_bundle(bundle()), now=lambda: 7)
- assert (tmp_path / ".switch-controller.replaced-7" / "controller.db").read_text() == "old"
+ assert (
+ tmp_path / ".switch-controller.replaced-7" / "controller.db"
+ ).read_text() == "old"
assert not (tmp_path / ".switch-controller" / "controller.db").exists()
assert len(commands.ran("enroll")) == 1
assert (tmp_path / ".switch-controller-code").read_text() != hashlib.sha256(
@@ -279,7 +295,9 @@ def test_a_new_code_sets_a_revoked_enrollment_aside(monkeypatch, tmp_path):
).hexdigest()
-def test_an_enrollment_made_with_this_code_is_kept_when_the_boot_runs_again(monkeypatch, tmp_path):
+def test_an_enrollment_made_with_this_code_is_kept_when_the_boot_runs_again(
+ monkeypatch, tmp_path
+):
monkeypatch.setattr(boot, "DATA_MOUNT", tmp_path)
first = FakeCommands({" status ": [(1, "Not enrolled.")]})
boot.start_controller(first, CONFIG, boot.parse_bundle(bundle()))