anchor.diff:              _ -> ok
           end,
   
  -        "0" = os:cmd("riak-admin cluster join " ++ ?DB_NAME ++ "@" ++ ClusterHost
  -            ++ " 1>/dev/null 2>&1 ; echo -n $?"),
  -        ok = wait_until("riak-admin ring_status", "Ring Ready:\s*true"),
  +        JoinCommand = "/opt/couchbase/couchbase-cli rebalance -c " ++ ClusterHost ++ ":8091 -u admin -p " ++ ?COUCHBASE_PASSWORD ++
  +            " --server-add=" ++ Host ++ ":8091 --server-add-username=admin --server-add-password=" ++ ?COUCHBASE_PASSWORD
  +            ++ " 1>/dev/null 2>&1 ; echo -n $?",
  +        ?info("Running couchbase command ~p", [JoinCommand]),
  +        "0" = os:cmd(JoinCommand),
   
           {ok, Host}
       catch
  
positive.code: %% ====================================================================
  local_commit() ->
      try
          ok = wait_until("riak-admin ring_status", "Ring Ready:\s*true"),
          "0" = os:cmd("riak-admin cluster plan 1>/dev/null 2>&1 ; echo -n $?"),
          "0" = os:cmd("riak-admin cluster commit 1>/dev/null 2>&1 ; echo -n $?"),
          CreateMaps = "riak-admin bucket-type create maps '{\"props\":{\"n_val\":2,"
          " \"datatype\":\"map\"}}'",
          case os:cmd(CreateMaps ++ " | grep already_active") of
              "already_active\n" ->
                  ok;
              _ ->
                  "0" = os:cmd(CreateMaps ++ " 1>/dev/null 2>&1 ; echo -n $?"),
                  ok = wait_until("riak-admin bucket-type status maps", "maps has been created and may be activated"),
                  "0" = os:cmd("riak-admin bucket-type activate maps 1>/dev/null 2>&1 ; echo -n $?")
          end,
  
          ok
      catch
          _:Reason ->
  
negative.code:             {ok, Host};
          _:Reason ->
              ?error_stacktrace("Cannot join database cluster: ~p", [Reason]),
              {error, Host}
      end.
  
  
  %% init_cluster/1
  %% ====================================================================
  %% @doc Init database cluster. ClusterHost is one of current
  %% database cluster hosts.
  %% @end
  -spec init_cluster(ClusterHost :: string()) -> Result when
      Result :: {ok, Host :: string()} | {error, Host :: string()}.
  %% ====================================================================
  init_cluster(ClusterHost) ->
      Host = onepanel_utils:get_host(node()),
      try
          catch memsup:start_link(),
          {_, Mem} = proplists:lookup(system_total_memory, memsup:get_system_memory_data()),
          MemMB = erlang:round(Mem / 1024 / 1024),
          MemToAllocate = erlang:round(MemMB / 2),
  
          InitCommand = "/opt/couchbase/couchbase-cli cluster-init -c " ++ ClusterHost ++ ":8091 --cluster-init-username=admin" ++
              " --cluster-init-password=" ++ ?COUCHBASE_PASSWORD ++ " --cluster-init-ramsize=" ++ integer_to_list(MemToAllocate) ++
              " 1>/dev/null 2>&1 ; echo -n $?",
          ?info("Running couchbase command ~p", [InitCommand]),
          "0" = os:cmd(InitCommand),
  
          BucketCommand = "/opt/couchbase/couchbase-cli bucket-create -c " ++ ClusterHost ++ ":8091" ++
              " -u admin -p " ++ ?COUCHBASE_PASSWORD ++ " --bucket=default --bucket-ramsize=" ++ integer_to_list(MemToAllocate) ++
              " 1>/dev/null 2>&1 ; echo -n $?",
          ?info("Running couchbase command ~p", [BucketCommand]),
          "0" = os:cmd(BucketCommand),
  
          {ok, Host}
      catch
          _:Reason ->
              ?error_stacktrace("Cannot init database cluster: ~p", [Reason]),
              {error, Host}
      end.
  
  