ha-handler/lib/ha_handler/drbd/watcher.ex

52 lines
1.1 KiB
Elixir
Raw Normal View History

defmodule HAHandler.DRBD.Watcher do
# TODO: add support for SSH public keys authentication.
use GenServer
require Logger
def start_link(opts) do
GenServer.start_link(__MODULE__, opts)
end
defp connect(hostname, password) do
case :inet.gethostbyname(to_charlist(hostname), :inet6) do
{:ok, {:hostent, _name, _aliases, _addrtype, _length, [addr]}} ->
SSHEx.connect(
ip: addr,
user: 'root',
password: password,
silently_accept_hosts: true
)
err ->
err
end
end
@impl true
def init(opts) do
hostname = Keyword.get(opts, :hostname)
password = Keyword.get(opts, :password)
{:ok, pid} = connect(hostname, password)
state = %{
backend: pid,
hostname: hostname,
password: password
}
{:ok, state}
end
@impl true
def handle_call({:execute, cmd}, _from, %{backend: backend} = state) do
case SSHEx.run(backend, cmd) do
{:ok, _output, _status} = reply->
{:reply, reply, state}
{:error, _err} = reply ->
{:error, reply, state}
end
end
end