Be careful with how you quit attached consoles in production systems :)

Configure Prod and Dev Cluster Releases

Building Prod Release

We start by adding a new section to rebar.config called profiles, and define
4 profiles that override the default release config with specific values,
let's start by trying the prod profile, which we will use to create production
releases of the project:

The results of the commands run "as prod" are stored in the prod folder.

You will notice if you explore the prod/rel/cadena folder that there's a folder
called erts-8.3 (the version may differ if you are using a different erlang
version), that folder is there because of the include_erts option we overrided
in the prod profile.

This means you can zip the _build/prod/rel/cadena folder, upload it to a server
that doesn't have erlang installed in it and still run your release there.

This is a good way to be sure that the version running in production is the
same you use in development or at build time in your build server.

Just be careful with deploying to an operating system too different to the one
you used to create the release becase you may have problems with bindings like
libc or openssl.

Joining the Cluster Together

Until here we built 3 releases of the same code with slight modifications to
allow running a cluster on one computer, but 3 nodes running doesn't mean
we have a cluster, for that we need to use what we learned in the Multi-Paxos with riak_ensemble Part 1 but now on code and not interactively.

join([NodeStr])->% node name comes as a list string, we need it as an atomNode=list_to_atom(NodeStr),% check that the node exists and is alivecasenet_adm:ping(Node)of% if not, return an errorpang->{error,not_reachable};% if it replies, let's join him passing our node referencepong->riak_ensemble_manager:join(Node,node())end.create([])->% enable riak_ensemble_managerriak_ensemble_manager:enable(),% wait until it stabilizeswait_stable().cluster_status()->caseriak_ensemble_manager:enabled()offalse->{error,not_enabled};true->Nodes=lists:sort(riak_ensemble_manager:cluster()),io:format("Nodes in cluster: ~p~n",[Nodes]),LeaderNode=node(riak_ensemble_manager:get_leader_pid(root)),io:format("Leader: ~p~n",[LeaderNode])end.

We also need to add the riak_ensemble supervisor to our supervisor tree in cadena_sup:

init([])->% get the configuration from sys.configDataRoot=application:get_env(riak_ensemble,data_root,"./data"),% create a unique path for each node to avoid clashes if running more% than one node in the same computerNodeDataDir=filename:join(DataRoot,atom_to_list(node())),Ensemble={riak_ensemble_sup,{riak_ensemble_sup,start_link,[NodeDataDir]},permanent,20000,supervisor,[riak_ensemble_sup]},{ok,{{one_for_all,0,1},[Ensemble]}}.

Before building the dev cluster we need to add the crypto app to cadena.app.src
since it's needed by riak_ensemble to create the cluster.

Now let's build the dev cluster, I created a Makefile to make it simpler: