Skip to content

Commit

Permalink
fix!(containers): remove ready status
Browse files Browse the repository at this point in the history
- `:ready` and `:stopped` are redundant statuses, moved to using only
  `:stopped` status;
- the `check_deployment` check when the deployment is available sets the
  deployment status tio the device deployment status

Signed-off-by: Luca Zaninotto <[email protected]>
  • Loading branch information
lusergit committed Nov 26, 2024
1 parent 15344fd commit a1ca665
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ defmodule Edgehog.Containers.Deployment.Changes.CheckDeployments do

with {:ok, :created_containers} <- Ash.Changeset.fetch_argument_or_change(changeset, :status),
{:ok, deployment} <- Ash.load(deployment, :device),
{:ok, available_deployments} <-
{:ok, available_deployments_statuses} <-
Devices.available_deployments(deployment.device, tenant: tenant) do
available_deployments =
Enum.map(available_deployments, & &1.id)
deployment_status =
Enum.find(available_deployments_statuses, &(&1.id == deployment.id))

if deployment.id in available_deployments do
if deployment_status do
changeset
|> Ash.Changeset.change_attribute(:status, :ready)
|> Ash.Changeset.change_attribute(:status, deployment_status.status)
|> Ash.Changeset.after_transaction(fn _changeset, transaction_result ->
with {:ok, deployment} <- transaction_result do
Containers.run_ready_actions(deployment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ defmodule Edgehog.Containers.Deployment.Changes.CheckImages do
|> Enum.reject(&(&1 in available_images_ids))

if missing_images == [] do
Ash.Changeset.change_attribute(changeset, :status, :pulled_images)
Ash.Changeset.change_attribute(changeset, :status, :created_images)
else
changeset
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ defmodule Edgehog.Containers.Deployment.Changes.CheckNetworks do
%{tenant: tenant} = context
deployment = changeset.data

with {:ok, :pulled_images} <- Ash.Changeset.fetch_argument_or_change(changeset, :status),
with {:ok, :created_images} <- Ash.Changeset.fetch_argument_or_change(changeset, :status),
{:ok, deployment} <-
Ash.load(deployment, [:device, release: [containers: [:networks]]], reuse_values?: true),
{:ok, available_networks} <-
Expand Down
9 changes: 4 additions & 5 deletions backend/lib/edgehog/containers/types/deployment_status.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ defmodule Edgehog.Containers.Types.DeploymentStatus do
stopping: "The deploymen is stopping.",
created: "The deployment has been received by the backend and will be sent to the device.",
sent: "All the necessary resources have been sent to the device.",
ready: "The deployment is ready on the device. All necessary resources are available.",
# TODO: these are internal states that should not be exposed.
# Remove when reimplementing the deployment and its status as a state machine
pulled_images: "The device is currently pulling the necessary images for the deployment.",
created_networks: "The device is setting up the networks necessary for the deployment.",
created_containers: "The device is setting up the containers necessary for the deployment.",
created_deployment: "The device is setting up the release of the the deployment."
created_images: "The device has received the necessary image descriptions for the deployment.",
created_networks: "The device has received all the network descriptions necessary for the deployment.",
created_containers: "The device has received all the container descriptions necessary for the deployment.",
created_deployment: "The device has received the release description of the the deployment."
]

def graphql_type(_), do: :application_deployment_status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ defmodule Edgehog.Triggers.Handler.ManualActions.HandleTrigger do
@ota_response "io.edgehog.devicemanager.OTAResponse"
@system_info "io.edgehog.devicemanager.SystemInfo"

@initial_statuses [
:created,
:sent,
:created_images,
:created_networks,
:created_containers,
:created_deployment
]

@impl Ash.Resource.Actions.Implementation
def run(input, _opts, _context) do
realm_name = input.arguments.realm_name
Expand Down Expand Up @@ -198,8 +207,14 @@ defmodule Edgehog.Triggers.Handler.ManualActions.HandleTrigger do
# Skip Stopping if already Stopped
{:ok, deployment}

_ ->
{_, "Error"} ->
# Errors have precedence
Containers.deployment_set_status(deployment, status, message, tenant: tenant)

_ ->
if deployment.status in @initial_statuses,
do: Containers.deployment_update_status(deployment, tenant: tenant),
else: Containers.deployment_set_status(deployment, status, message, tenant: tenant)
end
end
end
Expand All @@ -210,10 +225,15 @@ defmodule Edgehog.Triggers.Handler.ManualActions.HandleTrigger do
status = event.value

with {:ok, deployment} <- Containers.fetch_deployment(deployment_id, tenant: tenant) do
if status == nil do
Containers.delete_deployment(deployment)
else
Containers.deployment_set_status(deployment, status, deployment.message, tenant: tenant)
cond do
status == nil ->
Containers.delete_deployment(deployment)

deployment.status in @initial_statuses ->
Containers.deployment_update_status(deployment, tenant: tenant)

true ->
Containers.deployment_set_status(deployment, status, deployment.message, tenant: tenant)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ defmodule EdgehogWeb.Controllers.AstarteTriggerControllerTest do
|> response(200)

deployment = Ash.get!(Deployment, deployment.id, tenant: tenant)
assert deployment.status == :ready
assert deployment.status == :stopped
end

test "AvailableContainers triggers update deployment status", context do
Expand Down Expand Up @@ -638,7 +638,7 @@ defmodule EdgehogWeb.Controllers.AstarteTriggerControllerTest do
|> response(200)

deployment = Ash.get!(Deployment, deployment.id, tenant: tenant)
assert deployment.status == :ready
assert deployment.status == :stopped
end

test "AvailableDeployments triggers update deployment status", context do
Expand Down

0 comments on commit a1ca665

Please sign in to comment.