Skip to content

Commit bb98ffd

Browse files
author
benbot
committed
Adds automatic volume id discovery and matching
1 parent 744d2fe commit bb98ffd

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

lib/flame/fly_backend.ex

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ defmodule FLAME.FlyBackend do
105105
memory_mb: 4096,
106106
boot_timeout: 30_000,
107107
runner_node_basename: node_base,
108-
services: []
108+
services: [],
109+
mounts: []
109110
}
110111

111112
provided_opts =
@@ -168,8 +169,65 @@ defmodule FLAME.FlyBackend do
168169
{result, div(micro, 1000)}
169170
end
170171

172+
defp get_volume_id(%FlyBackend{ mounts: mounts } = state) when is_list(mounts) do
173+
case mounts do
174+
[] ->
175+
{nil, 0}
176+
mounts ->
177+
{vols, time} = get_volumes(state)
178+
179+
case vols do
180+
[] ->
181+
{:error, "no volumes to mount"}
182+
all_vols ->
183+
vols =
184+
all_vols
185+
|> Enum.filter(fn vol ->
186+
vol["attached_machine_id"] == nil
187+
end)
188+
189+
volume_ids_by_name =
190+
vols
191+
|> Enum.group_by(fn vol ->
192+
vol["name"]
193+
end)
194+
|> Enum.map(fn {name, vols} ->
195+
{name, Enum.map(vols, fn vol -> vol["id"] end)}
196+
end)
197+
198+
{new_mounts, leftover_vols} = Enum.reduce(mounts, {[], volume_ids_by_name}, fn mount, {new_mounts, leftover_vols} ->
199+
case Enum.find(leftover_vols, fn {name, ids} -> name == mount.name end) do
200+
nil ->
201+
raise ArgumentError, "not enough fly volumes with the name \"#{mount.name}\" to a FLAME child"
202+
{_, [id | rest]} ->
203+
{new_mount, leftover_vols} = Enum.split(rest, 1)
204+
{new_mounts ++ [%{mount | volume: id}], leftover_vols}
205+
end
206+
end)
207+
208+
{new_mounts, time}
209+
end
210+
end
211+
end
212+
defp get_volume_id(_) do
213+
raise ArgumentError, "expected a list of mounts"
214+
end
215+
216+
defp get_volumes(%FlyBackend{} = state) do
217+
{vols, get_vols_time} = with_elapsed_ms(fn ->
218+
Req.get!("#{state.host}/v1/apps/#{state.app}/volumes",
219+
connect_options: [timeout: state.boot_timeout],
220+
retry: false,
221+
auth: {:bearer, state.token},
222+
)
223+
end)
224+
225+
{vols.body, get_vols_time}
226+
end
227+
171228
@impl true
172229
def remote_boot(%FlyBackend{parent_ref: parent_ref} = state) do
230+
{mounts, volume_validate_time} = get_volume_id(state)
173231
{req, req_connect_time} =
174232
with_elapsed_ms(fn ->
175233
Req.post!("#{state.host}/v1/apps/#{state.app}/machines",
@@ -181,7 +239,7 @@ defmodule FLAME.FlyBackend do
181239
name: "#{state.app}-flame-#{rand_id(20)}",
182240
config: %{
183241
image: state.image,
184-
mounts: state.mounts,
242+
mounts: mounts,
185243
guest: %{
186244
cpu_kind: state.cpu_kind,
187245
cpus: state.cpus,
@@ -197,7 +255,7 @@ defmodule FLAME.FlyBackend do
197255
)
198256
end)
199257

200-
remaining_connect_window = state.boot_timeout - req_connect_time
258+
remaining_connect_window = state.boot_timeout - req_connect_time - volume_validate_time
201259

202260
case req.body do
203261
%{"id" => id, "instance_id" => instance_id, "private_ip" => ip} ->

lib/flame/fly_backend/mounts.ex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
defmodule FLAME.FlyBackend.Mounts do
22
@derive Jason.Encoder
33
defstruct name: nil,
4-
path: nil
5-
# extend_threshold_percent: 0,
6-
# add_size_gb: 0,
7-
# size_gb_limit: 0
4+
path: nil,
5+
volume: nil,
6+
extend_threshold_percent: 0,
7+
add_size_gb: 0,
8+
size_gb_limit: 0
89
end

0 commit comments

Comments
 (0)