-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement dedicated cloud console types
This provisions a plural console in its own cluster managed entirely by a plural stack. Since it's cost-inefficient, only allow enterprise accounts to use these.
- Loading branch information
1 parent
6f1ef89
commit 45c23b3
Showing
19 changed files
with
299 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,12 @@ defmodule Core.Services.Cloud.Workflow do | |
|
||
require Logger | ||
|
||
def sync(%ConsoleInstance{type: :dedicated, external_id: id} = inst) when is_binary(id) do | ||
with {:ok, project_id} <- Poller.project(), | ||
{:ok, repo_id} <- Poller.repository(), | ||
do: Console.update_stack(dedicated_console(), id, Configuration.stack_attributes(inst, project_id, repo_id)) | ||
end | ||
|
||
def sync(%ConsoleInstance{external_id: id} = instance) when is_binary(id) do | ||
instance = Repo.preload(instance, [:cluster, :postgres]) | ||
Console.update_service(console(), id, %{ | ||
|
@@ -39,6 +45,7 @@ defmodule Core.Services.Cloud.Workflow do | |
case down(acc) do | ||
{:ok, %ConsoleInstance{status: :pending} = inst} -> {:halt, inst} | ||
{:ok, %ConsoleInstance{status: :database_deleted} = inst} -> {:halt, inst} | ||
{:ok, %ConsoleInstance{status: :stack_deleted} = inst} -> {:halt, inst} | ||
{:ok, inst} -> {:cont, inst} | ||
err -> | ||
:timer.sleep(:timer.seconds(10)) | ||
|
@@ -49,6 +56,36 @@ defmodule Core.Services.Cloud.Workflow do | |
|> finalize(:down) | ||
end | ||
|
||
defp up(%ConsoleInstance{status: :pending, type: :dedicated} = inst) do | ||
with {:ok, id} <- Poller.project(), | ||
{:ok, repo_id} <- Poller.repository(), | ||
{:ok, stack_id} <- Console.create_stack(dedicated_console(), Configuration.stack_attributes(inst, id, repo_id)) do | ||
ConsoleInstance.changeset(inst, %{ | ||
instance_status: %{stack: true}, | ||
status: :stack_created, | ||
external_id: stack_id | ||
}) | ||
|> Repo.update() | ||
end | ||
end | ||
|
||
defp up(%ConsoleInstance{type: :dedicated, status: :stack_created, external_id: id} = inst) do | ||
Enum.reduce_while(0..120, inst, fn _, inst -> | ||
dedicated_console() | ||
|> Console.stack(id) | ||
|> case do | ||
%{"status" => "SUCCESSFUL"} -> | ||
{:ok, inst} = ConsoleInstance.changeset(inst, %{status: :provisioned}) | ||
|> Repo.update() | ||
{:halt, inst} | ||
status -> | ||
Logger.info "stack not ready yet, sleeping: #{inspect(status)}" | ||
:timer.sleep(:timer.minutes(1)) | ||
{:cont, inst} | ||
end | ||
end) | ||
end | ||
|
||
defp up(%ConsoleInstance{status: :deployment_created, url: url} = inst) do | ||
case {DNS.resolve(url), DNS.resolve(url, :cname)} do | ||
{{:ok, [_ | _]}, _} -> mark_provisioned(inst) | ||
|
@@ -97,6 +134,13 @@ defmodule Core.Services.Cloud.Workflow do | |
end | ||
end | ||
|
||
defp down(%ConsoleInstance{type: :dedicated, instance_status: %{stack: true}, external_id: id} = inst) do | ||
with {:ok, _} <- Console.delete_stack(dedicated_console(), id) do | ||
ConsoleInstance.changeset(inst, %{status: :stack_deleted}) | ||
|> Repo.update() | ||
end | ||
end | ||
|
||
defp down(%ConsoleInstance{instance_status: %{svc: false, db: true}, configuration: conf, postgres: pg} = inst) do | ||
with {:ok, pid} <- connect(pg), | ||
{:ok, _} <- Postgrex.query(pid, "DROP DATABASE IF EXISTS #{conf.database}", []), | ||
|
@@ -139,6 +183,18 @@ defmodule Core.Services.Cloud.Workflow do | |
|> execute(extract: :inst) | ||
end | ||
|
||
defp finalize(%ConsoleInstance{type: :dedicated} = inst, :down) do | ||
start_transaction() | ||
|> add_operation(:inst, fn _ -> Repo.delete(inst) end) | ||
|> add_operation(:sa, fn %{inst: %{name: name}} -> | ||
case Users.get_user_by_email("#{name}[email protected]") do | ||
%User{} = u -> Repo.delete(u) | ||
_ -> {:ok, nil} | ||
end | ||
end) | ||
|> execute(extract: :inst) | ||
end | ||
|
||
defp finalize(inst, _) do | ||
Logger.warn "failed to finalize console instance: #{inst.id}" | ||
{:ok, inst} | ||
|
@@ -172,4 +228,6 @@ defmodule Core.Services.Cloud.Workflow do | |
end | ||
|
||
defp console(), do: Console.new(Core.conf(:console_url), Core.conf(:console_token)) | ||
|
||
defp dedicated_console(), do: Console.new(Core.conf(:console_url), Core.conf(:dedicated_console_token)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
apps/core/priv/repo/migrations/20240917232002_add_dedicated_console.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
defmodule Core.Repo.Migrations.AddDedicatedConsole do | ||
use Ecto.Migration | ||
|
||
def change do | ||
alter table(:console_instances) do | ||
add :type, :integer, default: 0 | ||
end | ||
end | ||
end |
Oops, something went wrong.