Skip to content

Commit

Permalink
Assign an archive to a deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
oestrich committed Dec 12, 2023
1 parent d7cec19 commit e208153
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 21 deletions.
5 changes: 5 additions & 0 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ config :nerves_hub, NervesHub.RateLimit, limit: 10
# NervesHubWWW
#
config :nerves_hub, NervesHubWeb.Endpoint,
url: [
host: System.get_env("WEB_HOST", "localhost"),
scheme: System.get_env("WEB_SCHEME", "http"),
port: String.to_integer(System.get_env("WEB_PORT", "4000"))
],
http: [ip: {0, 0, 0, 0}, port: 4000],
debug_errors: true,
code_reloader: true,
Expand Down
3 changes: 3 additions & 0 deletions lib/nerves_hub/deployments/deployment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule NervesHub.Deployments.Deployment do
import Ecto.Query

alias NervesHub.Accounts.Org
alias NervesHub.Archives.Archive
alias NervesHub.Devices.InflightUpdate
alias NervesHub.Firmwares.Firmware
alias NervesHub.Products.Product
Expand All @@ -26,6 +27,7 @@ defmodule NervesHub.Deployments.Deployment do
]

@optional_fields [
:archive_id,
:device_failure_threshold,
:device_failure_rate_seconds,
:device_failure_rate_amount,
Expand All @@ -43,6 +45,7 @@ defmodule NervesHub.Deployments.Deployment do
belongs_to(:firmware, Firmware)
belongs_to(:product, Product, where: [deleted_at: nil])
belongs_to(:org, Org, where: [deleted_at: nil])
belongs_to(:archive, Archive)

has_many(:inflight_updates, InflightUpdate)

Expand Down
12 changes: 11 additions & 1 deletion lib/nerves_hub/uploads.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,17 @@ defmodule NervesHub.Uploads.File do
def url("/" <> key, opts), do: url(key, opts)

def url(key, _opts) do
"/uploads/#{key}"
config = Application.get_env(:nerves_hub, NervesHubWeb.Endpoint)[:url]
uri = URI.parse("/uploads/#{key}")

uri = %{
uri
| host: config[:host],
port: config[:port],
scheme: config[:scheme]
}

URI.to_string(uri)
end
end

Expand Down
18 changes: 9 additions & 9 deletions lib/nerves_hub_web/controllers/deployment_controller.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule NervesHubWeb.DeploymentController do
use NervesHubWeb, :controller

alias NervesHub.Archives
alias NervesHub.AuditLogs
alias NervesHub.Firmwares
alias NervesHub.Deployments
Expand Down Expand Up @@ -172,18 +173,16 @@ defmodule NervesHubWeb.DeploymentController do
end

def edit(%{assigns: %{deployment: deployment}} = conn, _params) do
archives = Archives.all_by_product(deployment.product)
firmwares = Firmwares.get_firmwares_for_deployment(deployment)

conn
|> render(
"edit.html",
deployment: deployment,
firmware: deployment.firmware,
firmwares: firmwares,
changeset:
Deployment.changeset(deployment, %{})
|> tags_to_string()
)
|> assign(:archives, archives)
|> assign(:deployment, deployment)
|> assign(:firmware, deployment.firmware)
|> assign(:firmwares, firmwares)
|> assign(:changeset, Deployment.changeset(deployment, %{}) |> tags_to_string())
|> render("edit.html")
end

def update(conn, %{"deployment" => params}) do
Expand Down Expand Up @@ -218,6 +217,7 @@ defmodule NervesHubWeb.DeploymentController do
conn
|> assign(:deployment, deployment)
|> assign(:firmware, deployment.firmware)
|> assign(:archives, Archives.all_by_product(deployment.product))
|> assign(:firmwares, Firmwares.get_firmwares_by_product(product.id))
|> assign(:changeset, tags_to_string(changeset))
|> render("edit.html")
Expand Down
9 changes: 9 additions & 0 deletions lib/nerves_hub_web/templates/deployment/edit.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@
<div class="has-error"><%= error_tag(f, :firmware_id) %></div>
</div>

<div class="form-group">
<label for="firmware_id" class="tooltip-label">
<span>Additional Archive version</span>
</label>
<%= select(f, :archive_id, archive_dropdown_options(@archives), id: "archive_id", prompt: "Select an Archive", class: "form-control") %>
<div class="select-icon"></div>
<div class="has-error"><%= error_tag(f, :archive_id) %></div>
</div>

<h3 class="mb-2">Conditions</h3>

<p>Changing any conditions will reset any attached devices.</p>
Expand Down
21 changes: 21 additions & 0 deletions lib/nerves_hub_web/views/deployment_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@ defmodule NervesHubWeb.DeploymentView do
"#{f.version} #{f.uuid}"
end

def archive_dropdown_options(acrhives) do
acrhives
|> Enum.sort_by(
fn archive ->
case Version.parse(archive.version) do
{:ok, version} ->
version

:error ->
%Version{major: 0, minor: 0, patch: 0}
end
end,
{:desc, Version}
)
|> Enum.map(&[value: &1.id, key: archive_display_name(&1)])
end

def archive_display_name(%{} = a) do
"#{a.version} #{a.platform} #{a.architecture} #{a.uuid}"
end

def help_message_for(field) do
case field do
:failure_threshold ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule NervesHub.Repo.Migrations.AddArchiveIdToDeployments do
use Ecto.Migration

def change do
alter table(:deployments) do
add(:archive_id, references(:archives))
end
end
end
23 changes: 12 additions & 11 deletions test/nerves_hub_web/channels/websocket_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ defmodule NervesHubWeb.WebsocketTest do

{:ok, socket} = SocketClient.start_link(@socket_config)
SocketClient.wait_connect(socket)
SocketClient.join(socket, "device")
SocketClient.join(socket, "device", %{"device_api_version" => "2.0.0"})
SocketClient.wait_join(socket)

device =
Expand Down Expand Up @@ -133,7 +133,7 @@ defmodule NervesHubWeb.WebsocketTest do

{:ok, socket} = SocketClient.start_link(config)
SocketClient.wait_connect(socket)
SocketClient.join(socket, "device")
SocketClient.join(socket, "device", %{"device_api_version" => "2.0.0"})
SocketClient.wait_join(socket)

device =
Expand Down Expand Up @@ -211,7 +211,7 @@ defmodule NervesHubWeb.WebsocketTest do

{:ok, socket} = SocketClient.start_link(opts)
SocketClient.wait_connect(socket)
SocketClient.join(socket, "device")
SocketClient.join(socket, "device", %{"device_api_version" => "2.0.0"})
SocketClient.wait_join(socket)

device =
Expand Down Expand Up @@ -289,7 +289,7 @@ defmodule NervesHubWeb.WebsocketTest do

{:ok, socket} = SocketClient.start_link(opts)
SocketClient.wait_connect(socket)
SocketClient.join(socket, "device")
SocketClient.join(socket, "device", %{"device_api_version" => "2.0.0"})
SocketClient.wait_join(socket)

device =
Expand Down Expand Up @@ -331,7 +331,7 @@ defmodule NervesHubWeb.WebsocketTest do

{:ok, socket} = SocketClient.start_link(@socket_config)
SocketClient.wait_connect(socket)
SocketClient.join(socket, "device")
SocketClient.join(socket, "device", %{"device_api_version" => "2.0.0"})
SocketClient.wait_join(socket)
update = SocketClient.wait_update(socket)
assert %{"update_available" => true, "firmware_url" => _, "firmware_meta" => %{}} = update
Expand Down Expand Up @@ -370,7 +370,7 @@ defmodule NervesHubWeb.WebsocketTest do

{:ok, socket} = SocketClient.start_link(@socket_config)
SocketClient.wait_connect(socket)
SocketClient.join(socket, "device")
SocketClient.join(socket, "device", %{"device_api_version" => "2.0.0"})
SocketClient.wait_join(socket)
reply = SocketClient.reply(socket)

Expand Down Expand Up @@ -405,7 +405,7 @@ defmodule NervesHubWeb.WebsocketTest do

{:ok, socket} = SocketClient.start_link(@socket_config)
SocketClient.wait_connect(socket)
SocketClient.join(socket, "device")
SocketClient.join(socket, "device", %{"device_api_version" => "2.0.0"})
SocketClient.wait_join(socket)
reply = SocketClient.reply(socket)
assert %{} = reply
Expand Down Expand Up @@ -449,6 +449,7 @@ defmodule NervesHubWeb.WebsocketTest do

# Device has updated and no longer matches the attached deployment
SocketClient.join(socket, "device", %{
"device_api_version" => "2.0.0",
"nerves_fw_uuid" => Ecto.UUID.generate(),
"nerves_fw_product" => "test",
"nerves_fw_architecture" => "arm",
Expand Down Expand Up @@ -504,7 +505,7 @@ defmodule NervesHubWeb.WebsocketTest do

{:ok, socket} = SocketClient.start_link(opts)
SocketClient.wait_connect(socket)
SocketClient.join(socket, "device")
SocketClient.join(socket, "device", %{"device_api_version" => "2.0.0"})
SocketClient.wait_join(socket)

device =
Expand Down Expand Up @@ -567,7 +568,7 @@ defmodule NervesHubWeb.WebsocketTest do

{:ok, socket} = SocketClient.start_link(opts)
SocketClient.wait_connect(socket)
SocketClient.join(socket, "device")
SocketClient.join(socket, "device", %{"device_api_version" => "2.0.0"})
SocketClient.wait_join(socket)

device =
Expand Down Expand Up @@ -620,13 +621,13 @@ defmodule NervesHubWeb.WebsocketTest do

{:ok, socket} = SocketClient.start_link(opts)
SocketClient.wait_connect(socket)
SocketClient.join(socket, "device")
SocketClient.join(socket, "device", %{"device_api_version" => "2.0.0"})
SocketClient.wait_join(socket)
GenServer.stop(socket)

{:ok, socket} = SocketClient.start_link(opts)
SocketClient.wait_connect(socket)
SocketClient.join(socket, "device")
SocketClient.join(socket, "device", %{"device_api_version" => "2.0.0"})
SocketClient.wait_join(socket)

[%{last_used: updated_last_used}] = Devices.get_ca_certificates(org)
Expand Down

0 comments on commit e208153

Please sign in to comment.