Class: Daytona::SnapshotService
- Inherits:
-
Object
- Object
- Daytona::SnapshotService
- Defined in:
- lib/daytona/snapshot_service.rb
Constant Summary collapse
- SNAPSHOTS_FETCH_LIMIT =
200
Class Method Summary collapse
-
.process_image_context(object_storage_api, image) ⇒ Array<String>
Processes the image context by uploading it to object storage.
Instance Method Summary collapse
-
#activate(snapshot) ⇒ Daytona::Snapshot
Activate a snapshot.
-
#create(params, on_logs: nil) ⇒ Daytona::Snapshot
Creates and registers a new snapshot from the given Image definition.
-
#delete(snapshot) ⇒ void
Delete a Snapshot.
-
#get(name) ⇒ Daytona::Snapshot
Get a Snapshot by name.
-
#initialize(snapshots_api:, object_storage_api:, default_region_id: nil) ⇒ SnapshotService
constructor
A new instance of SnapshotService.
-
#list(page: nil, limit: nil) ⇒ Daytona::PaginatedResource
List all Snapshots.
Constructor Details
#initialize(snapshots_api:, object_storage_api:, default_region_id: nil) ⇒ SnapshotService
Returns a new instance of SnapshotService.
12 13 14 15 16 |
# File 'lib/daytona/snapshot_service.rb', line 12 def initialize(snapshots_api:, object_storage_api:, default_region_id: nil) @snapshots_api = snapshots_api @object_storage_api = object_storage_api @default_region_id = default_region_id end |
Class Method Details
.process_image_context(object_storage_api, image) ⇒ Array<String>
Processes the image context by uploading it to object storage
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/daytona/snapshot_service.rb', line 126 def self.process_image_context(object_storage_api, image) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength return [] unless image.context_list && !image.context_list.empty? push_access_creds = object_storage_api.get_push_access object_storage = ObjectStorage.new( endpoint_url: push_access_creds.storage_url, aws_access_key_id: push_access_creds.access_key, aws_secret_access_key: push_access_creds.secret, aws_session_token: push_access_creds.session_token, bucket_name: push_access_creds.bucket ) image.context_list.map do |context| object_storage.upload( context.source_path, push_access_creds.organization_id, context.archive_path ) end end |
Instance Method Details
#activate(snapshot) ⇒ Daytona::Snapshot
Activate a snapshot
120 |
# File 'lib/daytona/snapshot_service.rb', line 120 def activate(snapshot) = Snapshot.from_dto(snapshots_api.activate_snapshot(snapshot.id)) |
#create(params, on_logs: nil) ⇒ Daytona::Snapshot
Creates and registers a new snapshot from the given Image definition.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/daytona/snapshot_service.rb', line 78 def create(params, on_logs: nil) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength create_snapshot_req = DaytonaApiClient::CreateSnapshot.new(name: params.name) if params.image.is_a?(String) create_snapshot_req.image_name = params.image create_snapshot_req.entrypoint = params.entrypoint else create_snapshot_req.build_info = DaytonaApiClient::CreateBuildInfo.new( context_hashes: self.class.process_image_context(object_storage_api, params.image), dockerfile_content: if params.entrypoint params.image.entrypoint(params.entrypoint).dockerfile else params.image.dockerfile end ) end if params.resources create_snapshot_req.cpu = params.resources.cpu create_snapshot_req.gpu = params.resources.gpu create_snapshot_req.memory = params.resources.memory create_snapshot_req.disk = params.resources.disk end create_snapshot_req.region_id = params.region_id || @default_region_id snapshot = snapshots_api.create_snapshot(create_snapshot_req) # Always wait for snapshot to be ready, regardless of on_logs snapshot = wait_for_snapshot(snapshot, on_logs:) if [DaytonaApiClient::SnapshotState::ERROR, DaytonaApiClient::SnapshotState::BUILD_FAILED].include?(snapshot.state) raise Sdk::Error, "Failed to create snapshot #{snapshot.name}, reason: #{snapshot.error_reason}" end Snapshot.from_dto(snapshot) end |
#delete(snapshot) ⇒ void
This method returns an undefined value.
Delete a Snapshot.
53 |
# File 'lib/daytona/snapshot_service.rb', line 53 def delete(snapshot) = snapshots_api.remove_snapshot(snapshot.id) |
#get(name) ⇒ Daytona::Snapshot
Get a Snapshot by name.
64 |
# File 'lib/daytona/snapshot_service.rb', line 64 def get(name) = Snapshot.from_dto(snapshots_api.get_snapshot(name)) |
#list(page: nil, limit: nil) ⇒ Daytona::PaginatedResource
List all Snapshots.
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/daytona/snapshot_service.rb', line 29 def list(page: nil, limit: nil) raise Sdk::Error, 'page must be positive integer' if page && page < 1 raise Sdk::Error, 'limit must be positive integer' if limit && limit < 1 response = snapshots_api.get_all_snapshots(page:, limit:) PaginatedResource.new( total: response.total, page: response.page, total_pages: response.total_pages, items: response.items.map { |snapshot_dto| Snapshot.from_dto(snapshot_dto) } ) end |