コンテンツにスキップ

サンドボックス

class Sandbox(SandboxDto)

Daytona のサンドボックスを表します。

属性:

  • fs FileSystem - ファイルシステム操作インターフェース。
  • git Git - Git 操作インターフェース。
  • process Process - プロセス実行インターフェース。
  • computer_use ComputerUse - デスクトップ自動化のためのコンピューター使用インターフェース。
  • id str - サンドボックスの一意の識別子。
  • organization_id str - サンドボックスの組織 ID。
  • snapshot str - サンドボックスの作成に使用された Daytona のスナップショット。
  • user str - サンドボックス内で稼働する OS ユーザー。
  • env Dict[str, str] - サンドボックスで設定される環境変数。
  • labels Dict[str, str] - サンドボックスに付与されたカスタムラベル。
  • public bool - サンドボックスが公開アクセス可能かどうか。
  • target str - サンドボックスが実行されるランナーのターゲット(リージョン)。
  • cpu int - サンドボックスに割り当てられた CPU 数。
  • gpu int - サンドボックスに割り当てられた GPU 数。
  • memory int - サンドボックスに割り当てられたメモリ量(GiB)。
  • disk int - サンドボックスに割り当てられたディスク容量(GiB)。
  • state SandboxState - サンドボックスの現在の状態(例: “started”、“stopped”)。
  • error_reason str - サンドボックスがエラー状態の場合のエラーメッセージ。
  • backup_state SandboxBackupStateEnum - サンドボックスのバックアップの現在の状態。
  • backup_created_at str - バックアップが作成された日時。
  • auto_stop_interval int - 自動停止の間隔(分)。
  • auto_archive_interval int - 自動アーカイブの間隔(分)。
  • auto_delete_interval int - 自動削除の間隔(分)。
  • runner_domain str - サンドボックスのランナーのドメイン名。
  • volumes List[str] - サンドボックスに接続されたボリューム。
  • build_info str - 動的ビルドから作成された場合のサンドボックスのビルド情報。
  • created_at str - サンドボックスが作成された日時。
  • updated_at str - サンドボックスが最後に更新された日時。
  • network_block_all bool - サンドボックスのすべてのネットワークアクセスをブロックするかどうか。
  • network_allow_list str - サンドボックスで許可される CIDR ネットワークアドレスのカンマ区切りリスト。

Sandbox.__init__

def __init__(sandbox_dto: SandboxDto, sandbox_api: SandboxApi,
toolbox_api: ToolboxApi, code_toolbox: SandboxCodeToolbox)

新しいサンドボックスインスタンスを初期化します。

引数:

  • id str - サンドボックスの一意の識別子。
  • instance SandboxInstance - 基盤となるサンドボックスインスタンス。
  • sandbox_api SandboxApi - サンドボックス操作用の API クライアント。
  • toolbox_api ToolboxApi - ツールボックス操作用の API クライアント。
  • code_toolbox SandboxCodeToolbox - 言語固有のツールボックス実装。

Sandbox.refresh_data

def refresh_data() -> None

API からサンドボックスのデータをリフレッシュします。

:

sandbox.refresh_data()
print(f"Sandbox {sandbox.id}:")
print(f"State: {sandbox.state}")
print(f"Resources: {sandbox.cpu} CPU, {sandbox.memory} GiB RAM")

Sandbox.get_user_root_dir

@intercept_errors(message_prefix="Failed to get sandbox root directory: ")
def get_user_root_dir() -> str

サンドボックス内でログイン中のユーザーのルートディレクトリパスを取得します。

戻り値:

  • str - ログイン中のユーザー用サンドボックスのルートディレクトリの絶対パス。

:

root_dir = sandbox.get_user_root_dir()
print(f"Sandbox root: {root_dir}")

Sandbox.create_lsp_server

def create_lsp_server(language_id: LspLanguageId,
path_to_project: str) -> LspServer

新しい Language Server Protocol (LSP) サーバーインスタンスを作成します。

LSP サーバーは、コード補完、診断などの言語固有の機能を提供します。

引数:

  • language_id LspLanguageId - 言語サーバーの種類(例: LspLanguageId.PYTHON)。
  • path_to_project str - プロジェクトのルートディレクトリへのパス。相対パスはユーザーの ルートディレクトリを基準に解決されます。

