Skip to content
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.

Commit

Permalink
Merge 43e5c58 into 3f7a266
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomassaro committed Dec 12, 2017
2 parents 3f7a266 + 43e5c58 commit 23520b5
Show file tree
Hide file tree
Showing 18 changed files with 767 additions and 54 deletions.
2 changes: 1 addition & 1 deletion lib/network/action/network/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule Helix.Network.Action.Network.Connection do
end
end

@spec update_nic(Network.Connection.t, Component.nic) ::
@spec update_nic(Network.Connection.t, Component.nic | nil) ::
{:ok, Network.Connection.t}
| {:error, :internal}
@doc """
Expand Down
4 changes: 2 additions & 2 deletions lib/network/internal/network/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ defmodule Helix.Network.Internal.Network.Connection do
|> Repo.insert()
end

@spec update_nic(Network.Connection.t, Component.nic) ::
@spec update_nic(Network.Connection.t, Component.nic | nil) ::
repo_result
@doc """
Updates the NIC assigned to the NetworkConnection
"""
def update_nic(nc = %Network.Connection{}, new_nic = %Component{}) do
def update_nic(nc = %Network.Connection{}, new_nic) do
nc
|> Network.Connection.update_nic(new_nic)
|> Repo.update()
Expand Down
7 changes: 7 additions & 0 deletions lib/network/model/network/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ defmodule Helix.Network.Model.Network.Connection do
|> validate_required(@required_fields)
end

def update_nic(nc = %__MODULE__{}, nil) do
nc
|> change
|> put_change(:nic_id, nil)
|> validate_required(@required_fields)
end

@spec update_ip(t, ip) ::
changeset
def update_ip(nc = %__MODULE__{}, new_ip) do
Expand Down
3 changes: 2 additions & 1 deletion lib/process/resources.ex
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ defmodule Helix.Process.Resources do
they are identical to the resource's initial value.
"""
def reject_empty(resources) do
Enum.reject(resources, fn {res, val} ->
resources
|> Enum.reject(fn {res, val} ->
val == call_resource(res, :initial, [])
end)
|> Map.new()
Expand Down
2 changes: 1 addition & 1 deletion lib/server/action/flow/motherboard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ defmodule Helix.Server.Action.Flow.Motherboard do
[Motherboard.slot]
defp map_components_slots(components) do
Enum.map(components, fn component ->
{component, Utils.concat_atom(component.type, :_0)}
{component, Utils.concat_atom(component.type, :_1)}
end)
end

Expand Down
103 changes: 103 additions & 0 deletions lib/server/action/flow/motherboard/update.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# TODO: Move to motherboard action (outside flow)
defmodule Helix.Server.Action.Motherboard.Update do

import HELL.Macros

alias Helix.Network.Action.Network, as: NetworkAction
alias Helix.Network.Model.Network
alias Helix.Server.Repo, as: ServerRepo

alias Helix.Server.Query.Component, as: ComponentQuery

alias Helix.Server.Internal.Motherboard, as: MotherboardInternal

def update(nil, mobo_data, entity_ncs) do
{:ok, new_mobo} =
MotherboardInternal.setup(mobo_data.mobo, mobo_data.components)

hespawn fn ->
update_network_connections(mobo_data, entity_ncs)
end

{:ok, new_mobo, []}
end

def update(
motherboard,
mobo_data,
entity_ncs)
do
{:ok, {:ok, new_mobo}} =
ServerRepo.transaction fn ->
MotherboardInternal.unlink_all(motherboard)
MotherboardInternal.setup(mobo_data.mobo, mobo_data.components)
end

hespawn fn ->
update_network_connections(mobo_data, entity_ncs)
end

{:ok, new_mobo, []}
end

defp update_network_connections(mobo_data, entity_ncs) do
# Secondary index used to figure out whether a specific NIP was specified on
# `mobo_data.network_connections`.
assigned_nips =
Enum.reduce(mobo_data.network_connections, [], fn {_, nip}, acc ->
acc ++ [nip]
end)

entity_ncs

# Get the required operations we may have to do on NetworkConnections...
|> Enum.reduce([], fn nc, acc ->
cond do
# The NIC already has an NC attached to it
Map.has_key?(mobo_data.network_connections, nc.nic_id) ->
nip = mobo_data.network_connections[nc.nic_id]

# The given NC is the same as before; we don't have to do anything
if {nc.network_id, nc.ip} == nip do
acc

# There will be a new NC attached to this NIC, so we have to
# remove the previous NC reference to this NIC, as it's no longer
# used. Certainly we'll also have to update the new NC to point
# to this NIC. That's done on another iteration at :set_nic below
else
acc ++ [{:nilify_nic, nc}]
end

# TODO: What if NIP is in use? Henforce!
# The current NC nic is not in use, but its nip is being assigned.
# This means the NC will start being used, so we need to link it to
# the underlying NIC.
{nc.network_id, nc.ip} in assigned_nips ->
{nic_id, _} =
mobo_data.network_connections
|> Enum.find(fn {_, {network_id, ip}} ->
network_id == nc.network_id and ip == nc.ip
end)

acc ++ [{:set_nic, nc, nic_id}]

# This NC is not modified at all by the mobo update
true ->
acc

end
end)

# Perform those NetworkConnection operations
|> Enum.each(&perform_network_op/1)
end

defp perform_network_op({:nilify_nic, nc = %Network.Connection{}}),
do: {:ok, _} = NetworkAction.Connection.update_nic(nc, nil)
defp perform_network_op({:set_nic, nc = %Network.Connection{}, nic_id}) do
nic = ComponentQuery.fetch(nic_id)

{:ok, _} = NetworkAction.Connection.update_nic(nc, nic)
end
end
32 changes: 32 additions & 0 deletions lib/server/action/flow/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Helix.Server.Action.Flow.Server do
alias Helix.Event
alias Helix.Entity.Action.Entity, as: EntityAction
alias Helix.Entity.Model.Entity
alias Helix.Server.Action.Motherboard, as: MotherboardAction
alias Helix.Server.Action.Server, as: ServerAction
alias Helix.Server.Model.Component
alias Helix.Server.Model.Server
Expand Down Expand Up @@ -43,4 +44,35 @@ defmodule Helix.Server.Action.Flow.Server do
"""
def set_hostname(server, hostname, _relay),
do: ServerAction.set_hostname(server, hostname)

def update_mobo(
server = %Server{},
cur_mobo_data,
new_mobo_data,
entity_ncs,
relay)
do
new_mobo_id = new_mobo_data.mobo.component_id

flowing do
with \
{:ok, new_mobo, events} <-
MotherboardAction.update(
cur_mobo_data, new_mobo_data, entity_ncs
),
on_success(fn -> Event.emit(events, from: relay) end),

{:ok, new_server} <- update_server_mobo(server, new_mobo_id)
do
{:ok, new_server, new_mobo}
end
end
end

defp update_server_mobo(%Server{motherboard_id: mobo_id}, mobo_id),
do: {:ok, mobo_id}
defp update_server_mobo(server, nil),
do: ServerAction.detach(server)
defp update_server_mobo(server, mobo_id),
do: ServerAction.attach(server, mobo_id)
end
3 changes: 3 additions & 0 deletions lib/server/action/motherboard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ defmodule Helix.Server.Action.Motherboard do

defdelegate unlink(component),
to: MotherboardInternal

defdelegate update(cur_mobo_data, new_mobo_data, entity_ncs),
to: __MODULE__.Update
end
28 changes: 14 additions & 14 deletions lib/server/component/specs/specable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,10 @@ defmodule Helix.Server.Component.Specable do
price: 100,

slots: %{
cpu: %{0 => %{}},
ram: %{0 => %{}},
hdd: %{0 => %{}},
nic: %{0 => %{}},
cpu: %{1 => %{}},
ram: %{1 => %{}},
hdd: %{1 => %{}},
nic: %{1 => %{}},
usb: %{}
}
}
Expand All @@ -295,11 +295,11 @@ defmodule Helix.Server.Component.Specable do
price: 200,

slots: %{
cpu: %{0 => %{}, 1 => %{}},
ram: %{0 => %{}, 1 => %{}},
hdd: %{0 => %{}},
nic: %{0 => %{}},
usb: %{0 => %{}}
cpu: %{1 => %{}, 2 => %{}},
ram: %{1 => %{}, 2 => %{}},
hdd: %{1 => %{}},
nic: %{1 => %{}},
usb: %{1 => %{}}
}
}
end
Expand All @@ -310,11 +310,11 @@ defmodule Helix.Server.Component.Specable do
price: 999_999_999,

slots: %{
cpu: %{0 => %{}, 1 => %{}, 2 => %{}, 3 => %{}},
ram: %{0 => %{}, 1 => %{}, 2 => %{}, 3 => %{}},
hdd: %{0 => %{}, 1 => %{}, 2 => %{}, 3 => %{}},
nic: %{0 => %{}, 1 => %{}, 2 => %{}, 3 => %{}},
usb: %{0 => %{}, 1 => %{}, 2 => %{}, 3 => %{}}
cpu: %{1 => %{}, 2 => %{}, 3 => %{}, 4 => %{}},
ram: %{1 => %{}, 2 => %{}, 3 => %{}, 4 => %{}},
hdd: %{1 => %{}, 2 => %{}, 3 => %{}, 4 => %{}},
nic: %{1 => %{}, 2 => %{}, 3 => %{}, 4 => %{}},
usb: %{1 => %{}, 2 => %{}, 3 => %{}, 4 => %{}}
}
}
end
Expand Down
17 changes: 15 additions & 2 deletions lib/server/internal/motherboard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule Helix.Server.Internal.Motherboard do
"""
def fetch(motherboard_id) do
motherboard_id
|> Motherboard.Query.by_motherboard()
|> Motherboard.Query.by_motherboard(eager: true)
|> Repo.all()
|> Motherboard.format()
end
Expand Down Expand Up @@ -217,7 +217,7 @@ defmodule Helix.Server.Internal.Motherboard do
@spec unlink(Component.pluggable) ::
:ok
@doc """
Unlinks `component` from `motherboard`.
Unlinks `component` from the motherboard.
Notice we are not *updating* any entries. All `unlink` operations are removing
data from the `motherboards` table.
Expand All @@ -230,6 +230,19 @@ defmodule Helix.Server.Internal.Motherboard do
:ok
end

@spec unlink_all(Motherboard.t) ::
:ok
@doc """
Unlinks all components from `motherboard`.
"""
def unlink_all(motherboard = %Motherboard{}) do
motherboard.motherboard_id
|> Motherboard.Query.by_motherboard(eager: false)
|> Repo.delete_all()

:ok
end

@spec get_component(Motherboard.t, Component.type) ::
[Component.pluggable]
defp get_component(motherboard = %Motherboard{}, component_type) do
Expand Down
10 changes: 6 additions & 4 deletions lib/server/model/motherboard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,12 @@ defmodule Helix.Server.Model.Motherboard do

@spec by_motherboard(Queryable.t, Motherboard.idt) ::
Queryable.t
def by_motherboard(query \\ Motherboard, motherboard_id)
def by_motherboard(query, mobo = %Component{type: :mobo}),
do: by_motherboard(query, mobo.component_id)
def by_motherboard(query, motherboard_id) do
def by_motherboard(query \\ Motherboard, motherboard_id, eager?)
def by_motherboard(query, mobo = %Component{type: :mobo}, eager?),
do: by_motherboard(query, mobo.component_id, eager?)
def by_motherboard(query, motherboard_id, eager: false),
do: where(query, [m], m.motherboard_id == ^motherboard_id)
def by_motherboard(query, motherboard_id, _) do
from entries in query,
inner_join: component in assoc(entries, :linked_component),
where: entries.motherboard_id == ^to_string(motherboard_id),
Expand Down
2 changes: 2 additions & 0 deletions lib/server/query/motherboard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ defmodule Helix.Server.Query.Motherboard do
to: MotherboardInternal
defdelegate get_nics(motherboard),
to: MotherboardInternal
defdelegate get_rams(motherboard),
to: MotherboardInternal
end
18 changes: 18 additions & 0 deletions lib/server/websocket/channel/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ channel Helix.Server.Websocket.Channel.Server do
alias Helix.Server.Websocket.Requests.Bootstrap, as: BootstrapRequest
alias Helix.Server.Websocket.Requests.Config.Check, as: ConfigCheckRequest
alias Helix.Server.Websocket.Requests.Config.Set, as: ConfigSetRequest
alias Helix.Server.Websocket.Requests.MotherboardUpdate,
as: MotherboardUpdateRequest
alias Helix.Server.Websocket.Requests.SetHostname, as: SetHostnameRequest

@doc """
Expand Down Expand Up @@ -124,6 +126,22 @@ channel Helix.Server.Websocket.Channel.Server do
"""
topic "config.check", ConfigCheckRequest

@doc """
{} => mobo.detach
\/ update
{
"motherboard_id" : "motherboard_id"
, "slots" :
{ "slot_id_a" : "component_id" , "slot_id_b" : null}
, "network_connections" :
{ "component_id" :
{ "ip" : "ip" , "network_id" : "network_id"}
}
}
"""
topic "motherboard.update", MotherboardUpdateRequest

@doc """
Updates the server hostname.
Expand Down
Loading

0 comments on commit 23520b5

Please sign in to comment.