Skip to content

Commit

Permalink
Detect and defer upgrades for lock (#1239)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljguarino authored Sep 5, 2023
1 parent 13722be commit 16c95ba
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
21 changes: 14 additions & 7 deletions apps/core/lib/core/services/rollouts/rollable/versions.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defimpl Core.Rollouts.Rollable, for: [Core.PubSub.VersionCreated, Core.PubSub.VersionUpdated] do
use Core.Rollable.Base
alias Core.Services.{Dependencies, Upgrades, Payments}
alias Core.Schema.{ChartInstallation, TerraformInstallation, User}
alias Core.Schema.{ChartInstallation, TerraformInstallation, User, Installation}

def name(%Core.PubSub.VersionCreated{}), do: "version:created"
def name(%Core.PubSub.VersionUpdated{}), do: "version:updated"
Expand Down Expand Up @@ -35,20 +35,27 @@ defimpl Core.Rollouts.Rollable, for: [Core.PubSub.VersionCreated, Core.PubSub.Ve
Upgrades.create_deferred_update(%{reasons: reason("installation locked")}, vsn.id, inst, user)
end

def process(%{item: version}, %{installation: %{user: user}} = inst) do
case {Dependencies.valid?(version.dependencies, user), Payments.delinquent?(user)} do
{true, false} -> Upgrades.install_version(version, inst)
def process(%{item: version}, %{installation: %{user: user} = repo_inst} = inst) do
case {Dependencies.valid?(version.dependencies, user), Payments.delinquent?(user), locked?(repo_inst)} do
{true, false, false} -> Upgrades.install_version(version, inst)
failed -> Upgrades.create_deferred_update(reasons(failed), version.id, inst, user)
end
end

# defp maybe_ignore_version(q, Core.PubSub.VersionUpdated, _, _), do: q
defp maybe_ignore_version(q, _, mod, id), do: mod.ignore_version(q, id)

defp reasons({false, _}), do: %{reasons: reason("missing dependencies")}
defp reasons({{:locked, _}, _}), do: %{reasons: reason("dependency is locked")}
defp reasons({_, true}), do: %{reasons: reason("your billing account is delinquent")}
defp reasons({false, _, _}), do: %{reasons: reason("missing dependencies")}
defp reasons({{:locked, _}, _, _}), do: %{reasons: reason("dependency is locked")}
defp reasons({_, true, _}), do: %{reasons: reason("your billing account is delinquent")}
defp reasons({_, _, true}), do: %{reasons: reason("installation is locked")}
defp reasons(_), do: %{}

defp locked?(%{id: id}) do
Installation.for_ids([id])
|> Installation.locks()
|> Core.Repo.exists?()
end

defp reason(msg), do: [%{message: msg}]
end
28 changes: 28 additions & 0 deletions apps/core/test/services/rollable/versions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,34 @@ defmodule Core.Rollable.VersionsTest do
assert deferred.version_id == version.id
end

test "it will defer updates if an adjacent installation is locked" do
user = insert(:user, account: build(:account))
%{chart: chart} = chart_version = insert(:version, version: "0.1.0")
installation = insert(:installation, auto_upgrade: true, user: user)
inst = insert(:chart_installation,
installation: installation,
chart: chart,
version: chart_version
)
insert(:terraform_installation, installation: installation, locked: true)

version = insert(:version, version: "0.1.1", chart: chart)
insert(:version_tag, version: version, chart: chart, tag: "latest")

event = %PubSub.VersionCreated{item: version}
{:ok, rollout} = Rollouts.create_rollout(chart.repository_id, event)

{:ok, rolled} = Rollouts.execute(rollout)

assert rolled.status == :finished
assert rolled.count == 1

[deferred] = Core.Repo.all(Core.Schema.DeferredUpdate)

assert deferred.chart_installation_id == inst.id
assert deferred.version_id == version.id
end

test "it will defer updates if a user has promotions enabled" do
user = insert(:user, account: build(:account), upgrade_to: uuid(0))
%{chart: chart} = chart_version = insert(:version, version: "0.1.0")
Expand Down

0 comments on commit 16c95ba

Please sign in to comment.