Skip to content

Commit

Permalink
made ns_cluster_membership:add_node work
Browse files Browse the repository at this point in the history
Change-Id: Id947f9a7aeca91188de917efe0c61a43287c6b0a
Reviewed-on: http://review.northscale.com:8080/307
Tested-by: Steve Yen <steve.yen@gmail.com>
Reviewed-by: Steve Yen <steve.yen@gmail.com>
  • Loading branch information
Aliaksey Kandratsenka authored and steveyen committed Jun 7, 2010
1 parent f2b8ea2 commit 91963cb
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 17 deletions.
34 changes: 23 additions & 11 deletions deps/menelaus/priv/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1161,43 +1161,55 @@ var ServersSection = {
form.bind('submit', function (e) {
e.preventDefault();

function simpleValidation() {
var p = {};
_.each("clusterMemberHostIp clusterMemberPort user password".split(' '), function (name) {
p[name] = form.find('[name=' + name + ']').val();
});
var data = {}
_.each("clusterMemberHostIp clusterMemberPort user password".split(' '), function (name) {
data[name] = form.find('[name=' + name + ']').val();
});

function simpleValidation(data) {
var errors = [];

if (p['clusterMemberHostIp'] == "")
if (data['clusterMemberHostIp'] == "")
errors.push("Web Console IP Address cannot be blank.");
if (p['clusterMemberPort'] == '')
if (data['clusterMemberPort'] == '')
errors.push("Web Console Port cannot be blank.");
if ((p['user'] || p['password']) && !(p['user'] && p['password'])) {
if ((data['user'] || data['password']) && !(data['user'] && data['password'])) {
errors.push("Username and Password must either both be present or missing.");
}

return errors;
}

var errors = simpleValidation();
var errors = simpleValidation(data);
if (errors.length) {
renderTemplate('join_cluster_dialog_errors', errors);
return;
}

$('#join_cluster_dialog_errors_container').html('');
var overlay = overlayWithSpinner(form);

var uri = self.poolDetails.value.controllers.addNode.uri;
self.poolDetails.setValue(undefined);
postWithValidationErrors(uri, form, function (data, status) {

var toSend = {
hostname: data['clusterMemberHostIp'],
user: data['user'],
password: data['password']
};
if (data['clusterMemberPort'] != '8080')
toSend['hostname'] += ':' + data['clusterMemberPort']

postWithValidationErrors(uri, $.param(toSend), function (data, status) {
self.poolDetails.invalidate();
overlay.remove();
if (status != 'success') {
overlay.remove();
renderTemplate('join_cluster_dialog_errors', data)
} else {
hideDialog('join_cluster_dialog');
}
}, {
timeout: 15000
})
});
},
Expand Down
3 changes: 2 additions & 1 deletion deps/menelaus/src/menelaus_rest.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

%% API

-export([rest_url/3, json_request/3, rest_get_json/2, rest_get_otp/3, rest_engage_cluster/4]).
-export([rest_url/3, json_request/3, rest_get_json/2,
rest_get_otp/3, rest_engage_cluster/4]).

rest_url(Host, Port, Path) ->
"http://" ++ Host ++ ":" ++ integer_to_list(Port) ++ Path.
Expand Down
43 changes: 40 additions & 3 deletions deps/menelaus/src/menelaus_web.erl
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ loop(Req, AppRoot, DocRoot) ->
case PathTokens of
["engageCluster"] ->
{auth, fun handle_engage_cluster/1};
["node", "controller", "doJoinCluster"] ->
["addNodeRequest"] ->
{auth, fun handle_add_node_request/1};
["node", "controller", "doJoinCluster"] ->
{auth, fun handle_join/1};
["node", "controller", "initStatus"] ->
{auth, fun handle_init_status_post/1};
Expand Down Expand Up @@ -289,6 +291,24 @@ handle_engage_cluster(Req) ->
_ -> nothing
end.

handle_add_node_request(Req) ->
Params = Req:parse_post(),
OtpNode = proplists:get_value("otpNode", Params, undefined),
OtpCookie = proplists:get_value("otpCookie", Params, undefined),
case {OtpNode, OtpCookie} of
{undefined, _} ->
reply_json(Req, [<<"Missing parameter(s)">>], 400);
{_, undefined} ->
reply_json(Req, [<<"Missing parameter(s)">>], 400);
_ ->
erlang:process_flag(trap_exit, true),
case ns_cluster_membership:handle_add_node_request(list_to_atom(OtpNode),
list_to_atom(OtpCookie)) of
ok -> reply_json(Req, <<"ok">>, 200);
{failed, Msg} -> reply_json(Req, [list_to_binary(Msg)], 400)
end
end.

build_versions() ->
[{implementationVersion, implementation_version()},
{componentsVersion, {struct,
Expand Down Expand Up @@ -1269,9 +1289,26 @@ handle_node_settings_post(Node, Req) ->
Errs -> Req:respond({400, add_header(), lists:flatten(Errs)})
end.

%% TODO
validate_add_node_params(_Hostname, _Port, _User, _Password) ->
ok.

handle_add_node(Req) ->
Req:respond({200, [], []}).
%% parameter example: hostname=epsilon.local, user=Administrator, password=asd!23
Params = Req:parse_post(),
{Hostname, Port} = case string:tokens(proplists:get_value("hostname", Params, ""), ":") of
[N] -> {N, 8080};
[N, P] -> {N, list_to_integer(P)}
end,
User = proplists:get_value("user", Params, ""),
Password = proplists:get_value("password", Params, ""),
case validate_add_node_params(Hostname, Port, User, Password) of
ok ->
process_flag(trap_exit, true),
ns_cluster_membership:add_node(Hostname, Port, User, Password),
timer:sleep(3000),
Req:respond({200, [], []});
ErrorList -> reply_json(Req, ErrorList, 400)
end.

%% TODO
handle_failover(Req) ->
Expand Down
20 changes: 18 additions & 2 deletions src/ns_cluster_membership.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
add_node/4,
engage_cluster/1,
engage_cluster/2,
handle_add_node_request/2,
join_cluster/4]).

-export([ns_log_cat/1,
Expand Down Expand Up @@ -80,9 +81,24 @@ engage_cluster(RemoteIP, Options) ->
{failed, ErrorMsg}
end.

%% TODO
add_node(OtherHost, OtherPort, OtherUser, OtherPswd) ->
ok.
case engage_cluster(OtherHost) of
ok ->
URL = menelaus_rest:rest_url(OtherHost, OtherPort, "/addNodeRequest"),
menelaus_rest:json_request(post,
{URL, [], "application/x-www-form-urlencoded",
mochiweb_util:urlencode([{<<"otpNode">>, node()},
{<<"otpCookie">>, erlang:get_cookie()}])},
{OtherUser, OtherPswd});
X -> X
end.

handle_add_node_request(OtpNode, OtpCookie) ->
[_Local, Hostname] = string:tokens(atom_to_list(OtpNode), "@"),
case engage_cluster(Hostname, []) of
ok -> ns_cluster:join(OtpNode, OtpCookie);
X -> X
end.

handle_join_rest_failure(ReturnValue, OtherHost, OtherPort) ->
case ReturnValue of
Expand Down

0 comments on commit 91963cb

Please sign in to comment.