From f7f7d08d17a0f70fa0a783699444898da6c6bf4b Mon Sep 17 00:00:00 2001 From: Nicolas Niclausse Date: Fri, 23 Sep 2011 18:29:20 +0200 Subject: [PATCH] if filename is "-", read config from stdin (TSUN-193) --- src/tsung_controller/ts_config.erl | 50 +++++++++++++++-------- src/tsung_controller/tsung_controller.erl | 28 ++++++++----- tsung.sh.in | 2 +- 3 files changed, 51 insertions(+), 29 deletions(-) diff --git a/src/tsung_controller/ts_config.erl b/src/tsung_controller/ts_config.erl index 4e18d5921..e490a027b 100644 --- a/src/tsung_controller/ts_config.erl +++ b/src/tsung_controller/ts_config.erl @@ -57,24 +57,26 @@ %%% @doc: read and parse the xml config file %%% @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) -> - case catch xmerl_scan:file(Filename, - [{fetch_path,["/usr/share/tsung/","./"]}, - {validation,true}]) of - {ok, Root = #xmlElement{}} -> % xmerl-0.15 - ?LOGF("Reading config file: ~s~n", [Filename], ?NOTICE), - Table = ets:new(sessiontable, [ordered_set, protected]), - {ok, parse(Root, #config{session_tab = Table})}; - {Root = #xmlElement{}, _Tail} -> % xmerl-0.19 and up - ?LOGF("Reading config file: ~s~n", [Filename], ?NOTICE), - Table = ets:new(sessiontable, [ordered_set, protected]), - backup_config(LogDir, Filename, Root), - {ok, parse(Root, #config{session_tab = Table, proto_opts=#proto_opts{}})}; - {error,Reason} -> - {error, Reason}; - {'EXIT',Reason} -> - {error, Reason} - end. + ?LOGF("Reading config file: ~s~n", [Filename], ?NOTICE), + handle_read(catch xmerl_scan:file(Filename, + [{fetch_path,["/usr/share/tsung/","./"]}, + {validation,true}]),Filename,LogDir). + +handle_read( {Root = #xmlElement{}, _Tail}, Filename, LogDir) -> + Table = ets:new(sessiontable, [ordered_set, protected]), + backup_config(LogDir, Filename, Root), + {ok, parse(Root, #config{session_tab = Table, proto_opts=#proto_opts{}})}; +handle_read({error,Reason},_,_) -> + {error, Reason}; +handle_read({'EXIT',Reason},_,_) -> + {error, Reason}. %%%---------------------------------------------------------------------- %%% Function: parse/2 @@ -948,6 +950,8 @@ shortnames(Hostname)-> %% Use parsed config file to expand all ENTITY %% @end %%---------------------------------------------------------------------- +backup_config(Dir,standard_io, Config) -> + backup_config(Dir, "tsung_stdin.xml", Config); backup_config(Dir, Name, Config) -> BaseName = filename:basename(Name), {ok,IOF}=file:open(filename:join(Dir,BaseName),[write]), @@ -959,3 +963,15 @@ backup_config(Dir, Name, Config) -> ok end, 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]). + diff --git a/src/tsung_controller/tsung_controller.erl b/src/tsung_controller/tsung_controller.erl index 256445ed0..132f5aba5 100644 --- a/src/tsung_controller/tsung_controller.erl +++ b/src/tsung_controller/tsung_controller.erl @@ -60,17 +60,23 @@ start(_Type, _StartArgs) -> end. start_phase(load_config, _StartType, _PhaseArgs) -> - Conf = ?config(config_file), - Timeout = case file:read_file_info(Conf) of - {ok, #file_info{size=Size}} when Size > 10000000 -> % > 10MB - erlang:display(["Can take up to 5mn to read config ",Size]), - 300000; % 10mn - {ok, #file_info{size=Size}} when Size > 1000000 -> % > 1MB - erlang:display(["Can take up to 3mn to read config ",Size]), - 180000; % 5mn - {ok, #file_info{size=Size}} -> - 120000 % 2mn - end, + {Conf,Timeout} = + case ?config(config_file) of + "-" -> + {standard_io, 120000}; %2mn timeout + File -> + T = case file:read_file_info(File) of + {ok, #file_info{size=Size}} when Size > 10000000 -> % > 10MB + erlang:display(["Can take up to 5mn to read config ",Size]), + 300000; % 10mn + {ok, #file_info{size=Size}} when Size > 1000000 -> % > 1MB + 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 {error,Reason}-> erlang:display(["Config Error, aborting ! ", Reason]), diff --git a/tsung.sh.in b/tsung.sh.in index abbb87641..9dae9755c 100755 --- a/tsung.sh.in +++ b/tsung.sh.in @@ -90,7 +90,7 @@ version() { } checkconfig() { - if [ ! -e $CONF_OPT_FILE ] + if [ ! -e $CONF_OPT_FILE ] && [ $CONF_OPT_FILE != "-" ] then echo "Config file $CONF_OPT_FILE doesn't exist, aborting !" exit 1