]> git.proxmox.com Git - pve-network.git/blob - PVE/Network/Network.pm
write configuration to networks.cfg.new
[pve-network.git] / PVE / Network / Network.pm
1 package PVE::Network::Network;
2
3 use strict;
4 use warnings;
5 use Data::Dumper;
6 use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);
7 use PVE::Network::Network::Plugin;
8 use PVE::Network::Network::VnetPlugin;
9 use PVE::Network::Network::VlanPlugin;
10 use PVE::Network::Network::VxlanMulticastPlugin;
11
12 PVE::Network::Network::VnetPlugin->register();
13 PVE::Network::Network::VlanPlugin->register();
14 PVE::Network::Network::VxlanMulticastPlugin->register();
15 PVE::Network::Network::Plugin->init();
16
17
18 sub network_config {
19 my ($cfg, $networkid, $noerr) = @_;
20
21 die "no network ID specified\n" if !$networkid;
22
23 my $scfg = $cfg->{ids}->{$networkid};
24 die "network '$networkid' does not exists\n" if (!$noerr && !$scfg);
25
26 return $scfg;
27 }
28
29 sub config {
30 my $config = cfs_read_file("networks.cfg.new");
31 $config = cfs_read_file("networks.cfg") if !keys %{$config->{ids}};
32 return $config;
33 }
34
35 sub write_config {
36 my ($cfg) = @_;
37
38 cfs_write_file("networks.cfg.new", $cfg);
39 }
40
41 sub lock_network_config {
42 my ($code, $errmsg) = @_;
43
44 cfs_lock_file("networks.cfg.new", undef, $code);
45 my $err = $@;
46 if ($err) {
47 $errmsg ? die "$errmsg: $err" : die $err;
48 }
49 }
50
51 sub networks_ids {
52 my ($cfg) = @_;
53
54 return keys %{$cfg->{ids}};
55 }
56
57 sub complete_network {
58 my ($cmdname, $pname, $cvalue) = @_;
59
60 my $cfg = PVE::Network::Network::config();
61
62 return $cmdname eq 'add' ? [] : [ PVE::Network::Network::networks_ids($cfg) ];
63 }
64
65 1;