Skip to content
This repository was archived by the owner on May 25, 2021. It is now read-only.

Commit 616789b

Browse files
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: #2 Reviewed-By: Jan Lehnardt <[email protected]> Reviewed-By: Alexander Shorin <[email protected]>
1 parent ecb601b commit 616789b

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

src/setup.erl

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,47 @@ has_cluster_system_dbs([Db|Dbs]) ->
6161
end.
6262

6363
enable_cluster(Options) ->
64-
enable_cluster_int(Options, is_cluster_enabled()).
64+
65+
case couch_util:get_value(remote_node, Options, undefined) of
66+
undefined ->
67+
enable_cluster_int(Options, is_cluster_enabled());
68+
_ ->
69+
enable_cluster_http(Options)
70+
end.
71+
72+
enable_cluster_http(Options) ->
73+
% POST to nodeB/_setup
74+
RequestOptions = [
75+
{basic_auth, {
76+
binary_to_list(couch_util:get_value(remote_current_user, Options)),
77+
binary_to_list(couch_util:get_value(remote_current_password, Options))
78+
}}
79+
],
80+
81+
Body = ?JSON_ENCODE({[
82+
{<<"action">>, <<"enable_cluster">>},
83+
{<<"username">>, couch_util:get_value(username, Options)},
84+
{<<"password">>, couch_util:get_value(password, Options)},
85+
{<<"bind_address">>, couch_util:get_value(bind_address, Options)},
86+
{<<"port">>, couch_util:get_value(port, Options)}
87+
]}),
88+
89+
Headers = [
90+
{"Content-Type","application/json"}
91+
],
92+
93+
RemoteNode = couch_util:get_value(remote_node, Options),
94+
Port = get_port(couch_util:get_value(port, Options, 5984)),
95+
96+
Url = binary_to_list(<<"http://", RemoteNode/binary, ":", Port/binary, "/_cluster_setup">>),
97+
98+
case ibrowse:send_req(Url, Headers, post, Body, RequestOptions) of
99+
{ok, "201", _, _} ->
100+
ok;
101+
Else ->
102+
io:format("~nsend_req: ~p~n", [Else]),
103+
{error, Else}
104+
end.
65105

66106
enable_cluster_int(_Options, ok) ->
67107
{error, cluster_enabled};

src/setup_httpd.erl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ handle_action("enable_cluster", Setup) ->
5555
{username, <<"username">>},
5656
{password, <<"password">>},
5757
{bind_address, <<"bind_address">>},
58-
{port, <<"port">>}
58+
{port, <<"port">>},
59+
{remote_node, <<"remote_node">>},
60+
{remote_current_user, <<"remote_current_user">>},
61+
{remote_current_password, <<"remote_current_password">>}
5962
], Setup),
6063
case setup:enable_cluster(Options) of
6164
{error, cluster_enabled} ->

test/t-frontend-setup.sh

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/sh -ex
2+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
3+
# use this file except in compliance with the License. You may obtain a copy of
4+
# the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
# License for the specific language governing permissions and limitations under
12+
# the License.
13+
14+
HEADERS="-HContent-Type:application/json"
15+
# show cluster state:
16+
curl a:[email protected]:15986/_nodes/_all_docs
17+
18+
# Enable Cluster on node A
19+
curl a:[email protected]:15984/_cluster_setup -d '{"action":"enable_cluster","username":"foo","password":"baz","bind_address":"0.0.0.0"}' $HEADERS
20+
21+
# Enable Cluster on node B
22+
curl a:[email protected]: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
23+
24+
# Add node B on node A
25+
curl a:[email protected]:15984/_cluster_setup -d '{"action":"add_node","username":"foo","password":"baz","host":"127.0.0.1","port":25984}' $HEADERS
26+
27+
# Show cluster state:
28+
curl a:[email protected]:15986/_nodes/_all_docs
29+
30+
# Show db doesn’t exist on node A
31+
curl a:[email protected]:15984/foo
32+
33+
# Show db doesn’t exist on node B
34+
curl a:[email protected]:25984/foo
35+
36+
# Create database (on node A)
37+
curl -X PUT a:[email protected]:15984/foo
38+
39+
# Show db does exist on node A
40+
curl a:[email protected]:15984/foo
41+
42+
# Show db does exist on node B
43+
curl a:[email protected]:25984/foo
44+
45+
# Finish cluster
46+
curl a:[email protected]:15984/_cluster_setup -d '{"action":"finish_cluster"}' $HEADERS
47+
48+
# Show system dbs exist on node A
49+
curl a:[email protected]:15984/_users
50+
curl a:[email protected]:15984/_replicator
51+
curl a:[email protected]:15984/_metadata
52+
curl a:[email protected]:15984/_global_changes
53+
54+
# Show system dbs exist on node B
55+
curl a:[email protected]:25984/_users
56+
curl a:[email protected]:25984/_replicator
57+
curl a:[email protected]:25984/_metadata
58+
curl a:[email protected]:25984/_global_changes
59+
60+
echo "YAY ALL GOOD"

0 commit comments

Comments
 (0)