戻り値:

  • LspServer - 指定した言語向けに構成された新しい LSP サーバーインスタンス。

:

lsp = sandbox.create_lsp_server("python", "workspace/project")

Sandbox.set_labels

@intercept_errors(message_prefix="Failed to set labels: ")
def set_labels(labels: Dict[str, str]) -> Dict[str, str]

サンドボックスにラベルを設定します。

ラベルはサンドボックスを整理・識別するために使用できるキーと値のペアです。

引数:

  • labels Dict[str, str] - サンドボックスのラベルを表すキーと値のペアのディクショナリ。

戻り値:

Dict[str, str]: 更新後のサンドボックスのラベルを含むディクショナリ。

:

new_labels = sandbox.set_labels({
"project": "my-project",
"environment": "development",
"team": "backend"
})
print(f"Updated labels: {new_labels}")

Sandbox.start

@intercept_errors(message_prefix="Failed to start sandbox: ")
@with_timeout(error_message=lambda self, timeout: (
f"Sandbox {self.id} failed to start within the {timeout} seconds timeout period"
))
def start(timeout: Optional[float] = 60)

サンドボックスを起動し、準備完了になるまで待機します。

引数:

  • timeout Optional[float] - 待機する最大時間(秒)。0 はタイムアウトなしを意味します。デフォルトは 60 秒。

送出:

  • DaytonaError - タイムアウトが負の場合、またはサンドボックスの起動に失敗/タイムアウトした場合。

:

sandbox = daytona.get_current_sandbox("my-sandbox")
sandbox.start(timeout=40) # 最大 40 秒待機
print("Sandbox started successfully")

Sandbox.stop

@intercept_errors(message_prefix="Failed to stop sandbox: ")
@with_timeout(error_message=lambda self, timeout: (
f"Sandbox {self.id} failed to stop within the {timeout} seconds timeout period"
))
def stop(timeout: Optional[float] = 60)

サンドボックスを停止し、完全に停止するまで待機します。

引数:

  • timeout Optional[float] - 待機する最大時間(秒)。0 はタイムアウトなしを意味します。デフォルトは 60 秒。

送出:

  • DaytonaError - タイムアウトが負の場合、またはサンドボックスの停止に失敗/タイムアウトした場合。

:

sandbox = daytona.get_current_sandbox("my-sandbox")
sandbox.stop()
print("Sandbox stopped successfully")

Sandbox.delete

@intercept_errors(message_prefix="Failed to remove sandbox: ")
def delete(timeout: Optional[float] = 60) -> None

サンドボックスを削除します。

引数:

  • timeout Optional[float] - サンドボックス削除のタイムアウト(秒)。0 はタイムアウトなしを意味します。 デフォルトは 60 秒。

Sandbox.wait_for_sandbox_start

@intercept_errors(
message_prefix="Failure during waiting for sandbox to start: ")
@with_timeout(error_message=lambda self, timeout: (
f"Sandbox {self.id} failed to become ready within the {timeout} seconds timeout period"
))
def wait_for_sandbox_start(timeout: Optional[float] = 60) -> None

サンドボックスが「started」状態になるまで待機します。サンドボックスのステータスをポーリングし、 「started」状態に達するか、エラーが発生するか、タイムアウトするまで待機します。

引数:

  • timeout Optional[float] - 待機する最大時間(秒)。0 はタイムアウトなしを意味します。デフォルトは 60 秒。

送出:

  • DaytonaError - タイムアウトが負の場合、またはサンドボックスの起動に失敗/タイムアウトした場合。

Sandbox.wait_for_sandbox_stop

@intercept_errors(
message_prefix="Failure during waiting for sandbox to stop: ")
@with_timeout(error_message=lambda self, timeout: (
f"Sandbox {self.id} failed to become stopped within the {timeout} seconds timeout period"
))
def wait_for_sandbox_stop(timeout: Optional[float] = 60) -> None

サンドボックスが「stopped」状態になるまで待機します。サンドボックスのステータスをポーリングし、 「stopped」状態に達するか、エラーが発生するか、タイムアウトするまで待機します。サンドボックスが停止するまで最長 60 秒待機します。

引数:

  • timeout Optional[float] - 待機する最大時間(秒)。0 はタイムアウトなしを意味します。デフォルトは 60 秒。

