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): refactor deployment #754

Draft
wants to merge 17 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 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
69 changes: 0 additions & 69 deletions backend/lib/edgehog/containers/changes/create_default_network.ex

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#
# 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.DeployContainerOnDevice do
@moduledoc false
use Ash.Resource.Change

alias Edgehog.Containers
alias Edgehog.Devices

@impl Ash.Resource.Change
def change(changeset, _opts, context) do
%{tenant: tenant} = context

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, _device} <-
Devices.send_create_container_request(deployment.device, deployment.container, tenant: tenant) do
{:ok, deployment}
end
end)
end

def deploy_image(deployment, tenant) do
image = deployment.container.image
device = deployment.device

Containers.deploy_image(image.id, device.id, tenant: tenant)
end
end
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
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ defmodule Edgehog.Containers.Container do
through Edgehog.Containers.ContainerNetwork
public? true
end

many_to_many :devices, Edgehog.Devices.Device do
through Edgehog.Containers.Container.Deployment
join_relationship :container_deployments
end
end

calculations do
Expand Down
118 changes: 118 additions & 0 deletions backend/lib/edgehog/containers/container/deployment.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#
# 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.Deployment do
@moduledoc false
use Edgehog.MultitenantResource,
domain: Edgehog.Containers,
extensions: [AshGraphql.Resource, AshStateMachine]

alias Edgehog.Containers.Container.Changes

state_machine do
initial_states([:init, :sent])
default_initial_state(:init)

transitions do
transition(:received, from: :sent, to: :received)
transition(:created, from: :received, to: :created)
transition(:stopped, from: :created, to: :stopped)
transition(:running, from: :stopped, to: :running)
transition(:errored, from: :*, to: :error)
end
end

graphql do
type :container_deployment
end

actions do
defaults [:read, :destroy]

create :deploy do
description """
Deploys an image on a device, the status according to device triggers.
"""

accept [:container_id]

argument :device_id, :id do
allow_nil? false
end

change manage_relationship(:device_id, :device, type: :append)
change Changes.DeployContainerOnDevice
change transition_state(:sent)
end

update :received do
change transition_state(:received)
end

update :created do
change transition_state(:created)
end

update :stopped do
change transition_state(:stopped)
end

update :running do
change transition_state(:running)
end

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

change set_attribute(:last_message, arg(:message))
change transition_state(:error)
end
end

attributes do
uuid_primary_key :id

timestamps()
end

relationships do
belongs_to :container, Edgehog.Containers.Container do
attribute_type :uuid
public? true
end

belongs_to :device, Edgehog.Devices.Device
end

identities do
identity :container_instance, [:container_id, :device_id]
end

postgres do
table "container_deployments"

references do
reference :container, on_delete: :delete
reference :device, on_delete: :delete
end
end
end
26 changes: 26 additions & 0 deletions backend/lib/edgehog/containers/container/status.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# 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.Status do
@moduledoc false
use Ash.Type.Enum, values: [:received, :created, :running, :stopped]

def graphql_type(_), do: :application_container_status
end
Loading
Loading