Class: Daytona::SnapshotService

Inherits:
Object
  • Object
show all
Defined in:
lib/daytona/snapshot_service.rb

Constant Summary collapse

SNAPSHOTS_FETCH_LIMIT =
200

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(snapshots_api:, object_storage_api:) ⇒ SnapshotService

Returns a new instance of SnapshotService.

Parameters:

  • The snapshots API client

  • The object storage API client



11
12
13
14
# File 'lib/daytona/snapshot_service.rb', line 11

def initialize(snapshots_api:, object_storage_api:)
  @snapshots_api = snapshots_api
  @object_storage_api = object_storage_api
end

Class Method Details

.process_image_context(object_storage_api, image) ⇒ Array<String>

Processes the image context by uploading it to object storage

Parameters:

  • The Image instance

Returns:

  • List of context hashes stored in object storage



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/daytona/snapshot_service.rb', line 121

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

Parameters:

  • The snapshot instance

Returns:



115
# File 'lib/daytona/snapshot_service.rb', line 115

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.

Examples:

image = Image.debianSlim('3.12').pipInstall('numpy')
params = CreateSnapshotParams.new(name: 'my-snapshot', image: image)
snapshot = daytona.snapshot.create(params) do |chunk|
  print chunk
end

Parameters:

  • Parameters for snapshot creation

  • (defaults to: nil)

    Callback proc handling snapshot creation logs

Returns:

  • The created snapshot



76
77
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
# File 'lib/daytona/snapshot_service.rb', line 76

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

  snapshot = snapshots_api.create_snapshot(create_snapshot_req)

  snapshot = stream_logs(snapshot, on_logs:) if 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.

Examples:

daytona = Daytona::Daytona.new
snapshot = daytona.snapshot.get("demo")
daytona.snapshot.delete(snapshot)
puts "Snapshot deleted"

Parameters:

  • Snapshot to delete



51
# File 'lib/daytona/snapshot_service.rb', line 51

def delete(snapshot) = snapshots_api.remove_snapshot(snapshot.id)

#get(name) ⇒ Daytona::Snapshot

Get a Snapshot by name.

Examples:

daytona = Daytona::Daytona.new
snapshot = daytona.snapshot.get("demo")
puts "#{snapshot.name} (#{snapshot.image_name})"

Parameters:

  • Name of the Snapshot to get

Returns:

  • The Snapshot object



62
# File 'lib/daytona/snapshot_service.rb', line 62

def get(name) = Snapshot.from_dto(snapshots_api.get_snapshot(name))

#list(page: nil, limit: nil) ⇒ Daytona::PaginatedResource

List all Snapshots.

Examples:

daytona = Daytona::Daytona.new
response = daytona.snapshot.list(page: 1, limit: 10)
snapshots.items.each { |snapshot| puts "#{snapshot.name} (#{snapshot.image_name})" }

Parameters:

  • (defaults to: nil)
  • (defaults to: nil)

Returns:

  • Paginated list of all Snapshots

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/daytona/snapshot_service.rb', line 27

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