]> git.proxmox.com Git - pve-network.git/blame - test/generateconfig.pl
d/compat: bump level to 11 and fix lintian warnings
[pve-network.git] / test / generateconfig.pl
CommitLineData
8df9fab5
AD
1use strict;
2use warnings;
3use File::Copy;
4use PVE::Cluster qw(cfs_read_file);
5
86d22462
AD
6use PVE::Network::SDN::Plugin;
7use PVE::Network::SDN::VnetPlugin;
8use PVE::Network::SDN::VlanPlugin;
9use PVE::Network::SDN::VxlanMulticastPlugin;
8df9fab5 10
86d22462
AD
11PVE::Network::SDN::VnetPlugin->register();
12PVE::Network::SDN::VlanPlugin->register();
13PVE::Network::SDN::VxlanMulticastPlugin->register();
14PVE::Network::SDN::Plugin->init();
8df9fab5
AD
15
16
6bffe819 17my $rawconfig = generate_sdn_config();
02abc721 18print $rawconfig;
8df9fab5 19write_final_config($rawconfig);
8df9fab5 20
6bffe819 21sub generate_sdn_config {
8df9fab5
AD
22
23 #only support ifupdown2
24 die "you need ifupdown2 to reload networking\n" if !-e '/usr/share/ifupdown2';
25
26 #read main config for physical interfaces
27 my $current_config_file = "/etc/network/interfaces";
28 my $fh = IO::File->new($current_config_file);
29 my $interfaces_config = PVE::INotify::read_etc_network_interfaces(1,$fh);
30 $fh->close();
31
32 #check uplinks
33 my $uplinks = {};
34 foreach my $id (keys %{$interfaces_config->{ifaces}}) {
35 my $interface = $interfaces_config->{ifaces}->{$id};
36 if (my $uplink = $interface->{'uplink-id'}) {
37 die "uplink-id $uplink is already defined on $uplinks->{$uplink}" if $uplinks->{$uplink};
c1ae8486
AD
38 $interface->{name} = $id;
39 $uplinks->{$interface->{'uplink-id'}} = $interface;
8df9fab5
AD
40 }
41 }
42
6bffe819 43 my $sdn_cfg = PVE::Cluster::cfs_read_file('sdn.cfg');
adf8e915
AD
44 my $vnet_cfg = undef;
45 my $transport_cfg = undef;
46
6bffe819
AD
47 foreach my $id (keys %{$sdn_cfg->{ids}}) {
48 if ($sdn_cfg->{ids}->{$id}->{type} eq 'vnet') {
49 $vnet_cfg->{ids}->{$id} = $sdn_cfg->{ids}->{$id};
adf8e915 50 } else {
6bffe819 51 $transport_cfg->{ids}->{$id} = $sdn_cfg->{ids}->{$id};
adf8e915
AD
52 }
53 }
8df9fab5
AD
54
55 #generate configuration
56 my $rawconfig = "";
57 foreach my $id (keys %{$vnet_cfg->{ids}}) {
58 my $vnet = $vnet_cfg->{ids}->{$id};
59 my $zone = $vnet->{transportzone};
60
adf8e915 61 die "zone $zone don't exist" if !$zone;
8df9fab5
AD
62 my $plugin_config = $transport_cfg->{ids}->{$zone};
63 die "zone $zone don't exist" if !defined($plugin_config);
86d22462 64 my $plugin = PVE::Network::SDN::Plugin->lookup($plugin_config->{type});
6bffe819 65 $rawconfig .= $plugin->generate_sdn_config($plugin_config, $zone, $id, $vnet, $uplinks);
8df9fab5
AD
66 }
67
68return $rawconfig;
69}
70
8df9fab5
AD
71
72sub write_final_config {
73 my ($rawconfig) = @_;
74 #now write final separate filename
75 my $tmp_file = "/var/tmp/pve-vnet.cfg";
76
77 my $vnet_interfaces_file = "/etc/network/interfaces.d/vnet";
78
79 my $writefh = IO::File->new($vnet_interfaces_file,">");
80 print $writefh $rawconfig;
81 $writefh->close();
82}
83
84
85
86