Navigation Menu

Skip to content

Commit

Permalink
if filename is "-", read config from stdin (TSUN-193)
Browse files Browse the repository at this point in the history
  • Loading branch information
nniclausse committed Sep 23, 2011
1 parent 0dfefc2 commit f7f7d08
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 29 deletions.
50 changes: 33 additions & 17 deletions src/tsung_controller/ts_config.erl
Expand Up @@ -57,24 +57,26 @@
%%% @doc: read and parse the xml config file %%% @doc: read and parse the xml config file
%%% @end %%% @end
%%%---------------------------------------------------------------------- %%%----------------------------------------------------------------------
read(Filename=standard_io, LogDir) ->
?LOG("Reading config file from stdin~n", ?NOTICE),
XML = read_stdio(),
handle_read(catch xmerl_scan:string(XML,
[{fetch_path,["/usr/share/tsung/","./"]},
{validation,true}]),Filename,LogDir);
read(Filename, LogDir) -> read(Filename, LogDir) ->
case catch xmerl_scan:file(Filename, ?LOGF("Reading config file: ~s~n", [Filename], ?NOTICE),
[{fetch_path,["/usr/share/tsung/","./"]}, handle_read(catch xmerl_scan:file(Filename,
{validation,true}]) of [{fetch_path,["/usr/share/tsung/","./"]},
{ok, Root = #xmlElement{}} -> % xmerl-0.15 {validation,true}]),Filename,LogDir).
?LOGF("Reading config file: ~s~n", [Filename], ?NOTICE),
Table = ets:new(sessiontable, [ordered_set, protected]), handle_read( {Root = #xmlElement{}, _Tail}, Filename, LogDir) ->
{ok, parse(Root, #config{session_tab = Table})}; Table = ets:new(sessiontable, [ordered_set, protected]),
{Root = #xmlElement{}, _Tail} -> % xmerl-0.19 and up backup_config(LogDir, Filename, Root),
?LOGF("Reading config file: ~s~n", [Filename], ?NOTICE), {ok, parse(Root, #config{session_tab = Table, proto_opts=#proto_opts{}})};
Table = ets:new(sessiontable, [ordered_set, protected]), handle_read({error,Reason},_,_) ->
backup_config(LogDir, Filename, Root), {error, Reason};
{ok, parse(Root, #config{session_tab = Table, proto_opts=#proto_opts{}})}; handle_read({'EXIT',Reason},_,_) ->
{error,Reason} -> {error, Reason}.
{error, Reason};
{'EXIT',Reason} ->
{error, Reason}
end.


%%%---------------------------------------------------------------------- %%%----------------------------------------------------------------------
%%% Function: parse/2 %%% Function: parse/2
Expand Down Expand Up @@ -948,6 +950,8 @@ shortnames(Hostname)->
%% Use parsed config file to expand all ENTITY %% Use parsed config file to expand all ENTITY
%% @end %% @end
%%---------------------------------------------------------------------- %%----------------------------------------------------------------------
backup_config(Dir,standard_io, Config) ->
backup_config(Dir, "tsung_stdin.xml", Config);
backup_config(Dir, Name, Config) -> backup_config(Dir, Name, Config) ->
BaseName = filename:basename(Name), BaseName = filename:basename(Name),
{ok,IOF}=file:open(filename:join(Dir,BaseName),[write]), {ok,IOF}=file:open(filename:join(Dir,BaseName),[write]),
Expand All @@ -959,3 +963,15 @@ backup_config(Dir, Name, Config) ->
ok ok
end, end,
file:close(IOF). file:close(IOF).

%% @spec read_stdio()-> string()
%% @doc Read config from standard input
%% @end
read_stdio()->
read_stdio(io:get_line(""),[]).

read_stdio(eof, Data)->
lists:flatten(Data);
read_stdio(Data,Acc) ->
read_stdio(io:get_line(""),[Acc,Data]).

28 changes: 17 additions & 11 deletions src/tsung_controller/tsung_controller.erl
Expand Up @@ -60,17 +60,23 @@ start(_Type, _StartArgs) ->
end. end.


start_phase(load_config, _StartType, _PhaseArgs) -> start_phase(load_config, _StartType, _PhaseArgs) ->
Conf = ?config(config_file), {Conf,Timeout} =
Timeout = case file:read_file_info(Conf) of case ?config(config_file) of
{ok, #file_info{size=Size}} when Size > 10000000 -> % > 10MB "-" ->
erlang:display(["Can take up to 5mn to read config ",Size]), {standard_io, 120000}; %2mn timeout
300000; % 10mn File ->
{ok, #file_info{size=Size}} when Size > 1000000 -> % > 1MB T = case file:read_file_info(File) of
erlang:display(["Can take up to 3mn to read config ",Size]), {ok, #file_info{size=Size}} when Size > 10000000 -> % > 10MB
180000; % 5mn erlang:display(["Can take up to 5mn to read config ",Size]),
{ok, #file_info{size=Size}} -> 300000; % 10mn
120000 % 2mn {ok, #file_info{size=Size}} when Size > 1000000 -> % > 1MB
end, erlang:display(["Can take up to 3mn to read config ",Size]),
180000; % 5mn
{ok, #file_info{size=_}} ->
120000 % 2mn
end,
{File, T}
end,
case ts_config_server:read_config(Conf,Timeout) of case ts_config_server:read_config(Conf,Timeout) of
{error,Reason}-> {error,Reason}->
erlang:display(["Config Error, aborting ! ", Reason]), erlang:display(["Config Error, aborting ! ", Reason]),
Expand Down
2 changes: 1 addition & 1 deletion tsung.sh.in
Expand Up @@ -90,7 +90,7 @@ version() {
} }


checkconfig() { checkconfig() {
if [ ! -e $CONF_OPT_FILE ] if [ ! -e $CONF_OPT_FILE ] && [ $CONF_OPT_FILE != "-" ]
then then
echo "Config file $CONF_OPT_FILE doesn't exist, aborting !" echo "Config file $CONF_OPT_FILE doesn't exist, aborting !"
exit 1 exit 1
Expand Down

0 comments on commit f7f7d08

Please sign in to comment.