From 5ce3d5a760bb316426dc24413639dbf6cf5eb576 Mon Sep 17 00:00:00 2001 From: Kyle A Anderson Date: Thu, 30 May 2024 09:30:15 -0700 Subject: [PATCH] Fetch instance metadata at startup only Signed-off-by: Kyle A Anderson --- lib/libcluster_ec2.ex | 1 - lib/strategy/tags.ex | 21 +++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/libcluster_ec2.ex b/lib/libcluster_ec2.ex index 84e7369..6c58930 100644 --- a/lib/libcluster_ec2.ex +++ b/lib/libcluster_ec2.ex @@ -1,5 +1,4 @@ defmodule ClusterEC2 do - @moduledoc File.read!("#{__DIR__}/../README.md") @doc """ diff --git a/lib/strategy/tags.ex b/lib/strategy/tags.ex index 7c02d7f..9b0f2ec 100644 --- a/lib/strategy/tags.ex +++ b/lib/strategy/tags.ex @@ -49,19 +49,32 @@ defmodule ClusterEC2.Strategy.Tags do # libcluster ~> 3.0 @impl GenServer def init([%State{} = state]) do - state = state |> Map.put(:meta, MapSet.new()) + instance_meta = [ + instance_id: ClusterEC2.local_instance_id(), + region: ClusterEC2.instance_region() + ] + + state = + state + |> Map.put(:meta, MapSet.new()) + |> Map.put(:config, state.config ++ instance_meta) {:ok, load(state)} end # libcluster ~> 2.0 def init(opts) do + instance_meta = [ + instance_id: ClusterEC2.local_instance_id(), + region: ClusterEC2.instance_region() + ] + state = %State{ topology: Keyword.fetch!(opts, :topology), connect: Keyword.fetch!(opts, :connect), disconnect: Keyword.fetch!(opts, :disconnect), list_nodes: Keyword.fetch!(opts, :list_nodes), - config: Keyword.fetch!(opts, :config), + config: Keyword.fetch!(opts, :config) ++ instance_meta, meta: MapSet.new([]) } @@ -121,8 +134,8 @@ defmodule ClusterEC2.Strategy.Tags do @spec get_nodes(State.t()) :: {:ok, [atom()]} | {:error, []} defp get_nodes(%State{topology: topology, config: config}) do - instance_id = ClusterEC2.local_instance_id() - region = ClusterEC2.instance_region() + instance_id = Keyword.get(config, :instance_id, "") + region = Keyword.get(config, :region, "") tag_name = Keyword.fetch!(config, :ec2_tagname) tag_value = Keyword.get(config, :ec2_tagvalue, &local_instance_tag_value(&1, instance_id, region)) app_prefix = Keyword.get(config, :app_prefix, "app")