Created
March 10, 2017 02:20
-
-
Save lucca65/a8abcc481dfbcc10dab6b2b80088a72d to your computer and use it in GitHub Desktop.
Gen server that handle nerves nodes changing its IP and updates its node name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.DistributionManager do | |
use GenServer | |
require Logger | |
@app Mix.Project.config[:app] | |
def start_link(iface) do | |
GenServer.start_link(__MODULE__, iface) | |
end | |
def init(iface) do | |
iface = to_string(iface) | |
:os.cmd 'epmd -daemon' | |
{:ok, pid} = Registry.register(Nerves.Udhcpc, iface, []) | |
{:ok, %{registry: pid, iface: iface}} | |
end | |
def handle_info({Nerves.Udhcpc, event, %{ipv4_address: ip}}, s) | |
when event in [:bound, :renew] do | |
Logger.info "IP Address Changed" | |
:net_kernel.stop() | |
:net_kernel.start([:"#{@app}@#{ip}"]) | |
{:noreply, s} | |
end | |
def handle_info(_event, s) do | |
{:noreply, s} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment