Skip to content

Adds support for enabling and Disabling relays #572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Apr 25, 2016
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 70 additions & 12 deletions lib/cog/relay/relays.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Cog.Relay.Relays do

alias Carrier.Messaging
alias Cog.Models.Bundle
alias Cog.Models.Relay
alias Cog.Repo
alias Cog.Relay.Util

Expand All @@ -33,6 +34,18 @@ defmodule Cog.Relay.Relays do
GenServer.call(__MODULE__, {:drop_bundle, bundle}, :infinity)
end

@doc "Enables the relay"
@spec enable_relay(%Relay{}) :: :ok
def enable_relay(%Relay{}=relay) do
GenServer.call(__MODULE__, {:enable_relay, relay}, :infinity)
end

@doc "Disables the relay"
@spec disable_relay(%Relay{}) :: :ok
def disable_relay(%Relay{}=relay) do
GenServer.call(__MODULE__, {:disable_relay, relay}, :infinity)
end

@doc """
Returns the IDs of all Relays currently running `bundle_name`. If no
Relays are running the bundle, an empty list is returned.
Expand Down Expand Up @@ -74,6 +87,14 @@ defmodule Cog.Relay.Relays do
tracker = Tracker.drop_bundle(state.tracker, bundle)
{:reply, :ok, %{state | tracker: tracker}}
end
def handle_call({:enable_relay, relay}, _from, state) do
tracker = enable_relay(state.tracker, relay.id)
{:reply, :ok, %{state | tracker: tracker}}
end
def handle_call({:disable_relay, relay}, _from, state) do
tracker = disable_relay(state.tracker, relay.id)
{:reply, :ok, %{state | tracker: tracker}}
end
def handle_call({:relays_running, bundle_name} , _from, state),
do: {:reply, Tracker.relays(state.tracker, bundle_name), state}

Expand All @@ -99,7 +120,7 @@ defmodule Cog.Relay.Relays do
|> Enum.partition(&Util.is_ok?/1)
|> Util.unwrap_partition_results

tracker = update_tracker(announcement, tracker, success_bundles)
tracker = update_tracker(announcement, tracker, success_bundles, internal)

case Map.fetch(announcement, "reply_to") do
:error ->
Expand All @@ -120,30 +141,34 @@ defmodule Cog.Relay.Relays do
defp receipt(announcement_id, failed_bundles),
do: %{"announcement_id" => announcement_id, "status" => "failed", "bundles" => failed_bundles}

defp update_tracker(announcement, tracker, success_bundles) do
defp update_tracker(announcement, tracker, success_bundles, internal) do
relay_id = Map.fetch!(announcement, "relay")

online_status = case Map.fetch!(announcement, "online") do
true -> :online
false -> :offline
end

enabled_status = if internal || relay_enabled?(relay_id) do
:enabled
else
:disabled
end

snapshot_status = case Map.fetch!(announcement, "snapshot") do
true -> :snapshot
false -> :incremental
end

bundle_names = Enum.map(success_bundles, &Map.get(&1, :name)) # Just for logging purposes
case {online_status, snapshot_status} do
case {online_status, enabled_status} do
{:offline, _} ->
Logger.info("Removed Relay #{relay_id} from active relay list")
Tracker.remove_relay(tracker, relay_id)
{:online, :incremental} ->
Logger.info("Incrementally adding bundles for Relay #{relay_id}: #{inspect bundle_names}")
Tracker.add_bundles_for_relay(tracker, relay_id, success_bundles)
{:online, :snapshot} ->
Logger.info("Setting bundles list for Relay #{relay_id}: #{inspect bundle_names}")
Tracker.set_bundles_for_relay(tracker, relay_id, success_bundles)
remove_relay(tracker, relay_id)
{:online, :disabled} ->
load_bundles(snapshot_status, tracker, relay_id, success_bundles)
|> disable_relay(relay_id)
{:online, :enabled} ->
load_bundles(snapshot_status, tracker, relay_id, success_bundles)
|> enable_relay(relay_id)
end
end

Expand Down Expand Up @@ -184,4 +209,37 @@ defmodule Cog.Relay.Relays do
end
end

defp load_bundles(:incremental, tracker, relay_id, success_bundles) do
bundle_names = Enum.map(success_bundles, &Map.get(&1, :name)) # Just for logging purposes
Logger.info("Incrementally adding bundles for Relay #{relay_id}: #{inspect bundle_names}")
Tracker.add_bundles_for_relay(tracker, relay_id, success_bundles)
end
defp load_bundles(:snapshot, tracker, relay_id, success_bundles) do
bundle_names = Enum.map(success_bundles, &Map.get(&1, :name)) # Just for logging purposes
Logger.info("Setting bundles list for Relay #{relay_id}: #{inspect bundle_names}")
Tracker.set_bundles_for_relay(tracker, relay_id, success_bundles)
end

defp enable_relay(tracker, relay_id) do
Logger.info("Enabled Relay #{relay_id}")
Tracker.enable_relay(tracker, relay_id)
end

defp disable_relay(tracker, relay_id) do
Logger.info("Disabled Relay #{relay_id}")
Tracker.disable_relay(tracker, relay_id)
end

defp remove_relay(tracker, relay_id) do
Logger.info("Removed Relay #{relay_id} from active relay list")
Tracker.remove_relay(tracker, relay_id)
end

defp relay_enabled?(relay_id) do
case Repo.get(Relay, relay_id) do
%Relay{enabled: true} -> true
_ -> false
end
end

end
54 changes: 51 additions & 3 deletions lib/cog/relay/tracker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ defmodule Cog.Relay.Tracker do

Tracks all the relays that have checked in with the bot, recording
which bundles they each serve.

Maintains a set of disabled relays. Relays that appear in the disabled
set will be filtered out when the list of relays for a bundle is requested.
Note: Relays must be explicitly disabled, otherwise they are assumed to be
available.
"""

