]>
Commit | Line | Data |
---|---|---|
92b6f291 AD |
1 | package PVE::Network::Network; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
434125ce | 5 | |
92b6f291 | 6 | use Data::Dumper; |
c665cefc | 7 | use JSON; |
434125ce TL |
8 | |
9 | use PVE::Tools qw(extract_param dir_glob_regex run_command); | |
92b6f291 AD |
10 | use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file); |
11 | use PVE::Network::Network::Plugin; | |
12 | use PVE::Network::Network::VnetPlugin; | |
13 | use PVE::Network::Network::VlanPlugin; | |
14 | use PVE::Network::Network::VxlanMulticastPlugin; | |
15 | ||
16 | PVE::Network::Network::VnetPlugin->register(); | |
17 | PVE::Network::Network::VlanPlugin->register(); | |
18 | PVE::Network::Network::VxlanMulticastPlugin->register(); | |
19 | PVE::Network::Network::Plugin->init(); | |
20 | ||
21 | ||
22 | sub network_config { | |
23 | my ($cfg, $networkid, $noerr) = @_; | |
24 | ||
25 | die "no network ID specified\n" if !$networkid; | |
26 | ||
27 | my $scfg = $cfg->{ids}->{$networkid}; | |
28 | die "network '$networkid' does not exists\n" if (!$noerr && !$scfg); | |
29 | ||
30 | return $scfg; | |
31 | } | |
32 | ||
33 | sub config { | |
39d04c82 AD |
34 | my $config = cfs_read_file("networks.cfg.new"); |
35 | $config = cfs_read_file("networks.cfg") if !keys %{$config->{ids}}; | |
36 | return $config; | |
92b6f291 AD |
37 | } |
38 | ||
39 | sub write_config { | |
40 | my ($cfg) = @_; | |
41 | ||
39d04c82 | 42 | cfs_write_file("networks.cfg.new", $cfg); |
92b6f291 AD |
43 | } |
44 | ||
45 | sub lock_network_config { | |
46 | my ($code, $errmsg) = @_; | |
47 | ||
39d04c82 | 48 | cfs_lock_file("networks.cfg.new", undef, $code); |
0c5021ad | 49 | if (my $err = $@) { |
92b6f291 AD |
50 | $errmsg ? die "$errmsg: $err" : die $err; |
51 | } | |
52 | } | |
53 | ||
54 | sub networks_ids { | |
55 | my ($cfg) = @_; | |
56 | ||
57 | return keys %{$cfg->{ids}}; | |
58 | } | |
59 | ||
60 | sub complete_network { | |
61 | my ($cmdname, $pname, $cvalue) = @_; | |
62 | ||
63 | my $cfg = PVE::Network::Network::config(); | |
64 | ||
65 | return $cmdname eq 'add' ? [] : [ PVE::Network::Network::networks_ids($cfg) ]; | |
66 | } | |
67 | ||
c665cefc AD |
68 | sub status { |
69 | ||
70 | my $cmd = ['ifquery', '-a', '-c', '-o','json']; | |
c665cefc | 71 | |
0c5021ad TL |
72 | my $result = ''; |
73 | my $reader = sub { $result .= shift }; | |
c665cefc AD |
74 | |
75 | eval { | |
6e9fff39 | 76 | run_command($cmd, outfunc => $reader); |
c665cefc AD |
77 | }; |
78 | ||
6e9fff39 | 79 | my $resultjson = decode_json($result); |
c665cefc AD |
80 | my $interfaces = {}; |
81 | ||
82 | foreach my $interface (@$resultjson) { | |
6e9fff39 AD |
83 | my $name = $interface->{name}; |
84 | $interfaces->{$name} = { | |
85 | status => $interface->{status}, | |
86 | config => $interface->{config}, | |
87 | config_status => $interface->{config_status}, | |
88 | }; | |
c665cefc AD |
89 | } |
90 | ||
91 | return $interfaces; | |
92 | } | |
93 | ||
92b6f291 | 94 | 1; |