feat(agent): supervisor boots reporter when config is present
This commit is contained in:
parent
3ae38f95a9
commit
bfe39e71e1
3 changed files with 35 additions and 11 deletions
7
agent/config/config.exs
Normal file
7
agent/config/config.exs
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
import Config
|
||||||
|
|
||||||
|
config :logger, :default_formatter, format: "$time [$level] $message\n"
|
||||||
|
|
||||||
|
if File.exists?(Path.join([__DIR__, "#{config_env()}.exs"])) do
|
||||||
|
import_config "#{config_env()}.exs"
|
||||||
|
end
|
||||||
5
agent/config/runtime.exs
Normal file
5
agent/config/runtime.exs
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
import Config
|
||||||
|
|
||||||
|
if path = System.get_env("AGENT_CONFIG") do
|
||||||
|
config :agent, :config_path, path
|
||||||
|
end
|
||||||
|
|
@ -1,20 +1,32 @@
|
||||||
defmodule ProxmoxAgent.Application do
|
defmodule ProxmoxAgent.Application do
|
||||||
# See https://hexdocs.pm/elixir/Application.html
|
|
||||||
# for more information on OTP Applications
|
|
||||||
@moduledoc false
|
@moduledoc false
|
||||||
|
|
||||||
use Application
|
use Application
|
||||||
|
require Logger
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def start(_type, _args) do
|
def start(_type, _args) do
|
||||||
children = [
|
children =
|
||||||
# Starts a worker by calling: ProxmoxAgent.Worker.start_link(arg)
|
case load_config() do
|
||||||
# {ProxmoxAgent.Worker, arg}
|
{:ok, cfg} ->
|
||||||
]
|
Logger.info("agent: starting with host_id=#{cfg.host_id}")
|
||||||
|
[{ProxmoxAgent.Reporter, cfg}]
|
||||||
|
|
||||||
# See https://hexdocs.pm/elixir/Supervisor.html
|
{:error, reason} ->
|
||||||
# for other strategies and supported options
|
Logger.error("agent: no config loaded (#{inspect(reason)}); running in idle mode")
|
||||||
opts = [strategy: :one_for_one, name: ProxmoxAgent.Supervisor]
|
[]
|
||||||
Supervisor.start_link(children, opts)
|
end
|
||||||
|
|
||||||
|
Supervisor.start_link(children, strategy: :one_for_one, name: ProxmoxAgent.Supervisor)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp load_config do
|
||||||
|
path =
|
||||||
|
System.get_env("AGENT_CONFIG") ||
|
||||||
|
Application.get_env(:agent, :config_path, "/etc/proxmox-monitor/agent.toml")
|
||||||
|
|
||||||
|
case File.exists?(path) do
|
||||||
|
true -> ProxmoxAgent.Config.load(path)
|
||||||
|
false -> {:error, {:file_missing, path}}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue