Class: Daytona::Sandbox
- Inherits:
-
Object
- Object
- Daytona::Sandbox
- Defined in:
- lib/daytona/sandbox.rb
Overview
rubocop:disable Metrics/ClassLength
Constant Summary collapse
- DEFAULT_TIMEOUT =
60
Instance Attribute Summary collapse
-
#auto_archive_interval ⇒ Float
Auto-archive interval in minutes.
-
#auto_delete_interval ⇒ Float
(negative value means disabled, 0 means delete immediately upon stopping).
-
#auto_stop_interval ⇒ Float
Auto-stop interval in minutes (0 means disabled).
-
#backup_created_at ⇒ String
readonly
The creation timestamp of the last backup.
-
#backup_state ⇒ String
readonly
The state of the backup.
-
#build_info ⇒ DaytonaApiClient::BuildInfo
readonly
Build information for the sandbox.
- #code_toolbox ⇒ Daytona::SandboxPythonCodeToolbox, Daytona::SandboxTsCodeToolbox readonly
- #computer_use ⇒ Daytona::ComputerUse readonly
- #config ⇒ Daytona::Config readonly
-
#cpu ⇒ Float
readonly
The CPU quota for the sandbox.
-
#created_at ⇒ String
readonly
The creation timestamp of the sandbox.
-
#daemon_version ⇒ String
readonly
The version of the daemon running in the sandbox.
-
#desired_state ⇒ DaytonaApiClient::SandboxDesiredState
readonly
The desired state of the sandbox.
-
#disk ⇒ Float
readonly
The disk quota for the sandbox.
-
#env ⇒ Hash<String, String>
readonly
Environment variables for the sandbox.
-
#error_reason ⇒ String
readonly
The error reason of the sandbox.
- #fs ⇒ Daytona::FileSystem readonly
- #git ⇒ Daytona::Git readonly
-
#gpu ⇒ Float
readonly
The GPU quota for the sandbox.
-
#id ⇒ String
readonly
The ID of the sandbox.
-
#labels ⇒ Hash<String, String>
Labels for the sandbox.
-
#memory ⇒ Float
readonly
The memory quota for the sandbox.
-
#network_allow_list ⇒ String
readonly
Comma-separated list of allowed CIDR network addresses for the sandbox.
-
#network_block_all ⇒ Boolean
readonly
Whether to block all network access for the sandbox.
-
#organization_id ⇒ String
readonly
The organization ID of the sandbox.
- #process ⇒ Daytona::Process readonly
-
#public ⇒ Boolean
readonly
Whether the sandbox http preview is public.
- #sandbox_api ⇒ DaytonaApiClient::SandboxApi readonly
-
#snapshot ⇒ String
readonly
The snapshot used for the sandbox.
-
#state ⇒ DaytonaApiClient::SandboxState
readonly
The state of the sandbox.
-
#target ⇒ String
readonly
The target environment for the sandbox.
-
#updated_at ⇒ String
readonly
The last update timestamp of the sandbox.
-
#user ⇒ String
readonly
The user associated with the project.
-
#volumes ⇒ Array<DaytonaApiClient::SandboxVolume>
readonly
Array of volumes attached to the sandbox.
Instance Method Summary collapse
-
#archive ⇒ void
Archives the sandbox, making it inactive and preserving its state.
-
#create_lsp_server(language_id:, path_to_project:) ⇒ Daytona::LspServer
Creates a new Language Server Protocol (LSP) server instance.
-
#create_ssh_access(expires_in_minutes) ⇒ DaytonaApiClient::SshAccessDto
Creates an SSH access token for the sandbox.
- #delete ⇒ void
-
#initialize(code_toolbox:, sandbox_dto:, config:, sandbox_api:, get_proxy_toolbox_url:) ⇒ Sandbox
constructor
A new instance of Sandbox.
-
#preview_url(port) ⇒ DaytonaApiClient::PortPreviewUrl
Retrieves the preview link for the sandbox at the specified port.
-
#refresh ⇒ void
Refresh the Sandbox data from the API.
-
#revoke_ssh_access(token) ⇒ void
Revokes an SSH access token for the sandbox.
-
#start(timeout = DEFAULT_TIMEOUT) ⇒ void
Starts the Sandbox and waits for it to be ready.
-
#stop(timeout = DEFAULT_TIMEOUT) ⇒ void
Stops the Sandbox and waits for it to be stopped.
-
#validate_ssh_access(token) ⇒ DaytonaApiClient::SshAccessValidationDto
Validates an SSH access token for the sandbox.
-
#wait_for_sandbox_start(_timeout = DEFAULT_TIMEOUT) ⇒ void
Waits for the Sandbox to reach the ‘started’ state.
Constructor Details
#initialize(code_toolbox:, sandbox_dto:, config:, sandbox_api:, get_proxy_toolbox_url:) ⇒ Sandbox
Returns a new instance of Sandbox.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/daytona/sandbox.rb', line 116 def initialize(code_toolbox:, sandbox_dto:, config:, sandbox_api:, get_proxy_toolbox_url:) # rubocop:disable Metrics/MethodLength process_response(sandbox_dto) @code_toolbox = code_toolbox @config = config @sandbox_api = sandbox_api @get_proxy_toolbox_url = get_proxy_toolbox_url # Create toolbox API clients with dynamic configuration toolbox_api_config = build_toolbox_api_config # Helper to create API client with authentication header create_authenticated_client = lambda do client = DaytonaToolboxApiClient::ApiClient.new(toolbox_api_config) client.default_headers['Authorization'] = "Bearer #{config.api_key || config.jwt_token}" client end process_api = DaytonaToolboxApiClient::ProcessApi.new(create_authenticated_client.call) fs_api = DaytonaToolboxApiClient::FileSystemApi.new(create_authenticated_client.call) git_api = DaytonaToolboxApiClient::GitApi.new(create_authenticated_client.call) lsp_api = DaytonaToolboxApiClient::LspApi.new(create_authenticated_client.call) computer_use_api = DaytonaToolboxApiClient::ComputerUseApi.new(create_authenticated_client.call) @process = Process.new( sandbox_id: id, code_toolbox:, toolbox_api: process_api, get_preview_link: proc { |port| preview_url(port) } ) @fs = FileSystem.new(sandbox_id: id, toolbox_api: fs_api) @git = Git.new(sandbox_id: id, toolbox_api: git_api) @computer_use = ComputerUse.new(sandbox_id: id, toolbox_api: computer_use_api) @lsp_api = lsp_api end |
Instance Attribute Details
#auto_archive_interval ⇒ Float
Returns Auto-archive interval in minutes.
70 71 72 |
# File 'lib/daytona/sandbox.rb', line 70 def auto_archive_interval @auto_archive_interval end |
#auto_delete_interval ⇒ Float
(negative value means disabled, 0 means delete immediately upon stopping)
74 75 76 |
# File 'lib/daytona/sandbox.rb', line 74 def auto_delete_interval @auto_delete_interval end |
#auto_stop_interval ⇒ Float
Returns Auto-stop interval in minutes (0 means disabled).
67 68 69 |
# File 'lib/daytona/sandbox.rb', line 67 def auto_stop_interval @auto_stop_interval end |
#backup_created_at ⇒ String (readonly)
Returns The creation timestamp of the last backup.
64 65 66 |
# File 'lib/daytona/sandbox.rb', line 64 def backup_created_at @backup_created_at end |
#backup_state ⇒ String (readonly)
Returns The state of the backup.
61 62 63 |
# File 'lib/daytona/sandbox.rb', line 61 def backup_state @backup_state end |
#build_info ⇒ DaytonaApiClient::BuildInfo (readonly)
Returns Build information for the sandbox.
80 81 82 |
# File 'lib/daytona/sandbox.rb', line 80 def build_info @build_info end |
#code_toolbox ⇒ Daytona::SandboxPythonCodeToolbox, Daytona::SandboxTsCodeToolbox (readonly)
92 93 94 |
# File 'lib/daytona/sandbox.rb', line 92 def code_toolbox @code_toolbox end |
#computer_use ⇒ Daytona::ComputerUse (readonly)
110 111 112 |
# File 'lib/daytona/sandbox.rb', line 110 def computer_use @computer_use end |
#config ⇒ Daytona::Config (readonly)
95 96 97 |
# File 'lib/daytona/sandbox.rb', line 95 def config @config end |
#cpu ⇒ Float (readonly)
Returns The CPU quota for the sandbox.
40 41 42 |
# File 'lib/daytona/sandbox.rb', line 40 def cpu @cpu end |
#created_at ⇒ String (readonly)
Returns The creation timestamp of the sandbox.
83 84 85 |
# File 'lib/daytona/sandbox.rb', line 83 def created_at @created_at end |
#daemon_version ⇒ String (readonly)
Returns The version of the daemon running in the sandbox.
89 90 91 |
# File 'lib/daytona/sandbox.rb', line 89 def daemon_version @daemon_version end |
#desired_state ⇒ DaytonaApiClient::SandboxDesiredState (readonly)
Returns The desired state of the sandbox.
55 56 57 |
# File 'lib/daytona/sandbox.rb', line 55 def desired_state @desired_state end |
#disk ⇒ Float (readonly)
Returns The disk quota for the sandbox.
49 50 51 |
# File 'lib/daytona/sandbox.rb', line 49 def disk @disk end |
#env ⇒ Hash<String, String> (readonly)
Returns Environment variables for the sandbox.
22 23 24 |
# File 'lib/daytona/sandbox.rb', line 22 def env @env end |
#error_reason ⇒ String (readonly)
Returns The error reason of the sandbox.
58 59 60 |
# File 'lib/daytona/sandbox.rb', line 58 def error_reason @error_reason end |
#fs ⇒ Daytona::FileSystem (readonly)
104 105 106 |
# File 'lib/daytona/sandbox.rb', line 104 def fs @fs end |
#git ⇒ Daytona::Git (readonly)
107 108 109 |
# File 'lib/daytona/sandbox.rb', line 107 def git @git end |
#gpu ⇒ Float (readonly)
Returns The GPU quota for the sandbox.
43 44 45 |
# File 'lib/daytona/sandbox.rb', line 43 def gpu @gpu end |
#id ⇒ String (readonly)
Returns The ID of the sandbox.
10 11 12 |
# File 'lib/daytona/sandbox.rb', line 10 def id @id end |
#labels ⇒ Hash<String, String>
Returns Labels for the sandbox.
25 26 27 |
# File 'lib/daytona/sandbox.rb', line 25 def labels @labels end |
#memory ⇒ Float (readonly)
Returns The memory quota for the sandbox.
46 47 48 |
# File 'lib/daytona/sandbox.rb', line 46 def memory @memory end |
#network_allow_list ⇒ String (readonly)
Returns Comma-separated list of allowed CIDR network addresses for the sandbox.
34 35 36 |
# File 'lib/daytona/sandbox.rb', line 34 def network_allow_list @network_allow_list end |
#network_block_all ⇒ Boolean (readonly)
Returns Whether to block all network access for the sandbox.
31 32 33 |
# File 'lib/daytona/sandbox.rb', line 31 def network_block_all @network_block_all end |
#organization_id ⇒ String (readonly)
Returns The organization ID of the sandbox.
13 14 15 |
# File 'lib/daytona/sandbox.rb', line 13 def organization_id @organization_id end |
#process ⇒ Daytona::Process (readonly)
101 102 103 |
# File 'lib/daytona/sandbox.rb', line 101 def process @process end |
#public ⇒ Boolean (readonly)
Returns Whether the sandbox http preview is public.
28 29 30 |
# File 'lib/daytona/sandbox.rb', line 28 def public @public end |
#sandbox_api ⇒ DaytonaApiClient::SandboxApi (readonly)
98 99 100 |
# File 'lib/daytona/sandbox.rb', line 98 def sandbox_api @sandbox_api end |
#snapshot ⇒ String (readonly)
Returns The snapshot used for the sandbox.
16 17 18 |
# File 'lib/daytona/sandbox.rb', line 16 def snapshot @snapshot end |
#state ⇒ DaytonaApiClient::SandboxState (readonly)
Returns The state of the sandbox.
52 53 54 |
# File 'lib/daytona/sandbox.rb', line 52 def state @state end |
#target ⇒ String (readonly)
Returns The target environment for the sandbox.
37 38 39 |
# File 'lib/daytona/sandbox.rb', line 37 def target @target end |
#updated_at ⇒ String (readonly)
Returns The last update timestamp of the sandbox.
86 87 88 |
# File 'lib/daytona/sandbox.rb', line 86 def updated_at @updated_at end |
#user ⇒ String (readonly)
Returns The user associated with the project.
19 20 21 |
# File 'lib/daytona/sandbox.rb', line 19 def user @user end |
#volumes ⇒ Array<DaytonaApiClient::SandboxVolume> (readonly)
Returns Array of volumes attached to the sandbox.
77 78 79 |
# File 'lib/daytona/sandbox.rb', line 77 def volumes @volumes end |
Instance Method Details
#archive ⇒ void
This method returns an undefined value.
Archives the sandbox, making it inactive and preserving its state. When sandboxes are archived, the entire filesystem state is moved to cost-effective object storage, making it possible to keep sandboxes available for an extended period. The tradeoff between archived and stopped states is that starting an archived sandbox takes more time, depending on its size. Sandbox must be stopped before archiving.
158 159 160 161 |
# File 'lib/daytona/sandbox.rb', line 158 def archive sandbox_api.archive_sandbox(id) refresh end |
#create_lsp_server(language_id:, path_to_project:) ⇒ Daytona::LspServer
Creates a new Language Server Protocol (LSP) server instance. The LSP server provides language-specific features like code completion, diagnostics, and more.
281 282 283 |
# File 'lib/daytona/sandbox.rb', line 281 def create_lsp_server(language_id:, path_to_project:) LspServer.new(language_id:, path_to_project:, toolbox_api: @lsp_api, sandbox_id: id) end |
#create_ssh_access(expires_in_minutes) ⇒ DaytonaApiClient::SshAccessDto
Creates an SSH access token for the sandbox.
206 |
# File 'lib/daytona/sandbox.rb', line 206 def create_ssh_access(expires_in_minutes) = sandbox_api.create_ssh_access(id, { expires_in_minutes: }) |
#delete ⇒ void
This method returns an undefined value.
209 210 211 212 |
# File 'lib/daytona/sandbox.rb', line 209 def delete sandbox_api.delete_sandbox(id) refresh end |
#preview_url(port) ⇒ DaytonaApiClient::PortPreviewUrl
Retrieves the preview link for the sandbox at the specified port. If the port is closed, it will be opened automatically. For private sandboxes, a token is included to grant access to the URL.
228 |
# File 'lib/daytona/sandbox.rb', line 228 def preview_url(port) = sandbox_api.get_port_preview_url(id, port) |
#refresh ⇒ void
This method returns an undefined value.
Refresh the Sandbox data from the API.
233 |
# File 'lib/daytona/sandbox.rb', line 233 def refresh = process_response(sandbox_api.get_sandbox(id)) |
#revoke_ssh_access(token) ⇒ void
This method returns an undefined value.
Revokes an SSH access token for the sandbox.
239 |
# File 'lib/daytona/sandbox.rb', line 239 def revoke_ssh_access(token) = sandbox_api.revoke_ssh_access(id, token:) |
#start(timeout = DEFAULT_TIMEOUT) ⇒ void
This method returns an undefined value.
Starts the Sandbox and waits for it to be ready.
245 246 247 248 249 250 251 |
# File 'lib/daytona/sandbox.rb', line 245 def start(timeout = DEFAULT_TIMEOUT) with_timeout( timeout:, message: "Sandbox #{id} failed to become ready within the #{timeout} seconds timeout period", setup: proc { process_response(sandbox_api.start_sandbox(id)) } ) { wait_for_states(operation: OPERATION_START, target_states: [DaytonaApiClient::SandboxState::STARTED]) } end |
#stop(timeout = DEFAULT_TIMEOUT) ⇒ void
This method returns an undefined value.
Stops the Sandbox and waits for it to be stopped.
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/daytona/sandbox.rb', line 257 def stop(timeout = DEFAULT_TIMEOUT) # rubocop:disable Metrics/MethodLength with_timeout( timeout:, message: "Sandbox #{id} failed to become stopped within the #{timeout} seconds timeout period", setup: proc { sandbox_api.stop_sandbox(id) refresh } ) do wait_for_states( operation: OPERATION_STOP, target_states: [DaytonaApiClient::SandboxState::STOPPED, DaytonaApiClient::SandboxState::DESTROYED] ) end end |
#validate_ssh_access(token) ⇒ DaytonaApiClient::SshAccessValidationDto
Validates an SSH access token for the sandbox.
289 |
# File 'lib/daytona/sandbox.rb', line 289 def validate_ssh_access(token) = sandbox_api.validate_ssh_access(token) |
#wait_for_sandbox_start(_timeout = DEFAULT_TIMEOUT) ⇒ void
This method returns an undefined value.
Waits for the Sandbox to reach the ‘started’ state. Polls the Sandbox status until it reaches the ‘started’ state or encounters an error.
296 297 298 |
# File 'lib/daytona/sandbox.rb', line 296 def wait_for_sandbox_start(_timeout = DEFAULT_TIMEOUT) wait_for_states(operation: OPERATION_START, target_states: [DaytonaApiClient::SandboxState::STARTED]) end |