nix-builder-autoscaler/agent/nix_builder_autoscaler/runtime/base.py

50 lines
1.5 KiB
Python
Raw Normal View History

2026-02-27 11:59:16 +01:00
"""Abstract base class for runtime adapters."""
from __future__ import annotations
from abc import ABC, abstractmethod
class RuntimeError(Exception):
"""Base error for runtime adapter failures.
Attributes:
category: Normalized error category for retry/classification logic.
"""
def __init__(self, message: str, category: str = "unknown") -> None:
super().__init__(message)
self.category = category
class RuntimeAdapter(ABC):
"""Interface for compute runtime backends (EC2, fake, etc.)."""
@abstractmethod
def launch_instance(
self, slot_id: str, user_data: str, *, nested_virtualization: bool = False
) -> str:
"""Launch an instance for slot_id. Return instance_id.
When nested_virtualization is True, an on-demand instance is launched using
the on-demand launch template. When False (default), a spot instance is launched.
"""
2026-02-27 11:59:16 +01:00
@abstractmethod
def describe_instance(self, instance_id: str) -> dict:
"""Return normalized instance info dict.
Keys: state, tailscale_ip (or None), launch_time.
"""
@abstractmethod
def terminate_instance(self, instance_id: str) -> None:
"""Terminate the instance."""
@abstractmethod
def list_managed_instances(self) -> list[dict]:
"""Return list of instances tagged ManagedBy=nix-builder-autoscaler.
Each entry has instance_id, state, slot_id (from AutoscalerSlot tag).
"""