summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Kowalski <rok@kowalski.gd>2015-05-26 02:33:50 +0200
committerRobert Kowalski <robertkowalski@apache.org>2015-06-24 15:37:16 +0200
commit616789bac1bcdf9897e6725baaf0249f742389fd (patch)
tree4de15fa47756e6edadbfcc62133347c05916f241
parentecb601b2004f33f396530a6ae974f2f0bff4d816 (diff)
downloadcouchdb-616789bac1bcdf9897e6725baaf0249f742389fd.tar.gz
cluster_enable: add remote_node feature
this feature makes it easier to setup a cluster for browser applications like fauxton as browsers follow the same-origin policy. Before this PR you had to open the wizard in Fauxton on all three nodes and enter your data there, which was quite confusing and hard to explain. Now you can stay in the same tab at the same address. This PR enables three new params in the body: `remote_node`: ip of the remote node where we want to send the `enable_cluster` request `remote_current_user`: the current admin username of the remote node `remote_current_password`: the current admin password of the remote node To test, I run: ``` rm -rf dev/lib/ && ./dev/run --no-join --admin=a:b ``` and then run the test script: ``` ./src/setup/test/t-frontend-setup.sh ``` COUCHDB-2598 PR: #2 PR-URL: https://github.com/apache/couchdb-setup/pull/2 Reviewed-By: Jan Lehnardt <jan@apache.org> Reviewed-By: Alexander Shorin <kxepal@apache.org>
-rw-r--r--src/setup.erl42
-rw-r--r--src/setup_httpd.erl5
-rwxr-xr-xtest/t-frontend-setup.sh60
3 files changed, 105 insertions, 2 deletions
diff --git a/src/setup.erl b/src/setup.erl
index 194383556..9aa98f945 100644
--- a/src/setup.erl
+++ b/src/setup.erl
@@ -61,7 +61,47 @@ has_cluster_system_dbs([Db|Dbs]) ->
end.
enable_cluster(Options) ->
- enable_cluster_int(Options, is_cluster_enabled()).
+
+ case couch_util:get_value(remote_node, Options, undefined) of
+ undefined ->
+ enable_cluster_int(Options, is_cluster_enabled());
+ _ ->
+ enable_cluster_http(Options)
+ end.
+
+enable_cluster_http(Options) ->
+ % POST to nodeB/_setup
+ RequestOptions = [
+ {basic_auth, {
+ binary_to_list(couch_util:get_value(remote_current_user, Options)),
+ binary_to_list(couch_util:get_value(remote_current_password, Options))
+ }}
+ ],
+
+ Body = ?JSON_ENCODE({[
+ {<<"action">>, <<"enable_cluster">>},
+ {<<"username">>, couch_util:get_value(username, Options)},
+ {<<"password">>, couch_util:get_value(password, Options)},
+ {<<"bind_address">>, couch_util:get_value(bind_address, Options)},
+ {<<"port">>, couch_util:get_value(port, Options)}
+ ]}),
+
+ Headers = [
+ {"Content-Type","application/json"}
+ ],
+
+ RemoteNode = couch_util:get_value(remote_node, Options),
+ Port = get_port(couch_util:get_value(port, Options, 5984)),
+
+ Url = binary_to_list(<<"http://", RemoteNode/binary, ":", Port/binary, "/_cluster_setup">>),
+
+ case ibrowse:send_req(Url, Headers, post, Body, RequestOptions) of
+ {ok, "201", _, _} ->
+ ok;
+ Else ->
+ io:format("~nsend_req: ~p~n", [Else]),
+ {error, Else}
+ end.
enable_cluster_int(_Options, ok) ->
{error, cluster_enabled};
diff --git a/src/setup_httpd.erl b/src/setup_httpd.erl
index aca98e7e5..de1bff5fa 100644
--- a/src/setup_httpd.erl
+++ b/src/setup_httpd.erl
@@ -55,7 +55,10 @@ handle_action("enable_cluster", Setup) ->
{username, <<"username">>},
{password, <<"password">>},
{bind_address, <<"bind_address">>},
- {port, <<"port">>}
+ {port, <<"port">>},
+ {remote_node, <<"remote_node">>},
+ {remote_current_user, <<"remote_current_user">>},
+ {remote_current_password, <<"remote_current_password">>}
], Setup),
case setup:enable_cluster(Options) of
{error, cluster_enabled} ->
diff --git a/test/t-frontend-setup.sh b/test/t-frontend-setup.sh
new file mode 100755
index 000000000..1c610b6cc
--- /dev/null
+++ b/test/t-frontend-setup.sh
@@ -0,0 +1,60 @@
+#!/bin/sh -ex
+# Licensed under the Apache License, Version 2.0 (the "License"); you may not
+# use this file except in compliance with the License. You may obtain a copy of
+# the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations under
+# the License.
+
+HEADERS="-HContent-Type:application/json"
+# show cluster state:
+curl a:b@127.0.0.1:15986/_nodes/_all_docs
+
+# Enable Cluster on node A
+curl a:b@127.0.0.1:15984/_cluster_setup -d '{"action":"enable_cluster","username":"foo","password":"baz","bind_address":"0.0.0.0"}' $HEADERS
+
+# Enable Cluster on node B
+curl a:b@127.0.0.1:15984/_cluster_setup -d '{"action":"enable_cluster","remote_node":"127.0.0.1","port":"25984","remote_current_user":"a","remote_current_password":"b","username":"foo","password":"baz","bind_address":"0.0.0.0"}' $HEADERS
+
+# Add node B on node A
+curl a:b@127.0.0.1:15984/_cluster_setup -d '{"action":"add_node","username":"foo","password":"baz","host":"127.0.0.1","port":25984}' $HEADERS
+
+# Show cluster state:
+curl a:b@127.0.0.1:15986/_nodes/_all_docs
+
+# Show db doesn’t exist on node A
+curl a:b@127.0.0.1:15984/foo
+
+# Show db doesn’t exist on node B
+curl a:b@127.0.0.1:25984/foo
+
+# Create database (on node A)
+curl -X PUT a:b@127.0.0.1:15984/foo
+
+# Show db does exist on node A
+curl a:b@127.0.0.1:15984/foo
+
+# Show db does exist on node B
+curl a:b@127.0.0.1:25984/foo
+
+# Finish cluster
+curl a:b@127.0.0.1:15984/_cluster_setup -d '{"action":"finish_cluster"}' $HEADERS
+
+# Show system dbs exist on node A
+curl a:b@127.0.0.1:15984/_users
+curl a:b@127.0.0.1:15984/_replicator
+curl a:b@127.0.0.1:15984/_metadata
+curl a:b@127.0.0.1:15984/_global_changes
+
+# Show system dbs exist on node B
+curl a:b@127.0.0.1:25984/_users
+curl a:b@127.0.0.1:25984/_replicator
+curl a:b@127.0.0.1:25984/_metadata
+curl a:b@127.0.0.1:25984/_global_changes
+
+echo "YAY ALL GOOD"