[pve-devel] [PATCH pve-network 2/4] sdn: use .version and global lock

Alexandre Derumier aderumier at odiso.com
Mon Apr 13 08:13:11 CEST 2020


---
 PVE/API2/Network/SDN/Controllers.pm | 14 +++++++--
 PVE/API2/Network/SDN/Vnets.pm       | 14 +++++++--
 PVE/API2/Network/SDN/Zones.pm       | 13 ++++++--
 PVE/Network/SDN.pm                  | 46 +++++++++++++++++++++++++++++
 PVE/Network/SDN/Vnets.pm            |  9 ------
 PVE/Network/SDN/Zones.pm            |  9 ------
 6 files changed, 78 insertions(+), 27 deletions(-)

diff --git a/PVE/API2/Network/SDN/Controllers.pm b/PVE/API2/Network/SDN/Controllers.pm
index 50491eb..0f8dcbe 100644
--- a/PVE/API2/Network/SDN/Controllers.pm
+++ b/PVE/API2/Network/SDN/Controllers.pm
@@ -6,6 +6,7 @@ use warnings;
 use PVE::SafeSyslog;
 use PVE::Tools qw(extract_param);
 use PVE::Cluster qw(cfs_read_file cfs_write_file);
+use PVE::Network::SDN;
 use PVE::Network::SDN::Zones;
 use PVE::Network::SDN::Controllers;
 use PVE::Network::SDN::Controllers::Plugin;