@type t :: %__MODULE__{map: %{String.t => MapSet.t}}
defstruct [map: %{}]
@type t :: %__MODULE__{map: %{String.t => MapSet.t},
disabled: MapSet.t}
defstruct [map: %{}, disabled: MapSet.new]

@doc """
Create a new, empty Tracker
Expand All @@ -21,6 +27,40 @@ defmodule Cog.Relay.Tracker do
def new(),
do: %__MODULE__{}

@doc """
Enables a relay if it exists in the disabled set by removing it from the
disabled set. When the list of relays for a bundle is requested, disabled
bundles are filtered out.

Note: If a relay is assigned no bundles it is unknown to the tracker. When
enabling or disabling make sure to load bundles first or this will just be
a noop.
"""
@spec enable_relay(t, String.t) :: t
def enable_relay(tracker, relay_id) do
disabled = MapSet.delete(tracker.disabled, relay_id)
%{tracker | disabled: disabled}
end

@doc """
Disables a relay if it exists in the tracker by adding it to the disabled
set. When the list of relays for a bundle is requested, disabled bundles
are filtered out.

Note: If a relay is assigned no bundles it is unknown to the tracker. When
enabling or disabling make sure to load bundles first or this will just be
a noop.
"""
@spec disable_relay(t, String.t) :: t
def disable_relay(tracker, relay_id) do
if in_tracker?(tracker, relay_id) do
disabled = MapSet.put(tracker.disabled, relay_id)
%{tracker | disabled: disabled}
else
tracker
end
end

@doc """
Removes all record of `relay` from the tracker. If `relay` is the
last one serving a given bundle, that bundle is removed from the
Expand All @@ -36,7 +76,9 @@ defmodule Cog.Relay.Tracker do
Map.put(acc, bundle, remaining)
end
end)
%{tracker | map: updated}

disabled = MapSet.delete(tracker.disabled, relay)
%{tracker | map: updated, disabled: disabled}
end

