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

chore(containers): deployment state machine #818

Draft
wants to merge 3 commits into
base: feature/application-management
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions backend/lib/edgehog/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ defmodule Edgehog.Application do
{Finch, name: EdgehogFinch},
# Start the UpdateCampaigns supervisor
Edgehog.UpdateCampaigns.Supervisor,
# Start the Deployer supervisor
Edgehog.Containers.Deployment.Supervisor,
# Start the Tenant Reconciler Supervisor
{Edgehog.Tenants.Reconciler.Supervisor, tenant_to_trigger_url_fun: tenant_to_trigger_url_fun},
# Start the Endpoint (http/https)
Expand Down
23 changes: 17 additions & 6 deletions backend/lib/edgehog/containers/containers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,21 @@ defmodule Edgehog.Containers do

resource Edgehog.Containers.Deployment do
define :deploy, action: :deploy, args: [:release_id, :device_id]
define :send_deploy_request, action: :send_deploy_request, args: [:deployment]
# define :send_deploy_request, action: :send_deploy_request, args: [:deployment]
define :fetch_deployment, action: :read, get_by: [:id]
define :deployment_set_status, action: :set_status, args: [:status, :message]

define :fetch_deployment_by_identity, action: :read, get_by_identity: :release_instance
define :delete_deployment, action: :destroy
define :deployment_update_status, action: :update_status
# define :deployment_update_resources_state, action: :update_resources_state
define :deployments_with_release, action: :filter_by_release, args: [:release_id]
define :run_ready_actions, action: :run_ready_actions

define :mark_deployment_as_sent, action: :mark_as_sent
define :mark_deployment_as_deleting, action: :mark_as_deleting
define :mark_deployment_as_errored, action: :mark_as_errored, args: [:message]
define :mark_deployment_as_started, action: :mark_as_started
define :mark_deployment_as_starting, action: :mark_as_starting
define :mark_deployment_as_stopped, action: :mark_as_stopped
define :mark_deployment_as_stopping, action: :mark_as_stopping
end

resource Edgehog.Containers.Image do
Expand Down Expand Up @@ -144,7 +151,9 @@ defmodule Edgehog.Containers do
args: [:container_id]
end

resource Edgehog.Containers.Network
resource Edgehog.Containers.Network do
define :fetch_network, action: :read, get_by: [:id]
end

resource Edgehog.Containers.Network.Deployment do
define :deploy_network, action: :deploy, args: [:network, :device]
Expand All @@ -155,7 +164,9 @@ defmodule Edgehog.Containers do
define :mark_network_deployment_as_errored, action: :mark_as_errored, args: [:message]
end

resource Edgehog.Containers.Volume
resource Edgehog.Containers.Volume do
define :fetch_volume, action: :read, get_by: [:id]
end

resource Edgehog.Containers.Volume.Deployment do
define :deploy_volume, action: :deploy, args: [:volume, :device]
Expand Down
76 changes: 52 additions & 24 deletions backend/lib/edgehog/containers/deployment.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2024 SECO Mind Srl
# Copyright 2024 - 2025 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -25,9 +25,10 @@ defmodule Edgehog.Containers.Deployment do
extensions: [AshGraphql.Resource]

alias Edgehog.Containers.Deployment.Changes
alias Edgehog.Containers.Deployment.Types.DeploymentState
alias Edgehog.Containers.Deployment.Types.ResourcesState
alias Edgehog.Containers.ManualActions
alias Edgehog.Containers.Release
alias Edgehog.Containers.Types.DeploymentStatus
alias Edgehog.Containers.Validations.IsUpgrade
alias Edgehog.Containers.Validations.SameApplication

Expand All @@ -36,7 +37,11 @@ defmodule Edgehog.Containers.Deployment do
end

actions do
defaults [:read, :destroy, create: [:device_id, :release_id, :status, :message]]
defaults [
:read,
:destroy,
create: [:device_id, :release_id, :state, :last_error_message, :resources_state]
]

create :deploy do
description """
Expand All @@ -52,7 +57,7 @@ defmodule Edgehog.Containers.Deployment do

change manage_relationship(:device_id, :device, type: :append)

change Changes.CreateDeploymentOnDevice
change Changes.StartDeployer
end

update :start do
Expand Down Expand Up @@ -87,15 +92,6 @@ defmodule Edgehog.Containers.Deployment do
manual ManualActions.RunReadyActions
end

action :send_deploy_request do
argument :deployment, :struct do
constraints instance_of: __MODULE__
allow_nil? false
end

run ManualActions.SendDeployRequest
end

update :upgrade_release do
argument :target, :uuid do
allow_nil? false
Expand All @@ -107,18 +103,41 @@ defmodule Edgehog.Containers.Deployment do
manual ManualActions.SendDeploymentUpgrade
end

update :set_status do
accept [:status, :message]
update :mark_as_sent do
change set_attribute(:state, :sent)
end

update :update_status do
change Changes.CheckImages
change Changes.CheckNetworks
change Changes.CheckVolumes
change Changes.CheckContainers
change Changes.CheckDeployments
update :mark_as_started do
change set_attribute(:state, :started)
end

update :mark_as_starting do
require_atomic? false

change Changes.MarkAsStarting
end

update :mark_as_stopped do
change set_attribute(:state, :stopped)
end

update :mark_as_stopping do
require_atomic? false

change Changes.MarkAsStopping
end

update :mark_as_errored do
argument :message, :string do
allow_nil? false
end

change set_attribute(:last_error_message, arg(:message))
change set_attribute(:state, :error)
end

update :mark_as_deleting do
change set_attribute(:state, :deleting)
end

read :filter_by_release do
Expand All @@ -131,13 +150,17 @@ defmodule Edgehog.Containers.Deployment do
attributes do
uuid_primary_key :id

attribute :status, DeploymentStatus do
allow_nil? false
attribute :resources_state, ResourcesState do
default :initial
public? true
end

attribute :state, DeploymentState do
default :created
public? true
end

attribute :message, :string do
attribute :last_error_message, :string do
public? true
end

Expand All @@ -159,6 +182,11 @@ defmodule Edgehog.Containers.Deployment do
end
end

calculations do
calculate :ready?, :boolean, expr(state in [:started, :starting, :stopped, :stopping])
calculate :resources_ready?, :boolean, expr(resources_state == :ready)
end

identities do
identity :release_instance, [:device_id, :release_id]
end
Expand Down

This file was deleted.

This file was deleted.

54 changes: 0 additions & 54 deletions backend/lib/edgehog/containers/deployment/changes/check_images.ex

This file was deleted.

Loading
Loading