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