Skip to content

Commit 11541e2

Browse files
feat: filter deployment targets by operation type (#1015)
Add operation-aware filtering for deployment campaign targets Previously, the deployment target computation logic treated all operations the same way, selecting all deployable devices based only on system model compatibility. This ensures that lifecycle operations can only be performed on devices where the release is already deployed, preventing invalid operations on devices without the target release. Signed-off-by: Omar <omar.brbutovic@secomind.com>
1 parent abc18f3 commit 11541e2

File tree

3 files changed

+503
-2
lines changed

3 files changed

+503
-2
lines changed

backend/lib/edgehog/deployment_campaigns/deployment_campaign/changes/compute_deployment_targets.ex

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ defmodule Edgehog.DeploymentCampaigns.DeploymentCampaign.Changes.ComputeDeployme
2323
Change to compute deployment targets.
2424
2525
It starts from the channel of the campaign and finds the suitable targets for the campaign.
26+
27+
For deploy operations, all devices in the channel that match the system model requirements
28+
are included as targets.
29+
30+
For other operations (start, stop, upgrade, delete), only devices that already have the
31+
specified release deployed are included as targets.
2632
"""
2733
use Ash.Resource.Change
2834

@@ -37,9 +43,11 @@ defmodule Edgehog.DeploymentCampaigns.DeploymentCampaign.Changes.ComputeDeployme
3743

3844
with {:ok, release} <- fetch_release(changeset, tenant),
3945
{:ok, channel} <- fetch_channel(changeset, tenant) do
46+
operation_type = Ash.Changeset.get_attribute(changeset, :operation_type)
4047
channel = Ash.load!(channel, deployable_devices: [release: release])
4148

42-
deployable_devices = channel.deployable_devices
49+
deployable_devices =
50+
filter_devices_by_operation(channel.deployable_devices, operation_type, release, tenant)
4351

4452
if Enum.empty?(deployable_devices) do
4553
changeset
@@ -104,4 +112,24 @@ defmodule Edgehog.DeploymentCampaigns.DeploymentCampaign.Changes.ComputeDeployme
104112
end
105113

106114
defp start_campaign_executor(transaction_result), do: transaction_result
115+
116+
# For deploy operations, return all deployable devices
117+
defp filter_devices_by_operation(devices, :deploy, _release, _tenant) do
118+
devices
119+
end
120+
121+
# For other operations (start, stop, upgrade, delete), filter devices that have the release deployed
122+
defp filter_devices_by_operation(devices, _operation_type, release, tenant) do
123+
devices_with_release = Ash.load!(devices, [:application_deployments], tenant: tenant)
124+
125+
Enum.filter(devices_with_release, fn device ->
126+
has_release_deployed?(device, release)
127+
end)
128+
end
129+
130+
defp has_release_deployed?(device, release) do
131+
Enum.any?(device.application_deployments, fn deployment ->
132+
deployment.release_id == release.id
133+
end)
134+
end
107135
end

0 commit comments

Comments
 (0)