-
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 #731 from noaccOS/feat/run-ready-action
feat(containers): run ready actions when the deployment is ready
- Loading branch information
Showing
7 changed files
with
204 additions
and
4 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
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
59 changes: 59 additions & 0 deletions
59
backend/lib/edgehog/containers/manual_actions/run_ready_action.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,59 @@ | ||
# | ||
# 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.ManualActions.RunReadyAction do | ||
@moduledoc false | ||
|
||
use Ash.Resource.ManualUpdate | ||
|
||
alias Edgehog.Devices | ||
|
||
require Ash.Query | ||
|
||
@impl Ash.Resource.ManualUpdate | ||
def update(changeset, _opts, _context) do | ||
ready_action = changeset.data | ||
action_type = ready_action.action_type | ||
|
||
extra_loads = | ||
case action_type do | ||
:upgrade_deployment -> [upgrade_deployment: [:upgrade_target]] | ||
end | ||
|
||
# We always load the device | ||
loads = extra_loads ++ [deployment: [:device]] | ||
|
||
with {:ok, ready_action} <- Ash.load(ready_action, loads), | ||
:ok <- run_ready_action(ready_action, action_type) do | ||
{:ok, ready_action} | ||
end | ||
end | ||
|
||
defp run_ready_action(action, :upgrade_deployment) do | ||
with {:ok, _device} <- | ||
Devices.update_application( | ||
action.deployment.device, | ||
action.upgrade_deployment.upgrade_target, | ||
action.deployment | ||
) do | ||
:ok | ||
end | ||
end | ||
end |
45 changes: 45 additions & 0 deletions
45
backend/lib/edgehog/containers/manual_actions/run_ready_actions.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,45 @@ | ||
# | ||
# 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.ManualActions.RunReadyActions do | ||
@moduledoc false | ||
|
||
use Ash.Resource.ManualUpdate | ||
|
||
alias Edgehog.Containers | ||
|
||
require Ash.Query | ||
require Logger | ||
|
||
@impl Ash.Resource.ManualUpdate | ||
def update(changeset, _opts, _context) do | ||
deployment = changeset.data | ||
|
||
with {:ok, deployment} <- Ash.load(deployment, :ready_actions) do | ||
ready_actions_results = Enum.map(deployment.ready_actions, &Containers.run_ready_action/1) | ||
|
||
for {status, result} <- ready_actions_results, status == :error do | ||
Logger.error("Error running ready action for deployment #{deployment.id}: #{inspect(result)}") | ||
end | ||
|
||
{:ok, deployment} | ||
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