<commit_before>%% @author Northscale <<EMAIL>>
%% @copyright 2009 NorthScale, Inc.
%%
%% 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.
%%
%% Distributed erlang configuration and management
%%
-module(dist_manager).

-behaviour(gen_server).

-export([start_link/0]).

-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).

-export([adjust_my_address/1, read_address_config/0, save_address_config/1, ip_config_path/0]).

-record(state, {self_started, my_ip}).

start_link() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

ip_config_path() ->
    %% this smells bad, but we live higher on supervisor/process
    %% hierarchy than ns_config, so I see no other way
    {ok, CfgPath} = application:get_env(ns_server, ns_server_config),
    Path = filename:dirname(CfgPath),
    filename:join(Path, "ip").

read_address_config() ->
    Path = ip_config_path(),
    error_logger:info_msg("reading ip config from ~p~n", [Path]),
    case file:read_file(Path) of
        {ok, BinaryContents} ->
            AddrString = string:strip(binary_to_list(BinaryContents)),
            case inet:getaddr(AddrString, inet) of
                {error, Errno1} ->
                    error_logger:error_msg("Got error:~p. Ignoring bad address:~s~n", [Errno1, AddrString]),
                    undefined;
                {ok, IpAddr} ->
                    case gen_tcp:listen(0, [inet, {ip, IpAddr}]) of
                        {error, Errno2} ->
                            error_logger:error_msg("Got error:~p. Cannot listen on configured address:~s~n", [Errno2, AddrString]),
                            undefined;
                        {ok, Socket} ->
                            gen_tcp:close(Socket),
                            AddrString
                    end
            end;
        _ -> undefined
    end.

save_address_config(State) ->
    Path = ip_config_path(),
    error_logger:info_msg("saving ip config to ~p~n", [Path]),
    TmpPath = Path ++ ".tmp",
    case file:write_file(TmpPath, State#state.my_ip) of
        ok ->
            file:rename(TmpPath, Path);
        X -> X
    end.

init([]) ->
    InitialAddr = case read_address_config() of<commit_msg>properly strip contents of priv/ip<commit_after>%% @author Northscale <<EMAIL>>
%% @copyright 2009 NorthScale, Inc.
%%
%% 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.
%%
%% Distributed erlang configuration and management
%%
-module(dist_manager).

-behaviour(gen_server).

-export([start_link/0]).

-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).

-export([adjust_my_address/1, read_address_config/0, save_address_config/1, ip_config_path/0]).

-record(state, {self_started, my_ip}).

start_link() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

ip_config_path() ->
    %% this smells bad, but we live higher on supervisor/process
    %% hierarchy than ns_config, so I see no other way
    {ok, CfgPath} = application:get_env(ns_server, ns_server_config),
    Path = filename:dirname(CfgPath),
    filename:join(Path, "ip").

strip_full(String) ->
    String2 = string:strip(String),
    String3 = string:strip(String2, both, $\n),
    String4 = string:strip(String3, both, $\r),
    case String4 =:= String of
        true ->
            String4;
        _ ->
            strip_full(String4)
    end.

read_address_config() ->
    Path = ip_config_path(),
    error_logger:info_msg("reading ip config from ~p~n", [Path]),
    case file:read_file(Path) of
        {ok, BinaryContents} ->
            AddrString = strip_full(binary_to_list(BinaryContents)),
            case inet:getaddr(AddrString, inet) of
                {error, Errno1} ->
                    error_logger:error_msg("Got error:~p. Ignoring bad address:~p~n", [Errno1, AddrString]),
                    undefined;
                {ok, IpAddr} ->
                    case gen_tcp:listen(0, [inet, {ip, IpAddr}]) of
                        {error, Errno2} ->
                            error_logger:error_msg("Got error:~p. Cannot listen on configured address:~s~n", [Errno2, AddrString]),
                            undefined;
                        {ok, Socket} ->
                            gen_tcp:close(Socket),
                            AddrString
                    end
            end;
        _ -> undefined
    end.

save_address_config(State) ->
    Path = ip_config_path(),
    error_logger:info_msg("saving ip config to ~p~n", [Path]),
    TmpPath = Path ++ ".tmp",
    case file:write_file(TmpPath, State#state.my_ip) of
        ok ->
            file:rename(TmpPath, Path);
        X -> X
    end.

init([]) ->
    InitialAddr = case read_address_config() of
{'fraction_non_alphanumeric': 0.12300208478109799, 'fraction_numerical': 0.009728978457261988, 'mean_word_length': 2.5449507389162562, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7017882', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1690, 'n_words': 470}