Skip to content

Commit 4401f48

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

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ Readonly my $NEW => 2;
194194
# changes to file, but same config (eg for new file formats)
195195
Readonly my $KEEPS_STATE => 3;
196196

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

198199
# wrapper around -x for easy unittesting
199200
# is not part of CAF::Path
@@ -1934,6 +1935,34 @@ sub routing_table
19341935
} else {
19351936
# nothing needs to be done upon change, returned for unittesting
19361937
return $fh->close();
1938+
1939+
# Prepare and take the action for all daemons as defined in hash-reference C<$daemons>.
1940+
# Does not return anything.
1941+
sub _process_service_actions
1942+
{
1943+
my ($self, $daemons) = @_;
1944+
my $actions = {};
1945+
my @daemon_action = ();
1946+
1947+
foreach my $daemon (sort keys %{$daemons || {}}) {
1948+
push(@daemon_action, $daemon, $daemons->{$daemon});
1949+
}
1950+
1951+
while (my ($daemon,$action) = splice(@daemon_action, 0, 2)) {
1952+
if (exists($ALLOWED_ACTIONS{$action})) {
1953+
$actions->{$action} ||= {};
1954+
$actions->{$action}->{$daemon} = 1;
1955+
} else {
1956+
$self->error("Not a CAF::Service allowed action $action for daemon $daemon in profile (component/schema mismatch?).");
1957+
}
1958+
}
1959+
1960+
foreach my $action (sort keys %$actions) {
1961+
my @daemons = sort keys %{$actions->{$action}};
1962+
$self->info("Executing action $action on services: ", join(', ', @daemons));
1963+
my $srv = CAF::Service->new(\@daemons, log => $self);
1964+
# CAF::Service does all the logging we need
1965+
$srv->$action();
19371966
}
19381967
}
19391968

@@ -2221,6 +2250,12 @@ sub Configure
22212250
}
22222251
};
22232252

2253+
# If the configuration was changed, process any requested daemon actions
2254+
if ($config_changed) {
2255+
$self->debug(5, "Configuration changed, processing daemon actions.");
2256+
$self->_process_service_actions($comp_tree->{daemons});
2257+
}
2258+
22242259
return 1;
22252260
}
22262261

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

Lines changed: 43 additions & 0 deletions
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();
Lines changed: 14 additions & 0 deletions
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)