Set editorconfig, format whole codebase

This commit is contained in:
Timothée Floure 2022-02-23 17:53:00 +01:00
parent e62aafd172
commit ebcfabdbd2
Signed by: tfloure
GPG key ID: 4502C902C00A1E12
11 changed files with 380 additions and 329 deletions

12
.editorconfig Normal file
View file

@ -0,0 +1,12 @@
# EditorConfig is awesome: https://EditorConfig.org
# top-most EditorConfig file
root = true
[*.ex]
indent_style = space
indent_size = 2
[*.eex]
indent_style = space
indent_size = 2

View file

@ -6,8 +6,8 @@ defmodule HAHandler do
# Mix is not available in releases, and these things are static
# anyway (@variables are evaluated at compile time).
@otp_app Mix.Project.config()[:app]
@version Mix.Project.config[:version]
@env Mix.env
@version Mix.Project.config()[:version]
@env Mix.env()
def http_port, do: Application.get_env(@otp_app, :http_port)
def haproxy_socket, do: Application.get_env(@otp_app, :haproxy_socket)
@ -26,6 +26,7 @@ defmodule HAHandler do
from: acme_challenge_path()
]
end
def assets_static_config() do
[
at: "/static",

View file

@ -10,7 +10,8 @@ defmodule HAHandler.Application do
@impl true
def start(_type, _args) do
children = [
{Plug.Cowboy, scheme: :http, plug: HAHandler.Web.Router, options: [port: HAHandler.http_port()]},
{Plug.Cowboy,
scheme: :http, plug: HAHandler.Web.Router, options: [port: HAHandler.http_port()]},
{HAHandler.PGSQL.Supervisor, HAHandler.pgsql_instances()},
{HAHandler.Control, []}
]

View file

@ -1,82 +1,104 @@
defmodule HAHandler.Control do
@moduledoc """
This module handles the decision-logic and actions to be
taken regarding the current state of the infrastructure.
"""
@moduledoc """
This module handles the decision-logic and actions to be
taken regarding the current state of the infrastructure.
"""
@haproxy_pgsql_backend "pgsql"
@haproxy_pgsql_backend "pgsql"
use GenServer
use GenServer
require Logger
require Logger
alias HAHandler.{PGSQL, HAProxy}
alias HAHandler.{PGSQL, HAProxy}
# How much do we wait (ms) between each check/decision-making round?
@refresh 15_000
# How much do we wait (ms) between each check/decision-making round?
@refresh 15_000
def start_link(opts) do
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
end
def start_link(opts) do
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
end
@impl true
def init(_opts) do
state = []
@impl true
def init(_opts) do
state = []
# Let's skip the initial startup round so that other components are all up
# and running.
Process.send_after self(), :sync, @refresh
# Let's skip the initial startup round so that other components are all up
# and running.
Process.send_after(self(), :sync, @refresh)
{:ok, state}
end
{:ok, state}
end
@impl true
def handle_info(:sync, state) do
Logger.debug("Executing control logic.")
@impl true
def handle_info(:sync, state) do
Logger.debug("Executing control logic.")
# Fetch PGSQL state, make sure HAProxy routes to the master
# process.
pgsql_state = PGSQL.get_instances()
|> Enum.map(fn {hostname, pid} = instance->
haproxy_server = HAHandler.pgsql_instances()
|> Enum.filter(fn opts -> Keyword.get(opts, :hostname) == hostname end)
|> Enum.at(0)
|> Keyword.get(:haproxy_server)
# Fetch PGSQL state, make sure HAProxy routes to the master
# process.
pgsql_state =
PGSQL.get_instances()
|> Enum.map(fn {hostname, pid} = instance ->
haproxy_server =
HAHandler.pgsql_instances()
|> Enum.filter(fn opts -> Keyword.get(opts, :hostname) == hostname end)
|> Enum.at(0)
|> Keyword.get(:haproxy_server)
%{
haproxy_server: haproxy_server,
pgsql_watcher_pid: pid,
pgsql_operation_mode: PGSQL.get_operation_mode(instance)
}
end)
haproxy_state = HAProxy.get_stats()
|> Map.get("Server", [])
|> Enum.filter(fn mapping -> mapping["pxname"] == @haproxy_pgsql_backend end)
|> Enum.map(fn mapping -> %{mapping["svname"] => mapping["status"]} end)
|> Enum.reduce(&Map.merge/2)
%{
haproxy_server: haproxy_server,
pgsql_watcher_pid: pid,
pgsql_operation_mode: PGSQL.get_operation_mode(instance)
}
end)
for pgsql_instance <- pgsql_state do
haproxy_state = Map.get(haproxy_state, pgsql_instance.haproxy_server)
haproxy_state =
HAProxy.get_stats()
|> Map.get("Server", [])
|> Enum.filter(fn mapping -> mapping["pxname"] == @haproxy_pgsql_backend end)
|> Enum.map(fn mapping -> %{mapping["svname"] => mapping["status"]} end)
|> Enum.reduce(&Map.merge/2)
case {pgsql_instance.pgsql_operation_mode, haproxy_state} do
{:primary, "UP"} ->
:noop
{:primary, "MAINT"} ->
Logger.info("Enabling routing PGSQL to (now) primary #{pgsql_instance.haproxy_server}.")
HAProxy.set_server(@haproxy_pgsql_backend, pgsql_instance.haproxy_server, "state", "ready")
{:secondary, "UP"} ->
Logger.info("Disabling routing PGSQL to (now) secondary #{pgsql_instance.haproxy_server}.")
HAProxy.set_server(@haproxy_pgsql_backend, pgsql_instance.haproxy_server, "state", "maint")
{:secondary, "MAINT"} ->
:noop
unknown ->
Logger.warning("Unhandled PGSQL/HAProxy state: #{inspect(unknown)}")
end
end
for pgsql_instance <- pgsql_state do
haproxy_state = Map.get(haproxy_state, pgsql_instance.haproxy_server)
# Schedule next round.
Process.send_after self(), :sync, @refresh
case {pgsql_instance.pgsql_operation_mode, haproxy_state} do
{:primary, "UP"} ->
:noop
{:noreply, state}
end
{:primary, "MAINT"} ->
Logger.info("Enabling routing PGSQL to (now) primary #{pgsql_instance.haproxy_server}.")
HAProxy.set_server(
@haproxy_pgsql_backend,
pgsql_instance.haproxy_server,
"state",
"ready"
)
{:secondary, "UP"} ->
Logger.info(
"Disabling routing PGSQL to (now) secondary #{pgsql_instance.haproxy_server}."
)
HAProxy.set_server(
@haproxy_pgsql_backend,
pgsql_instance.haproxy_server,
"state",
"maint"
)
{:secondary, "MAINT"} ->
:noop
unknown ->
Logger.warning("Unhandled PGSQL/HAProxy state: #{inspect(unknown)}")
end
end
# Schedule next round.
Process.send_after(self(), :sync, @refresh)
{:noreply, state}
end
end

View file

@ -33,28 +33,31 @@ defmodule HAHandler.HAProxy do
# run out - which will block all operations.
close_socket(fd)
data
{:error, err} ->
{:error, err}
end
end
defp extract_stats(data) do
extracted = for entry <- data do
for mapping <- entry do
case mapping do
%{
"id" => id,
"proxyId" => proxy_id,
"objType" => type,
"field" => %{"name" => name},
"value" => %{"value" => value},
} ->
%{:id => id, :proxy_id => proxy_id, :type => type, :field => name, :value => value}
_ ->
nil
extracted =
for entry <- data do
for mapping <- entry do
case mapping do
%{
"id" => id,
"proxyId" => proxy_id,
"objType" => type,
"field" => %{"name" => name},
"value" => %{"value" => value}
} ->
%{:id => id, :proxy_id => proxy_id, :type => type, :field => name, :value => value}
_ ->
nil
end
end
end
end
extracted
|> List.flatten()
@ -62,18 +65,14 @@ defmodule HAHandler.HAProxy do
fn mapping -> {mapping.type, mapping.id, mapping.proxy_id} end,
fn mapping -> %{mapping.field => mapping.value} end
)
|> Enum.map(
fn {{type, id, proxy_id}, grouped_mappings} ->
grouped_mappings
|> Enum.reduce(fn l, r -> Map.merge(l,r) end)
|> Map.put("type", type)
|> Map.put("id", id)
|> Map.put("proxy_id", proxy_id)
end
)
|> Enum.group_by(
fn entry -> Map.get(entry, "type") end
)
|> Enum.map(fn {{type, id, proxy_id}, grouped_mappings} ->
grouped_mappings
|> Enum.reduce(fn l, r -> Map.merge(l, r) end)
|> Map.put("type", type)
|> Map.put("id", id)
|> Map.put("proxy_id", proxy_id)
end)
|> Enum.group_by(fn entry -> Map.get(entry, "type") end)
end
@doc """
@ -81,7 +80,8 @@ defmodule HAHandler.HAProxy do
a list of Maps.
"""
def get_stats(opts \\ [])
def get_stats([hide_error: true]) do
def get_stats(hide_error: true) do
case get_stats() do
{:error, _err} ->
%{
@ -89,9 +89,12 @@ defmodule HAHandler.HAProxy do
"Backend" => %{},
"Server" => %{}
}
stats -> stats
stats ->
stats
end
end
def get_stats(_opts) do
case execute("show stat json") do
{:ok, raw} ->
@ -117,8 +120,10 @@ defmodule HAHandler.HAProxy do
case execute("set server #{backend}/#{server} #{key} #{value}") do
{:ok, ""} ->
:ok
{:ok, err} ->
{:error, err}
{:error, err} ->
{:error, err}
end

