You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by va...@apache.org on 2021/12/08 21:33:58 UTC

[couchdb] branch 3.x updated: Fix TLS custom (couch) dist for Erlang 20

This is an automated email from the ASF dual-hosted git repository.

vatamane pushed a commit to branch 3.x
in repository https://gitbox.apache.org/repos/asf/couchdb.git


The following commit(s) were added to refs/heads/3.x by this push:
     new 728f3b9  Fix TLS custom (couch) dist for Erlang 20
728f3b9 is described below

commit 728f3b936b2eb7f9572ebf90d6887a3c2849f571
Author: Nick Vatamaniuc <va...@gmail.com>
AuthorDate: Mon Dec 6 17:21:09 2021 -0500

    Fix TLS custom (couch) dist for Erlang 20
    
     * Fix quoting so that it works with all OTP versions 20 through 24 [1].
    
     * Dist API in 20 [2] did not have a `listen/2` [3] callback. Implement
       `listen/1` so we're compatible with all the supported OTP versions.
    
    [1] https://github.com/apache/couchdb/issues/3821#issuecomment-985089867
    [2] https://github.com/erlang/otp/blob/maint-20/lib/kernel/src/inet_tcp_dist.erl#L71-L72
    [3] https://github.com/erlang/otp/blob/master/lib/kernel/src/inet_tcp_dist.erl#L79-L80
---
 rel/overlay/etc/vm.args           |  2 +-
 src/couch_dist/src/couch_dist.erl | 43 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/rel/overlay/etc/vm.args b/rel/overlay/etc/vm.args
index 805e9ec..8da600b 100644
--- a/rel/overlay/etc/vm.args
+++ b/rel/overlay/etc/vm.args
@@ -91,5 +91,5 @@
 ## Don't forget to override the paths to point to your certificate(s) and key(s)!
 ##
 #-proto_dist couch
-#-couch_dist no_tls \"clouseau@127.0.0.1\"
+#-couch_dist no_tls '"clouseau@127.0.0.1"'
 #-ssl_dist_optfile <path/to/couch_ssl_dist.conf>
diff --git a/src/couch_dist/src/couch_dist.erl b/src/couch_dist/src/couch_dist.erl
index 9a6b26d..a0922eb 100644
--- a/src/couch_dist/src/couch_dist.erl
+++ b/src/couch_dist/src/couch_dist.erl
@@ -14,6 +14,7 @@
 
 -export([
     childspecs/0,
+    listen/1,
     listen/2,
     accept/1,
     accept_connection/5,
@@ -33,6 +34,16 @@ childspecs() ->
         ]}
     ]}.
 
+listen(Name) ->
+    NodeName =
+        case is_atom(Name) of
+            true -> atom_to_list(Name);
+            false -> Name
+        end,
+    Host = get_node_host(),
+    Mod = inet_dist(NodeName ++ "@" ++ Host),
+    Mod:listen(NodeName).
+
 listen(Name, Host) ->
     NodeName =
         case is_atom(Name) of
@@ -66,6 +77,38 @@ is_node_name(Node) ->
 get_init_args() ->
     init:get_argument(couch_dist).
 
+get_node_host() ->
+    % Cannot use `node()` since distribution hasn't started yet. Use
+    % similar logic as erl_distribition and net_kernel to parse it
+    % from the arguments list
+    case {init:get_argument(sname), init:get_argument(name)} of
+        {{ok, [[SName]]}, _} ->
+            case split_host(SName) of
+                [$@ | Host] when length(Host) > 0 ->
+                    Host;
+                _ ->
+                    inet_db:gethostname()
+            end;
+        {error, {ok, [[Name]]}} ->
+            case split_host(Name) of
+                [$@ | Host] when length(Host) > 0 ->
+                    Host;
+                _ ->
+                    OwnHost = inet_db:gethostname(),
+                    case inet_db:res_option(domain) of
+                        Domain when is_list(Domain), length(Domain) > 0 ->
+                            OwnHost ++ "." ++ Domain;
+                        _ ->
+                            OwnHost
+                    end
+            end
+    end.
+
+split_host(Name) ->
+    % Copied from net_kernel. Modifed to return Host only
+    {_, Host} = lists:splitwith(fun(C) -> C =/= $@ end, Name),
+    Host.
+
 inet_dist(Node) ->
     case no_tls(Node) of
         true -> inet_tcp_dist;