Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 66 additions & 4 deletions lib/flame/fly_backend.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ defmodule FLAME.FlyBackend do
:cpu_kind,
:cpus,
:gpu_kind,
:mounts,
:memory_mb,
:image,
:app,
Expand All @@ -73,6 +74,7 @@ defmodule FLAME.FlyBackend do
cpus: nil,
memory_mb: nil,
gpu_kind: nil,
mounts: [],
image: nil,
services: [],
app: nil,
Expand All @@ -86,7 +88,7 @@ defmodule FLAME.FlyBackend do
runner_private_ip: nil,
runner_node_name: nil

@valid_opts ~w(app image token host cpu_kind cpus memory_mb gpu_kind boot_timeout env terminator_sup log services)a
@valid_opts ~w(app image token host cpu_kind cpus mounts memory_mb gpu_kind boot_timeout env terminator_sup log services)a

@impl true
def init(opts) do
Expand All @@ -103,7 +105,8 @@ defmodule FLAME.FlyBackend do
memory_mb: 4096,
boot_timeout: 30_000,
runner_node_basename: node_base,
services: []
services: [],
mounts: []
}

provided_opts =
Expand Down Expand Up @@ -166,8 +169,66 @@ defmodule FLAME.FlyBackend do
{result, div(micro, 1000)}
end

defp get_volume_id(%FlyBackend{ mounts: mounts } = state) when is_list(mounts) do
case mounts do
[] ->
{nil, 0}
mounts ->
{vols, time} = get_volumes(state)

case vols do
[] ->
{:error, "no volumes to mount"}
all_vols ->
vols =
all_vols
|> Enum.filter(fn vol ->
vol["attached_machine_id"] == nil
and vol["state"] == "created"
end)

volume_ids_by_name =
vols
|> Enum.group_by(fn vol ->
vol["name"]
end)
|> Enum.map(fn {name, vols} ->
{name, Enum.map(vols, fn vol -> vol["id"] end)}
end)

{new_mounts, leftover_vols} = Enum.reduce(mounts, {[], volume_ids_by_name}, fn mount, {new_mounts, leftover_vols} ->
case Enum.find(leftover_vols, fn {name, ids} -> name == mount.name end) do
nil ->
raise ArgumentError, "not enough fly volumes with the name \"#{mount.name}\" to a FLAME child"
{_, [id | rest]} ->
{new_mount, leftover_vols} = Enum.split(rest, 1)
{new_mounts ++ [%{mount | volume: id}], leftover_vols}
end
end)

{new_mounts, time}
end
end
end
defp get_volume_id(_) do
raise ArgumentError, "expected a list of mounts"
end

defp get_volumes(%FlyBackend{} = state) do
{vols, get_vols_time} = with_elapsed_ms(fn ->
Req.get!("#{state.host}/v1/apps/#{state.app}/volumes",
connect_options: [timeout: state.boot_timeout],
retry: false,
auth: {:bearer, state.token},
)
end)

{vols.body, get_vols_time}
end

@impl true
def remote_boot(%FlyBackend{parent_ref: parent_ref} = state) do
{mounts, volume_validate_time} = get_volume_id(state)
{req, req_connect_time} =
with_elapsed_ms(fn ->
Req.post!("#{state.host}/v1/apps/#{state.app}/machines",
Expand All @@ -179,11 +240,12 @@ defmodule FLAME.FlyBackend do
name: "#{state.app}-flame-#{rand_id(20)}",
config: %{
image: state.image,
mounts: mounts,
guest: %{
cpu_kind: state.cpu_kind,
cpus: state.cpus,
memory_mb: state.memory_mb,
gpu_kind: state.gpu_kind
gpu_kind: state.gpu_kind,
},
auto_destroy: true,
restart: %{policy: "no"},
Expand All @@ -194,7 +256,7 @@ defmodule FLAME.FlyBackend do
)
end)

remaining_connect_window = state.boot_timeout - req_connect_time
remaining_connect_window = state.boot_timeout - req_connect_time - volume_validate_time

case req.body do
%{"id" => id, "instance_id" => instance_id, "private_ip" => ip} ->
Expand Down
9 changes: 9 additions & 0 deletions lib/flame/fly_backend/mounts.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule FLAME.FlyBackend.Mounts do
@derive Jason.Encoder
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey, you should be aware that FLAME is not using Jason module for last versions it is using FLAME.Parser.JSON which is a wrapper which detects the version you are using and sets Jason or :json parsers.

defstruct name: nil,
path: nil,
volume: nil,
extend_threshold_percent: 0,
add_size_gb: 0,
size_gb_limit: 0
end