@@ -136,7 +137,7 @@ __PACKAGE__->register_method ({
         PVE::Cluster::check_cfs_quorum();
         mkdir("/etc/pve/sdn");
 
-        PVE::Network::SDN::Controllers::lock_sdn_controllers_config(
+        PVE::Network::SDN::lock_sdn_config(
 	    sub {
 
 		my $controller_cfg = PVE::Network::SDN::Controllers::config();
@@ -151,6 +152,8 @@ __PACKAGE__->register_method ({
 
 		PVE::Network::SDN::Controllers::write_config($controller_cfg);
 
+		PVE::Network::SDN::increase_version();
+
 	    }, "create sdn controller object failed");
 
 	return undef;
@@ -173,7 +176,7 @@ __PACKAGE__->register_method ({
 	my $id = extract_param($param, 'controller');
 	my $digest = extract_param($param, 'digest');
 
-        PVE::Network::SDN::Controllers::lock_sdn_controllers_config(
+        PVE::Network::SDN::lock_sdn_config(
 	 sub {
 
 	    my $controller_cfg = PVE::Network::SDN::Controllers::config();
@@ -193,6 +196,9 @@ __PACKAGE__->register_method ({
 
 	    PVE::Network::SDN::Controllers::write_config($controller_cfg);
 
+	    PVE::Network::SDN::increase_version();
+
+
 	    }, "update sdn controller object failed");
 
 	return undef;
@@ -221,7 +227,7 @@ __PACKAGE__->register_method ({
 
 	my $id = extract_param($param, 'controller');
 
-        PVE::Network::SDN::Controllers::lock_sdn_controllers_config(
+        PVE::Network::SDN::lock_sdn_config(
 	    sub {
 
 		my $cfg = PVE::Network::SDN::Controllers::config();
@@ -237,6 +243,8 @@ __PACKAGE__->register_method ({
 		delete $cfg->{ids}->{$id};
 		PVE::Network::SDN::Controllers::write_config($cfg);
 
+		PVE::Network::SDN::increase_version();
+
 	    }, "delete sdn controller object failed");
 
 
diff --git a/PVE/API2/Network/SDN/Vnets.pm b/PVE/API2/Network/SDN/Vnets.pm
index a9561f1..8f70bab 100644
--- a/PVE/API2/Network/SDN/Vnets.pm
+++ b/PVE/API2/Network/SDN/Vnets.pm
@@ -6,6 +6,7 @@ use warnings;
 use PVE::SafeSyslog;
 use PVE::Tools qw(extract_param);
 use PVE::Cluster qw(cfs_read_file cfs_write_file);
+use PVE::Network::SDN;
 use PVE::Network::SDN::Vnets;
 use PVE::Network::SDN::VnetPlugin;
 
@@ -116,7 +117,7 @@ __PACKAGE__->register_method ({
         PVE::Cluster::check_cfs_quorum();
         mkdir("/etc/pve/sdn");
 
-        PVE::Network::SDN::Vnets::lock_sdn_vnets_config(
+        PVE::Network::SDN::lock_sdn_config(
 	    sub {
 
 		my $cfg = PVE::Network::SDN::Vnets::config();
@@ -132,6 +133,9 @@ __PACKAGE__->register_method ({
 
 		PVE::Network::SDN::Vnets::write_config($cfg);
 
+		PVE::Network::SDN::increase_version();
+
+
 	    }, "create sdn vnet object failed");
 
 	return undef;
@@ -154,7 +158,7 @@ __PACKAGE__->register_method ({
 	my $id = extract_param($param, 'vnet');
 	my $digest = extract_param($param, 'digest');
 
-        PVE::Network::SDN::Vnets::lock_sdn_vnets_config(
+        PVE::Network::SDN::lock_sdn_config(
 	 sub {
 
 	    my $cfg = PVE::Network::SDN::Vnets::config();
@@ -168,6 +172,8 @@ __PACKAGE__->register_method ({
 
 	    PVE::Network::SDN::Vnets::write_config($cfg);
 
+	    PVE::Network::SDN::increase_version();
+
 	    }, "update sdn vnet object failed");
 
 	return undef;
@@ -196,7 +202,7 @@ __PACKAGE__->register_method ({
 
 	my $id = extract_param($param, 'vnet');
 
-        PVE::Network::SDN::Vnets::lock_sdn_vnets_config(
+        PVE::Network::SDN::lock_sdn_config(
 	    sub {
 
 		my $cfg = PVE::Network::SDN::Vnets::config();
@@ -210,6 +216,8 @@ __PACKAGE__->register_method ({
 		delete $cfg->{ids}->{$id};
 		PVE::Network::SDN::Vnets::write_config($cfg);
 
+		PVE::Network::SDN::increase_version();
+
 	    }, "delete sdn vnet object failed");
 
 
diff --git a/PVE/API2/Network/SDN/Zones.pm b/PVE/API2/Network/SDN/Zones.pm
index 9ea2189..3201b10 100644
--- a/PVE/API2/Network/SDN/Zones.pm
+++ b/PVE/API2/Network/SDN/Zones.pm
@@ -6,6 +6,7 @@ use warnings;
 use PVE::SafeSyslog;
 use PVE::Tools qw(extract_param);
 use PVE::Cluster qw(cfs_read_file cfs_write_file);
+use PVE::Network::SDN;
 use PVE::Network::SDN::Vnets;
 use PVE::Network::SDN::Zones;
 use PVE::Network::SDN::Zones::Plugin;
@@ -143,7 +144,7 @@ __PACKAGE__->register_method ({
         PVE::Cluster::check_cfs_quorum();
         mkdir("/etc/pve/sdn");
 
-        PVE::Network::SDN::Zones::lock_sdn_zones_config(
+        PVE::Network::SDN::lock_sdn_config(
 	    sub {
 
 		my $zone_cfg = PVE::Network::SDN::Zones::config();
@@ -159,6 +160,8 @@ __PACKAGE__->register_method ({
 
 		PVE::Network::SDN::Zones::write_config($zone_cfg);
 
+		PVE::Network::SDN::increase_version();
+
 	    }, "create sdn zone object failed");
 
 	return undef;
@@ -181,7 +184,7 @@ __PACKAGE__->register_method ({
 	my $id = extract_param($param, 'zone');
 	my $digest = extract_param($param, 'digest');
 
-        PVE::Network::SDN::Zones::lock_sdn_zones_config(
+        PVE::Network::SDN::lock_sdn_config(
 	 sub {
 
 	    my $zone_cfg = PVE::Network::SDN::Zones::config();
@@ -202,6 +205,8 @@ __PACKAGE__->register_method ({
 
 	    PVE::Network::SDN::Zones::write_config($zone_cfg);
 
+	    PVE::Network::SDN::increase_version();
+
 	    }, "update sdn zone object failed");
 
 	return undef;
@@ -230,7 +235,7 @@ __PACKAGE__->register_method ({
 
 	my $id = extract_param($param, 'zone');
 
-        PVE::Network::SDN::Zones::lock_sdn_zones_config(
+        PVE::Network::SDN::lock_sdn_config(
 	    sub {
 
 		my $cfg = PVE::Network::SDN::Zones::config();
@@ -246,6 +251,8 @@ __PACKAGE__->register_method ({
 		delete $cfg->{ids}->{$id};
 		PVE::Network::SDN::Zones::write_config($cfg);
 
+		PVE::Network::SDN::increase_version();
+
 	    }, "delete sdn zone object failed");
 
 
diff --git a/PVE/Network/SDN.pm b/PVE/Network/SDN.pm
index 2b423a7..108a1ab 100644
--- a/PVE/Network/SDN.pm
+++ b/PVE/Network/SDN.pm
@@ -12,6 +12,28 @@ use PVE::Network::SDN::Zones;
 use PVE::Tools qw(extract_param dir_glob_regex run_command);
 use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);
 
+
+my $version_cfg = "sdn/.version";
+
+my $parse_version_cfg = sub {
+    my ($filename, $raw) = @_;
+
+    warn "invalid sdn version" if $raw !~ m/\d+$/;
+
+    return $raw,
+};
+
+my $write_version_cfg = sub {
+    my ($filename, $version) = @_;
+
+    warn "invalid sdn version" if $version !~ m/\d+$/;
+
+    return $version;
+};
+
+PVE::Cluster::cfs_register_file($version_cfg, $parse_version_cfg, $write_version_cfg);
+
+
 # improve me : move status code inside plugins ?
 
 sub ifquery_check {
@@ -46,6 +68,30 @@ sub status {
     return($zone_status, $vnet_status);
 }
 
+
+sub increase_version {
+
+    my $version = cfs_read_file($version_cfg);
+
+    if($version) {
+	$version++;
+    } else {
+	$version = 1;
+    }
+
+    cfs_write_file($version_cfg, $version);
+}
+
+sub lock_sdn_config {
+    my ($code, $errmsg) = @_;
+
+    cfs_lock_file($version_cfg, undef, $code);
+
+    if (my $err = $@) {
+        $errmsg ? die "$errmsg: $err" : die $err;
+    }
+}
+
 sub get_local_vnets {
 
     my $rpcenv = PVE::RPCEnvironment::get();
diff --git a/PVE/Network/SDN/Vnets.pm b/PVE/Network/SDN/Vnets.pm
index ef698e8..97814e6 100644
--- a/PVE/Network/SDN/Vnets.pm
+++ b/PVE/Network/SDN/Vnets.pm
@@ -31,15 +31,6 @@ sub write_config {
     cfs_write_file("sdn/vnets.cfg", $cfg);
 }
 
-sub lock_sdn_vnets_config {
-    my ($code, $errmsg) = @_;
-
-    cfs_lock_file("sdn/vnets.cfg", undef, $code);
-    if (my $err = $@) {
-        $errmsg ? die "$errmsg: $err" : die $err;
-    }
-}
-
 sub sdn_vnets_ids {
     my ($cfg) = @_;
 
diff --git a/PVE/Network/SDN/Zones.pm b/PVE/Network/SDN/Zones.pm
index 9791494..8f12976 100644
--- a/PVE/Network/SDN/Zones.pm
+++ b/PVE/Network/SDN/Zones.pm
@@ -55,15 +55,6 @@ sub write_config {
     cfs_write_file("sdn/zones.cfg", $cfg);
 }
 
-sub lock_sdn_zones_config {
-    my ($code, $errmsg) = @_;
-
-    cfs_lock_file("sdn/zones.cfg", undef, $code);
-    if (my $err = $@) {
-        $errmsg ? die "$errmsg: $err" : die $err;
-    }
-}
-
 sub sdn_zones_ids {
     my ($cfg) = @_;
 
-- 
2.20.1




More information about the pve-devel mailing list