]>
Commit | Line | Data |
---|---|---|
86d22462 | 1 | package PVE::API2::Network::SDN; |
92b6f291 AD |
2 | |
3 | use strict; | |
4 | use warnings; | |
5 | ||
4140be9e | 6 | use PVE::Cluster qw(cfs_lock_file cfs_read_file cfs_write_file); |
14e55c65 TL |
7 | use PVE::Exception qw(raise_param_exc); |
8 | use PVE::JSONSchema qw(get_standard_option); | |
92b6f291 | 9 | use PVE::RESTHandler; |
4140be9e | 10 | use PVE::RPCEnvironment; |
14e55c65 TL |
11 | use PVE::SafeSyslog; |
12 | use PVE::Tools qw(run_command); | |
5d3e0248 | 13 | use PVE::Network::SDN; |
14e55c65 TL |
14 | |
15 | use PVE::API2::Network::SDN::Controllers; | |
4140be9e AD |
16 | use PVE::API2::Network::SDN::Vnets; |
17 | use PVE::API2::Network::SDN::Zones; | |
70b03506 | 18 | use PVE::API2::Network::SDN::Ipams; |
ee4f339e | 19 | use PVE::API2::Network::SDN::Dns; |
92b6f291 AD |
20 | |
21 | use base qw(PVE::RESTHandler); | |
22 | ||
4140be9e | 23 | __PACKAGE__->register_method ({ |
35854815 | 24 | subclass => "PVE::API2::Network::SDN::Vnets", |
4140be9e | 25 | path => 'vnets', |
35854815 | 26 | }); |
92b6f291 | 27 | |
4140be9e | 28 | __PACKAGE__->register_method ({ |
35854815 | 29 | subclass => "PVE::API2::Network::SDN::Zones", |
4140be9e | 30 | path => 'zones', |
35854815 | 31 | }); |
92b6f291 AD |
32 | |
33 | __PACKAGE__->register_method ({ | |
35854815 | 34 | subclass => "PVE::API2::Network::SDN::Controllers", |
4140be9e AD |
35 | path => 'controllers', |
36 | }); | |
37 | ||
70b03506 AD |
38 | __PACKAGE__->register_method ({ |
39 | subclass => "PVE::API2::Network::SDN::Ipams", | |
40 | path => 'ipams', | |
41 | }); | |
42 | ||
ee4f339e AD |
43 | __PACKAGE__->register_method ({ |
44 | subclass => "PVE::API2::Network::SDN::Dns", | |
45 | path => 'dns', | |
46 | }); | |
47 | ||
4140be9e | 48 | __PACKAGE__->register_method({ |
35854815 TL |
49 | name => 'index', |
50 | path => '', | |
92b6f291 | 51 | method => 'GET', |
4140be9e | 52 | description => "Directory index.", |
7d35eaf5 | 53 | permissions => { |
3551b612 | 54 | check => ['perm', '/', [ 'SDN.Audit' ]], |
92b6f291 AD |
55 | }, |
56 | parameters => { | |
57 | additionalProperties => 0, | |
4140be9e | 58 | properties => {}, |
92b6f291 AD |
59 | }, |
60 | returns => { | |
61 | type => 'array', | |
62 | items => { | |
63 | type => "object", | |
4140be9e AD |
64 | properties => { |
65 | id => { type => 'string' }, | |
66 | }, | |
92b6f291 | 67 | }, |
4140be9e | 68 | links => [ { rel => 'child', href => "{id}" } ], |
92b6f291 AD |
69 | }, |
70 | code => sub { | |
71 | my ($param) = @_; | |
72 | ||
35854815 | 73 | my $res = [ |
4140be9e AD |
74 | { id => 'vnets' }, |
75 | { id => 'zones' }, | |
76 | { id => 'controllers' }, | |
70b03506 | 77 | { id => 'ipams' }, |
ee4f339e | 78 | { id => 'dns' }, |
4140be9e | 79 | ]; |
92b6f291 AD |
80 | |
81 | return $res; | |
82 | }}); | |
83 | ||
5d50e70b AD |
84 | my $create_reload_network_worker = sub { |
85 | my ($nodename) = @_; | |
86 | ||
14e55c65 TL |
87 | # FIXME: how to proxy to final node ? |
88 | my $upid; | |
89 | run_command(['pvesh', 'set', "/nodes/$nodename/network"], outfunc => sub { | |
90 | my $line = shift; | |
91 | if ($line =~ /^["']?(UPID:[^\s"']+)["']?$/) { | |
92 | $upid = $1; | |
93 | } | |
94 | }); | |
5d50e70b AD |
95 | #my $upid = PVE::API2::Network->reload_network_config(node => $nodename}); |
96 | my $res = PVE::Tools::upid_decode($upid); | |
97 | ||
98 | return $res->{pid}; | |
99 | }; | |
100 | ||
101 | __PACKAGE__->register_method ({ | |
102 | name => 'reload', | |
103 | protected => 1, | |
104 | path => '', | |
105 | method => 'PUT', | |
106 | description => "Apply sdn controller changes && reload.", | |
3551b612 AD |
107 | permissions => { |
108 | check => ['perm', '/sdn', ['SDN.Allocate']], | |
109 | }, | |
5d50e70b AD |
110 | parameters => { |
111 | additionalProperties => 0, | |
112 | }, | |
113 | returns => { | |
114 | type => 'string', | |
115 | }, | |
116 | code => sub { | |
117 | my ($param) = @_; | |
118 | ||
119 | my $rpcenv = PVE::RPCEnvironment::get(); | |
120 | my $authuser = $rpcenv->get_user(); | |
121 | ||
5d3e0248 AD |
122 | PVE::Network::SDN::commit_config(); |
123 | ||
5d50e70b AD |
124 | my $code = sub { |
125 | $rpcenv->{type} = 'priv'; # to start tasks in background | |
126 | PVE::Cluster::check_cfs_quorum(); | |
127 | my $nodelist = PVE::Cluster::get_nodelist(); | |
14e55c65 TL |
128 | for my $node (@$nodelist) { |
129 | my $pid = eval { $create_reload_network_worker->($node) }; | |
5d50e70b | 130 | warn $@ if $@; |
5d50e70b | 131 | } |
14e55c65 TL |
132 | |
133 | # FIXME: use libpve-apiclient (like in cluster join) to create | |
134 | # tasks and moitor the tasks. | |
135 | ||
5d50e70b AD |
136 | return; |
137 | }; | |
138 | ||
139 | return $rpcenv->fork_worker('reloadnetworkall', undef, $authuser, $code); | |
140 | ||
141 | }}); | |
142 | ||
92b6f291 AD |
143 | |
144 | 1; |