]> git.proxmox.com Git - pve-network.git/blob - test/generateconfig.pl
rename PVE::Network::Network to PVE::Network::SDN
[pve-network.git] / test / generateconfig.pl
1 use strict;
2 use warnings;
3 use File::Copy;
4 use PVE::Cluster qw(cfs_read_file);
5
6 use PVE::Network::SDN::Plugin;
7 use PVE::Network::SDN::VnetPlugin;
8 use PVE::Network::SDN::VlanPlugin;
9 use PVE::Network::SDN::VxlanMulticastPlugin;
10
11 PVE::Network::SDN::VnetPlugin->register();
12 PVE::Network::SDN::VlanPlugin->register();
13 PVE::Network::SDN::VxlanMulticastPlugin->register();
14 PVE::Network::SDN::Plugin->init();
15
16
17 my $rawconfig = generate_network_config();
18 print $rawconfig;
19 write_final_config($rawconfig);
20
21 sub generate_network_config {
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};
38 $interface->{name} = $id;
39 $uplinks->{$interface->{'uplink-id'}} = $interface;
40 }
41 }
42
43 my $network_cfg = PVE::Cluster::cfs_read_file('networks.cfg');
44 my $vnet_cfg = undef;
45 my $transport_cfg = undef;
46
47 foreach my $id (keys %{$network_cfg->{ids}}) {
48 if ($network_cfg->{ids}->{$id}->{type} eq 'vnet') {
49 $vnet_cfg->{ids}->{$id} = $network_cfg->{ids}->{$id};
50 } else {
51 $transport_cfg->{ids}->{$id} = $network_cfg->{ids}->{$id};
52 }
53 }
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
61 die "zone $zone don't exist" if !$zone;
62 my $plugin_config = $transport_cfg->{ids}->{$zone};
63 die "zone $zone don't exist" if !defined($plugin_config);
64 my $plugin = PVE::Network::SDN::Plugin->lookup($plugin_config->{type});
65 $rawconfig .= $plugin->generate_network_config($plugin_config, $zone, $id, $vnet, $uplinks);
66 }
67
68 return $rawconfig;
69 }
70
71
72 sub 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