1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
package PVE::API2::Network::SDN;
use strict;
use warnings;
use PVE::Cluster qw(cfs_lock_file cfs_read_file cfs_write_file);
use PVE::Exception qw(raise_param_exc);
use PVE::JSONSchema qw(get_standard_option);
use PVE::RESTHandler;
use PVE::RPCEnvironment;
use PVE::SafeSyslog;
use PVE::Tools qw(run_command extract_param);
use PVE::Network::SDN;
use PVE::API2::Network::SDN::Controllers;
use PVE::API2::Network::SDN::Vnets;
use PVE::API2::Network::SDN::Zones;
use PVE::API2::Network::SDN::Ipams;
use PVE::API2::Network::SDN::Dns;
use PVE::API2::Network::SDN::Fabrics;
use base qw(PVE::RESTHandler);
__PACKAGE__->register_method({
subclass => "PVE::API2::Network::SDN::Vnets",
path => 'vnets',
});
__PACKAGE__->register_method({
subclass => "PVE::API2::Network::SDN::Zones",
path => 'zones',
});
__PACKAGE__->register_method({
subclass => "PVE::API2::Network::SDN::Controllers",
path => 'controllers',
});
__PACKAGE__->register_method({
subclass => "PVE::API2::Network::SDN::Ipams",
path => 'ipams',
});
__PACKAGE__->register_method({
subclass => "PVE::API2::Network::SDN::Dns",
path => 'dns',
});
__PACKAGE__->register_method({
subclass => "PVE::API2::Network::SDN::Fabrics",
path => 'fabrics',
});
__PACKAGE__->register_method({
name => 'index',
path => '',
method => 'GET',
description => "Directory index.",
permissions => {
check => ['perm', '/sdn', ['SDN.Audit']],
},
parameters => {
additionalProperties => 0,
properties => {},
},
returns => {
type => 'array',
items => {
type => "object",
properties => {
id => { type => 'string' },
},
},
links => [{ rel => 'child', href => "{id}" }],
},
code => sub {
my ($param) = @_;
my $res = [
{ id => 'vnets' },
{ id => 'zones' },
{ id => 'controllers' },
{ id => 'ipams' },
{ id => 'dns' },
{ id => 'fabrics' },
];
return $res;
},
});
my $create_reload_network_worker = sub {
my ($nodename, $regenerate_frr) = @_;
my @command = ('pvesh', 'set', "/nodes/$nodename/network");
push(@command, '--regenerate-frr') if $regenerate_frr;
# FIXME: how to proxy to final node ?
my $upid;
print "$nodename: reloading network config\n";
run_command(
\@command,
outfunc => sub {
my $line = shift;
if ($line =~ /["']?(UPID:[^\s"']+)["']?$/) {
$upid = $1;
}
},
);
#my $upid = PVE::API2::Network->reload_network_config(node => $nodename});
my $res = PVE::Tools::upid_decode($upid);
return $res->{pid};
};
__PACKAGE__->register_method({
name => 'lock',
protected => 1,
path => 'lock',
method => 'POST',
description => "Acquire global lock for SDN configuration",
permissions => {
check => ['perm', '/sdn', ['SDN.Allocate']],
},
parameters => {
additionalProperties => 0,
properties => {
'allow-pending' => {
type => 'boolean',
optional => 1,
default => 0,
description =>
'if true, allow acquiring lock even though there are pending changes',
},
},
},
returns => {
type => 'string',
},
code => sub {
my ($param) = @_;
return PVE::Network::SDN::lock_sdn_config(
sub {
die "configuration has pending changes"
if !$param->{'allow-pending'} && PVE::Network::SDN::has_pending_changes();
return PVE::Network::SDN::create_global_lock();
},
"could not acquire lock for SDN config",
);
},
});
__PACKAGE__->register_method({
name => 'release_lock',
protected => 1,
path => 'lock',
method => 'DELETE',
description => "Release global lock for SDN configuration",
permissions => {
check => ['perm', '/sdn', ['SDN.Allocate']],
},
parameters => {
additionalProperties => 0,
properties => {
'lock-token' => get_standard_option('pve-sdn-lock-token'),
'force' => {
type => 'boolean',
optional => 1,
default => 0,
description => 'if true, allow releasing lock without providing the token',
},
},
},
returns => {
type => 'null',
},
code => sub {
my ($param) = @_;
my $code = sub {
PVE::Network::SDN::delete_global_lock();
};
if ($param->{force}) {
$code->();
} else {
PVE::Network::SDN::lock_sdn_config(
$code,
"could not release lock",
$param->{'lock-token'},
);
}
},
});
__PACKAGE__->register_method({
name => 'rollback',
protected => 1,
path => 'rollback',
method => 'POST',
description => "Rollback pending changes to SDN configuration",
permissions => {
check => ['perm', '/sdn', ['SDN.Allocate']],
},
parameters => {
additionalProperties => 0,
properties => {
'lock-token' => get_standard_option('pve-sdn-lock-token'),
'release-lock' => {
type => 'boolean',
optional => 1,
default => 1,
description =>
'When lock-token has been provided and configuration successfully rollbacked, release the lock automatically afterwards',
},
},
},
returns => {
type => 'null',
},
code => sub {
my ($param) = @_;
my $lock_token = extract_param($param, 'lock-token');
my $release_lock = extract_param($param, 'release-lock');
my $rollback = sub {
my $running_config = PVE::Network::SDN::running_config();
PVE::Network::SDN::Zones::write_config($running_config->{zones});
PVE::Network::SDN::Vnets::write_config($running_config->{vnets});
PVE::Network::SDN::Subnets::write_config($running_config->{subnets});
PVE::Network::SDN::Controllers::write_config($running_config->{controllers});
# if the config hasn't yet been applied after the introduction of
# fabrics then the key does not exist in the running config so we
# default to an empty hash
my $fabrics_config = $running_config->{fabrics}->{ids} // {};
my $parsed_fabrics_config = PVE::RS::SDN::Fabrics->running_config($fabrics_config);
PVE::Network::SDN::Fabrics::write_config($parsed_fabrics_config);
PVE::Network::SDN::delete_global_lock() if $lock_token && $release_lock;
};
PVE::Network::SDN::lock_sdn_config(
$rollback, "could not rollback SDN configuration", $lock_token,
);
},
});
__PACKAGE__->register_method({
name => 'reload',
protected => 1,
path => '',
method => 'PUT',
description => "Apply sdn controller changes && reload.",
permissions => {
check => ['perm', '/sdn', ['SDN.Allocate']],
},
parameters => {
additionalProperties => 0,
properties => {
'lock-token' => get_standard_option('pve-sdn-lock-token'),
'release-lock' => {
type => 'boolean',
optional => 1,
default => 1,
description =>
'When lock-token has been provided and configuration successfully commited, release the lock automatically afterwards',
},
},
},
returns => {
type => 'string',
},
code => sub {
my ($param) = @_;
my $rpcenv = PVE::RPCEnvironment::get();
my $authuser = $rpcenv->get_user();
my $lock_token = extract_param($param, 'lock-token');
my $release_lock = extract_param($param, 'release-lock');
my $previous_config_has_frr;
my $new_config_has_frr;
PVE::Network::SDN::lock_sdn_config(
sub {
$previous_config_has_frr = PVE::Network::SDN::running_config_has_frr();
PVE::Network::SDN::commit_config();
$new_config_has_frr = PVE::Network::SDN::running_config_has_frr();
PVE::Network::SDN::delete_global_lock() if $lock_token && $release_lock;
},
"could not commit SDN config",
$lock_token,
);
my $regenerate_frr = $previous_config_has_frr || $new_config_has_frr;
my $code = sub {
$rpcenv->{type} = 'priv'; # to start tasks in background
PVE::Cluster::check_cfs_quorum();
my $nodelist = PVE::Cluster::get_nodelist();
for my $node (@$nodelist) {
my $pid = eval { $create_reload_network_worker->($node, $regenerate_frr) };
warn $@ if $@;
}
# FIXME: use libpve-apiclient (like in cluster join) to create
# tasks and moitor the tasks.
return;
};
return $rpcenv->fork_worker('reloadnetworkall', undef, $authuser, $code);
},
});
1;
|