From cb98db4cf19d4a607121c3ed789dc3eabbd675a1 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Fri, 5 Feb 2021 16:11:12 -0800 Subject: [PATCH] Adding the ability ro run a script --- docker_run_net.sh | 18 +++++++++++++++++- docker_start.sh | 7 ++++++- priv/templates/simulation.yaml | 4 ++-- src/owls_app.erl | 6 ++++++ src/user_default.erl | 34 +++++++++++++++++++++++++++++++++- 5 files changed, 64 insertions(+), 5 deletions(-) diff --git a/docker_run_net.sh b/docker_run_net.sh index b2b7c83..e53d54b 100755 --- a/docker_run_net.sh +++ b/docker_run_net.sh @@ -32,11 +32,27 @@ mkdir docker_logs_node1 HOSTNAMES="--add-host mgr.owls.net:172.21.10.2 --add-host node1.owls.net:172.21.10.3 --add-host $TIP_CONTROLLER_NAME:$TIP_CONTROLLER_IP" +# +# A simulation file is used to describe how a simulation should run. Here is the content... +# +# simulation: +# name: sim1 +# ca: +# name: tip1 +# cert: tipcert.pem (this file should be in the $PWD/ssl dir) +# key: tipkey.pem (this file should be in the $PWD/ssl dir) +# password: mypassword +# server: (should be the same name as TIP_CONTROLLER_NAME +# port: 6643 +# devices: 10 +# + docker run -d -p 9091:9090 --init \ --network=owls_net \ --volume="$PWD/ssl:/etc/ssl/certs" \ --volume="$PWD/docker_logs_manager:/app_data/logs" \ - -e ERL_NODE_NAME="mgr@mgr.owls.net" -e ERL_OPTIONS="-noshell -noinput" -e ERL_NODE_TYPE="manager" -e TIP_AUTH="2" \ + --volume="$PWD/scripts:/scripts" \ + -e ERL_NODE_NAME="mgr@mgr.owls.net" -e ERL_OPTIONS="-noshell -noinput" -e ERL_NODE_TYPE="manager" -e TIP_AUTH="2" -e SIM_SCRIPT="sim1.yaml" \ --ip="172.21.10.2" $HOSTNAMES \ --name="manager" $DOCKER_NAME diff --git a/docker_start.sh b/docker_start.sh index 4e9e3ed..5762e92 100755 --- a/docker_start.sh +++ b/docker_start.sh @@ -6,7 +6,12 @@ case $ERL_NODE_TYPE in manager) cp priv/templates/simmanager.args.template.docker config/vm.args cp priv/templates/simmanager.config.template.docker config/sys.config - erl -config config/sys.config -args_file config/vm.args -name $ERL_NODE_NAME $ERL_OPTIONS -pa deps/*/ebin -pa ebin -tipauth $TIP_AUTH + if [ -z "$SIM_SCRIPT" ] + then + erl -config config/sys.config -args_file config/vm.args -name $ERL_NODE_NAME $ERL_OPTIONS -pa deps/*/ebin -pa ebin -tipauth $TIP_AUTH + else + erl -config config/sys.config -args_file config/vm.args -name $ERL_NODE_NAME $ERL_OPTIONS -pa deps/*/ebin -pa ebin -tipauth $TIP_AUTH -sim $SIM_SCRIPT + fi ;; node) cp priv/templates/simnode.args.template.docker config/vm.args diff --git a/priv/templates/simulation.yaml b/priv/templates/simulation.yaml index 0e16878..b88c2cd 100644 --- a/priv/templates/simulation.yaml +++ b/priv/templates/simulation.yaml @@ -2,9 +2,9 @@ simulation: name: sim1 ca: name: tip1 -# (this file should be in the $PWD/ssl dir) +# (this file should be in the $PWD/ssl dir for docker. In docker, this name should be /etc/ssl/.) cert: tipcert.pem -# (this file should be in the $PWD/ssl dir) +# (this file should be in the $PWD/ssl dir for docker. In docker, this name should be /etc/ssl/.) key: tipkey.pem password: mypassword # should be the same name as TIP_CONTROLLER_NAME diff --git a/src/owls_app.erl b/src/owls_app.erl index b821586..861b563 100644 --- a/src/owls_app.erl +++ b/src/owls_app.erl @@ -23,6 +23,12 @@ stop(_State) -> start() -> ok = app_settings(), + case init:get_argument(sim) of + {ok,[[Script]]} -> + timer:apply_after(30*1000,user_default,run_script,[Script]); + _ -> + ok + end, application:ensure_all_started(owls). load_cli()-> diff --git a/src/user_default.erl b/src/user_default.erl index e9a28c8..ed9f273 100644 --- a/src/user_default.erl +++ b/src/user_default.erl @@ -433,7 +433,7 @@ wait_for_nodes(Seconds)-> end. wait_job_id(Id)-> - wait_job_id(Id,120). + wait_job_id(Id,600). wait_job_id(_,0)-> throw("operation did not complete...~n"); @@ -531,3 +531,35 @@ run(SimName,Delay)-> io:format("~nAne error occured while trying to start the simulation. ~p~n",[Error]) end. +run_script(ScriptName) -> + try + [ Script ] = yamerl_constr:file(ScriptName), + %% verify that we have everything + {"simulation", SimInfo} = proplists:lookup("simulation",Script), + {"name", SimName} = proplists:lookup("name",SimInfo), + {"server", SimServer} = proplists:lookup("server",SimInfo), + {"port", SimPort} = proplists:lookup("port", SimInfo), + {"devices", NumDevices} = proplists:lookup("devices",SimInfo), + {"ca",CAInfo} = proplists:lookup("ca",SimInfo), + {"name",CAName} = proplists:lookup("name",CAInfo), + {"cert",CACert} = proplists:lookup("cert",CAInfo), + {"key",CAKey} = proplists:lookup("key",CAInfo), + {"password",CAPassword} = proplists:lookup("password",CAInfo), + io:format("SIM: ~p: ~p/~p/~p CA: ~p: ~p/~p/~p",[SimName,SimServer,SimPort,NumDevices,CAName,CACert,CAKey,CAPassword]), + inventory:import_raw_ca(CAName,CAPassword,CAKey,CACert), + Nodes = wait_for_nodes(), + Simulation = #simulation{ name = utils:safe_binary(SimName), + ca = utils:safe_binary(CAName), + num_devices = NumDevices, + opensync_server_port = 6643, + opensync_server_name = utils:safe_binary(SimServer), + nodes = Nodes }, + _ = simengine:create(Simulation), + {ok,ID} = simengine:prepare(SimName,#{},utils:noop_mfa()), + wait_job_id(ID), + run(SimName,120), + ?L_IA("Simulation ~p is now running.",[SimName]) + catch + _:_ -> + ?L_IA("Cannot run script: ~p. Please look at the template simulation.yaml",[ScriptName]) + end.