@doc """
Expand Down Expand Up @@ -81,7 +123,13 @@ defmodule Cog.Relay.Tracker do
def relays(tracker, bundle_name) do
tracker.map
|> Map.get(bundle_name, MapSet.new)
|> MapSet.difference(tracker.disabled)
|> MapSet.to_list
end

defp in_tracker?(tracker, relay_id) do
Map.values(tracker.map)
|> Enum.reduce(&MapSet.union(&1, &2))
|> MapSet.member?(relay_id)
end
end
1 change: 1 addition & 0 deletions lib/cog/support/model_utilities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ defmodule Cog.Support.ModelUtilities do
relay = %Relay{}
|> Relay.changeset(%{name: name,
token: token,
enabled: Keyword.get(opts, :enabled, false),
desc: Keyword.get(opts, :desc, nil)})
|> Repo.insert!
%{relay | token: nil}
Expand Down
69 changes: 69 additions & 0 deletions test/controllers/v1/relay_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ defmodule Cog.V1.RelayControllerTest do
use Cog.ConnCase

alias Cog.Models.Relay
alias Cog.Relay.Relays
alias Cog.FakeRelay
alias Cog.Queries

@create_attrs %{name: "test-1", token: "foo"}
@update_attrs %{enabled: true, description: "My test"}
@disable_attrs %{@update_attrs | enabled: false}

setup do
# Requests handled by the role controller require this permission
Expand Down Expand Up @@ -95,6 +98,72 @@ defmodule Cog.V1.RelayControllerTest do
assert updated["description"] == @update_attrs.description
end

test "relays are enabled in more than just name", %{authed: requestor} do
# We create a relay and add a bundle to it so we can query for it in
# 'Cog.Relay.Relays'
relay = relay("relay-enable-relay-1", "foobar")
bundle = bundle("relay-enable-bundle-1")
relay_group = relay_group("relay-enable-group-1")
add_relay_to_group(relay_group.id, relay.id)
assign_bundle_to_group(relay_group.id, bundle.id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about making some helper methods that can do all this in one shot, like we now do with cogctl.

I definitely think we should still have the fine-grained helpers that you've got here, but I think it'd help the tests to put a lot of that tedious setup logic behind some well-named helpers that can do it all in one shot.


# We shouldn't see any relays running our bundle yet, because the relay
# has not yet announced it's presence.
assert Relays.relays_running(bundle.name) == []

# Relays don't show up as available unless they are online and enabled.
# FakeRelay lets us send announcement messages like a real relay, so Cog
# will add the relay to the available relays list.
FakeRelay.new(relay) |> FakeRelay.get_bundles |> FakeRelay.announce
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, I wonder if some of these could be coalesced behind a single function call.


# After announcing, our relay should be online but it still won't show up,
# because we haven't enabled it yet.
assert Relays.relays_running(bundle.name) == []

# This should enable our relay
conn = api_request(requestor, :put, "/v1/relays/#{relay.id}",
body: %{"relay" => @update_attrs})
# Confirm that the api thinks the relay is enabled
updated = json_response(conn, 200)["relay"]
assert updated["enabled"] == @update_attrs.enabled

# Now if we check for relays_running we should see our relay
assert Relays.relays_running(bundle.name) == [relay.id]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like this test 👍

end

test "relays are disabled in more than just name", %{authed: requestor} do
# We create a relay and add a bundle to it so we can query for it in
# 'Cog.Relay.Relays'
relay = relay("relay-disable-1", "foobaz", enabled: true)
bundle = bundle("relay-disable-bundle-1")
relay_group = relay_group("relay-disable-group-1")
add_relay_to_group(relay_group.id, relay.id)
assign_bundle_to_group(relay_group.id, bundle.id)

# We shouldn't see any relays running our bundle yet, because the relay
# has not yet announced it's presence.
assert Relays.relays_running(bundle.name) == []

# Relays don't show up as available unless they are online and enabled.
# FakeRelay lets us send announcement messages like a real relay, so Cog
# will add the relay to the available relays list.
FakeRelay.new(relay) |> FakeRelay.get_bundles |> FakeRelay.announce

# After announcing, our relay should be online and enabled since we created
# it enabled.
assert Relays.relays_running(bundle.name) == [relay.id]

# This should disable our relay
conn = api_request(requestor, :put, "/v1/relays/#{relay.id}",
body: %{"relay" => @disable_attrs})
# Confirm that the api thinks the relay is disabled
updated = json_response(conn, 200)["relay"]
assert updated["enabled"] == @disable_attrs.enabled

# Now if we check for relays_running we should see nothing again
assert Relays.relays_running(bundle.name) == []
end

test "updated token changes token digest", %{authed: requestor} do
relay = relay("test-1", "foobar")
conn = api_request(requestor, :put, "/v1/relays/#{relay.id}",
Expand Down
37 changes: 12 additions & 25 deletions test/support/database_test_setup.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,37 +43,24 @@ defmodule DatabaseTestSetup do
@doc """
Creates a bundle record
"""
def bundle(name, commands \\ [%{"name": "echo"}], opts \\ []) do
command_template = %{
"options" => [],
"name" => "echo",
"executable" => "/bin/echo",
"execution" => "multiple",
"enforcing" => false,
"documentation" => "does stuff",
"calling_convention" => "bound"
}
def bundle(name, commands \\ %{"echo": %{"executable" => "/bin/echo"}}, opts \\ []) do

bundle_template = %{
"bundle" => %{"name" => "bundle_name",
"version" => "0.0.1"},
"templates" => [],
"rules" => [],
"permissions" => [],
"commands" => []
"name" => name,
"version" => "0.1.0",
"cog_bundle_version" => 2,
"commands" => commands
}

command_config = for command <- commands do
Map.merge(command_template, command)
end

bundle_config = bundle_template
|> Map.put("name", name)
|> Map.put("commands", command_config)
|> Map.merge(Enum.into(opts, %{}))
bundle_config = Enum.into(opts, bundle_template, fn
({key, value}) when is_atom(key) ->
{Atom.to_string(key), value}
(opt) ->
opt
end)

bundle = %Bundle{}
|> Bundle.changeset(%{name: name, version: "0.0.1", config_file: bundle_config})
|> Bundle.changeset(%{name: name, version: bundle_config["version"], config_file: bundle_config})
|> Repo.insert!

namespace(name, bundle.id)
Expand Down
Loading