2023-06-27 19:28:00 +00:00
|
|
|
defmodule Prymn.Agents do
|
|
|
|
@moduledoc ~S"""
|
|
|
|
Prymn Agents are programs that manage a remote client machine. Prymn backend
|
|
|
|
communicates with them using GRPC calls. GRPC connections are started using
|
2023-08-25 21:51:04 +00:00
|
|
|
the Prymn.Agents.ConnectionSupervisor and are book-kept using the
|
2023-06-27 19:28:00 +00:00
|
|
|
Prymn.Agents.Registry.
|
2023-08-25 21:51:04 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2023-08-28 20:32:42 +00:00
|
|
|
alias Prymn.Agents.Connection
|
|
|
|
alias Prymn.Agents.Health
|
2023-06-27 19:28:00 +00:00
|
|
|
|
2023-08-28 20:32:42 +00:00
|
|
|
def start_connection(host_address) do
|
|
|
|
spec = {Connection, host_address}
|
2023-06-27 19:28:00 +00:00
|
|
|
|
2023-08-28 20:32:42 +00:00
|
|
|
case DynamicSupervisor.start_child(Prymn.Agents.ConnectionSupervisor, spec) do
|
|
|
|
{:ok, _pid} -> :ok
|
|
|
|
{:error, {:already_started, _pid}} -> :ok
|
|
|
|
{:error, error} -> {:error, error}
|
2023-06-27 19:28:00 +00:00
|
|
|
end
|
|
|
|
end
|
2023-08-25 21:51:04 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Subscribe to the host's Health using Phoenix.PubSub Broadcasted messages are
|
2023-08-28 20:32:42 +00:00
|
|
|
the Health struct:
|
2023-08-25 21:51:04 +00:00
|
|
|
|
2023-08-28 20:32:42 +00:00
|
|
|
%Prymn.Agents.Health{}
|
2023-08-25 21:51:04 +00:00
|
|
|
"""
|
|
|
|
def subscribe_to_health(host_address) do
|
2023-08-28 20:32:42 +00:00
|
|
|
:ok = Health.subscribe(host_address)
|
2023-08-25 21:51:04 +00:00
|
|
|
end
|
|
|
|
|
2023-08-28 20:32:42 +00:00
|
|
|
@doc """
|
|
|
|
Return the last known health status of the Agent, or `nil` if it doesn't
|
|
|
|
exist.
|
|
|
|
"""
|
|
|
|
def get_health(host_address) do
|
|
|
|
Health.lookup(host_address)
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: We should not expose this api, instead wrap every GRPC call in this
|
|
|
|
# module GRPC is an "internal implementation detail" (although it probably
|
|
|
|
# wont ever change)
|
|
|
|
#
|
|
|
|
# E.g.
|
|
|
|
# def get_sys_info(agent) do
|
|
|
|
# PrymnProto.Prymn.Agent.Stub.get_sys_info(agent.channel, %Google.Protobuf.Empty{})
|
|
|
|
# end
|
2023-08-25 21:51:04 +00:00
|
|
|
def get_channel(host_address) do
|
2023-08-28 20:32:42 +00:00
|
|
|
with [{pid, _}] <- Registry.lookup(Prymn.Agents.Registry, host_address),
|
|
|
|
channel when channel != nil <- Connection.get_channel(pid) do
|
|
|
|
{:ok, channel}
|
|
|
|
else
|
|
|
|
_ -> {:error, :not_found}
|
|
|
|
end
|
2023-08-25 21:51:04 +00:00
|
|
|
end
|
2023-06-27 19:28:00 +00:00
|
|
|
end
|