Skip to content

Commit

Permalink
Correctly tag instances and ensure deployment is live before bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Oct 17, 2024
1 parent f7ebdef commit c82d689
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 52 deletions.
22 changes: 22 additions & 0 deletions lib/uplink/instances.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule Uplink.Instances do
alias Uplink.Cache

def mark(state, install_id, instance_name) do
Cache.transaction(
[keys: [{:install, install_id, state}]],
fn ->
Cache.get_and_update(
{:install, install_id, state},
fn current_value ->
executing_instances =
if current_value,
do: current_value ++ [instance_name],
else: [instance_name]

{current_value, Enum.uniq(executing_instances)}
end
)
end
)
end
end
18 changes: 2 additions & 16 deletions lib/uplink/packages/install/execute.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Uplink.Packages.Install.Execute do

alias Uplink.Repo
alias Uplink.Cache
alias Uplink.Instances

alias Uplink.Clients.LXD
alias Uplink.Clients.Instellar
Expand Down Expand Up @@ -96,22 +97,7 @@ defmodule Uplink.Packages.Install.Execute do
)
end)

Cache.transaction(
[keys: [{:install, state.install.id, "executing"}]],
fn ->
Cache.get_and_update(
{:install, state.install.id, "executing"},
fn current_value ->
executing_instances =
if current_value,
do: current_value ++ [instance.slug],
else: [instance.slug]

{current_value, Enum.uniq(executing_instances)}
end
)
end
)
Instances.mark("executing", state.install.id, instance.slug)

case event_name do
"upgrade" ->
Expand Down
31 changes: 19 additions & 12 deletions lib/uplink/packages/instance/bootstrap.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule Uplink.Packages.Instance.Bootstrap do

alias Uplink.Repo
alias Uplink.Cache
alias Uplink.Instances

alias Uplink.Members.Actor

alias Uplink.Packages
Expand Down Expand Up @@ -46,28 +48,33 @@ defmodule Uplink.Packages.Instance.Bootstrap do

%Actor{} = actor = Repo.get(Actor, actor_id)

%Install{} =
install =
%{install: %Install{deployment: deployment}} =
state =
Install
|> preload([:deployment])
|> Repo.get(install_id)
|> Packages.build_install_state(actor)

state = Packages.build_install_state(install, actor)

state
|> handle_placement(instance_params)
|> case do
%{client: _, lxd_project_name: _, placement: _} = updated_state ->
handle_provisioning(updated_state, instance_params)
if deployment.current_state == "live" do
state
|> handle_placement(instance_params)
|> case do
%{client: _, lxd_project_name: _, placement: _} = updated_state ->
handle_provisioning(updated_state, instance_params)

error ->
handle_error(state, error, instance_params)
error ->
handle_error(state, error, instance_params)
end
else
{:snooze, 10}
end
end

defp handle_placement(state, %{"slug" => instance_name}) do
defp handle_placement(%{install: install} = state, %{"slug" => instance_name}) do
placement_name = Placement.name(instance_name)

Instances.mark("executing", install.id, instance_name)

Cache.transaction([keys: [{:available_nodes, placement_name}]], fn ->
place_instance(state, instance_name)
end)
Expand Down
13 changes: 2 additions & 11 deletions lib/uplink/packages/instance/install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Uplink.Packages.Instance.Install do

alias Uplink.Repo
alias Uplink.Cache
alias Uplink.Instances

alias Uplink.Clients.LXD
alias Uplink.Clients.Caddy
Expand Down Expand Up @@ -92,17 +93,7 @@ defmodule Uplink.Packages.Instance.Install do
|> Formation.add_package_and_restart_lxd_instance(formation_instance)
|> case do
{:ok, add_package_output} ->
Cache.transaction([keys: [{:install, install_id, "completed"}]], fn ->
Cache.get_and_update(
{:install, install_id, "completed"},
fn current_value ->
completed_instances =
if current_value, do: current_value ++ [name], else: [name]

{current_value, Enum.uniq(completed_instances)}
end
)
end)
Instances.mark("completed", install_id, name)

Caddy.schedule_config_reload(install)

Expand Down
15 changes: 2 additions & 13 deletions lib/uplink/packages/instance/upgrade.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Uplink.Packages.Instance.Upgrade do

alias Uplink.Repo
alias Uplink.Cache
alias Uplink.Instances

alias Uplink.Packages
alias Uplink.Packages.Install
Expand Down Expand Up @@ -133,19 +134,7 @@ defmodule Uplink.Packages.Instance.Upgrade do
|> Formation.lxd_upgrade_alpine_package(formation_instance)
|> case do
{:ok, upgrade_package_output} ->
Cache.transaction([keys: [{:install, install.id, "completed"}]], fn ->
Cache.get_and_update(
{:install, install.id, "completed"},
fn current_value ->
completed_instances =
if current_value,
do: current_value ++ [formation_instance.slug],
else: [formation_instance.slug]

{current_value, Enum.uniq(completed_instances)}
end
)
end)
Instances.mark("completed", install.id, formation_instance.slug)

Caddy.schedule_config_reload(install)

Expand Down
20 changes: 20 additions & 0 deletions test/scenarios/deployment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@ defmodule Uplink.Scenarios.Deployment do
{:ok, deployment} =
Packages.get_or_create_deployment(app, @deployment_params)

{:ok, %{resource: preparing_deployment}} =
Packages.transition_deployment_with(deployment, actor, "prepare")

{:ok, %{resource: deployment}} =
Packages.transition_deployment_with(
preparing_deployment,
actor,
"complete"
)

{:ok, install} =
Packages.create_install(deployment, %{
"installation_id" => 1,
Expand Down Expand Up @@ -245,6 +255,16 @@ defmodule Uplink.Scenarios.Deployment do
@deployment_params_with_package_size
)

{:ok, %{resource: preparing_deployment}} =
Packages.transition_deployment_with(deployment, actor, "prepare")

{:ok, %{resource: deployment}} =
Packages.transition_deployment_with(
preparing_deployment,
actor,
"complete"
)

{:ok, install} =
Packages.create_install(deployment, %{
"installation_id" => 1,
Expand Down

0 comments on commit c82d689

Please sign in to comment.