defmodule NodeActivator do @moduledoc File.read!("README.md") |> String.split("") |> Enum.fetch!(1) require Logger alias NodeActivator.Epmd alias NodeActivator.Utils @doc """ Turns a non-distributed node into a distributed node after ensuring that the `epmd` operation system process is running. Returns the name of the started distributed node. The node name will be generated by appending a random string to the provided `node_name_prefix` string. This function does nothing when the distribution has already been started. For more info, see `Node`. ## Examples {:ok, node_name} = NodeActivator.run("foo") """ @spec run(binary()) :: {:ok, node()} | {:error, any()} def run(node_name_prefix) do if Node.alive?() do {:ok, Node.self()} else start_distributed_node(node_name_prefix) end end @doc """ Checks if the `epmd` process is running. """ @spec epmd_running?() :: boolean() defdelegate epmd_running?(), to: Epmd defp start_distributed_node(node_name_prefix) do Epmd.launch_epmd() name = Utils.generate_node_name(node_name_prefix) Logger.info("starting node #{name}") case Node.start(name) do {:ok, _node} -> Logger.info("Node #{name} has been launched.") {:ok, Node.self()} {:error, error} -> Logger.error("Node #{name} cannot be launched.") {:error, error} end end end