Search code examples
erlangdistributederlang-otp

How can we load sys.config in slave nodes in Erlang?


I have encountered an issue retrieving sys.config values using application:get_env/2 in slave nodes. I have started slave nodes using peer:start/1 as follows.

spawn_node(NodeName) ->
    Cookie = atom_to_list(erlang:get_cookie()),
    InetLoaderArgs =
        "-loader inet -hosts 127.0.0.1
        -config ../../config/sys.config
        -setcookie "
        ++ Cookie,
    {ok, _Pid, Node} = start_node(NodeName, InetLoaderArgs),
    pong = net_adm:ping(Node),
    ct:log("Slave started as ~p~n", [Node]),
    ok = rpc:block_call(Node, code, add_paths, [code:get_path()]),
    {ok, _} = rpc:block_call(Node, application, ensure_all_started, [toy_kv]),
    Node.

start_node(NodeName, InetLoaderArgs) ->
    peer:start(#{name => node_name(NodeName),
                 host => "127.0.0.1",
                 args => [InetLoaderArgs]}).

When slave nodes try application:get_env/2, it returns undefined. It seems the configuration is not loaded in peer nodes. What am I missing here?

PS -

After loading the application, I know we can set the configurations by executing application:set_env(Application, Par, Val)

ok = rpc:call(Node, application, set_env, [toy_kv, mode, distributed])

But I want to know, in peer nodes, whether we can load the configuration using the sys.config file or not.


Solution

  • We can sys.config to args option of peer:start(Options) like this,

    InetLoaderArgs = ["-loader", "inet", "-hosts", "127.0.0.1",
                "-config", "../../config/sys.config"
                "-setcookie", Cookie]
    

    I have written this blog post on starting slaves using the peer module. Also, this blog post comprehensively explains distributed app testing in Erlang.