Skip to content

Commit

Permalink
wip - deployment executor and supervisor
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Zaninotto <[email protected]>
  • Loading branch information
lusergit committed Dec 20, 2024
1 parent 4b1c9b7 commit 2680b1e
Show file tree
Hide file tree
Showing 15 changed files with 660 additions and 13 deletions.
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 Deployments supervisor
Edgehog.Containers.Release.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
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule Edgehog.Containers.Container.Changes.DeployContainerOnDevice do
Ash.Changeset.after_action(changeset, fn _changeset, deployment ->
with {:ok, deployment} <-
Ash.load(deployment, [:device, container: [:image, :networks]]),
{:ok, _image_deployment} <- deploy_image(deployment, tenant),
{:ok, image_deployment} <- deploy_image(deployment, tenant),
{:ok, _device} <-
Devices.send_create_container_request(deployment.device, deployment.container, tenant: tenant) do
{:ok, deployment}
Expand Down
34 changes: 34 additions & 0 deletions backend/lib/edgehog/containers/container/changes/emit_if_new.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#
# This file is part of Edgehog.
#
# Copyright 2024 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.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule Edgehog.Containers.Container.Changes.EmitIfNew do
@moduledoc false
use Ash.Resource.Change

alias Edgehog.PubSub

def change(changeset, _opts, _context) do
Ash.Changeset.after_transaction(changeset, fn _changeset, result ->
with {:ok, deployment} <- result do
PubSub.publish!(:available_container, deployment)
end
end)
end
end
2 changes: 2 additions & 0 deletions backend/lib/edgehog/containers/containers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ defmodule Edgehog.Containers do
resource Image.Deployment do
define :deploy_image, action: :deploy, args: [:image_id, :device_id]
define :fetch_image_deployment, action: :read, get_by_identity: :image_instance
define :image_deployment_sent, action: :sent
define :image_deployment_unpulled, action: :unpulled
define :image_deployment_pulled, action: :pulled
define :image_deployment_errored, action: :errored, args: [:message]
Expand Down Expand Up @@ -168,6 +169,7 @@ defmodule Edgehog.Containers do
resource Network.Deployment do
define :deploy_network, action: :deploy, args: [:network_id, :device_id]
define :fetch_network_deployment, action: :read, get_by_identity: :network_instance
define :network_deployment_sent, action: :sent
define :network_deployment_available, action: :available
define :network_deployment_unavailable, action: :unavailable
define :network_deployment_errored, action: :errored, args: [:message]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# SPDX-License-Identifier: Apache-2.0
#

defmodule Edgehog.Containers.Image.Changes.DeployImageOnDevice do
defmodule Edgehog.Containers.Image.Deployment.Changes.DeployImageOnDevice do
@moduledoc false
use Ash.Resource.Change

Expand Down
36 changes: 36 additions & 0 deletions backend/lib/edgehog/containers/image/changes/emit_if_new.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# This file is part of Edgehog.
#
# Copyright 2024 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.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule Edgehog.Containers.Image.Changes.EmitIfNew do
@moduledoc false
use Ash.Resource.Change

alias Edgehog.PubSub

def change(changeset, _opts, _context) do
Ash.Changeset.after_transaction(changeset, fn _changeset, result ->
with {:ok, deployment} <- result do
PubSub.publish!(:available_image, deployment)
end

result
end)
end
end
5 changes: 4 additions & 1 deletion backend/lib/edgehog/containers/image/deployment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule Edgehog.Containers.Image.Deployment do
domain: Edgehog.Containers,
extensions: [AshGraphql.Resource, AshStateMachine]

alias Edgehog.Containers.Image.Changes
alias Edgehog.Containers.Image.Deployment.Changes

state_machine do
initial_states([:created, :sent])
Expand Down Expand Up @@ -58,6 +58,9 @@ defmodule Edgehog.Containers.Image.Deployment do
change transition_state(:created)
change manage_relationship(:device_id, :device, type: :append)
change Changes.DeployImageOnDevice
end

update :sent do
change transition_state(:sent)
end

Expand Down
34 changes: 34 additions & 0 deletions backend/lib/edgehog/containers/network/changes/emit_if_new.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#
# This file is part of Edgehog.
#
# Copyright 2024 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.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule Edgehog.Containers.Network.Changes.EmitIfNew do
@moduledoc false
use Ash.Resource.Change

alias Edgehog.PubSub

def change(changeset, _opts, _context) do
Ash.Changeset.after_transaction(changeset, fn _changeset, result ->
with {:ok, deployment} <- result do
PubSub.publish!(:available_network, deployment)
end
end)
end
end
3 changes: 3 additions & 0 deletions backend/lib/edgehog/containers/network/deployment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ defmodule Edgehog.Containers.Network.Deployment do

change manage_relationship(:device_id, :device, type: :append)
change Changes.DeployNetworkOnDevice
end

update :sent do
change transition_state(:sent)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule Edgehog.Containers.Release.Deployment.Changes.CreateDeploymentOnDevice
use Ash.Resource.Change

alias Edgehog.Containers
alias Edgehog.Containers.Release.Deployment.CheckerSupervisor
alias Edgehog.Devices

@impl Ash.Resource.Change
Expand All @@ -32,9 +33,17 @@ defmodule Edgehog.Containers.Release.Deployment.Changes.CreateDeploymentOnDevice
Ash.Changeset.after_action(changeset, fn _changeset, deployment ->
with {:ok, deployment} <-
Ash.load(deployment, [:device, release: [:containers, :networks]]),
:ok <- deploy_containers(deployment, tenant),
:ok <- deploy_networks(deployment, tenant),
{:ok, containers, images} <- deploy_containers(deployment, tenant),
{:ok, networks} <- deploy_networks(deployment, tenant),
{:ok, _device} <- Devices.send_create_deployment_request(deployment.device, deployment) do
data = %{
deployment: deployment,
containers: containers,
images: images,
networks: networks
}

CheckerSupervisor.start_checker!(data)
{:ok, deployment}
end
end)
Expand All @@ -44,10 +53,15 @@ defmodule Edgehog.Containers.Release.Deployment.Changes.CreateDeploymentOnDevice
containers = deployment.release.containers
device = deployment.device

Enum.reduce_while(containers, :ok, fn container, _acc ->
Enum.reduce_while(containers, {:ok, [], []}, fn container, {:ok, containers_deployments, image_deployments} ->
case Containers.deploy_container(container.id, device.id, tenant: tenant) do
{:ok, _container_deployment} -> {:cont, :ok}
error -> {:halt, error}
{:ok, container_deployment} ->
new_containers = [container_deployment | containers_deployments]
new_images = [image_deployment.resource | image_deployments]
{:cont, {:ok, new_containers, new_images}}

error ->
{:halt, error}
end
end)
end
Expand All @@ -56,9 +70,9 @@ defmodule Edgehog.Containers.Release.Deployment.Changes.CreateDeploymentOnDevice
containers = deployment.release.networks
device = deployment.device

Enum.reduce_while(containers, :ok, fn network, _acc ->
Enum.reduce_while(containers, {:ok, []}, fn network, {:ok, deployments} ->
case Containers.deploy_network(network.id, device.id, tenant: tenant) do
{:ok, _network_deployment} -> {:cont, :ok}
{:ok, network_deployment} -> {:cont, {:ok, [network_deployment | deployments]}}
error -> {:halt, error}
end
end)
Expand Down
Loading

0 comments on commit 2680b1e

Please sign in to comment.