View file

@ -1,45 +1,52 @@
defmodule HAHandler.PGSQL do
@supervisor HAHandler.PGSQL.Supervisor
@version_query "SELECT version();"
@is_in_recovery_query "SELECT pg_is_in_recovery();"
@supervisor HAHandler.PGSQL.Supervisor
@version_query "SELECT version();"
@is_in_recovery_query "SELECT pg_is_in_recovery();"
def get_instances() do
watchers = Supervisor.which_children(@supervisor)
for {hostname, pid, _type, _modules} <- watchers do
{hostname, pid}
end
end
def get_instances() do
watchers = Supervisor.which_children(@supervisor)
def get_version({hostname, pid}) do
case GenServer.call(pid, {:execute, @version_query, []}) do
{:ok, %Postgrex.Result{rows: [[raw_version_string]]}} ->
version = case Regex.run(~r/^PostgreSQL (\d+\.\d+)/, raw_version_string) do
[_, version_number] -> version_number
_ -> "unknown"
end
%{hostname: hostname, version: version, status: "up"}
{:error, %DBConnection.ConnectionError{message: _msg, reason: err}} ->
%{hostname: hostname, version: "unknown", status: err}
_ ->
%{hostname: hostname, version: "unknown", status: :unknown}
end
end
for {hostname, pid, _type, _modules} <- watchers do
{hostname, pid}
end
end
def get_operation_mode({_hostname, pid}) do
case GenServer.call(pid, {:execute, @is_in_recovery_query, []}) do
{:ok, %Postgrex.Result{rows: [[false]]}} ->
:primary
{:ok, %Postgrex.Result{rows: [[true]]}} ->
:secondary
_ ->
:unknown
end
end
def get_version({hostname, pid}) do
case GenServer.call(pid, {:execute, @version_query, []}) do
{:ok, %Postgrex.Result{rows: [[raw_version_string]]}} ->
version =
case Regex.run(~r/^PostgreSQL (\d+\.\d+)/, raw_version_string) do
[_, version_number] -> version_number
_ -> "unknown"
end
def get_stats() do
get_instances()
|> Enum.map(fn instance ->
get_version(instance) |> Map.put(:mode, get_operation_mode(instance))
end)
end
%{hostname: hostname, version: version, status: "up"}
{:error, %DBConnection.ConnectionError{message: _msg, reason: err}} ->
%{hostname: hostname, version: "unknown", status: err}
_ ->
%{hostname: hostname, version: "unknown", status: :unknown}
end
end
def get_operation_mode({_hostname, pid}) do
case GenServer.call(pid, {:execute, @is_in_recovery_query, []}) do
{:ok, %Postgrex.Result{rows: [[false]]}} ->
:primary
{:ok, %Postgrex.Result{rows: [[true]]}} ->
:secondary
_ ->
:unknown
end
end
def get_stats() do
get_instances()
|> Enum.map(fn instance ->
get_version(instance) |> Map.put(:mode, get_operation_mode(instance))
end)
end
end

