diff --git a/agent/config/config.exs b/agent/config/config.exs new file mode 100644 index 0000000..d08a018 --- /dev/null +++ b/agent/config/config.exs @@ -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 diff --git a/agent/config/runtime.exs b/agent/config/runtime.exs new file mode 100644 index 0000000..8e9450e --- /dev/null +++ b/agent/config/runtime.exs @@ -0,0 +1,5 @@ +import Config + +if path = System.get_env("AGENT_CONFIG") do + config :agent, :config_path, path +end diff --git a/agent/lib/proxmox_agent/application.ex b/agent/lib/proxmox_agent/application.ex index 4a7a876..264129f 100644 --- a/agent/lib/proxmox_agent/application.ex +++ b/agent/lib/proxmox_agent/application.ex @@ -1,20 +1,32 @@ defmodule ProxmoxAgent.Application do - # See https://hexdocs.pm/elixir/Application.html - # for more information on OTP Applications @moduledoc false - use Application + require Logger @impl true def start(_type, _args) do - children = [ - # Starts a worker by calling: ProxmoxAgent.Worker.start_link(arg) - # {ProxmoxAgent.Worker, arg} - ] + children = + case load_config() do + {:ok, cfg} -> + Logger.info("agent: starting with host_id=#{cfg.host_id}") + [{ProxmoxAgent.Reporter, cfg}] - # See https://hexdocs.pm/elixir/Supervisor.html - # for other strategies and supported options - opts = [strategy: :one_for_one, name: ProxmoxAgent.Supervisor] - Supervisor.start_link(children, opts) + {:error, reason} -> + Logger.error("agent: no config loaded (#{inspect(reason)}); running in idle mode") + [] + 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