Skip to content

Commit ab0d50f

Browse files
committed
ncm-network: Add support for CAF::Service actions after changes
Largely transcribed from ncm-metaconfig. Requires #1335. Resolves #1071.
1 parent ba8ca15 commit ab0d50f

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

ncm-network/src/main/pan/components/network/schema.pan

+6
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@ type network_component = {
66
include structure_component
77
@{experimental: rename (physical) devices}
88
'rename' ? boolean
9+
10+
@{
11+
A dict mapping daemon name to CAF::Service action to take if the network configuration changes.
12+
e.g. 'daemons/firewalld' = 'reload';
13+
}
14+
'daemons' ? caf_service_action{}
915
};

ncm-network/src/main/perl/network.pm

+37
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ Readonly my $NEW => 2;
177177
# changes to file, but same config (eg for new file formats)
178178
Readonly my $KEEPS_STATE => 3;
179179

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

181182
# wrapper around -x for easy unittesting
182183
# is not part of CAF::Path
@@ -1694,6 +1695,36 @@ sub down_rename_devices
16941695
return $action;
16951696
}
16961697

1698+
# Prepare and take the action for all daemons as defined in hash-reference C<$daemons>.
1699+
# Does not return anything.
1700+
sub _process_service_actions
1701+
{
1702+
my ($self, $daemons) = @_;
1703+
my $actions = {};
1704+
my @daemon_action = ();
1705+
1706+
foreach my $daemon (sort keys %{$daemons || {}}) {
1707+
push(@daemon_action, $daemon, $daemons->{$daemon});
1708+
}
1709+
1710+
while (my ($daemon,$action) = splice(@daemon_action, 0, 2)) {
1711+
if (exists($ALLOWED_ACTIONS{$action})) {
1712+
$actions->{$action} ||= {};
1713+
$actions->{$action}->{$daemon} = 1;
1714+
} else {
1715+
$self->error("Not a CAF::Service allowed action $action for daemon $daemon in profile (component/schema mismatch?).");
1716+
}
1717+
}
1718+
1719+
foreach my $action (sort keys %$actions) {
1720+
my @daemons = sort keys %{$actions->{$action}};
1721+
$self->info("Executing action $action on services: ", join(', ', @daemons));
1722+
my $srv = CAF::Service->new(\@daemons, log => $self);
1723+
# CAF::Service does all the logging we need
1724+
$srv->$action();
1725+
}
1726+
}
1727+
16971728

16981729
sub Configure
16991730
{
@@ -1946,6 +1977,12 @@ sub Configure
19461977
}
19471978
};
19481979

1980+
# If the configuration was changed, process any requested daemon actions
1981+
if ($config_changed) {
1982+
$self->debug(5, "Configuration changed, processing daemon actions.");
1983+
$self->_process_service_actions($comp_tree->{daemons});
1984+
}
1985+
19491986
return 1;
19501987
}
19511988

ncm-network/src/test/perl/actions.t

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# -*- mode: cperl -*-
2+
use strict;
3+
use warnings;
4+
5+
BEGIN {
6+
*CORE::GLOBAL::sleep = sub {};
7+
}
8+
9+
use Test::More;
10+
use Test::Quattor qw(actions);
11+
use Test::MockModule;
12+
13+
use NCM::Component::network;
14+
15+
16+
my $mock = Test::MockModule->new('CAF::Service');
17+
18+
our ($restart, $reload, $stop_sleep_start) = qw(0 0 0);
19+
20+
$mock->mock('restart', sub {
21+
my $self = shift;
22+
$restart += scalar @{$self->{services}};
23+
});
24+
$mock->mock('reload', sub {
25+
my $self = shift;
26+
$reload += scalar @{$self->{services}};
27+
});
28+
$mock->mock('stop_sleep_start', sub {
29+
my $self = shift;
30+
$stop_sleep_start += scalar @{$self->{services}};
31+
});
32+
33+
34+
my $cfg = get_config_for_profile('actions');
35+
my $cmp = NCM::Component::network->new('network');
36+
37+
is($cmp->Configure($cfg), 1, "Component runs correctly with daemon actions set");
38+
39+
is($restart, 1, "$restart/1 restarts triggered");
40+
is($reload, 2, "$reload/2 reloads triggered");
41+
is($stop_sleep_start, 3, "$stop_sleep_start/3 stop_sleep_starts triggered");
42+
43+
done_testing();
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
object template actions;
2+
3+
include 'simple_base_profile';
4+
5+
prefix "/software/components/network/daemons";
6+
7+
"test_restart_1" = 'restart';
8+
9+
"test_reload_1" = 'reload';
10+
"test_reload_2" = 'reload';
11+
12+
"test_stop_sleep_start_1" = 'stop_sleep_start';
13+
"test_stop_sleep_start_2" = 'stop_sleep_start';
14+
"test_stop_sleep_start_3" = 'stop_sleep_start';

0 commit comments

Comments
 (0)