Skip to content

Commit

Permalink
ncm-network: Add support for CAF::Service actions after changes
Browse files Browse the repository at this point in the history
Largely transcribed from ncm-metaconfig.
Requires quattor#1335.
Resolves quattor#1071.
  • Loading branch information
jrha committed Jul 28, 2023
1 parent b46145e commit 4401f48
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
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>.
# 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';

0 comments on commit 4401f48

Please sign in to comment.