コンテンツにスキップ

AsyncSandbox

class AsyncSandbox(SandboxDto)

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

属性:

  • fs AsyncFileSystem - ファイルシステム操作インターフェース。
  • git AsyncGit - Git 操作インターフェース。
  • process AsyncProcess - プロセス実行インターフェース。
  • computer_use AsyncComputerUse - デスクトップ自動化のためのコンピューター使用インターフェース。
  • 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 ネットワークアドレスのカンマ区切りリスト。

AsyncSandbox.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 - 言語別のツールボックス実装。

AsyncSandbox.refresh_data

async def refresh_data() -> None

API からサンドボックスのデータを更新します。

:

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

AsyncSandbox.get_user_root_dir

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

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

戻り値:

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

:

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

AsyncSandbox.create_lsp_server

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

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

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

引数:

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

戻り値:

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

:

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

AsyncSandbox.set_labels

@intercept_errors(message_prefix="Failed to set labels: ")
async 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}")

AsyncSandbox.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"
))
async 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")

AsyncSandbox.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"
))
async 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")

AsyncSandbox.delete

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

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

引数:

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

AsyncSandbox.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"
))
async def wait_for_sandbox_start(timeout: Optional[float] = 60) -> None

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

引数:

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

例外:

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

AsyncSandbox.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"
))
async def wait_for_sandbox_stop(timeout: Optional[float] = 60) -> None

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

引数:

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

送出:

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

AsyncSandbox.set_autostop_interval

@intercept_errors(message_prefix="Failed to set auto-stop interval: ")
async 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)

AsyncSandbox.set_auto_archive_interval

@intercept_errors(message_prefix="Failed to set auto-archive interval: ")
async 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)

AsyncSandbox.set_auto_delete_interval

@intercept_errors(message_prefix="Failed to set auto-delete interval: ")
async 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: ")
async def get_preview_link(port: int) -> PortPreviewUrl

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

引数:

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

戻り値:

  • PortPreviewUrl - プレビューリンクのレスポンスオブジェクト。urltoken(プライベートなサンドボックスへのアクセス用)を含みます。

:

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

AsyncSandbox.archive

@intercept_errors(message_prefix="Failed to archive sandbox: ")
async 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
)