]> git.proxmox.com Git - pve-network.git/commitdiff
add generate_etc_network_config && write_etc_network_config subs
authorAlexandre Derumier <aderumier@odiso.com>
Thu, 29 Aug 2019 10:32:42 +0000 (12:32 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Tue, 3 Sep 2019 06:22:56 +0000 (08:22 +0200)
moved from test script,
also skip vnet generation instead die in case of error

Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
PVE/Network/SDN.pm
test/generateconfig.pl

index 8078c3f88c622ef4c5e94763494928a437f08566..9488f4fedcf0839aaa269e70b28f63e4536f5fea 100644 (file)
@@ -91,4 +91,77 @@ sub status {
     return $interfaces;
 }
 
+
+sub generate_etc_network_config {
+
+    my $sdn_cfg = PVE::Cluster::cfs_read_file('sdn.cfg');
+    return if !$sdn_cfg;
+
+    #read main config for physical interfaces
+    my $current_config_file = "/etc/network/interfaces";
+    my $fh = IO::File->new($current_config_file);
+    my $interfaces_config = PVE::INotify::read_etc_network_interfaces(1,$fh);
+    $fh->close();
+
+    #check uplinks
+    my $uplinks = {};
+    foreach my $id (keys %{$interfaces_config->{ifaces}}) {
+       my $interface = $interfaces_config->{ifaces}->{$id};
+       if (my $uplink = $interface->{'uplink-id'}) {
+           die "uplink-id $uplink is already defined on $uplinks->{$uplink}" if $uplinks->{$uplink};
+           $interface->{name} = $id;
+           $uplinks->{$interface->{'uplink-id'}} = $interface;
+       }
+    }
+
+    my $vnet_cfg = undef;
+    my $transport_cfg = undef;
+
+    foreach my $id (keys %{$sdn_cfg->{ids}}) {
+       if ($sdn_cfg->{ids}->{$id}->{type} eq 'vnet') {
+           $vnet_cfg->{ids}->{$id} = $sdn_cfg->{ids}->{$id};
+       } else {
+           $transport_cfg->{ids}->{$id} = $sdn_cfg->{ids}->{$id};
+       }
+    }
+
+    #generate configuration
+    my $rawconfig = "";
+    foreach my $id (keys %{$vnet_cfg->{ids}}) {
+       my $vnet = $vnet_cfg->{ids}->{$id};
+       my $zone = $vnet->{transportzone};
+
+       if(!$zone) {
+           warn "can't generate vnet $vnet : zone $zone don't exist";
+           next;
+       }
+
+       my $plugin_config = $transport_cfg->{ids}->{$zone};
+
+       if (!defined($plugin_config)) {
+           warn "can't generate vnet $vnet : zone $zone don't exist";
+           next;
+       }
+
+       my $plugin = PVE::Network::SDN::Plugin->lookup($plugin_config->{type});
+       $rawconfig .= $plugin->generate_sdn_config($plugin_config, $zone, $id, $vnet, $uplinks);
+    }
+
+    return $rawconfig;
+}
+
+sub write_etc_network_config {
+    my ($rawconfig) = @_;
+
+    return if !$rawconfig;
+    my $sdn_interfaces_file = "/etc/network/interfaces.d/sdn";
+
+    my $writefh = IO::File->new($sdn_interfaces_file,">");
+    print $writefh $rawconfig;
+    $writefh->close();
+}
+
 1;
+
+
+
index 0a652b83b2f04c7e696d41894a980fbd3347256a..1be9afd507274bb00e6bfcf725a7489de9cabcb7 100644 (file)
@@ -3,84 +3,9 @@ use warnings;
 use File::Copy;
 use PVE::Cluster qw(cfs_read_file);
 
-use PVE::Network::SDN::Plugin;
-use PVE::Network::SDN::VnetPlugin;
-use PVE::Network::SDN::VlanPlugin;
-use PVE::Network::SDN::VxlanMulticastPlugin;
+use PVE::Network::SDN;
 
-PVE::Network::SDN::VnetPlugin->register();
-PVE::Network::SDN::VlanPlugin->register();
-PVE::Network::SDN::VxlanMulticastPlugin->register();
-PVE::Network::SDN::Plugin->init();
 
-
-my $rawconfig = generate_sdn_config();
+my $rawconfig = PVE::Network::SDN::generate_etc_network_config();
+PVE::Network::SDN::write_etc_network_config($rawconfig);
 print $rawconfig;
-write_final_config($rawconfig);
-
-sub generate_sdn_config {
-
-     #only support ifupdown2
-    die "you need ifupdown2 to reload networking\n" if !-e '/usr/share/ifupdown2';
-
-     #read main config for physical interfaces
-     my $current_config_file = "/etc/network/interfaces";
-     my $fh = IO::File->new($current_config_file);
-     my $interfaces_config = PVE::INotify::read_etc_network_interfaces(1,$fh);
-     $fh->close();
-
-     #check uplinks
-     my $uplinks = {};
-     foreach my $id (keys %{$interfaces_config->{ifaces}}) {
-        my $interface = $interfaces_config->{ifaces}->{$id};
-       if (my $uplink = $interface->{'uplink-id'}) {
-               die "uplink-id $uplink is already defined on $uplinks->{$uplink}" if $uplinks->{$uplink};
-               $interface->{name} = $id;
-               $uplinks->{$interface->{'uplink-id'}} = $interface;
-       }
-     }
-
-    my $sdn_cfg = PVE::Cluster::cfs_read_file('sdn.cfg');
-    my $vnet_cfg = undef;
-    my $transport_cfg = undef;
-
-    foreach my $id (keys %{$sdn_cfg->{ids}}) {
-       if ($sdn_cfg->{ids}->{$id}->{type} eq 'vnet') {
-           $vnet_cfg->{ids}->{$id} = $sdn_cfg->{ids}->{$id};
-       } else {
-           $transport_cfg->{ids}->{$id} = $sdn_cfg->{ids}->{$id};
-       }
-    }
-
-       #generate configuration
-       my $rawconfig = "";
-       foreach my $id (keys %{$vnet_cfg->{ids}}) {
-            my $vnet = $vnet_cfg->{ids}->{$id};
-            my $zone = $vnet->{transportzone};
-
-            die "zone $zone don't exist" if !$zone;
-            my $plugin_config = $transport_cfg->{ids}->{$zone};
-            die "zone $zone don't exist" if !defined($plugin_config);
-             my $plugin = PVE::Network::SDN::Plugin->lookup($plugin_config->{type});
-             $rawconfig .= $plugin->generate_sdn_config($plugin_config, $zone, $id, $vnet, $uplinks);
-        }
-
-return $rawconfig;
-}
-
-
-sub write_final_config {
-    my ($rawconfig) = @_;
-       #now write final separate filename
-       my $tmp_file = "/var/tmp/pve-vnet.cfg";
-
-       my $vnet_interfaces_file = "/etc/network/interfaces.d/vnet";
-
-       my $writefh = IO::File->new($vnet_interfaces_file,">");
-       print $writefh $rawconfig;
-       $writefh->close();
-}
-
-
-
-