-
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.
feat(containers): application upgrade interface
expose a way to upgrade an application to a newer release . - does not allow downgrading - the releases must be of the same application Signed-off-by: Francesco Noacco <[email protected]>
- Loading branch information
Showing
11 changed files
with
299 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# | ||
# 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.Astarte.Device.DeploymentUpdate do | ||
@moduledoc false | ||
|
||
@behaviour Edgehog.Astarte.Device.DeploymentUpdate.Behaviour | ||
|
||
alias Astarte.Client.AppEngine | ||
alias Edgehog.Error.AstarteAPIError | ||
|
||
@interface "io.edgehog.devicemanager.apps.DeploymentUpdate" | ||
|
||
@impl Edgehog.Astarte.Device.DeploymentUpdate.Behaviour | ||
def update(client, device_id, data) do | ||
api_call = | ||
AppEngine.Devices.send_datastream(client, device_id, @interface, "/deployment", data) | ||
|
||
with {:error, api_error} <- api_call do | ||
reason = AstarteAPIError.exception(status: api_error.status, response: api_error.response) | ||
|
||
{:error, reason} | ||
end | ||
end | ||
end |
32 changes: 32 additions & 0 deletions
32
backend/lib/edgehog/astarte/device/deployment_update/behaviour.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,32 @@ | ||
# | ||
# 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.Astarte.Device.DeploymentUpdate.Behaviour do | ||
@moduledoc false | ||
|
||
alias Astarte.Client.AppEngine | ||
alias Edgehog.Astarte.Device.DeploymentUpdate.RequestData | ||
|
||
@callback update( | ||
client :: AppEngine.t(), | ||
device_id :: String.t(), | ||
data :: RequestData.t() | ||
) :: :ok | {:error, term()} | ||
end |
30 changes: 30 additions & 0 deletions
30
backend/lib/edgehog/astarte/device/deployment_update/request_data.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,30 @@ | ||
# | ||
# 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.Astarte.Device.DeploymentUpdate.RequestData do | ||
@moduledoc false | ||
|
||
defstruct [:from, :to] | ||
|
||
@type t :: %__MODULE__{ | ||
from: String.t(), | ||
to: String.t() | ||
} | ||
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,46 @@ | ||
# | ||
# 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.Validations.IsUpgrade do | ||
@moduledoc false | ||
|
||
use Ash.Resource.Validation | ||
|
||
@impl Ash.Resource.Validation | ||
def validate(changeset, opts, _context) do | ||
from = Ash.Changeset.get_argument(changeset, opts[:from]) | ||
to = Ash.Changeset.get_argument(changeset, opts[:to]) | ||
|
||
with {:ok, from_version} <- parse_version(from), | ||
{:ok, to_version} <- parse_version(to) do | ||
if Version.compare(to_version, from_version) == :gt do | ||
:ok | ||
else | ||
{:error, field: opts[:to], message: "must be a newer release than from"} | ||
end | ||
end | ||
end | ||
|
||
defp parse_version(release) do | ||
with :error <- release.version |> get_in() |> to_string() |> Version.parse() do | ||
{:error, :invalid_release} | ||
end | ||
end | ||
end |
37 changes: 37 additions & 0 deletions
37
backend/lib/edgehog/containers/validations/same_application.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,37 @@ | ||
# | ||
# 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.Validations.SameApplication do | ||
@moduledoc false | ||
|
||
use Ash.Resource.Validation | ||
|
||
@impl Ash.Resource.Validation | ||
def validate(changeset, opts, _context) do | ||
with {:ok, release_a} <- Ash.Changeset.fetch_argument(changeset, opts[:release_a]), | ||
{:ok, release_b} <- Ash.Changeset.fetch_argument(changeset, opts[:release_b]) do | ||
if release_a.application_id == release_b.application_id do | ||
:ok | ||
else | ||
{:error, field: opts[:release_b], message: "must belong to the same application as from"} | ||
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
52 changes: 52 additions & 0 deletions
52
backend/lib/edgehog/devices/device/manual_actions/update_application.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,52 @@ | ||
# | ||
# 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.Devices.Device.ManualActions.UpdateApplication do | ||
@moduledoc false | ||
use Ash.Resource.ManualUpdate | ||
|
||
alias Edgehog.Astarte.Device.DeploymentUpdate.RequestData | ||
alias Edgehog.Containers.Deployment | ||
|
||
@deployment_update Application.compile_env( | ||
:edgehog, | ||
:astarte_deployment_update_module, | ||
Edgehog.Astarte.Device.DeploymentUpdate | ||
) | ||
|
||
@impl Ash.Resource.ManualUpdate | ||
def update(changeset, _opts, _context) do | ||
device = changeset.data | ||
|
||
with {:ok, from} <- Ash.Changeset.fetch_argument(changeset, :from), | ||
{:ok, to} <- Ash.Changeset.fetch_argument(changeset, :to), | ||
{:ok, from} <- fetch_deployment(device, from), | ||
{:ok, to} <- fetch_deployment(device, to), | ||
{:ok, device} <- Ash.load(device, :appengine_client), | ||
data = %RequestData{from: from.id, to: to.id}, | ||
:ok <- @deployment_update.update(device.appengine_client, device.device_id, data) do | ||
{:ok, device} | ||
end | ||
end | ||
|
||
defp fetch_deployment(device, release) do | ||
Ash.get(Deployment, %{device_id: device.id, release_id: release.id}, tenant: device.tenant_id) | ||
end | ||
end |
24 changes: 24 additions & 0 deletions
24
...end/priv/astarte_resources/interfaces/io.edgehog.devicemanager.apps.DeploymentUpdate.json
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,24 @@ | ||
{ | ||
"interface_name": "io.edgehog.devicemanager.apps.DeploymentUpdate", | ||
"version_major": 0, | ||
"version_minor": 1, | ||
"type": "datastream", | ||
"ownership": "server", | ||
"aggregation": "object", | ||
"mappings": [ | ||
{ | ||
"endpoint": "/deployment/from", | ||
"type": "string", | ||
"database_retention_policy": "use_ttl", | ||
"database_retention_ttl": 31556952, | ||
"description": "Id of the deployment to update from" | ||
}, | ||
{ | ||
"endpoint": "/deployment/to", | ||
"type": "string", | ||
"database_retention_policy": "use_ttl", | ||
"database_retention_ttl": 31556952, | ||
"description": "Id of the deployment to update to" | ||
} | ||
] | ||
} |
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