View file

@ -1,22 +1,23 @@
defmodule HAHandler.PGSQL.Supervisor do
use Supervisor
use Supervisor
alias HAHandler.PGSQL.Watcher, as: PGSQLWatcher
alias HAHandler.PGSQL.Watcher, as: PGSQLWatcher
def start_link(opts) do
Supervisor.start_link(__MODULE__, opts, name: __MODULE__)
end
def start_link(opts) do
Supervisor.start_link(__MODULE__, opts, name: __MODULE__)
end
@impl true
def init(instances) do
children = Enum.map(instances, fn conf ->
%{
id: Keyword.get(conf, :hostname),
start: {PGSQLWatcher, :start_link, [conf]}
}
end)
@impl true
def init(instances) do
children =
Enum.map(instances, fn conf ->
%{
id: Keyword.get(conf, :hostname),
start: {PGSQLWatcher, :start_link, [conf]}
}
end)
opts = [ strategy: :one_for_one ]
Supervisor.init(children, opts)
end
opts = [strategy: :one_for_one]
Supervisor.init(children, opts)
end
end

View file

@ -1,29 +1,29 @@
defmodule HAHandler.PGSQL.Watcher do
use GenServer
require Logger
use GenServer
require Logger
def start_link(opts) do
GenServer.start_link(__MODULE__, opts)
end
def start_link(opts) do
GenServer.start_link(__MODULE__, opts)
end
@impl true
def init(opts) do
# Starts a Postgrex child but does not means the connection was
# successful.
# TODO: set dbconnections backoff and connect hooks
# See https://github.com/elixir-ecto/db_connection/blob/master/lib/db_connection.ex#L343
{:ok, pid} = Postgrex.start_link(opts)
@impl true
def init(opts) do
# Starts a Postgrex child but does not means the connection was
# successful.
# TODO: set dbconnections backoff and connect hooks
# See https://github.com/elixir-ecto/db_connection/blob/master/lib/db_connection.ex#L343
{:ok, pid} = Postgrex.start_link(opts)
state = %{
backend: pid,
hostname: Keyword.get(opts, :hostname)
}
state = %{
backend: pid,
hostname: Keyword.get(opts, :hostname)
}
{:ok, state}
end
{:ok, state}
end
@impl true
def handle_call({:execute, query, params}, _from, %{backend: backend} = state) do
{:reply, Postgrex.query(backend, query, params), state}
end
@impl true
def handle_call({:execute, query, params}, _from, %{backend: backend} = state) do
{:reply, Postgrex.query(backend, query, params), state}
end
end

