mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 06:38:20 +00:00
Add optional bare-mode boolean flag to Supervisor's node configuration
When omitted, the node inherits the Supervisor's bare-mode status. When true/false, the new Zeek node will enable/disable bare mode, respectively. It continues to load any scripts passed at the command line and in the additional scripts list already provided in the node configuration. Includes testcase.
This commit is contained in:
parent
efaa9ec3be
commit
7bee79b400
7 changed files with 108 additions and 0 deletions
|
@ -42,6 +42,9 @@ export {
|
|||
stdout_file: string &optional;
|
||||
## The filename/path to which the node's stderr will be redirected.
|
||||
stderr_file: string &optional;
|
||||
## Whether to start the node in bare mode. When left out, the node
|
||||
## inherits the bare-mode status the supervisor itself runs with.
|
||||
bare_mode: bool &optional;
|
||||
## Additional script filenames/paths that the node should load.
|
||||
scripts: vector of string &default = vector();
|
||||
## Environment variables to define in the supervised node.
|
||||
|
|
|
@ -1251,6 +1251,11 @@ Supervisor::NodeConfig Supervisor::NodeConfig::FromRecord(const RecordVal* node)
|
|||
if ( affinity_val )
|
||||
rval.cpu_affinity = affinity_val->AsInt();
|
||||
|
||||
const auto& bare_mode_val = node->GetField("bare_mode");
|
||||
|
||||
if ( bare_mode_val )
|
||||
rval.bare_mode = bare_mode_val->AsBool();
|
||||
|
||||
auto scripts_val = node->GetField("scripts")->AsVectorVal();
|
||||
|
||||
for ( auto i = 0u; i < scripts_val->Size(); ++i )
|
||||
|
@ -1324,6 +1329,9 @@ Supervisor::NodeConfig Supervisor::NodeConfig::FromJSON(std::string_view json)
|
|||
if ( auto it = j.FindMember("cpu_affinity"); it != j.MemberEnd() )
|
||||
rval.cpu_affinity = it->value.GetInt();
|
||||
|
||||
if ( auto it = j.FindMember("bare_mode"); it != j.MemberEnd() )
|
||||
rval.bare_mode = it->value.GetBool();
|
||||
|
||||
auto& scripts = j["scripts"];
|
||||
|
||||
for ( auto it = scripts.Begin(); it != scripts.End(); ++it )
|
||||
|
@ -1385,6 +1393,9 @@ RecordValPtr Supervisor::NodeConfig::ToRecord() const
|
|||
if ( cpu_affinity )
|
||||
rval->AssignField("cpu_affinity", *cpu_affinity);
|
||||
|
||||
if ( bare_mode )
|
||||
rval->AssignField("bare_mode", *bare_mode);
|
||||
|
||||
auto st = rt->GetFieldType<VectorType>("scripts");
|
||||
auto scripts_val = make_intrusive<VectorVal>(std::move(st));
|
||||
|
||||
|
@ -1590,6 +1601,9 @@ void SupervisedNode::Init(Options* options) const
|
|||
|
||||
options->filter_supervised_node_options();
|
||||
|
||||
if ( config.bare_mode )
|
||||
options->bare_mode = *config.bare_mode;
|
||||
|
||||
if ( config.interface )
|
||||
options->interface = *config.interface;
|
||||
|
||||
|
|
|
@ -186,6 +186,11 @@ public:
|
|||
* A cpu/core number to which the node will try to pin itself.
|
||||
*/
|
||||
std::optional<int> cpu_affinity;
|
||||
/**
|
||||
* Whether to start the node in bare mode. When not present, the
|
||||
* node inherits the bare-mode status of the supervisor.
|
||||
*/
|
||||
std::optional<bool> bare_mode;
|
||||
/**
|
||||
* Additional script filename/paths that the node should load.
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
|
||||
supervised node zeek_init()
|
||||
bare mode
|
||||
supervised node zeek_done()
|
|
@ -0,0 +1,4 @@
|
|||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
|
||||
supervised node zeek_init()
|
||||
default mode
|
||||
supervised node zeek_done()
|
|
@ -0,0 +1,4 @@
|
|||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
|
||||
supervised node zeek_init()
|
||||
bare mode
|
||||
supervised node zeek_done()
|
74
testing/btest/supervisor/config-bare-mode.zeek
Normal file
74
testing/btest/supervisor/config-bare-mode.zeek
Normal file
|
@ -0,0 +1,74 @@
|
|||
# This test verifies the functionality of the bare_mode flag in NodeConfig.
|
||||
# We launch two nodes, one regular, one in bare mode. Each outputs a different
|
||||
# string depending on mode, and exits. We verify the resulting outputs.
|
||||
|
||||
# @TEST-PORT: BROKER_PORT
|
||||
# @TEST-EXEC: btest-bg-run zeek zeek -j -b %INPUT
|
||||
# @TEST-EXEC: btest-bg-wait 30
|
||||
# @TEST-EXEC: btest-diff zeek/inherit/node.out
|
||||
# @TEST-EXEC: btest-diff zeek/bare/node.out
|
||||
# @TEST-EXEC: btest-diff zeek/default/node.out
|
||||
|
||||
|
||||
# So the supervised node doesn't terminate right away.
|
||||
redef exit_only_after_terminate=T;
|
||||
|
||||
global node_output_file: file;
|
||||
global topic = "test-topic";
|
||||
|
||||
event do_destroy(name: string)
|
||||
{
|
||||
Supervisor::destroy(name);
|
||||
|
||||
# When no nodes are left, exit.
|
||||
local status = Supervisor::status();
|
||||
if ( |status$nodes| == 0)
|
||||
terminate();
|
||||
}
|
||||
|
||||
event zeek_init()
|
||||
{
|
||||
if ( Supervisor::is_supervisor() )
|
||||
{
|
||||
Broker::subscribe(topic);
|
||||
Broker::listen("127.0.0.1", to_port(getenv("BROKER_PORT")));
|
||||
|
||||
# Create a node that inherits basre mode from us.
|
||||
local sn = Supervisor::NodeConfig($name="inherit", $directory="inherit");
|
||||
Supervisor::create(sn);
|
||||
|
||||
# Create a node that specifies bare mode.
|
||||
sn = Supervisor::NodeConfig($name="bare", $directory="bare", $bare_mode=T);
|
||||
Supervisor::create(sn);
|
||||
|
||||
# Create a node that specifies default mode.
|
||||
sn = Supervisor::NodeConfig($name="default", $directory="default", $bare_mode=F);
|
||||
Supervisor::create(sn);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
Broker::peer("127.0.0.1", to_port(getenv("BROKER_PORT")));
|
||||
node_output_file = open("node.out");
|
||||
print node_output_file, "supervised node zeek_init()";
|
||||
|
||||
# This is only defined when we're loading init-default.zeek:
|
||||
@ifdef ( Notice::Info )
|
||||
print node_output_file, "default mode";
|
||||
@else
|
||||
print node_output_file, "bare mode";
|
||||
@endif
|
||||
}
|
||||
}
|
||||
|
||||
event Broker::peer_added(endpoint: Broker::EndpointInfo, msg: string)
|
||||
{
|
||||
if ( Supervisor::is_supervised() )
|
||||
Broker::publish(topic, do_destroy, Supervisor::node()$name);
|
||||
}
|
||||
|
||||
event zeek_done()
|
||||
{
|
||||
if ( Supervisor::is_supervised() )
|
||||
print node_output_file, "supervised node zeek_done()";
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue