[pve-devel] [PATCH pve-network 2/4] add new status sub and move code from test

Alexandre Derumier aderumier at odiso.com
Wed Aug 21 05:22:26 CEST 2019


old status sub was renamed ifquery_check

also check if local config exist or if local config is too old.
(fixme : compare mtime, maybe could we use some kind of version for this?)

we can have 4 status code:

- pending : local config is absent but sdn.cfg exist
- unknown : local config is too old, we can't be sure of the running state
- error : local config is present, but don't match the running state
- available : all is ok, local config is present and match running state.

Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 PVE/Network/SDN.pm  | 60 +++++++++++++++++++++++++++++++++++++++++++--
 test/statuscheck.pl | 37 +---------------------------
 2 files changed, 59 insertions(+), 38 deletions(-)

diff --git a/PVE/Network/SDN.pm b/PVE/Network/SDN.pm
index 9488f4f..1b1529b 100644
--- a/PVE/Network/SDN.pm
+++ b/PVE/Network/SDN.pm
@@ -65,7 +65,7 @@ sub complete_sdn {
     return  $cmdname eq 'add' ? [] : [ PVE::Network::SDN::sdn_ids($cfg) ];
 }
 
-sub status {
+sub ifquery_check {
 
     my $cmd = ['ifquery', '-a', '-c', '-o','json'];
 
@@ -161,7 +161,63 @@ sub write_etc_network_config {
     $writefh->close();
 }
 
-1;
 
+sub status {
+
+    my $cluster_sdn_file = "/etc/pve/sdn.cfg";
+    my $local_sdn_file = "/etc/network/interfaces.d/sdn";
+    my $err_config = undef;
+
+    return if !-e $cluster_sdn_file;
+
+    if (!-e $local_sdn_file) {
+	warn "local sdn network configuration is not yet generated, please reload";
+	$err_config = 'pending';
+    } else {
+	# fixme : use some kind of versioning info?
+	my $cluster_sdn_timestamp = (stat($cluster_sdn_file))[9];
+	my $local_sdn_timestamp = (stat($local_sdn_file))[9];
+
+	if ($local_sdn_timestamp < $cluster_sdn_timestamp) {
+	    warn "local sdn network configuration is too old, please reload";
+	    $err_config = 'unknown';
+        }
+    }
+
+    my $status = ifquery_check();
 
+    my $network_cfg = PVE::Cluster::cfs_read_file('sdn.cfg');
+    my $vnet_cfg = undef;
+    my $transport_cfg = undef;
+
+    my $vnet_status = {};
+    my $transport_status = {};
+
+    foreach my $id (keys %{$network_cfg->{ids}}) {
+	if ($network_cfg->{ids}->{$id}->{type} eq 'vnet') {
+	    my $transportzone = $network_cfg->{ids}->{$id}->{transportzone};
+	    $vnet_status->{$id}->{transportzone} = $transportzone;
+	    $transport_status->{$transportzone}->{status} = 'available' if !defined($transport_status->{$transportzone}->{status});
+
+	    if($err_config) {
+		$vnet_status->{$id}->{status} = $err_config;
+		$transport_status->{$transportzone}->{status} = $err_config;
+	    } elsif ($status->{$id}->{status} && $status->{$id}->{status} eq 'pass') {
+		$vnet_status->{$id}->{status} = 'available';
+		my $bridgeport = $status->{$id}->{config}->{'bridge-ports'};
+
+		if ($status->{$bridgeport}->{status} && $status->{$bridgeport}->{status} ne 'pass') {
+		     $vnet_status->{$id}->{status} = 'error';
+		     $transport_status->{$transportzone}->{status} = 'error';
+		}
+	    } else {
+		$vnet_status->{$id}->{status} = 'error';
+		$transport_status->{$transportzone}->{status} = 'error';
+	    }
+	}
+    }
+    return($transport_status, $vnet_status);
+}
+
+1;
 
diff --git a/test/statuscheck.pl b/test/statuscheck.pl
index 619a957..e43003b 100644
--- a/test/statuscheck.pl
+++ b/test/statuscheck.pl
@@ -1,44 +1,9 @@
 use strict;
 use warnings;
-use File::Copy;
-use PVE::Cluster qw(cfs_read_file);
-
 use PVE::Network::SDN;
 use Data::Dumper;
-use PVE::Network::SDN::Plugin;
-use PVE::Network::SDN::VnetPlugin;
-use PVE::Network::SDN::VlanPlugin;
-use PVE::Network::SDN::VxlanMulticastPlugin;
-
-
-my $status = PVE::Network::SDN::status();
-
-my $network_cfg = PVE::Cluster::cfs_read_file('networks.cfg');
-my $vnet_cfg = undef;
-my $transport_cfg = undef;
-
-my $vnet_status = {};
-my $transport_status = {};
-
-foreach my $id (keys %{$network_cfg->{ids}}) {
-    if ($network_cfg->{ids}->{$id}->{type} eq 'vnet') {
-	my $transportzone = $network_cfg->{ids}->{$id}->{transportzone};
-	$transport_status->{$transportzone}->{status} = 1 if !defined($transport_status->{$transportzone}->{status});
-
-	if ($status->{$id}->{status} && $status->{$id}->{status} eq 'pass') {
-	    $vnet_status->{$id}->{status} = 1;
-	    my $bridgeport = $status->{$id}->{config}->{'bridge-ports'};
 
-	    if ($status->{$bridgeport}->{status} && $status->{$bridgeport}->{status} ne 'pass') {
-		$vnet_status->{$id}->{status} = 0;
-		$transport_status->{$transportzone}->{status} = 0;
-	    }
-	} else {
-	    $vnet_status->{$id}->{status} = 0;
-	    $transport_status->{$transportzone}->{status} = 0;
-	}
-    }
-}
+my ($transport_status, $vnet_status) = PVE::Network::SDN::status();
 
 print Dumper($vnet_status);
 print Dumper($transport_status);
-- 
2.20.1



More information about the pve-devel mailing list