View file

@ -1,35 +1,34 @@
defmodule HAHandler.Web.Controller do
import Plug.Conn
import Plug.Conn
alias HAHandler.{HAProxy, PGSQL}
alias HAHandler.{HAProxy, PGSQL}
@template_dir "lib/ha_handler/web/templates"
@index_template EEx.compile_file(
Path.join(@template_dir, "index.html.eex")
)
@index_template EEx.compile_file(Path.join(@template_dir, "index.html.eex"))
defp render(conn, template, assigns) do
{body, _binding} = Code.eval_quoted(template, assigns)
defp render(conn, template, assigns) do
{body, _binding} = Code.eval_quoted(template, assigns)
conn
|> put_resp_content_type("text/html")
|> send_resp(200, body)
end
conn
|> put_resp_content_type("text/html")
|> send_resp(200, body)
end
def index(conn) do
{:ok, hostname} = :net_adm.dns_hostname(:net_adm.localhost)
def index(conn) do
{:ok, hostname} = :net_adm.dns_hostname(:net_adm.localhost())
haproxy_stats = HAProxy.get_stats([hide_error: true])
pgsql_stats = PGSQL.get_stats()
haproxy_stats = HAProxy.get_stats(hide_error: true)
pgsql_stats = PGSQL.get_stats()
assigns = [
haproxy_stats: haproxy_stats,
pgsql_status: pgsql_stats,
hostname: hostname,
otp_app: HAHandler.otp_app(),
version: HAHandler.version(),
env: HAHandler.env()
]
render(conn, @index_template, assigns)
end
assigns = [
haproxy_stats: haproxy_stats,
pgsql_status: pgsql_stats,
hostname: hostname,
otp_app: HAHandler.otp_app(),
version: HAHandler.version(),
env: HAHandler.env()
]
render(conn, @index_template, assigns)
end
end

