Skip to content

Instantly share code, notes, and snippets.

@lucca65
Created March 10, 2017 02:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucca65/a8abcc481dfbcc10dab6b2b80088a72d to your computer and use it in GitHub Desktop.
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
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