forked from Telecominfraproject/wlan-cloud-loadsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simnode_config
executable file
·78 lines (70 loc) · 2.77 KB
/
simnode_config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env escript
main([]) ->
try
{ok,DefaultDirName}=file:get_cwd(),
Id = input_int("Please enter a node number", 1,99),
NodeName = input("Please enter a node name", make_nodename(Id) ),
Cookie = input("Please enter a network cookie", "oreo" ),
DirName = input("Please enter a directory name", DefaultDirName ),
file_substitute(filename:join(["priv","templates","simnode.config.template"]),
filename:join(["config","sys.config"]),
[{"$$PROJECT_HOME$$",DirName},
{"$$NODE_ID$$",integer_to_list(Id)}]),
file_substitute(filename:join(["priv","templates","simnode.args.template"]),
filename:join(["config","vm.args"]),
[{"$$NODE_NAME$$",NodeName},{"$$COOKIE$$",Cookie}])
catch
_:_ ->
usage()
end;
main(_) ->
usage().
usage() ->
io:format("usage: mqtt_config <project_home_prefix>~n"),
halt(1).
find_hostname()->
{ok,Hostname}=inet:gethostname(),
{ok,{hostent,_RealHostname,_,inet,4,[A|_]}}=inet_res:gethostbyname(Hostname),
{ok,{hostent,VerifiedHostName,_,inet,4,_}}=inet_res:gethostbyaddr(A),
VerifiedHostName.
make_nodename(Id)->
"simnode" ++ integer_to_list(Id) ++ "@" ++ find_hostname().
replace([],FinalBlob)->
FinalBlob;
replace([{Variable,Value}|T],Blob) when is_integer(Value)->
replace(T,string:replace(Blob,Variable,integer_to_list(Value),all));
replace([{Variable,Value}|T],Blob) when is_list(Value)->
replace(T,string:replace(Blob,Variable,Value,all)).
file_substitute(FileNameIn,FileNameOut,VariableList)->
{ok,FileBin}=file:read_file(FileNameIn),
FileBlob=binary_to_list(FileBin),
NewBlob=replace(VariableList,FileBlob),
file:write_file(FileNameOut,list_to_binary(NewBlob)).
input_int(Prompt,Min,Max)->
InputData=string:trim(io:get_line( Prompt ++ "(" ++ integer_to_list(Min) ++ ".." ++ integer_to_list(Max) ++ ") [" ++ integer_to_list(Min) ++ "] :")),
case InputData=="" of
true ->
Min;
false ->
D = string:trim(InputData),
try
I = list_to_integer(D),
case ((I >= Min) and (I =< Max)) of
true ->
I;
false ->
io:format("Must be a number between ~p and ~p.~n",[Min,Max]),
input_int(Prompt,Min,Max)
end
catch
_:_ ->
io:format("Must be a number between ~p and ~p.~n",[Min,Max]),
input_int(Prompt,Min,Max)
end
end.
input(Prompt,Default)->
InputData=string:trim(io:get_line( Prompt ++ " [" ++ Default ++ "] :")),
case InputData=="" of
true -> Default;
false -> InputData
end.