Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir: Set cookie and hostname for release created using mix escript.build

Tags:

elixir

When running iex interactively, one can use

iex --cookie <cookie> --name <hostname>

How do I set the same values for cookie and name when running an executable created using mix escript.build?

I figured out that I need to create a vm.args file with the following contents

## Name of the node
-name name@host

## Cookie for distributed erlang
-setcookie cookie

So I created a vm.args file in the same directory as the executable file. But when I print Node.self(), I get :nonode@nohost.

So where do I store vm.args so that it is read by the executable?

like image 642
Pramod Avatar asked Dec 21 '16 18:12

Pramod


1 Answers

As far as I know, vm.args is not read by escripts. You have (at least) 2 options:

  1. Set these values in emu_args key passed to escript in project/0 in mix.exs:

    def project do
      [app: :m,
       ...,
       escript: [main_module: M, emu_args: ["-name foo@bar -setcookie baz"]]]
    end
    
  2. Parse the CLI arguments and set the values in your main function:

    defmodule M do
      def main([name, cookie]) do
        Node.start String.to_atom(name)
        Node.set_cookie String.to_atom(cookie)
        IO.inspect {Node.self, Node.get_cookie}
      end
    end
    
    $ mix escript.build
    $ ./m foo@bar baz
    {:foo@bar, :baz}
    
like image 179
Dogbert Avatar answered Sep 18 '22 22:09

Dogbert