Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ncm-network: Add support for CAF::Service actions after changes #1336

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions ncm-metaconfig/src/main/pan/components/metaconfig/schema.pan
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ type ${project.artifactId}_textrender_convert = {
true;
};

type caf_service_action = string with match(SELF, '^(restart|condrestart|reload|stop_sleep_start)$');

type ${project.artifactId}_actions = {
@{Always run, happens before possible modifications.
A failure will cancel any file modification, unless the command is prefixed with -.}
Expand Down
6 changes: 6 additions & 0 deletions ncm-network/src/main/pan/components/network/schema.pan
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ type network_component = {
include structure_component
@{experimental: rename (physical) devices}
'rename' ? boolean

@{
A dict mapping daemon name to CAF::Service action to take if the network configuration changes.
e.g. 'daemons/firewalld' = 'reload';
}
'daemons' ? caf_service_action{}
};
35 changes: 35 additions & 0 deletions ncm-network/src/main/perl/network.pm
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ Readonly my $NEW => 2;
# changes to file, but same config (eg for new file formats)
Readonly my $KEEPS_STATE => 3;

Readonly::Hash my %ALLOWED_ACTIONS => { restart => 1, reload => 1, stop_sleep_start => 1 };

# wrapper around -x for easy unittesting
# is not part of CAF::Path
Expand Down Expand Up @@ -1934,6 +1935,34 @@ sub routing_table
} else {
# nothing needs to be done upon change, returned for unittesting
return $fh->close();

# Prepare and take the action for all daemons as defined in hash-reference C<$daemons>.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would like to see this moved (and perhaps generalised a bit) to CAF::Service. then we can use it in even more components. replace $self with $logger and export the function there

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me too, are you volunteering? 😸

# Does not return anything.
sub _process_service_actions
{
my ($self, $daemons) = @_;
my $actions = {};
my @daemon_action = ();

foreach my $daemon (sort keys %{$daemons || {}}) {
push(@daemon_action, $daemon, $daemons->{$daemon});
}

while (my ($daemon,$action) = splice(@daemon_action, 0, 2)) {
if (exists($ALLOWED_ACTIONS{$action})) {
$actions->{$action} ||= {};
$actions->{$action}->{$daemon} = 1;
} else {
$self->error("Not a CAF::Service allowed action $action for daemon $daemon in profile (component/schema mismatch?).");
}
}

foreach my $action (sort keys %$actions) {
my @daemons = sort keys %{$actions->{$action}};
$self->info("Executing action $action on services: ", join(', ', @daemons));
my $srv = CAF::Service->new(\@daemons, log => $self);
# CAF::Service does all the logging we need
$srv->$action();
}
}

Expand Down Expand Up @@ -2221,6 +2250,12 @@ sub Configure
}
};

# If the configuration was changed, process any requested daemon actions
if ($config_changed) {
$self->debug(5, "Configuration changed, processing daemon actions.");
$self->_process_service_actions($comp_tree->{daemons});
}

return 1;
}

Expand Down
43 changes: 43 additions & 0 deletions ncm-network/src/test/perl/actions.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- mode: cperl -*-
use strict;
use warnings;

BEGIN {
*CORE::GLOBAL::sleep = sub {};
}

use Test::More;
use Test::Quattor qw(actions);
use Test::MockModule;

use NCM::Component::network;


my $mock = Test::MockModule->new('CAF::Service');

our ($restart, $reload, $stop_sleep_start) = qw(0 0 0);

$mock->mock('restart', sub {
my $self = shift;
$restart += scalar @{$self->{services}};
});
$mock->mock('reload', sub {
my $self = shift;
$reload += scalar @{$self->{services}};
});
$mock->mock('stop_sleep_start', sub {
my $self = shift;
$stop_sleep_start += scalar @{$self->{services}};
});


my $cfg = get_config_for_profile('actions');
my $cmp = NCM::Component::network->new('network');

is($cmp->Configure($cfg), 1, "Component runs correctly with daemon actions set");

is($restart, 1, "$restart/1 restarts triggered");
is($reload, 2, "$reload/2 reloads triggered");
is($stop_sleep_start, 3, "$stop_sleep_start/3 stop_sleep_starts triggered");

done_testing();
14 changes: 14 additions & 0 deletions ncm-network/src/test/resources/actions.pan
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
object template actions;

include 'simple_base_profile';

prefix "/software/components/network/daemons";

"test_restart_1" = 'restart';

"test_reload_1" = 'reload';
"test_reload_2" = 'reload';

"test_stop_sleep_start_1" = 'stop_sleep_start';
"test_stop_sleep_start_2" = 'stop_sleep_start';
"test_stop_sleep_start_3" = 'stop_sleep_start';