Setting node name dynamically at boot time

2019-07-02 07:33发布

问题:

I am building my release using relx and I can not use the default -sname someuser@127.0.0.1. Instead I would like to do something like -sname someuser@`hostname -i` to define the node's public IP at boot time. Using -sname someuser@hostname -i`` does not work because hostname -i is not expanded to a real IP. I tried to use net_kernel:start(xxxxxxx) but it did not work.

Is there any way to get a dynamic -sname in vm.args using relx ?

Thanks :)

回答1:

As written in the documentation (and answered in the comments by @Soup d'Campbells), the -sname and -name flags do not need the hostname, and will automatically fill the Host part of the node's ID with the host'sid (short not fully qualified for -sname, FQDN for -name):

-sname Name Makes the Erlang runtime system into a distributed node, similar to -name, but the host name portion of the node name Name@Host will be the short name, not fully qualified.

This is sometimes the only way to run distributed Erlang if the DNS (Domain Name System) is not running. There can be no communication between nodes running with the -sname flag and those running with the -name flag, as node names must be unique in distributed Erlang systems.

-name Name Makes the Erlang runtime system into a distributed node. This flag invokes all network servers necessary for a node to become distributed. See net_kernel(3). It is also ensured that epmd runs on the current host before Erlang is started. See epmd(1).

The name of the node will be Name@Host, where Host is the fully qualified host name of the current host. For short names, use the -sname flag instead.

If you plan to use a distributed node, and you do have DNS in your domain, use the -name flag, as it will also invoke all necessary network servers (just as written above).

See for yourself:

$> erl -sname bouly
(bouly@myserver)1>

$> erl -name bouly
(bouly@myserver.myhome.net)1>


回答2:

Seems you have misfed net_kernel:start() with parameters. The proper form of argument is [Name, shortnames] or [Name, longnames]; Name is atom which shall be formed as "localpart" or "localpart@host" (likely you'll need list_to_atom() for this). For example:

2> net_kernel:start(['abc@localhost', shortnames]).
{ok,<0.39.0>}

and, as result:

$ epmd -names
epmd: up and running on port 4369 with data:
name abc at port 46623


标签: erlang relx