]> git.proxmox.com Git - pve-network.git/blob - test/generateconfig.pl
generateconfig test: remove verify merge config
[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::Network::Plugin;
7 use PVE::Network::Network::VnetPlugin;
8 use PVE::Network::Network::VlanPlugin;
9 use PVE::Network::Network::VxlanMulticastPlugin;
10
11 PVE::Network::Network::VnetPlugin->register();
12 PVE::Network::Network::VlanPlugin->register();
13 PVE::Network::Network::VxlanMulticastPlugin->register();
14 PVE::Network::Network::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 $uplinks->{$interface->{'uplink-id'}} = $id;
39 }
40 }
41
42 my $network_cfg = PVE::Cluster::cfs_read_file('networks.cfg');
43 my $vnet_cfg = undef;
44 my $transport_cfg = undef;
45
46 foreach my $id (keys %{$network_cfg->{ids}}) {
47 if ($network_cfg->{ids}->{$id}->{type} eq 'vnet') {
48 $vnet_cfg->{ids}->{$id} = $network_cfg->{ids}->{$id};
49 } else {
50 $transport_cfg->{ids}->{$id} = $network_cfg->{ids}->{$id};
51 }
52 }
53
54 #generate configuration
55 my $rawconfig = "";
56 foreach my $id (keys %{$vnet_cfg->{ids}}) {
57 my $vnet = $vnet_cfg->{ids}->{$id};
58 my $zone = $vnet->{transportzone};
59
60 die "zone $zone don't exist" if !$zone;
61 my $plugin_config = $transport_cfg->{ids}->{$zone};
62 die "zone $zone don't exist" if !defined($plugin_config);
63 my $plugin = PVE::Network::Network::Plugin->lookup($plugin_config->{type});
64 $rawconfig .= $plugin->generate_network_config($plugin_config, $zone, $id, $vnet, $uplinks);
65 }
66
67 return $rawconfig;
68 }
69
70
71 sub write_final_config {
72 my ($rawconfig) = @_;
73 #now write final separate filename
74 my $tmp_file = "/var/tmp/pve-vnet.cfg";
75
76 my $vnet_interfaces_file = "/etc/network/interfaces.d/vnet";
77
78 my $writefh = IO::File->new($vnet_interfaces_file,">");
79 print $writefh $rawconfig;
80 $writefh->close();
81 }
82
83
84
85