Skip to content

Commit

Permalink
Fixed bugs, now auto-connect works!
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunbajaj committed Sep 18, 2023
1 parent 8e18997 commit a888d2f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,12 @@ tailscale-*.tar

# Temporary files, for example, from tests.
/tmp/

# LSP, Editors and OS
.DS_Store
.elixir_ls/
.elixir-tools/
.lexical/
.vscode/
.zed/
.idea/
4 changes: 4 additions & 0 deletions lib/tailscale.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ defmodule Tailscale do
def status do
Tailscale.Local.Status.get!()
end

def topology do
GenServer.call(Tailscale.Cluster, :cluster_topology)
end
end
2 changes: 1 addition & 1 deletion lib/tailscale/change_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule Tailscale.ChangeServer do
end

def handle_continue(:refresh, %{refresh_interval: refresh_interval} = state) do
Logger.debug("Refreshing Tailscale Status")
Logger.info("Refreshing Tailscale Status")

old_status = state.status_map
new_status = Tailscale.Status.get!()
Expand Down
19 changes: 14 additions & 5 deletions lib/tailscale/cluster.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Tailscale.Cluster do

def init(opts) do
tags = opts[:tags]
match = opts[:match_tags] || :all
match = opts[:match_tags] || :any

if opts[:start_distribution] != false do
start_distribution(Tailscale.ChangeServer.get_status().self)
Expand Down Expand Up @@ -116,11 +116,17 @@ defmodule Tailscale.Cluster do

def handle_info(:ensure_connected, state) do
Process.send_after(self(), :ensure_connected, @ensure_interval)
{:noreply, ensure_connected(state)}
state = ensure_connected(state)
Logger.info("Tailscale Status: #{inspect(state.cluster_topology)}")
{:noreply, state}
end

def handle_call(:cluster_topology, state) do
{:reply, state.cluster_topology, state}
end

def terminate(reason, _state) do
Logger.debug("Tailscale.Cluster is terminating: #{inspect(reason)}.")
Logger.info("Tailscale.Cluster is terminating: #{inspect(reason)}.")
Node.stop()
end

Expand All @@ -141,6 +147,7 @@ defmodule Tailscale.Cluster do
_ ->
case Node.start(self.node, :longnames) do
{:ok, _pid} ->
Logger.info("[Tailscale] Started Erlang distribution: `#{self.node}`.")
:ok

{:error, _} ->
Expand All @@ -153,7 +160,7 @@ defmodule Tailscale.Cluster do
Tailscale.ChangeServer.get_status()
|> Map.get(:peers)
|> Enum.filter(&check_if_peer_matches_tags(&1, state))
|> Enum.filter(fn peer -> peer.online == true end)
|> Enum.filter(fn peer -> peer.online? == true end)
|> Enum.map(fn peer ->
case Node.connect(peer.node) do
true -> {peer.id, peer}
Expand All @@ -171,7 +178,7 @@ defmodule Tailscale.Cluster do
if state.disconnect_self_handler != nil do
state.disconnect_self_handler.(reason)
else
Logger.debug("Restarting Application: #{msg}")
Logger.info("Restarting Application: #{msg}")
System.stop(1)
end

Expand All @@ -182,6 +189,7 @@ defmodule Tailscale.Cluster do
state =
case Node.connect(peer.node) do
true ->
Logger.info("Connected to node: #{peer.node}")
update_in(state.cluster_topology, fn topology -> Map.put(topology, peer.id, peer) end)

false ->
Expand All @@ -195,6 +203,7 @@ defmodule Tailscale.Cluster do
state =
case Node.disconnect(peer.node) do
true ->
Logger.info("Disconnected from node: #{peer.node}")
update_in(state.cluster_topology, fn topology -> Map.delete(topology, peer.id) end)

false ->
Expand Down
16 changes: 11 additions & 5 deletions lib/tailscale/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,29 @@ defmodule Tailscale.Supervisor do

@impl true
def init(opts) do
tags = cond do
opts[:tag] != nil and is_binary(opts[:tag]) -> [opts[:tag]]
opts[:tags] != nil and is_list(opts[:tags]) -> opts[:tags]
true -> ["beam"]
end

change_server =
{Tailscale.ChangeServer, [refresh_interval: opts[:refresh_interval] || 30_000]}

cluster =
{Tailscale.Cluster,
[
tags: opts[:tags],
match_tags: opts[:match_tags] || :all,
tags: tags,
match_tags: opts[:match_tags],
disconnect_self_handler: opts[:disconnect_self_handler],
start_distribution: opts[:start_distribution] || true
]}

children =
if opts[:start_cluster] == true do
[change_server, cluster]
else
if opts[:start_cluster] == false do
[change_server]
else
[change_server, cluster]
end

Supervisor.init(children, strategy: :rest_for_one)
Expand Down

0 comments on commit a888d2f

Please sign in to comment.