Skip to content

Commit

Permalink
ncm-network: add support for device configuration dropin file
Browse files Browse the repository at this point in the history
provide support to create dropin file for device config and add option
to add keep-configuration option
  • Loading branch information
Abdul Karim authored and Abdul Karim committed Sep 10, 2024
1 parent 761169e commit 3718498
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,13 @@ type structure_ipv6 = {
"gatewaydev" ? valid_interface # sets IPV6_DEFAULTDEV
};

@documentation{
NetworkManager device configuration for drop in config file.
}
type structure_nm_device_config = {
"keep-configuration" ? choice("yes", "no")
};

@documentation{
Host network configuration
Expand Down Expand Up @@ -448,6 +455,7 @@ type structure_network = {
"ipv6" ? structure_ipv6
"manage_vips" : boolean = false
"vips" ? structure_vip{}
"nm_device_config" ? structure_nm_device_config
@{Manage custom routing table entries; key is the name; value is the id}
"routing_table" ? long(1..252){} with {
if (exists(SELF['main']) || exists(SELF['local']) || exists(SELF['default']) || exists(SELF['unspec'])) {
Expand Down
29 changes: 29 additions & 0 deletions ncm-network/src/main/perl/nmstate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Readonly my $NMCLI_CMD => '/usr/bin/nmcli';
# pick a config name for nmstate yml to configure dns-resolver: settings. if nm_manage_dns=true
Readonly my $NM_RESOLV_YML => "/etc/nmstate/resolv.yml";
Readonly my $NM_DROPIN_CFG_FILE => "/etc/NetworkManager/conf.d/90-quattor.conf";
Readonly my $NM_DEVICE_DROPIN_CFG_FILE => "/etc/NetworkManager/conf.d/89-device-quattor.conf";

# generate the correct fake yaml boolean value so TextRender can convert it in a yaml boolean
Readonly my $YTRUE => $EDG::WP4::CCM::TextRender::ELEMENT_CONVERT{yaml_boolean}->(1);
Expand Down Expand Up @@ -103,6 +104,26 @@ sub disable_nm_manage_dns
};
}

sub nm_create_device_config_dropin
{
my ($self, $nm_device_config, $nwsrv) = @_;
my @data = ('[device]');

if ( $nm_device_config ) {
foreach my $key (sort keys %$nm_device_config){
push @data, $key."=".$nm_device_config->{$key};
};

$self->verbose("setting device configuration dropin");
}
my $fh = CAF::FileWriter->new($NM_DEVICE_DROPIN_CFG_FILE, mode => oct(444), log => $self, keeps_state => 1);
print $fh join("\n", @data, '');
if ($fh->close()) {
$self->info("File $NM_DEVICE_DROPIN_CFG_FILE changed, reload network");
$nwsrv->reload();
};
}

# return hashref of ipv4 policy rule
sub make_nm_ip_rule
{
Expand Down Expand Up @@ -852,6 +873,14 @@ sub Configure
# 3. (re)start things
my $nwsrv = CAF::Service->new(['NetworkManager'], log => $self);

# NetworkManager device configuration, if defined.
my $nm_device_cfg = $nwtree->{nm_device_config};
if ($nm_device_cfg){
$self->nm_create_device_config_dropin($nm_device_cfg, $nwsrv);
} else {
$self->cleanup($NM_DEVICE_DROPIN_CFG_FILE);
}

# NetworkManager manages dns by default, but we manage dns with e.g. ncm-resolver, new option to enable/disable it.
$self->disable_nm_manage_dns($manage_dns, $nwsrv);

Expand Down
6 changes: 6 additions & 0 deletions ncm-network/src/test/perl/nmstate_simple.t
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ Readonly my $NODNS => <<EOF;
dns=none
EOF

Readonly my $DEVICE_CONFIG => <<EOF;
[device]
keep-configuration=no
EOF

Readonly my $ETH0_YML => <<EOF;
# File generated by NCM::Component::nmstate. Do not edit
---
Expand Down Expand Up @@ -89,6 +94,7 @@ is(get_file_contents("/etc/resolv.conf"), $RESOLV, "Exact network config");

# set nm config to disable dns mgmt
is(get_file_contents("/etc/NetworkManager/conf.d/90-quattor.conf"), $NODNS, "disable NM dns mgmt");
is(get_file_contents("/etc/NetworkManager/conf.d/89-device-quattor.conf"), $DEVICE_CONFIG, "networkmanager config for device");

# unconfigure nmstate yml is removed
ok(!$cmp->file_exists("/etc/nmstate/toremove0.yml"), "unconfigured yml nmstate is removed");
Expand Down
3 changes: 3 additions & 0 deletions ncm-network/src/test/resources/nmstate_simple.pan
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ object template nmstate_simple;

include 'simple_base_profile';
"/hardware/cards/nic/eth0/hwaddr" = "6e:a5:1b:55:77:0a";

"/system/network/nm_device_config/keep-configuration" = "no";

# the next include is mainly to the profile, it is not used in the tests
# (unless the component gets specific schema things)
include 'components/network/config-nmstate';

0 comments on commit 3718498

Please sign in to comment.