Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
wip: full receive feature, setup now works yay
  • Loading branch information
janl committed Nov 7, 2014
1 parent 277ca66 commit 92da54ed202802e4e8cc8f2e5c5e62fd70ea4dd7
Showing 2 changed files with 32 additions and 16 deletions.
@@ -12,7 +12,7 @@

-module(setup).

-export([enable_cluster/1, finish_cluster/0, add_node/1]).
-export([enable_cluster/1, finish_cluster/0, add_node/1, receive_cookie/1]).
-include_lib("../couch/include/couch_db.hrl").


@@ -121,8 +121,8 @@ add_node_int(Options, ok) ->
% POST to nodeB/_setup
RequestOptions = [
{basic_auth, {
proplists:get_value(username, Options),
proplists:get_value(password, Options)
binary_to_list(proplists:get_value(username, Options)),
binary_to_list(proplists:get_value(password, Options))
}}
],

@@ -136,24 +136,41 @@ add_node_int(Options, ok) ->
],

Host = proplists:get_value(host, Options),
Port = proplists:get_value(port, Options, <<"5984">>),
Url = binary_to_list(<<"http://", Host/binary, ":", Port/binary, "/_setup">>),

io:format("~nUrl: ~p~n", [Url]),
io:format("~nBody: ~p~n", [Body]),
Port = integer_to_binary(proplists:get_value(port, Options, 5984)),
Url = binary_to_list(<<"http://", Host/binary, ":", Port/binary, "/_cluster_setup">>),

case ibrowse:send_req(Url, Headers, post, Body, RequestOptions) of
{ok, 200, _, _} ->
{ok, "201", _, _} ->
% when done, PUT :5986/nodes/nodeB
create_node_doc(Host, Port);
Else ->
io:format("~nsend_req: ~p~n", [Else]),
Else
end.


create_node_doc(Host, Port) ->
{ok, Db} = couch_db:open_int("nodes"),
Doc = {[{<<"_id">>, <<Host/binary, ":", Port/binary>>}]},
{ok, Db} = couch_db:open_int(<<"nodes">>, []),
Name = get_name(Port),
Doc = {[{<<"_id">>, <<Name/binary, "@", Host/binary>>}]},
Options = [],
couch_db:update_doc(Db, Doc, Options).
CouchDoc = couch_doc:from_json_obj(Doc),

couch_db:update_doc(Db, CouchDoc, Options).

get_name(Port) ->
case Port of
% shortcut for easier development
<<"15984">> ->
<<"node1">>;
<<"25984">> ->
<<"node2">>;
<<"35984">> ->
<<"node3">>;
% by default, all nodes have the user `couchdb`
_ ->
<<"couchdb">>
end.

receive_cookie(Options) ->
Cookie = proplists:get_value(cookie, Options),
erlang:set_cookie(node(), binary_to_atom(Cookie, latin1)).
@@ -16,8 +16,7 @@

handle_setup_req(Req) ->
ok = chttpd:verify_is_server_admin(Req),
% TBD uncomment after devving
%couch_httpd:validate_ctype(Req, "application/json"),
couch_httpd:validate_ctype(Req, "application/json"),
Setup = get_body(Req),
io:format("~nSetup: ~p~n", [Setup]),
Action = binary_to_list(couch_util:get_value(<<"action">>, Setup, <<"missing">>)),
@@ -87,7 +86,7 @@ handle_action("remove_node", Setup) ->
handle_action("receive_cookie", Setup) ->
io:format("~nreceive_cookie: ~p~n", [Setup]),
Options = get_options([
{cookue, <<"cookie">>}
{cookie, <<"cookie">>}
], Setup),
case setup:receive_cookie(Options) of
{error, Error} ->

0 comments on commit 92da54e

Please sign in to comment.