View file

@ -1,35 +1,38 @@
defmodule HAHandler.Web.Router do
@moduledoc """
This module dispatch incoming HTTP requests to their
related logic. Please refer to [1] for details.
@moduledoc """
This module dispatch incoming HTTP requests to their
related logic. Please refer to [1] for details.
[1] https://hexdocs.pm/plug/Plug.Router.html#content
"""
[1] https://hexdocs.pm/plug/Plug.Router.html#content
"""
use Plug.Router
use Plug.Router
alias HAHandler.Web.Controller
alias HAHandler.Web.Controller
# Note for plugs: oder is important, as a plug may stop
# want to stop the pipeline!
# Note for plugs: oder is important, as a plug may stop
# want to stop the pipeline!
plug Plug.Logger, log: :debug
plug(Plug.Logger, log: :debug)
# We use replug to allow for runtime configuration in release (as macros such
# as the `plug` call ae evaluated are compile-time).
plug Replug,
plug: Plug.Static,
opts: {HAHandler, :acme_challenges_static_config}
plug Replug,
plug: Plug.Static,
opts: {HAHandler, :assets_static_config}
# We use replug to allow for runtime configuration in release (as macros such
# as the `plug` call ae evaluated are compile-time).
plug(Replug,
plug: Plug.Static,
opts: {HAHandler, :acme_challenges_static_config}
)
plug :match
plug :dispatch
plug(Replug,
plug: Plug.Static,
opts: {HAHandler, :assets_static_config}
)
get "/", do: Controller.index(conn)
plug(:match)
plug(:dispatch)
match _ do
send_resp(conn, 404, "Not found")
end
get("/", do: Controller.index(conn))
match _ do
send_resp(conn, 404, "Not found")
end
end

View file

@ -1,124 +1,124 @@
<!DOCTYPE html>
<html>
<head>
<title>[HA] <%= hostname %></title>
<link rel="stylesheet" href="/static/app.css">
</head>
<body>
<main>
<div>
<img id="logo" src="/static/logo.svg" />
</div>
<head>
<title>[HA] <%= hostname %></title>
<link rel="stylesheet" href="/static/app.css">
</head>
<body>
<main>
<div>
<img id="logo" src="/static/logo.svg" />
</div>
<h1>Recycled Cloud HA handler</h1>
<h1>Recycled Cloud HA handler</h1>
<p>
This service supervises the various components of
the Recycled Cloud's High Availability
infrastruture. Documentation and source code can be
found on <a
href="https://code.recycled.cloud/RecycledCloud/ha-handler">our
software forge</a>.
</p>
<p>
This service supervises the various components of
the Recycled Cloud's High Availability
infrastruture. Documentation and source code can be
found on <a
href="https://code.recycled.cloud/RecycledCloud/ha-handler">our
software forge</a>.
</p>
<hr />
<hr />
<h2>Handler</h2>
<h2>Handler</h2>
<%= otp_app %> <b>v<%= version %></b> (<%= env %>) running on <b><%= hostname %></b>
<%= otp_app %> <b>v<%= version %></b> (<%= env %>) running on <b><%= hostname %></b>
<hr />
<hr />
<h2>HAProxy</h2>
<h2>HAProxy</h2>
<h3>Frontends</h3>
<table>
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>Bytes in</th>
<th>Bytes out</th>
</tr>
</thead>
<tbody>
<%= for entry <- Map.get(haproxy_stats, "Frontend") do %>
<tr>
<td><%= entry["pxname"] %></td>
<td><%= entry["status"] %></td>
<td><%= entry["bin"] %></td>
<td><%= entry["bout"] %></td>
</tr>
<% end %>
</tbody>
</table>
<h3>Frontends</h3>
<table>
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>Bytes in</th>
<th>Bytes out</th>
</tr>
</thead>
<tbody>
<%= for entry <- Map.get(haproxy_stats, "Frontend") do %>
<tr>
<td><%= entry["pxname"] %></td>
<td><%= entry["status"] %></td>
<td><%= entry["bin"] %></td>
<td><%= entry["bout"] %></td>
</tr>
<% end %>
</tbody>
</table>
<h3>Backends</h3>
<table>
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>algo</th>
</tr>
</thead>
<tbody>
<%= for entry <- Map.get(haproxy_stats, "Backend") do %>
<tr>
<td><%= entry["pxname"] %></td>
<td><%= entry["status"] %></td>
<td><%= entry["algo"] %></td>
</tr>
<% end %>
</tbody>
</table>
<h3>Backends</h3>
<table>
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>algo</th>
</tr>
</thead>
<tbody>
<%= for entry <- Map.get(haproxy_stats, "Backend") do %>
<tr>
<td><%= entry["pxname"] %></td>
<td><%= entry["status"] %></td>
<td><%= entry["algo"] %></td>
</tr>
<% end %>
</tbody>
</table>
<h3>Servers</h3>
<table>
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>Mode</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<%= for entry <- Map.get(haproxy_stats, "Server") do %>
<tr>
<td><%= entry["pxname"] %>/<%= entry["svname"] %></td>
<td><%= entry["status"] %></td>
<td><%= entry["mode"] %></td>
<td><%= entry["addr"] %></td>
</tr>
<% end %>
</tbody>
</table>
<h3>Servers</h3>
<table>
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>Mode</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<%= for entry <- Map.get(haproxy_stats, "Server") do %>
<tr>
<td><%= entry["pxname"] %>/<%= entry["svname"] %></td>
<td><%= entry["status"] %></td>
<td><%= entry["mode"] %></td>
<td><%= entry["addr"] %></td>
</tr>
<% end %>
</tbody>
</table>
<hr />
<hr />
<h2>PostgreSQL</h2>
<h2>PostgreSQL</h2>
<table>
<thead>
<tr>
<th>Hostname</th>
<th>Version</th>
<th>Status</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
<%= for entry <- pgsql_status do %>
<tr>
<td><%= entry[:hostname] %></td>
<td><%= entry[:version] %></td>
<td><%= entry[:status] %></td>
<td><%= entry[:mode] %></td>
</tr>
<% end %>
</tbody>
</table>
</main>
</body>
<table>
<thead>
<tr>
<th>Hostname</th>
<th>Version</th>
<th>Status</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
<%= for entry <- pgsql_status do %>
<tr>
<td><%= entry[:hostname] %></td>
<td><%= entry[:version] %></td>
<td><%= entry[:status] %></td>
<td><%= entry[:mode] %></td>
</tr>
<% end %>
</tbody>
</table>
</main>
</body>
</html>