Ensure hostname is properly resolved when running in shortname distribution (#303)

* Ensure hostname is properly resolved when running in shortname distribution

* Update lib/livebook/application.ex

Co-authored-by: José Valim <jose.valim@dashbit.co>

* Pretty print host resolution error

* Add missing env var

Co-authored-by: José Valim <jose.valim@dashbit.co>
This commit is contained in:
Jonatan Kłosko 2021-05-28 13:41:08 +02:00 committed by GitHub
parent ee027f7fd8
commit d6c9ab1783
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,6 +7,7 @@ defmodule Livebook.Application do
def start(_type, _args) do
ensure_distribution!()
validate_hostname_resolution!()
set_cookie()
# We register our own :standard_error below
@ -83,6 +84,27 @@ defmodule Livebook.Application do
end
end
# See https://github.com/elixir-nx/livebook/issues/302
defp validate_hostname_resolution!() do
unless Livebook.Config.longname() do
hostname = Livebook.Utils.node_host() |> to_charlist()
if :inet.gethostbyname(hostname) == {:error, :nxdomain} do
Livebook.Config.abort!("""
your hostname "#{hostname}" does not resolve to any IP address, which indicates something wrong in your OS configuration.
Make sure your computer's name resolves locally or start Livebook using a long distribution name. If you are using Livebook's CLI, you can:
livebook server --name livebook@127.0.0.1
If you are running it from source, do instead:
MIX_ENV=prod elixir --name livebook@127.0.0.1 -S mix phx.server
""")
end
end
end
defp set_cookie() do
cookie = Application.fetch_env!(:livebook, :cookie)
Node.set_cookie(cookie)