Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query DNS before finalizing cloud instances #1357

Merged
merged 1 commit into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions apps/core/lib/core/schema/console_instance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ defmodule Core.Schema.ConsoleInstance do
)
end

def provisioned(query \\ __MODULE__) do
from(c in query, where: c.status == ^:provisioned)
end

def ordered(query \\ __MODULE__, order \\ [asc: :name]) do
from(c in query, order_by: ^order)
end
Expand Down
24 changes: 17 additions & 7 deletions apps/core/lib/core/services/cloud/workflow.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule Core.Services.Cloud.Workflow do

Enum.reduce_while(0..10, instance, fn _, acc ->
case up(acc) do
{:ok, %ConsoleInstance{status: :deployment_created} = inst} -> {:halt, inst}
{:ok, %ConsoleInstance{status: :provisioned} = inst} -> {:halt, inst}
{:ok, inst} -> {:cont, inst}
err ->
:timer.sleep(:timer.seconds(1))
Expand All @@ -35,20 +35,28 @@ defmodule Core.Services.Cloud.Workflow do
def deprovision(%ConsoleInstance{} = instance) do
instance = Repo.preload(instance, [:postgres, :cluster])

Enum.reduce_while(0..10, instance, fn _, acc ->
Enum.reduce_while(0..20, instance, fn _, acc ->
case down(acc) do
{:ok, %ConsoleInstance{status: :pending} = inst} -> {:halt, inst}
{:ok, %ConsoleInstance{status: :database_deleted} = inst} -> {:halt, inst}
{:ok, inst} -> {:cont, inst}
err ->
:timer.sleep(:timer.seconds(1))
:timer.sleep(:timer.seconds(10))
Logger.error "failed to transition deprovisioning console: #{inspect(err)}"
{:cont, acc}
end
end)
|> finalize(:down)
end

defp up(%ConsoleInstance{status: :deployment_created, url: url} = inst) do
case {DNS.resolve(url), DNS.resolve(url, :cname)} do
{{:ok, [_ | _]}, _} -> mark_provisioned(inst)
{_, {:ok, [_ | _]}} -> mark_provisioned(inst)
{{:error, err}, _} -> {:error, "cannot resolve #{url}: #{inspect(err)}"}
end
end

defp up(%ConsoleInstance{status: :pending, postgres: pg, configuration: conf} = inst) do
with {:ok, pid} <- connect(pg),
{:ok, _} <- Postgrex.query(pid, "CREATE DATABASE #{conf.database}", []),
Expand Down Expand Up @@ -115,10 +123,7 @@ defmodule Core.Services.Cloud.Workflow do

defp down(inst), do: {:ok, inst}

defp finalize(%ConsoleInstance{status: :deployment_created} = inst, :up) do
ConsoleInstance.changeset(inst, %{status: :provisioned})
|> Repo.update()
end
defp finalize(%ConsoleInstance{status: :provisioned} = inst, :up), do: {:ok, inst}

defp finalize(%ConsoleInstance{status: :database_deleted, cluster: cluster, postgres: pg} = inst, :down) do
start_transaction()
Expand Down Expand Up @@ -161,5 +166,10 @@ defmodule Core.Services.Cloud.Workflow do
end
defp userinfo(_), do: %{}

defp mark_provisioned(inst) do
ConsoleInstance.changeset(inst, %{status: :provisioned})
|> Repo.update()
end

defp console(), do: Console.new(Core.conf(:console_url), Core.conf(:console_token))
end
Loading