-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #812 from lusergit/feat/image-deployment
feat(containers): image deployment caching
- Loading branch information
Showing
11 changed files
with
612 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
backend/lib/edgehog/containers/image/changes/deploy_image_on_device.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# | ||
# This file is part of Edgehog. | ||
# | ||
# Copyright 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. | ||
# 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.DeployImageOnDevice do | ||
@moduledoc false | ||
use Ash.Resource.Change | ||
|
||
alias Edgehog.Containers | ||
alias Edgehog.Devices | ||
|
||
@impl Ash.Resource.Change | ||
@spec change(Ash.Changeset.t(), any(), %{:tenant => any(), optional(any()) => any()}) :: | ||
Ash.Changeset.t() | ||
def change(changeset, _opts, context) do | ||
%{tenant: tenant} = context | ||
image = Ash.Changeset.get_argument(changeset, :image) | ||
device = Ash.Changeset.get_argument(changeset, :device) | ||
|
||
Ash.Changeset.after_action(changeset, fn _changeset, image_deployment -> | ||
with {:ok, _device} <- Devices.send_create_image_request(device, image, tenant: tenant) do | ||
Containers.mark_image_deployment_as_sent(image_deployment) | ||
end | ||
end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
# | ||
# This file is part of Edgehog. | ||
# | ||
# Copyright 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. | ||
# 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.Deployment do | ||
@moduledoc false | ||
use Edgehog.MultitenantResource, | ||
domain: Edgehog.Containers, | ||
extensions: [AshGraphql.Resource] | ||
|
||
alias Edgehog.Containers.Image | ||
alias Edgehog.Containers.Image.Changes | ||
alias Edgehog.Devices.Device | ||
|
||
graphql do | ||
type :image_deployment | ||
end | ||
|
||
actions do | ||
defaults [:read, :destroy, create: [:image_id, :device_id, :state]] | ||
|
||
create :deploy do | ||
description """ | ||
Deploys an image on a device. | ||
""" | ||
|
||
argument :image, :struct do | ||
constraints instance_of: Image | ||
allow_nil? false | ||
end | ||
|
||
argument :device, :struct do | ||
constraints instance_of: Device | ||
allow_nil? false | ||
end | ||
|
||
change set_attribute(:state, :created) | ||
change manage_relationship(:image, type: :append) | ||
change manage_relationship(:device, type: :append) | ||
change Changes.DeployImageOnDevice | ||
end | ||
|
||
update :mark_as_sent do | ||
change set_attribute(:state, :sent) | ||
end | ||
|
||
update :mark_as_unpulled do | ||
change set_attribute(:state, :unpulled) | ||
end | ||
|
||
update :mark_as_pulled do | ||
change set_attribute(:state, :pulled) | ||
end | ||
|
||
update :mark_as_errored do | ||
argument :message, :string do | ||
allow_nil? false | ||
end | ||
|
||
change set_attribute(:last_message, arg(:message)) | ||
change set_attribute(:state, :error) | ||
end | ||
end | ||
|
||
attributes do | ||
uuid_primary_key :id | ||
|
||
attribute :last_message, :string | ||
|
||
attribute :state, :atom, | ||
constraints: [ | ||
one_of: [:created, :sent, :pulled, :unpulled, :error] | ||
] | ||
|
||
timestamps() | ||
end | ||
|
||
relationships do | ||
belongs_to :image, Edgehog.Containers.Image do | ||
attribute_type :uuid | ||
end | ||
|
||
belongs_to :device, Edgehog.Devices.Device | ||
end | ||
|
||
calculations do | ||
calculate :ready?, :boolean, expr(state in [:pulled, :unpulled]) | ||
end | ||
|
||
identities do | ||
identity :image_instance, [:image_id, :device_id] | ||
end | ||
|
||
postgres do | ||
table "image_deployments" | ||
|
||
references do | ||
reference :image, on_delete: :delete | ||
reference :device, on_delete: :delete | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.