送出:

  • DaytonaError - タイムアウトが負の場合、またはサンドボックスの停止に失敗/タイムアウトした場合。

Sandbox.set_autostop_interval

@intercept_errors(message_prefix="Failed to set auto-stop interval: ")
def set_autostop_interval(interval: int) -> None

サンドボックスの自動停止間隔を設定します。

サンドボックスは、指定した間隔のあいだアイドル状態(新しいイベントなし)が続くと自動的に停止します。 イベントには、SDK を介したサンドボックスとの状態変更や操作が含まれます。 サンドボックスのプレビュー経由の操作は含まれません。

引数:

  • interval int - 自動停止までの非アクティブ時間(分)。 0 に設定すると自動停止を無効化します。デフォルトは 15。

送出:

  • DaytonaError - interval が負の場合

:

# 1時間後に自動停止
sandbox.set_autostop_interval(60)
# または自動停止を無効化
sandbox.set_autostop_interval(0)

Sandbox.set_auto_archive_interval

@intercept_errors(message_prefix="Failed to set auto-archive interval: ")
def set_auto_archive_interval(interval: int) -> None

サンドボックスの自動アーカイブ間隔を設定します。

サンドボックスは、指定した間隔のあいだ連続して停止状態が続くと自動的にアーカイブされます。

引数:

  • interval int - 連続して停止してから自動アーカイブされるまでの分数。 0 に設定すると最大間隔になります。デフォルトは 7 日。

送出:

  • DaytonaError - interval が負の場合

:

# 1時間後に自動アーカイブ
sandbox.set_auto_archive_interval(60)
# または最大間隔を使用
sandbox.set_auto_archive_interval(0)

Sandbox.set_auto_delete_interval

@intercept_errors(message_prefix="Failed to set auto-delete interval: ")
def set_auto_delete_interval(interval: int) -> None

サンドボックスの自動削除間隔を設定します。

サンドボックスは、指定した間隔のあいだ連続して停止状態が続くと自動的に削除されます。

引数:

  • interval int - 連続して停止してから自動削除されるまでの分数。 負の値に設定すると自動削除を無効化します。0 に設定すると停止時に即時削除します。 既定では自動削除は無効です。

:

# 1時間後に自動削除
sandbox.set_auto_delete_interval(60)
# または停止時に即時削除
sandbox.set_auto_delete_interval(0)
# または自動削除を無効化
sandbox.set_auto_delete_interval(-1)
@intercept_errors(message_prefix="Failed to get preview link: ")
def get_preview_link(port: int) -> PortPreviewUrl

指定したポートのサンドボックスのプレビューリンクを取得します。ポートが閉じている場合は 自動的に開かれます。プライベートなサンドボックスでは、URL にアクセス権を与える トークンが含まれます。

引数:

  • port int - プレビューリンクを開くポート。

返り値:

  • PortPreviewUrl - プレビューリンクのレスポンスオブジェクト。url と(プライベートなサンドボックスにアクセスするための)token を含みます。

:

preview_link = sandbox.get_preview_link(3000)
print(f"Preview URL: {preview_link.url}")
print(f"Token: {preview_link.token}")

Sandbox.archive

@intercept_errors(message_prefix="Failed to archive sandbox: ")
def archive() -> None

サンドボックスをアーカイブし、非アクティブ化して状態を保持します。サンドボックスが アーカイブされると、ファイルシステム全体の状態はコスト効率の高いオブジェクトストレージに移動され、 長期間にわたりサンドボックスを利用可能な状態に保つことができます。アーカイブ状態と 停止状態のトレードオフは、アーカイブされたサンドボックスの起動には、そのサイズに応じて より時間がかかる点です。アーカイブする前にサンドボックスは停止している必要があります。

リソース

@dataclass
class Resources()

サンドボックスのリソース設定。

属性:

  • cpu Optional[int] - 割り当てる CPU コア数。
  • memory Optional[int] - 割り当てるメモリ容量(GiB)。
  • disk Optional[int] - 割り当てるディスク容量(GiB)。
  • gpu Optional[int] - 割り当てる GPU 数。

:

resources = Resources(
cpu=2,
memory=4, # 4GiB RAM
disk=20, # 20GiB disk
gpu=1
)
params = CreateSandboxFromImageParams(
image=Image.debian_slim("3.12"),
language="python",
resources=resources
)